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

filter: Statementstmt 0.0% 0 / 313 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 71 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 437 SCAR 60.4
CC
20
Cov
0%
_outputParsing
272.0
run
20.0
_processAwkSW
6.0
CRAP 420.0
1 package Lingua::Ogmios::NLPWrappers::AwkWordSegmentizer;
2  
3  
4 our $VERSION='0.1';
5  
6  
7 use Lingua::Ogmios::NLPWrappers::Wrapper;
8  
9 use Lingua::Ogmios::Annotations::Word;
10  
11 use Encode qw(:fallbacks);;
12  
13 use strict;
14 use warnings;
15  
16 our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper);
17  
18 sub new {
19 my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output) = @_;
20  
21 warn "[LOG] Creating a wrapper of the Awk Word segmentizer\n";
22  
23 my $AwkWordseg = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output);
24  
25 $AwkWordseg->_input_filename($tmpfile_prefix . ".awkWSeg.in");
26 $AwkWordseg->_output_filename($tmpfile_prefix . ".awkWSeg.out");
27  
28 unlink($AwkWordseg->_input_filename);
29 unlink($AwkWordseg->_output_filename);
30  
31 return($AwkWordseg);
32  
33 }
34  
35 sub _inputAwkWS {
36 my ($self) = @_;
37  
38 my $token;
39 my $corpus_in = "";
40 my $document;
41  
42 warn "[LOG] making Awk Word segmentizer input\n";
43  
44 foreach $document (@{$self->_documentSet}) {
45 foreach $token (@{$document->getAnnotations->getTokenLevel->getElements}) {
46 # warn "$token: " . $token->getContent . ";\n";
47 $corpus_in .= $token->getContent;
48 }
49 }
50 $corpus_in =~ s/[\x{A0}\x{2000}-\x{200B}]/ /go;
51 $corpus_in =~ s/\x{0153}/oe/go;
52 $corpus_in =~ s/\x{0152}/OE/go;
53
54  
55 open FILE_IN, ">" . $self->_input_filename;
56
57  
58 print FILE_IN Encode::encode("iso-8859-1", $corpus_in, Encode::FB_DEFAULT); #$corpus_in;
59  
60 close FILE_IN;
61  
62 warn "[LOG] done\n";
63 }
64  
65  
66 sub _processAwkSW {
67 my ($self, $lang) = @_;
68  
69 warn "[LOG] wordsegmentation with a awk\n";
70  
71 # warn "\tLanguage for the documentSet: " . $self->_documentSet->[0]->getAnnotations->getLanguage . "\n";
72 $lang = $self->_documentSet->[0]->getAnnotations->getLanguage;
73  
74 if (defined $self->_config->commands($lang)) {
75 # warn $self->_config->commands($lang)->{WSSegmentizer_CMD} . "\n";;
76  
77 return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{WordSegmentizer_CMD} . " < " . $self->_input_filename . ">" . $self->_output_filename)));
78 } else {
79 warn "No segmentizer defined for $lang\n";
80 return(undef);
81 }
82 warn "[LOG]\n";
83 }
84  
85 sub _outputParsing {
86 my ($self) = @_;
87  
88 my $line;
89 my @words;
90 my $token;
91  
92 my $doc_idx;
93 my $word_shift;
94 my $word_idx;
95 my $full_idx;
96 my $full;
97  
98 my $isInNE;
99 my $IsNE;
100  
101 my $word_start = 0;
102 my $word_end = 0;
103  
104 my $word;
105  
106 my $token_content1;
107  
108 my @Word2tokens;
109 my $word_type;
110  
111 my $document;
112  
113 my $word_form;
114  
115 warn "[LOG] . Parsing " . $self->_output_filename . "\n";
116  
117 open FILE_OUT, $self->_output_filename or warn "Can't open the file " . $self->_output_filename;;
118 # binmode(FILE_OUT, ":utf8");
119  
120 while($line = <FILE_OUT>) {
121 chomp $line;
122 if ($line ne "") {
123 push @words, $line;
124 }
125  
126 }
127 close FILE_OUT;
128  
129 $word_type = 0;
130 $word_idx = 0;
131 $word_shift = 0;
132 $isInNE = 0;
133 $IsNE = 0;
134  
135 my $token_content;
136  
137 foreach $document (@{$self->_documentSet}) {
138 $doc_idx = 0;
139  
140 # warn "Processing document " . $document ->getId . "\n";
141  
142 @Word2tokens = ();
143 $word_shift = 0;
144 $word_form = "";
145 $word_type = 0;
146 for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) {
147 # warn "\n";
148 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
149 $token_content1 = $token->getContent;
150 $token_content1 =~ s/\x{0153}/oe/go;
151 $token_content1 =~ s/\x{0152}/OE/go;
152 $token_content = Encode::encode("iso-8859-1", $token_content1, Encode::FB_DEFAULT);
153 # warn "(-1) -- " . ($isInNE*1) . " " . $token->getContent . " / $token_content (word $word_idx / token $doc_idx)\n";
154 # warn " == " . scalar(@Word2tokens) . "\n";
155 $isInNE = $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId);
156 # warn "(0) -- " . ($isInNE*1) . " " . $token->getContent . " / $token_content (word $word_idx / token $doc_idx)\n";
157  
158 if ((!$isInNE) && (scalar(@Word2tokens) != 0)) {
159 # warn "(b1) Add word \"$word_form\" ($word_type)\n";
160  
161 my $stok = $Word2tokens[0];
162 my $etok = $Word2tokens[$#Word2tokens];
163 @Word2tokens = ();
164 push @Word2tokens, $stok;
165 $word_form = $stok->getContent;
166 while(!$stok->equals($etok)) {
167 $stok = $stok->next;
168 $word_form .= $stok->getContent;
169 push @Word2tokens, $stok;
170 }
171  
172 $word = Lingua::Ogmios::Annotations::Word->new(
173 {'form' => $word_form,
174 'list_refid_token' => \@Word2tokens,
175 'isNE' => $IsNE,
176 });
177 $document->getAnnotations->addWord($word);
178 @Word2tokens = ();
179 $IsNE = 0;
180 $word_shift = 0;
181 $word_form = "";
182 $word_type = 0;
183 $isInNE = 0;
184 }
185 # if ($word_idx < scalar(@words)) {
186 # warn "(0b) " . $words[$word_idx] . ", " . $token->getContent . " / $token_content\n";
187 # }
188
189 if (($word_idx < scalar(@words)) && (index($words[$word_idx], $token_content) == 0)) {
190 # warn "in if\n";
191 push @Word2tokens, $token;
192 $IsNE ||= $isInNE;
193  
194 $word_start = $token->getId;
195 $word_form .= $token->getContent;
196 $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
197 $word_shift = length($token_content);
198 $doc_idx++;
199 if ($doc_idx >= scalar(@{$document->getAnnotations->getTokenLevel->getElements})) {
200 # warn "out\n";
201 $word_end = $token->previous->getId;
202 # next;
203 } else {
204  
205 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
206 $token_content = Encode::encode("iso-8859-1", $token->getContent, Encode::FB_DEFAULT);
207 $isInNE = $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId);
208 $word_end = $token->getId;
209 # warn " (1) -- $isInNE " . $token->getContent . "(word $word_idx / token $doc_idx)\n";
210 while(index($words[$word_idx], $token_content, $word_shift) == $word_shift) {
211 # warn $token->getContent . "\n";
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 # warn "out\n";
220 $word_end = $token->previous->getId;
221 next;
222 }
223 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
224 $token_content = Encode::encode("iso-8859-1", $token->getContent, Encode::FB_DEFAULT);
225 $isInNE = $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId);
226 $word_end = $token->getId;
227
228 # warn "\t(2a) -- $isInNE " . $token->getContent . "(word $word_idx / token $doc_idx)\n";
229 }
230 }
231 $doc_idx--;
232 # warn "(3) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . "(word $word_idx / token $doc_idx)\n";
233  
234 if (!$isInNE) {
235 if ($word_type != 0) {
236 # warn "Add word \"$word_form\" ($word_type)" . "(word $word_idx / token $doc_idx)\n";
237 my $stok = $Word2tokens[0];
238 my $etok = $Word2tokens[$#Word2tokens];
239 @Word2tokens = ();
240 push @Word2tokens, $stok;
241 $word_form = $stok->getContent;
242 while((!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $stok->getId)) &&
243 (!$stok->equals($etok))) {
244 $stok = $stok->next;
245 $word_form .= $stok->getContent;
246 push @Word2tokens, $stok;
247 }
248 if ($document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $stok->getId)) {
249 while(!$stok->equals($etok)) {
250 $doc_idx--;
251 $etok = $etok->previous;
252 }
253 }
254 $word = Lingua::Ogmios::Annotations::Word->new(
255 {'form' => $word_form,
256 'list_refid_token' => \@Word2tokens,
257 'isNE' => $IsNE,
258 });
259 $document->getAnnotations->addWord($word);
260 } else {
261 # warn "Go here\n";
262 # TODO XXXX
263 }
264 @Word2tokens = ();
265 $IsNE = 0;
266 $word_shift = 0;
267 $word_form = "";
268 $word_type = 0;
269  
270 $isInNE = 0;
271 }
272 # warn "(4a) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . "(word $word_idx / token $doc_idx)\n";
273 while(($doc_idx + 1< scalar(@{$document->getAnnotations->getTokenLevel->getElements})) &&
274 ($document->getAnnotations->getTokenLevel->getElements->[$doc_idx+1]->isSep)) {
275 $doc_idx++;
276 }
277 # do {
278 $word_idx++;
279 # warn "(W) " . $words[$word_idx] . "\n";
280 # } while(($word_idx < scalar(@words)) && (Encode::decode("iso-8859-1", $words[$word_idx], Encode::FB_DEFAULT) =~ /^\s+$/o));
281  
282 # warn "(4b) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . "(word $word_idx / token $doc_idx)\n";
283 } else {
284 # warn "Check special cases\n";
285 if (($word_idx < scalar(@words)) && (index($token_content, $words[$word_idx]) == 0)
286 && ($token->next->getContent eq "'")) {
287 # probably a elision
288 # warn "probably a elision\n";
289 push @Word2tokens, $token;
290 $IsNE ||= $isInNE;
291  
292 $word_start = $token->getId;
293 $word_form = $words[$word_idx];
294 $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
295 $word_shift = length($token_content);
296  
297 if (($self->_documentSet->[0]->getAnnotations->getLanguage eq "EN") &&
298 (lc($word_form) eq "ca")){
299 $word_form .= "n";
300 # warn "correction ca + n't as can + n't\n";
301 }
302  
303 my $stok = $Word2tokens[0];
304 my $etok = $Word2tokens[$#Word2tokens];
305 @Word2tokens = ();
306 push @Word2tokens, $stok;
307 $word_form = $stok->getContent;
308 while(!$stok->equals($etok)) {
309 $stok = $stok->next;
310 $word_form .= $stok->getContent;
311 push @Word2tokens, $stok;
312 }
313  
314 $word = Lingua::Ogmios::Annotations::Word->new(
315 {'form' => $word_form,
316 'list_refid_token' => \@Word2tokens,
317 'isNE' => $IsNE,
318 });
319 $document->getAnnotations->addWord($word);
320 # warn "(b2) Add word \"$word_form\" ($word_type)\n";
321 @Word2tokens = ();
322 $IsNE = 0;
323 $word_form = "";
324 $word_type = 0;
325  
326 push @Word2tokens, $token;
327 if ($token_content ne $words[$word_idx]) {
328 $word_form = substr($token->getContent, length($words[$word_idx]));
329 }
330 $word_idx++;
331 # warn "word_form: $word_form\n";
332 $doc_idx++;
333 while($word_form ne $words[$word_idx]) {
334 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
335 $token_content = Encode::encode("iso-8859-1", $token->getContent, Encode::FB_DEFAULT);
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] Awk 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->_inputAwkWS;
471 # Running the awk segmentizer
472 my $command_line = $self->_processAwkSW;
473 if (defined $command_line) {
474 # Parsing the output
475 $self->_outputParsing;
476 }
477 # Put log information
478 my $information = { 'software_name' => 'awk 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 warn "[LOG] done\n";
505 }
506  
507  
508 1;
509  
510  
511 __END__