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

filter: Statementstmt 0.0% 0 / 321 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 71 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 448 SCAR 61.4
CC
21
Cov
0%
_outputParsing
272.0
run
20.0
_inputPerlWS
6.0
CRAP 462.0
1 package Lingua::Ogmios::NLPWrappers::PerlWordSegmentizer;
2  
3  
4 our $VERSION='0.1';
5  
6 use utf8;
7  
8 use Lingua::Ogmios::NLPWrappers::Wrapper;
9  
10 use Lingua::Ogmios::Annotations::Word;
11  
12 use Encode qw(:fallbacks);;
13  
14 use strict;
15 use warnings;
16  
17 our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper);
18  
19 sub new {
20 my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output) = @_;
21  
22 warn "[LOG] Creating a wrapper of the Perl Word segmentizer\n";
23  
24 my $PerlWordseg = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output);
25  
26 $PerlWordseg->_input_filename($tmpfile_prefix . ".PerlWSeg.in");
27 $PerlWordseg->_output_filename($tmpfile_prefix . ".PerlWSeg.out");
28  
29 unlink($PerlWordseg->_input_filename);
30 unlink($PerlWordseg->_output_filename);
31  
32 return($PerlWordseg);
33  
34 }
35  
36 sub _inputPerlWS {
37 my ($self) = @_;
38  
39 my $token;
40 my $corpus_in = "";
41 my $document;
42  
43 warn "[LOG] making Perl Word segmentizer input\n";
44  
45 foreach $document (@{$self->_documentSet}) {
46 foreach $token (@{$document->getAnnotations->getTokenLevel->getElements}) {
47 # warn "$token: " . $token->getContent . ";\n";
48 $corpus_in .= $token->getContent;
49 if ($document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $token->getId)) {
50 $corpus_in .= "\n";
51 }
52 }
53 }
54 $corpus_in =~ s/[\x{A0}\x{2000}-\x{200B}]/ /go;
55 # $corpus_in =~ s/\x{0153}/oe/go;
56 # $corpus_in =~ s/\x{0152}/OE/go;
57 $corpus_in =~ s/[\x{2019}\x{2032}]/\'/go;
58
59  
60 ##XXX TH
61 open FILE_IN, ">:utf8", $self->_input_filename;
62  
63  
64 # print FILE_IN Encode::encode("iso-8859-1", $corpus_in, Encode::FB_DEFAULT); #$corpus_in;
65  
66 print FILE_IN $corpus_in;
67  
68 close FILE_IN;
69  
70 warn "[LOG] done\n";
71 }
72  
73  
74 sub _processPerlSW {
75 my ($self, $lang) = @_;
76  
77 warn "[LOG] wordsegmentation with a Perl script\n";
78  
79 # warn "\tLanguage for the documentSet: " . $self->_documentSet->[0]->getAnnotations->getLanguage . "\n";
80 $lang = $self->_documentSet->[0]->getAnnotations->getLanguage;
81  
82 if (defined $self->_config->commands($lang)) {
83 # warn $self->_config->commands($lang)->{WSSegmentizer_CMD} . "\n";;
84  
85 return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{WordSegmentizer_CMD} . " < " . $self->_input_filename . " > " . $self->_output_filename)));
86 } else {
87 warn "No segmentizer defined for $lang\n";
88 return(undef);
89 }
90 warn "[LOG]\n";
91 }
92  
93 sub _outputParsing {
94 my ($self) = @_;
95  
96 my $line;
97 my @words;
98 my $token;
99  
100 my $doc_idx;
101 my $word_shift;
102 my $word_idx;
103 my $full_idx;
104 my $full;
105  
106 my $isInNE;
107 my $IsNE;
108  
109 my $word_start = 0;
110 my $word_end = 0;
111  
112 my $word;
113  
114 my $token_content1;
115  
116 my @Word2tokens;
117 my $word_type;
118  
119 my $document;
120  
121 my $word_form;
122  
123 warn "[LOG] . Parsing " . $self->_output_filename . "\n";
124  
125 ##XXX TH
126 open FILE_OUT, "<:utf8", $self->_output_filename or warn "Can't open the file " . $self->_output_filename;;
127 binmode(FILE_OUT, ":utf8");
128  
129 while($line = <FILE_OUT>) {
130 chomp $line;
131 if ($line ne "") {
132 push @words, $line;
133 }
134  
135 }
136 close FILE_OUT;
137  
138 $word_type = 0;
139 $word_idx = 0;
140 $word_shift = 0;
141 $isInNE = 0;
142 $IsNE = 0;
143  
144 my $token_content;
145  
146 foreach $document (@{$self->_documentSet}) {
147 $doc_idx = 0;
148  
149 # warn "Processing document " . $document ->getId . "\n";
150  
151 @Word2tokens = ();
152 $word_shift = 0;
153 $word_form = "";
154 $word_type = 0;
155 for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) {
156 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
157 $token_content1 = $token->getContent;
158 # $token_content1 =~ s/\x{0153}/oe/go;
159 # $token_content1 =~ s/\x{0152}/OE/go;
160 # $token_content1 =~ s/[\x{2019}\x{2032}]/\'/go;
161 # $token_content = Encode::encode("iso-8859-1", $token_content1, Encode::FB_DEFAULT);
162 $token_content = $token_content1;
163 $token_content =~ s/[\x{2019}\x{2032}]/\'/go;
164 # warn "(1a) -- $isInNE - " . $words[$word_idx] . " / $word_idx / " . $token_content . " / " . $token->getType . " (word $word_idx / token $doc_idx)\n";
165 $isInNE = $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId);
166 if ((!$isInNE) && (scalar(@Word2tokens) != 0)) {
167 my $stok = $Word2tokens[0];
168 my $etok = $Word2tokens[$#Word2tokens];
169 @Word2tokens = ();
170 push @Word2tokens, $stok;
171 $word_form = $stok->getContent;
172 while(!$stok->equals($etok)) {
173 $stok = $stok->next;
174 $word_form .= $stok->getContent;
175 push @Word2tokens, $stok;
176 }
177  
178 $word = Lingua::Ogmios::Annotations::Word->new(
179 {'form' => $word_form,
180 'list_refid_token' => \@Word2tokens,
181 'isNE' => $IsNE,
182 });
183 $document->getAnnotations->addWord($word);
184 @Word2tokens = ();
185 $IsNE = 0;
186 $word_shift = 0;
187 $word_form = "";
188 $word_type = 0;
189 $isInNE = 0;
190 }
191 if (($word_idx < scalar(@words)) && (index($words[$word_idx], $token_content) == 0)) {
192 # warn "$token_content\n";
193 push @Word2tokens, $token;
194 $IsNE ||= $isInNE;
195 $word_start = $token->getId;
196 $word_form .= $token->getContent;
197 $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
198 $word_shift = length($token_content);
199 $doc_idx++;
200 if ($doc_idx >= scalar(@{$document->getAnnotations->getTokenLevel->getElements})) {
201 $word_end = $token->previous->getId;
202 } else {
203  
204 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
205 # $token_content = Encode::encode("iso-8859-1", $token->getContent, Encode::FB_DEFAULT);
206 $token_content = $token->getContent;
207 $token_content =~ s/[\x{2019}\x{2032}]/\'/go;
208 $isInNE = $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId);
209 $word_end = $token->getId;
210 # warn "(2a) -- $isInNE - " . $words[$word_idx] . " / $word_idx / " . $token_content . " / " . $token->getType . " (word $word_idx / token $doc_idx)\n";
211 while(index($words[$word_idx], $token_content, $word_shift) == $word_shift) {
212 push @Word2tokens, $token;
213 $IsNE ||= $isInNE;
214 $word_form .= $token->getContent;
215 $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
216 $word_shift += length($token_content);
217 $doc_idx++;
218 if ($doc_idx >= scalar(@{$document->getAnnotations->getTokenLevel->getElements})) {
219 $word_end = $token->previous->getId;
220 next;
221 }
222 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
223 # $token_content = Encode::encode("iso-8859-1", $token->getContent, Encode::FB_DEFAULT);
224 $token_content = $token->getContent;
225 $token_content =~ s/[\x{2019}\x{2032}]/\'/go;
226 $isInNE = $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId);
227 $word_end = $token->getId;
228 # warn "(2b) -- $isInNE - " . $words[$word_idx] . " / $word_idx / " . $token_content . " / " . $token->getType . " (word $word_idx / token $doc_idx / $word_shift : $word_form)\n";
229 }
230 }
231 $doc_idx--;
232 # if (index($words[$word_idx], $token_content, $word_shift) == $word_shift) {
233
234 # }
235 # warn "(2c) -- $isInNE - " . $words[$word_idx] . " / $word_idx / " . $token_content . " / " . $token->getType . " (word $word_idx / token $doc_idx / $word_shift : $word_form)\n";
236  
237 if (!$isInNE) {
238 if ($word_type != 0) {
239 my $stok = $Word2tokens[0];
240 my $etok = $Word2tokens[$#Word2tokens];
241 @Word2tokens = ();
242 push @Word2tokens, $stok;
243 $word_form = $stok->getContent;
244 while((!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $stok->getId)) &&
245 (!$stok->equals($etok))) {
246 $stok = $stok->next;
247 $word_form .= $stok->getContent;
248 push @Word2tokens, $stok;
249 }
250 if ($document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $stok->getId)) {
251 while(!$stok->equals($etok)) {
252 $doc_idx--;
253 $etok = $etok->previous;
254 }
255 }
256 # warn "=> $word_form (3a)\n";
257 $word = Lingua::Ogmios::Annotations::Word->new(
258 {'form' => $word_form,
259 'list_refid_token' => \@Word2tokens,
260 'isNE' => $IsNE,
261 });
262 $document->getAnnotations->addWord($word);
263 } else {
264 # warn "Go here\n";
265 # TODO XXXX
266 }
267 @Word2tokens = ();
268 $IsNE = 0;
269 $word_shift = 0;
270 $word_form = "";
271 $word_type = 0;
272  
273 $isInNE = 0;
274 }
275 while(($doc_idx + 1< scalar(@{$document->getAnnotations->getTokenLevel->getElements})) &&
276 ($document->getAnnotations->getTokenLevel->getElements->[$doc_idx+1]->isSep)) {
277 $doc_idx++;
278 }
279 $word_idx++;
280 } else {
281 # warn "Check special cases ($token_content)\n";
282 # warn "(4a) -- $isInNE - " . $words[$word_idx] . " / $word_idx / " . $token->getContent . " / " . $token->getType . " (word $word_idx / token $doc_idx)\n";
283 if (($word_idx < scalar(@words)) && (index($token_content, $words[$word_idx]) == 0)
284 && ($token->next->getContent eq "'")) {
285 # probably a elision
286 # warn "probably a elision\n";
287 push @Word2tokens, $token;
288 $IsNE ||= $isInNE;
289  
290 $word_start = $token->getId;
291 $word_form = $words[$word_idx];
292 $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
293 $word_shift = length($token_content);
294  
295 if (($self->_documentSet->[0]->getAnnotations->getLanguage eq "EN") &&
296 (lc($word_form) eq "ca")){
297 $word_form .= "n";
298 # warn "correction ca + n't as can + n't\n";
299 }
300  
301 my $stok = $Word2tokens[0];
302 my $etok = $Word2tokens[$#Word2tokens];
303 @Word2tokens = ();
304 push @Word2tokens, $stok;
305 $word_form = $stok->getContent;
306 while(!$stok->equals($etok)) {
307 $stok = $stok->next;
308 $word_form .= $stok->getContent;
309 push @Word2tokens, $stok;
310 }
311  
312 $word = Lingua::Ogmios::Annotations::Word->new(
313 {'form' => $word_form,
314 'list_refid_token' => \@Word2tokens,
315 'isNE' => $IsNE,
316 });
317 $document->getAnnotations->addWord($word);
318 # warn "(b2) Add word \"$word_form\" ($word_type)\n";
319 @Word2tokens = ();
320 $IsNE = 0;
321 $word_form = "";
322 $word_type = 0;
323  
324 push @Word2tokens, $token;
325 if ($token_content ne $words[$word_idx]) {
326 $word_form = substr($token->getContent, length($words[$word_idx]));
327 }
328 $word_idx++;
329 # warn "word_form: $word_form\n";
330 $doc_idx++;
331 while($word_form ne $words[$word_idx]) {
332 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
333 # $token_content = Encode::encode("iso-8859-1", $token->getContent, Encode::FB_DEFAULT);
334 $token_content = $token->getContent;
335 $token_content =~ s/[\x{2019}\x{2032}]/\'/go;
336 $isInNE ||= $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId);
337 push @Word2tokens, $token;
338 $word_form .= $token->getContent;
339 # warn "word_form: $word_form\n";
340 $doc_idx++;
341 }
342 $stok = $Word2tokens[0];
343 $etok = $Word2tokens[$#Word2tokens];
344 @Word2tokens = ();
345 push @Word2tokens, $stok;
346 $word_form = $stok->getContent;
347 while (!$stok->equals($etok)) {
348 $stok = $stok->next;
349 $word_form .= $stok->getContent;
350 push @Word2tokens, $stok;
351 }
352 $word = Lingua::Ogmios::Annotations::Word->new(
353 {'form' => $word_form,
354 'list_refid_token' => \@Word2tokens,
355 'isNE' => $IsNE,
356 });
357 $document->getAnnotations->addWord($word);
358 # warn "(b3) Add word \"$word_form\" ($word_type)\n";
359 @Word2tokens = ();
360 $IsNE = 0;
361 $word_form = "";
362 $word_type = 0;
363 $word_idx++;
364 if (!$document->getAnnotations->getTokenLevel->getElements->[$doc_idx]->isSep) {
365 $doc_idx--;
366 }
367 # warn "End of probably elision\n";
368 } else {
369 if ($isInNE) {
370 # warn "Named Entity\n";
371 push @Word2tokens, $token;
372 $IsNE ||= $isInNE;
373 $word_form .= $token->getContent;
374 $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
375 $word_shift += length($token_content);
376 } else {
377 # warn "word_type: $word_type\n";
378 if ($word_type != 0) {
379 # warn "(b4) Add word \"$word_form\" ($word_type)\n";
380 my $stok = $Word2tokens[0];
381 my $etok = $Word2tokens[$#Word2tokens];
382 @Word2tokens = ();
383 push @Word2tokens, $stok;
384 $word_form = $stok->getContent;
385 while(!$stok->equals($etok)) {
386 $stok = $stok->next;
387 $word_form .= $stok->getContent;
388 push @Word2tokens, $stok;
389 }
390 $word = Lingua::Ogmios::Annotations::Word->new(
391 {'form' => $word_form,
392 'list_refid_token' => \@Word2tokens,
393 'isNE' => $IsNE,
394 });
395 $document->getAnnotations->addWord($word);
396 } else {
397 my $simple_word_form = "";
398 if (($word_idx < scalar(@words)) && (index($token_content, $words[$word_idx]) == 0)) {
399 # warn "Seg word is inside current token\n";
400 # warn "Usually it's foreign words\n";
401 do {
402 $simple_word_form .= $words[$word_idx];
403 $word_idx++;
404 } while(($word_idx < scalar(@words)) && ($token_content ne $simple_word_form));
405 # } else {
406 # warn "(5aa) -- $isInNE - " . $words[$word_idx] . " / " . $token->getContent . " / " . $token->getType . " (word $word_idx / token $doc_idx)\n";
407 # if (($word_idx < scalar(@words)) && (index($words[$word_idx], $token_content))) {
408 # warn "current token is inside Seg word\n";
409 # warn scalar(@Word2tokens) . "\n";
410 # # if ($document->getAnnotations->getTokenLevel->getElements->[$doc_idx]->isSep) {
411 # # $word_form = $token->getContent;
412 # # $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
413 # # $word_start = $token->getId;
414 # # push @Word2tokens, $token;
415 # # $word_idx++;
416 # # }
417 # # warn "Usually it's foreign words\n";
418 # }
419  
420 # warn "(5a) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . " / " . $token->getType . " (word $word_idx / token $doc_idx)\n";
421 # while(($doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements})) &&
422 # ($document->getAnnotations->getTokenLevel->getElements->[$doc_idx]->isSep)) {
423 # $doc_idx++;
424 # }
425 # if (Encode::decode("iso-8859-1", $words[$word_idx], Encode::FB_DEFAULT) =~ /^\s+$/o) {
426 # while(($doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements})) &&
427 # (!$document->getAnnotations->getTokenLevel->getElements->[$doc_idx]->isSep)) {
428 # $doc_idx++;
429 # warn "(5b) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . " / " . $token->getType . " (word $word_idx / token $doc_idx)\n";
430 # }
431 # $word_idx++;
432 # }
433 # warn "(5c) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . " / " . $token->getType . " (word $word_idx / token $doc_idx)\n";
434 }
435 }
436 # warn "out\n";
437 @Word2tokens = ();
438 $IsNE = 0;
439 $word_shift = 0;
440 $word_form = "";
441 $word_type = 0;
442  
443 $isInNE = 0;
444  
445 }
446 }
447 }
448 }
449 }
450  
451  
452 }
453  
454 sub run {
455 my ($self, $documentSet) = @_;
456  
457 my $word;
458 my $document;
459 # Set variables according the the configuration
460  
461 warn "[LOG] Perl Word segmentizer... \n";
462 $self->_documentSet($documentSet);
463  
464 if ($documentSet->[0]->getAnnotations->existsWordLevel) {
465 warn "words exist in the first document\n";
466 warn " Assuming that no word segmentation is required for the current document set\n";
467 # return(0);
468 } else {
469 # Generating the input for one document
470 $self->_inputPerlWS;
471 # Running the Perl segmentizer
472 my $command_line = $self->_processPerlSW;
473 if (defined $command_line) {
474 # Parsing the output
475 $self->_outputParsing;
476 }
477 # Put log information
478 my $information = { 'software_name' => 'Perl word segmentizer',
479 'comments' => 'Word Segmentation\n',
480 'command_line' => "$command_line",
481 'list_modified_level' => ['word_level'],
482 };
483
484 $self->_log($information);
485
486 my $document;
487 foreach $document (@{$documentSet}) {
488 $document->getAnnotations->addLogProcessing(
489 Lingua::Ogmios::Annotations::LogProcessing->new(
490 { 'comments' => 'Found ' . $document->getAnnotations->getWordLevel->getSize . ' words \n',
491 }
492 )
493 );
494 }
495 }
496 if (($self->_position eq "last") && ($self->_no_standard_output)) {
497 warn "print no standard output\n";
498 foreach $document (@{$documentSet}) {
499 foreach $word (@{$document->getAnnotations->getWordLevel->getElements}) {
500 print $word->getForm . "\n";
501 }
502 }
503 }
504  
505 $self->getTimer->_printTimesInLine($self->_config->name);
506 warn "[LOG] done\n";
507 }
508  
509  
510 1;
511  
512 __END__