blib/lib/Lingua/Ogmios/NLPWrappers/GeniaTagger.pm untested

filter: Statementstmt 0.0% 0 / 204 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 312 CC 27 SCAR 45.8
CC
27
Cov
0%
_outputParsing
156.0
run
132.0
_inputGeniaTagger
42.0
CRAP 97.1
1 package Lingua::Ogmios::NLPWrappers::GeniaTagger;
2  
3  
4 our $VERSION='0.1';
5  
6  
7 use Lingua::Ogmios::NLPWrappers::Wrapper;
8  
9 use Lingua::Ogmios::Annotations::MorphosyntacticFeatures;
10 use Lingua::Ogmios::Annotations::Lemma;
11  
12 use Encode qw(:fallbacks);;
13  
14  
15 use strict;
16 use warnings;
17  
18 our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper);
19  
20 sub new {
21 my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output) = @_;
22  
23 warn "[LOG] Creating a wrapper of the GeniaTagger\n";
24  
25  
26 my $GeniaTagger = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output);
27  
28 $GeniaTagger->_input_filename($tmpfile_prefix . ".GeniaTagger.in");
29 $GeniaTagger->_output_filename($tmpfile_prefix . ".GeniaTagger.out");
30  
31 unlink($GeniaTagger->_input_filename);
32 unlink($GeniaTagger->_output_filename);
33  
34 return($GeniaTagger);
35  
36 }
37  
38 sub _processGeniaTagger {
39 my ($self, $lang) = @_;
40  
41 warn "[LOG] POS tagger\n";
42  
43 $lang = $self->_documentSet->[0]->getAnnotations->getLanguage;
44  
45 return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{GeniaTagger_CMD} . " < " . $self->_input_filename . ">" . $self->_output_filename)));
46  
47 warn "[LOG]\n";
48 }
49  
50 sub _inputGeniaTagger {
51 my ($self) = @_;
52  
53 my $token;
54 my $next_token;
55 # my $corpus_in = "";
56 my $document;
57 my $doc_idx;
58  
59 my @corpus_in_t;
60 my $wordform;
61  
62 my $word;
63 warn "[LOG] making GeniaTagger input\n";
64
65 foreach $document (@{$self->_documentSet}) {
66 for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) {
67 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
68 # warn "Current tokent (1): " . $token->getId . "\n";
69 if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
70 $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0];
71 # $corpus_in .= $word->getForm . "\n";
72 $wordform = $word->getForm;
73 # warn "$wordform\n";
74 $wordform =~ s/[\t\n]/ /gos;
75 $wordform =~ s/ +/___/gos;
76 # warn "$wordform\n";
77 push @corpus_in_t, $wordform;
78  
79 $doc_idx += $word->getReferenceSize - 1;
80 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
81 # warn "Current tokent (2w): " . $token->getId . "\n";
82  
83 # if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) {
84 # warn "Current tokent (3s): " . $token->getId . "\n";
85
86 # if ($token->isSymb) {
87 # $corpus_in .= $token->getContent . "\tSENT\n";
88 # } else {
89 # $corpus_in .= ".\tSENT\n";
90 # }
91 # }
92  
93 } else {
94 if (!($token->isSep)) {
95 # warn "Current tokent (2s): " . $token->getId . "\n";
96 # if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) {
97 # warn "Current tokent (3s): " . $token->getId . "\n";
98  
99 # $corpus_in .= ".\tSENT\n";
100  
101 # }
102 # } else {
103 # if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) {
104 # warn "Current tokent (3s): " . $token->getId . "\n";
105 # $corpus_in .= $token->getContent . "\tSENT\n";
106 # } else {
107 # $corpus_in .= $token->getContent;
108 $wordform = $token->getContent;
109 $wordform =~ s/[\t\n]/ /gos;
110 $wordform =~ s/ +/___/go;
111 push @corpus_in_t, $wordform;
112 # }
113 }
114 }
115 if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) {
116 if ($token->isSymb) {
117 if (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) {
118 # $corpus_in .= "\tSENT\n";
119 # $corpus_in_t[$#corpus_in_t] .= "\tSENT";
120 } else {
121 # $corpus_in .= $token->getContent . "\tSENT\n";
122 $wordform = $token->getContent;
123 $wordform =~ s/[\t\n]/ /gos;
124 $wordform =~ s/ +/___/go;
125 push @corpus_in_t, $wordform; #. "\tSENT";
126 }
127 } else {
128 # $corpus_in .= ".\tSENT\n";
129 push @corpus_in_t, ".\n";#\tSENT";
130  
131 }
132 # } else {
133 # if ((!($token->isSep)) && (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)))) {
134 # $corpus_in .= "\n";
135 # }
136 }
137  
138 }
139 # foreach $token (@{$document->getAnnotations->getTokenLevel->getElements}) {
140 # # warn "$token: " . $token->getContent . ";\n";
141 # $corpus_in .= $token->getContent . "\n";
142 # }
143 }
144  
145 open FILE_IN, ">" . $self->_input_filename;
146
147  
148 # print FILE_IN Encode::encode("iso-8859-1", $corpus_in, Encode::FB_DEFAULT); #$corpus_in;
149 print FILE_IN Encode::encode("iso-8859-1", join(" ",@corpus_in_t), Encode::FB_DEFAULT); #$corpus_in;
150 # print FILE_IN "\n";
151 close FILE_IN;
152
153 warn "[LOG] done\n";
154  
155  
156 }
157  
158  
159 sub _outputParsing {
160 my ($self) = @_;
161  
162 my $line;
163 my @GeniaTaggerOutput;
164  
165 my $doc_idx;
166 my $word_idx;
167 my $document;
168  
169 my $word;
170 my $token;
171  
172 my $posInWord;
173 my $posInLemma;
174  
175 my $substringBefore;
176 my $substringAfter;
177  
178 my $defaultPOS = "NN";
179 my $defaultLemma;
180  
181 warn "[LOG] . Parsing " . $self->_output_filename . "\n";
182  
183 open FILE_OUT, $self->_output_filename or warn "Can't open the file " . $self->_output_filename;;
184 # binmode(FILE_OUT, ":utf8");
185  
186 while($line = <FILE_OUT>) {
187 chomp $line;
188 if ($line ne "") {
189 $line =~ s/___/ /go;
190 my @tmp;
191 ($tmp[0], $tmp[2], $tmp[1]) = split /\t/, $line;
192 # work around some strange tagging
193 if ((!defined $tmp[1]) || ($tmp[1] eq "")){
194 $tmp[1] = "SYM";
195 }
196 if ((!defined $tmp[2]) || ($tmp[2] eq "")){
197 $tmp[2] = $tmp[0];
198 }
199 push @GeniaTaggerOutput, \@tmp;
200 }
201  
202 }
203 close FILE_OUT;
204  
205 # warn "TT output size: " . scalar(@GeniaTaggerOutput) . "\n";
206  
207 my $GeniaTaggerOutput_idx = 0;
208  
209 foreach $document (@{$self->_documentSet}) {
210 for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) {
211 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
212 if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
213 $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0];
214  
215 # warn $word->getForm . " (" . $word->getId . ") " . $GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[0] . "\n";
216  
217 # Correct POSTag if it's named entity
218  
219 if ($word->isNE) {
220 $GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[1] = "NP";
221 # $GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[1] = "named_entity";
222 # warn "Remove _ if it's a complex word i.e. a named entity\n";
223  
224 # Remove _ if it's a complex word i.e. a named entity
225  
226 $posInLemma = 0;
227 do {
228 if ((($posInLemma = index($GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[2], "_"), $posInLemma) != -1) &&
229 ((($posInWord = index($GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[0], "_", $posInLemma)) == -1) ||
230 ($posInLemma != $posInWord))) {
231 # warn "lemma to correct : " . $GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[2] . "\n";
232 $substringBefore = substr($GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[2], 0, $posInLemma);
233 $substringAfter = substr($GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[2], $posInLemma + 1);
234
235 # warn "New lemma : $substringBefore $substringAfter ($posInLemma)\n";
236 $GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[2] = "$substringBefore $substringAfter";
237 $posInLemma++;
238 }
239 } while (($posInLemma != -1) && ($posInLemma != $posInWord) && ($posInLemma < length($GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[2])));
240 }
241  
242 # work around some strange tagging
243 # warn "GeniaTaggerOutput_idx: $GeniaTaggerOutput_idx\n";
244 if ($GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[1] eq "SENT") {
245 $GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[1] = "named_entity";
246 }
247  
248  
249  
250 my $MSFeatures = Lingua::Ogmios::Annotations::MorphosyntacticFeatures->new(
251 {'refid_word' => $word,
252 'syntactic_category' => $GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[1],
253 });
254 $document->getAnnotations->addMorphosyntacticFeatures($MSFeatures);
255
256  
257 my $Lemma = Lingua::Ogmios::Annotations::Lemma->new(
258 {'refid_word' => $word,
259 'canonical_form' => $GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[2],
260 });
261 $document->getAnnotations->addLemma($Lemma);
262  
263 $GeniaTaggerOutput_idx++;
264  
265 $doc_idx += $word->getReferenceSize - 1;
266 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
267  
268 } else {
269 if (!($token->isSep)) {
270 $GeniaTaggerOutput_idx++;
271 # warn $token->getContent . " (" . $token->getId . ") " . $GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[0] . "\n";
272 }
273 }
274 if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) {
275 if ($token->isSymb) {
276 if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
277
278 # } else {
279 $GeniaTaggerOutput_idx++;
280 # warn $token->getContent . " (" . $token->getId . ") " . $GeniaTaggerOutput[$GeniaTaggerOutput_idx]->[0] . " 0 \n";
281 }
282 } else {
283 $GeniaTaggerOutput_idx++;
284 # warn ". ( 000 ) " . $GeniaTaggerOutput[$GeniaTaggerOutput_idx++]->[0] . "\n";
285 }
286 }
287 }
288 }
289 warn "[LOG] done\n";
290 }
291  
292 sub run {
293 my ($self, $documentSet) = @_;
294  
295 my $line;
296  
297 # Set variables according the the configuration
298  
299 $self->_documentSet($documentSet);
300  
301 if ($documentSet->[0]->getAnnotations->existsLemmaLevel) {
302 warn "lemmas exist in the first document\n";
303 warn " Assuming that no lemma idenfication is required for the current document set\n";
304 # return(0);
305 } else {
306 if ($documentSet->[0]->getAnnotations->existsMorphosyntacticFeaturesLevel) {
307 warn "MorphosyntacticFeaturres exist in the first document\n";
308 warn " Assuming that no Part-of-Speech tagging is required for the current document set\n";
309 # return(0);
310 } else {
311  
312 warn "[LOG] POS tagger ... \n";
313 $self->_inputGeniaTagger;
314  
315 my $command_line = $self->_processGeniaTagger;
316  
317 $self->_outputParsing;
318  
319 # Put log information
320 my $information = { 'software_name' => 'GeniaTagger',
321 'comments' => 'POS tagging\n',
322 'command_line' => $command_line,
323 'list_modified_level' => ['morphosyntactic_features_level', 'lemma_level'],
324 };
325 $self->_log($information);
326  
327 my $document;
328 foreach $document (@{$documentSet}) {
329 $document->getAnnotations->addLogProcessing(
330 Lingua::Ogmios::Annotations::LogProcessing->new(
331 { 'comments' => 'Found ' . $document->getAnnotations->getLemmaLevel->getSize . ' lemma\n',
332 }
333 )
334 );
335 $document->getAnnotations->addLogProcessing(
336 Lingua::Ogmios::Annotations::LogProcessing->new(
337 { 'comments' => 'Found ' . $document->getAnnotations->getMorphosyntacticFeaturesLevel->getSize . ' morphosyntactic features\n',
338 }
339 )
340 );
341 # basic check
342 if ($document->getAnnotations->getLemmaLevel->getSize != $document->getAnnotations->getWordLevel->getSize) {
343 warn "Document " . $document->getId . ": there is difference between word and lemma size\n";
344 }
345 if ($document->getAnnotations->getMorphosyntacticFeaturesLevel->getSize != $document->getAnnotations->getWordLevel->getSize) {
346 warn "Document " . $document->getId . ": there is difference between word and morphosyntactic feature size\n";
347 }
348 }
349 }
350 }
351 if (($self->_position eq "last") && ($self->_no_standard_output)) {
352 if ($self->_no_standard_output eq "IFPOSLMGT") {
353 warn "print Genia output\n";
354 open FILE_OUT, $self->_output_filename or warn "Can't open the file " . $self->_output_filename;;
355
356 while($line = <FILE_OUT>) {
357 print $line;
358 }
359 close FILE_OUT;
360 } elsif ($self->_no_standard_output eq "VOCABULARY") {
361 $self->_printVocabulary;
362 } elsif ($self->_no_standard_output eq "VOCABULARY_ALL") {
363 binmode(STDOUT, ":utf8");
364 print "#####################\n";
365 print "# FF\n";
366 $self->_printVocabulary_FF;
367 print "#####################\n";
368 print "# LM\n";
369 $self->_printVocabulary_LM;
370 print "#####################\n";
371 print "# TAGSET\n";
372 $self->_printTagSet;
373 } elsif ($self->_no_standard_output eq "TAGSET") {
374 $self->_printTagSet;
375 } elsif ($self->_no_standard_output eq "IFPOSLM") {
376 # $self->_outputParsing;
377 $self->_printTreeTaggerFormatOutput("stdout");
378 }
379 } else {
380 }
381  
382 # die "You call the 'rum' method of the wrapper class base\n
383 # You should define a 'run' method for your wrapper\n";
384 warn "[LOG] done\n";
385 }
386  
387  
388 1;
389  
390  
391 __END__