-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tag.pm
300 lines (274 loc) · 10.7 KB
/
Tag.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/perl
#===========File: Tag.pm===============
#Title: Tag.pm - text tagging Module for Tilde's POS Taggers.
#Description: The Module contains text Tagger method.
#Author: Kârlis Gediòð, SIA Tilde.
#Created: May, 2011.
#Last Changes: 03.08.2011. by Mârcis Pinnis, SIA Tilde.
#===============================================
package Tag;
use strict;
use warnings;
#=========Method: Tag==========
#Title: Tag
#Description: POS tags a document (parameter 2) using a specified POS tagger (parameter 0) and language code (parameter 1). The results are saved in a data file (parameter 3). Deletes temp files if argument 5 exists and true, and removes empty lines if specified (argument 6).
#Author: Kârlis Gediòð, SIA Tilde.
#Created: May, 2011.
#Last Changes: 26.05.2011. by Kârlis Gediòð, SIA Tilde.
#===============================================
sub TagText
{
if (not( (defined $_[0]) && (defined $_[1]) && (defined $_[2]) && (defined $_[3]) ))
{
die "Usage: Tag::TagText [POS Tagger Name] [Language] [Palaintext File] [Output File]\n";
}
BEGIN
{
use FindBin '$Bin'; #Gets the path of this file.
push @INC, "$Bin"; #Add this path to places where perl is searching for modules.
}
my @agrs;
my $outputDir = $_[3];
#Gets output file path if it is passed.
if ($outputDir =~ /[\\\/]/)
{
$outputDir =~ s/\\/\//gi;
$outputDir =~ s/\/[^\/]+$//gi;
$outputDir.="\/";
}
else
{
$outputDir = "";
}
if (defined $outputDir && $outputDir ne "")
{
unless(-d $outputDir){mkdir $outputDir or die "Cannot find or create output directory \"$outputDir\".";}
}
#Gets filename if output file parameter has a path.
my $filename =$_[2];
$filename =~ s/^.*\/([^\/]+)$/$1/gi;
$filename =~ s/\.[^\.]+$//g;
use NEPreprocess;
my $uTagger = uc ($_[1]);
my $uLang = uc ($_[0]);
if ($uTagger eq "TREE") #Tree tagger option.
{
my @tokAgrs;
my $tagData;
if($uLang eq 'EN') #Checks language options and sets the needed parameters to call the tagger for English.
{
@tokAgrs=($Bin."/Treetagger/tokenize.pl",$_[2] ,"$outputDir$filename.tokenized","-e","-a","$Bin/Treetagger/english-abbreviations");
$tagData ="$Bin/Treetagger/english.par";
}
elsif($uLang eq 'ES') #Checks language options and sets the needed parameters to call the tagger for Spanish.
{
@tokAgrs=($Bin."/Treetagger/tokenize.pl","$_[2]" ,"$outputDir$filename.tokenized","","-a","$Bin/Treetagger/spanish-abbreviations");
$tagData = "$Bin/Treetagger/spanish-utf8.par";
undef @tokAgrs;
}
elsif($uLang eq 'EL') #Checks language options and sets the needed parameters to call the tagger for Greek.
{
@tokAgrs=( $Bin."/Treetagger/tokenize.pl","$_[2]" ,"$outputDir$filename.tokenized");
$tagData ="$Bin/Treetagger/greek-utf8.par";
}
elsif($uLang eq 'IT') #Checks language options and sets the needed parameters to call the tagger for Itlaian.
{
@tokAgrs=($Bin."/Treetagger/tokenize.pl","$_[2]" ,"$outputDir$filename.tokenized","-i","-a","$Bin/Treetagger/italian-abbreviations");
$tagData = "$Bin/Treetagger/italian-utf8.par";
}
elsif($uLang eq 'DE') #Checks language options and sets the needed parameters to call the tagger for German.
{
@tokAgrs=($Bin."/Treetagger/tokenize.pl","$_[2]" ,"$outputDir$filename.tokenized","","-a","$Bin/Treetagger/german-abbreviations");
$tagData = "$Bin/Treetagger/german-utf8.par";
}
elsif($uLang eq 'FR') #Checks language options and sets the needed parameters to call the tagger for Friench.
{
@tokAgrs=($Bin."/Treetagger/tokenize.pl","$_[2]" ,"$outputDir$filename.tokenized","-f","-a","$Bin/Treetagger/french-abbreviations");
$tagData = "$Bin/Treetagger/french-utf8.par";
}
elsif($uLang eq 'ET') #Checks language options and sets the needed parameters to call the tagger for Estonian.
{
@tokAgrs=( $Bin."/Treetagger/tokenize.pl","$_[2]" ,"$outputDir$filename.tokenized");
$tagData ="$Bin/Treetagger/estonian.par";
}
elsif($uLang eq 'BG') #Checks language options and sets the needed parameters to call the tagger for Bulgarian.
{
@tokAgrs=($Bin."/Treetagger/tokenize.pl","$_[2]" ,"$outputDir$filename.tokenized");
$tagData = "$Bin/Treetagger/bulgarian.par";
}
else
{
print STDERR "[Tag::TagText] ERROR: no such tagger-language combination: \"$_[1]\"-\"$_[0]\"";
die;
}
system "perl",@tokAgrs; #Calling the tokenizer with the language options that where acquired above to tokenize plaintext.
@agrs=("-token", "-lemma",$tagData , "$outputDir$filename.tokenized", "$outputDir$filename.Tree");
if ($^O eq "MSWin32")
{
system $Bin."/Treetagger/tree-tagger.exe",@agrs; # calling tree tagger to tag the tokeinized file with langugae specific paramters (note: UTF-8 parameter files are used).
}
else #If current operating system is not Windows tries the Linux alternative.
{
system $Bin."/Treetagger/tree-tagger",@agrs;
}
#Adds token positions to existing Token file using plaintext file un creates a file with the same structure as the out from POS tagger.
NEPreprocess::FindTokenPos("$_[2]" ,"$outputDir$filename.Tree","$outputDir$filename.temp");
}
elsif ($uTagger eq "POS") # POS tagger - > Tilde's internal POS tagger.
{
if($uLang eq 'LV') # POS tagging for Latvian ("lv").
{
@agrs=("1062" ,"/S","$_[2]","$outputDir$filename.temp"); # calls Pos tagger to tokenize and tag plaintext
system $Bin."/POSTaggerCOMTest.exe",@agrs;
}
elsif($uLang eq 'ET') # POS tagging for Estonian ("et").
{
@agrs=("1061" ,"/S","$_[2]","$outputDir$filename.temp");
system $Bin."/POSTaggerCOMTest.exe",@agrs;
}
elsif($uLang eq 'LT') # POS tagging for Lithuanian ("lt").
{
@agrs=("1063" ,"/S","$_[2]","$outputDir$filename.temp");
system $Bin."/POSTaggerCOMTest.exe",@agrs;
}
else
{
print STDERR "[Tag::TagText] ERROR: no such tagger-language combination combination: \"$_[1]\"-\"$_[0]\"";
die;
}
}
elsif ($uTagger eq "TAGGER") # Tagger - > Tilde's external POS tagger (through a web service).
{
if ($^O eq "MSWin32") # If windows uses Teger.exe.
{
if($uLang eq 'LV') # POS tagging for Latvian ("lv").
{
#Calls POS tagger to tokenize and tag plaintext.
@agrs=("treetagger", "lv", "accurat", "tr\@nsl\@tion","$_[2]","$outputDir$filename.Tree");
system $Bin."/Tagger.exe",@agrs;
#Adds token positions to existing Token file using plaintext file un creates a file with the same structure as the out from POS tagger.
NEPreprocess::FindTokenPos("$_[2]" ,"$outputDir$filename.Tree","$outputDir$filename.temp");
}
elsif($uLang eq 'ET') # POS tagging for Estonian ("et")
{
@agrs=("treetagger", "et", "accurat", "tr\@nsl\@tion","$_[2]","$outputDir$filename.Tree");
system $Bin."/Tagger.exe",@agrs;
NEPreprocess::FindTokenPos("$_[2]" ,"$outputDir$filename.Tree","$outputDir$filename.temp");
}
elsif($uLang eq 'LT') # POS tagging for Lithuanian("lt")
{
@agrs=("treetagger", "lt", "accurat", "tr\@nsl\@tion","$_[2]","$outputDir$filename.Tree");
system $Bin."/Tagger.exe",@agrs;
NEPreprocess::FindTokenPos("$_[2]" ,"$outputDir$filename.Tree","$outputDir$filename.temp");
}
else # if not any of the above:
{
print STDERR "[Tag::TagText] ERROR: no such tagger-language combination: \"$_[1]\"-\"$_[0]\"";
die;
}
}
else #If current operating system is not Windows tries the Linux alternative.
{
use IPC::Open2;
use encoding "UTF-8";
if($uLang eq 'LV') #POS tagging for Latvian ("lv").
{
#Opens stream to pass pliantext to tagger.sh as STDIN and get tokenized and tagged text from it as STDOUT.
my $pid = open2(*Reader, *Writer, "./tagger.sh","treetagger", "lv", "accurat", "tr\@nsl\@tion" );
#Encodes "tagger.sh" passed stream in UTF-8.
binmode Reader, ":utf8";
binmode Writer, ":utf8";
open(FIN, "<:encoding(UTF-8)", "$_[2]");
open(FOUT, ">:encoding(UTF-8)", "$outputDir$filename.Tree");
#Reads plaintext file.
while(<FIN>)
{
my $line = $_;
$line =~ s/^\x{FEFF}//; #Handles BOM.
#Passes the plain text it to stream that was given to "tagger.sh".
print Writer $line;
}
close(Writer);
while (<Reader>) #Prints the STDOUT of "tagger.sh" in a file.
{
print FOUT $_;
}
waitpid( $pid, 0 );
close(Reader);
close(FIN);
close(FOUT);
#Adds token positions to existing Token file using plaintext file un creates a file with the same structure as the out from POS tagger.
NEPreprocess::FindTokenPos("$_[2]" ,"$outputDir$filename.Tree","$outputDir$filename.temp");
}
elsif($uLang eq 'ET') #POS tagging for Estonian ("et").
{
my $pid = open2(*Reader, *Writer, "./tagger.sh","treetagger", "et", "accurat", "tr\@nsl\@tion" );
binmode Reader, ":utf8";
binmode Writer, ":utf8";
open(FIN, "<:encoding(UTF-8)", "$_[2]");
open(FOUT, ">:encoding(UTF-8)", "$outputDir$filename.Tree");
while(<FIN>)
{
my $line = $_;
$line =~ s/^\x{FEFF}//;
print Writer $line;
}
close(Writer);
while (<Reader>)
{
print FOUT $_;
}
waitpid( $pid, 0 );
close(Reader);
close(FIN);
close(FOUT);
NEPreprocess::FindTokenPos("$_[2]" ,"$outputDir$filename.Tree","$outputDir$filename.temp");
}
elsif($uLang eq 'LT') #POS tagging for Lithuanian ("lt").
{
my $pid = open2(*Reader, *Writer, "./tagger.sh","treetagger", "lt", "accurat", "tr\@nsl\@tion" );
binmode Reader, ":utf8";
binmode Writer, ":utf8";
open(FIN, "<:encoding(UTF-8)", "$_[2]");
open(FOUT, ">:encoding(UTF-8)", "$outputDir$filename.Tree");
while(<FIN>)
{
my $line = $_;
$line =~ s/^\x{FEFF}//;
print Writer $line;
}
close(Writer);
while (<Reader>)
{
print FOUT $_;
}
waitpid( $pid, 0 );
close(Reader);
close(FIN);
close(FOUT);
NEPreprocess::FindTokenPos("$_[2]" ,"$outputDir$filename.Tree","$outputDir$filename.temp");
}
else
{
print STDERR "[Tag::TagText] ERROR: no such tagger-language combination: \"$_[1]\"-\"$_[0]\"";
die;
}
}
}
else {die "no such tagger : $_[1] ";}
if ($_[5]) #Removes lines with white spaces if necessary.
{
NEPreprocess::RemoveEmptyLines("$outputDir$filename.temp","$_[3]",$_[5]);
}
else
{
NEPreprocess::RemoveEmptyLines("$outputDir$filename.temp","$_[3]",1);
}
if($_[4]) #Deletes the created temp files if option selected.
{
unlink ("$outputDir$filename.temp");
unlink ("$outputDir$filename.Tree");
unlink ("$outputDir$filename.tokenized");
}
}
1;