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

filter: Statementstmt 0.0% 0 / 246 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 68 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 407 CC 18 SCAR 49.1
CC
18
Cov
0%
_outputParsing
240.0
run
12.0
_processUKSW
6.0
CRAP 135.6
1 package Lingua::Ogmios::NLPWrappers::UKWordSegmentizer;
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 UK Word segmentizer\n";
22  
23  
24 my $UKWordseg = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output);
25  
26 $UKWordseg->_input_filename($tmpfile_prefix . ".ukWSeg.in");
27 $UKWordseg->_output_filename($tmpfile_prefix . ".ukWSeg.out");
28  
29 return($UKWordseg);
30  
31 }
32  
33 sub _inputUKWS {
34 my ($self) = @_;
35  
36 my $token;
37 my $corpus_in = "";
38 my $document;
39  
40 warn "[LOG] making UK Word segmentizer input\n";
41  
42 foreach $document (@{$self->_documentSet}) {
43 foreach $token (@{$document->getAnnotations->getTokenLevel->getElements}) {
44 # warn "$token: " . $token->getContent . ";\n";
45 $corpus_in .= $token->getContent;
46 }
47 }
48 $corpus_in =~ s/\x{A0}/ /go;
49
50  
51 open FILE_IN, ">" . $self->_input_filename;
52
53  
54 print FILE_IN Encode::encode("UTF-8", $corpus_in, Encode::FB_DEFAULT); #$corpus_in;
55  
56 close FILE_IN;
57  
58 warn "[LOG] done\n";
59 }
60  
61  
62 sub _processUKSW {
63 my ($self, $lang) = @_;
64  
65 warn "[LOG] wordsegmentation with a uk\n";
66  
67 # warn "\tLanguage for the documentSet: " . $self->_documentSet->[0]->getAnnotations->getLanguage . "\n";
68 $lang = $self->_documentSet->[0]->getAnnotations->getLanguage;
69  
70 if (defined $self->_config->commands($lang)) {
71 # warn $self->_config->commands($lang)->{WSSegmentizer_CMD} . "\n";;
72  
73 return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{WordSegmentizer_CMD} . " < " . $self->_input_filename . ">" . $self->_output_filename)));
74 } else {
75 warn "No segmentizer defined for $lang\n";
76 return(undef);
77 }
78 warn "[LOG]\n";
79 }
80  
81 sub _outputParsing {
82 my ($self) = @_;
83  
84 my $line;
85 my @words;
86 my $token;
87  
88 my $doc_idx;
89 my $word_shift;
90 my $word_idx;
91 my $full_idx;
92 my $full;
93  
94 my $isInNE;
95 my $IsNE;
96  
97 my $word_start = 0;
98 my $word_end = 0;
99  
100 my $word;
101  
102 my @Word2tokens;
103 my $word_type;
104  
105 my $document;
106  
107 my $word_form;
108  
109 warn "[LOG] . Parsing " . $self->_output_filename . "\n";
110  
111 open FILE_OUT, $self->_output_filename or warn "Can't open the file " . $self->_output_filename;;
112 # binmode(FILE_OUT, ":utf8");
113  
114 while($line = <FILE_OUT>) {
115 chomp $line;
116 if ($line ne "") {
117 push @words, $line;
118 }
119  
120 }
121 close FILE_OUT;
122  
123 $word_type = 0;
124 $word_idx = 0;
125 $word_shift = 0;
126 $isInNE = 0;
127 $IsNE = 0;
128  
129 my $token_content;
130  
131 foreach $document (@{$self->_documentSet}) {
132 $doc_idx = 0;
133  
134 # warn "Processing document " . $document ->getId . "\n";
135  
136 @Word2tokens = ();
137 $word_shift = 0;
138 $word_form = "";
139 $word_type = 0;
140 for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) {
141 # warn "\n";
142 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
143 $token_content = Encode::encode("iso-8859-1", $token->getContent, Encode::FB_DEFAULT);
144 $isInNE = $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId);
145 # warn "(0) -- " . ($isInNE*1) . " " . $token->getContent . " / $token_content (word $word_idx / token $doc_idx)\n";
146  
147 if ((!$isInNE) && (scalar(@Word2tokens) != 0)) {
148 # warn "(b) Add word \"$word_form\" ($word_type)\n";
149 $word = Lingua::Ogmios::Annotations::Word->new(
150 {'form' => $word_form,
151 'list_refid_token' => \@Word2tokens,
152 'isNE' => $IsNE,
153 });
154 $document->getAnnotations->addWord($word);
155 @Word2tokens = ();
156 $IsNE = 0;
157 $word_shift = 0;
158 $word_form = "";
159 $word_type = 0;
160 $isInNE = 0;
161 }
162 # if ($word_idx < scalar(@words)) {
163 # warn "(0b) " . $words[$word_idx] . ", " . $token->getContent . " / $token_content\n";
164 # }
165
166 if (($word_idx < scalar(@words)) && (index($words[$word_idx], $token_content) == 0)) {
167 # warn "in if\n";
168 push @Word2tokens, $token;
169 $IsNE ||= $isInNE;
170  
171 $word_start = $token->getId;
172 $word_form .= $token->getContent;
173 $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
174 $word_shift = length($token_content);
175 $doc_idx++;
176 if ($doc_idx >= scalar(@{$document->getAnnotations->getTokenLevel->getElements})) {
177 # warn "out\n";
178 $word_end = $token->previous->getId;
179 # next;
180 } else {
181  
182 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
183 $token_content = Encode::encode("iso-8859-1", $token->getContent, Encode::FB_DEFAULT);
184 $isInNE = $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId);
185 $word_end = $token->getId;
186 # warn " (1) -- $isInNE " . $token->getContent . "(word $word_idx / token $doc_idx)\n";
187 while(index($words[$word_idx], $token_content, $word_shift) == $word_shift) {
188 # warn $token->getContent . "\n";
189 push @Word2tokens, $token;
190 $IsNE ||= $isInNE;
191 $word_form .= $token->getContent;
192 $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
193 $word_shift += length($token_content);
194 $doc_idx++;
195 if ($doc_idx >= scalar(@{$document->getAnnotations->getTokenLevel->getElements})) {
196 # warn "out\n";
197 $word_end = $token->previous->getId;
198 next;
199 }
200 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
201 $token_content = Encode::encode("iso-8859-1", $token->getContent, Encode::FB_DEFAULT);
202 $isInNE = $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId);
203 $word_end = $token->getId;
204
205 # warn "\t(2a) -- $isInNE " . $token->getContent . "(word $word_idx / token $doc_idx)\n";
206 }
207 }
208 $doc_idx--;
209 # warn "(3) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . "(word $word_idx / token $doc_idx)\n";
210  
211 if (!$isInNE) {
212 if ($word_type != 0) {
213 # warn "Add word \"$word_form\" ($word_type)" . "(word $word_idx / token $doc_idx)\n";
214 $word = Lingua::Ogmios::Annotations::Word->new(
215 {'form' => $word_form,
216 'list_refid_token' => \@Word2tokens,
217 'isNE' => $IsNE,
218 });
219 $document->getAnnotations->addWord($word);
220 } else {
221 # warn "Go here\n";
222 # TODO XXXX
223 }
224 @Word2tokens = ();
225 $IsNE = 0;
226 $word_shift = 0;
227 $word_form = "";
228 $word_type = 0;
229  
230 $isInNE = 0;
231 }
232 # warn "(4a) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . "(word $word_idx / token $doc_idx)\n";
233 while(($doc_idx + 1< scalar(@{$document->getAnnotations->getTokenLevel->getElements})) &&
234 ($document->getAnnotations->getTokenLevel->getElements->[$doc_idx+1]->isSep)) {
235 $doc_idx++;
236 }
237 # do {
238 $word_idx++;
239 # warn "(W) " . $words[$word_idx] . "\n";
240 # } while(($word_idx < scalar(@words)) && (Encode::decode("iso-8859-1", $words[$word_idx], Encode::FB_DEFAULT) =~ /^\s+$/o));
241  
242 # warn "(4b) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . "(word $word_idx / token $doc_idx)\n";
243 } else {
244 # warn "Check special cases\n";
245 if (($word_idx < scalar(@words)) && (index($token_content, $words[$word_idx]) == 0)
246 && ($token->next->getContent eq "'")) {
247 # probably a elision
248 # warn "probably a elision\n";
249 push @Word2tokens, $token;
250 $IsNE ||= $isInNE;
251  
252 $word_start = $token->getId;
253 $word_form = $words[$word_idx];
254 $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
255 $word_shift = length($token_content);
256  
257 if (($self->_documentSet->[0]->getAnnotations->getLanguage eq "EN") &&
258 (lc($word_form) eq "ca")){
259 $word_form .= "n";
260 # warn "correction ca + n't as can + n't\n";
261 }
262  
263 $word = Lingua::Ogmios::Annotations::Word->new(
264 {'form' => $word_form,
265 'list_refid_token' => \@Word2tokens,
266 'isNE' => $IsNE,
267 });
268 $document->getAnnotations->addWord($word);
269 # warn "(b) Add word \"$word_form\" ($word_type)\n";
270 @Word2tokens = ();
271 $IsNE = 0;
272 $word_form = "";
273 $word_type = 0;
274  
275 push @Word2tokens, $token;
276 if ($token_content ne $words[$word_idx]) {
277 $word_form = substr($token->getContent, length($words[$word_idx]));
278 }
279 $word_idx++;
280 # warn "word_form: $word_form\n";
281 $doc_idx++;
282 while($word_form ne $words[$word_idx]) {
283 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
284 $token_content = Encode::encode("iso-8859-1", $token->getContent, Encode::FB_DEFAULT);
285 $isInNE ||= $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId);
286 push @Word2tokens, $token;
287 $word_form .= $token->getContent;
288 # warn "word_form: $word_form\n";
289 $doc_idx++;
290 }
291 $word = Lingua::Ogmios::Annotations::Word->new(
292 {'form' => $word_form,
293 'list_refid_token' => \@Word2tokens,
294 'isNE' => $IsNE,
295 });
296 $document->getAnnotations->addWord($word);
297 # warn "(b) Add word \"$word_form\" ($word_type)\n";
298 @Word2tokens = ();
299 $IsNE = 0;
300 $word_form = "";
301 $word_type = 0;
302 $word_idx++;
303 if (!$document->getAnnotations->getTokenLevel->getElements->[$doc_idx]->isSep) {
304 $doc_idx--;
305 }
306 # warn "End of probably elision\n";
307 } else {
308 if ($isInNE) {
309 # warn "Named Entity\n";
310 push @Word2tokens, $token;
311 $IsNE ||= $isInNE;
312 $word_form .= $token->getContent;
313 $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
314 $word_shift += length($token_content);
315 } else {
316 # warn "word_type: $word_type\n";
317 if ($word_type != 0) {
318 # warn "(b) Add word \"$word_form\" ($word_type)\n";
319 $word = Lingua::Ogmios::Annotations::Word->new(
320 {'form' => $word_form,
321 'list_refid_token' => \@Word2tokens,
322 'isNE' => $IsNE,
323 });
324 $document->getAnnotations->addWord($word);
325 } else {
326 my $simple_word_form = "";
327 if (($word_idx < scalar(@words)) && (index($token_content, $words[$word_idx]) == 0)) {
328 # warn "Seg word is inside current token\n";
329 # warn "Usually it's foreign words\n";
330 do {
331 $simple_word_form .= $words[$word_idx];
332 $word_idx++;
333 } while(($word_idx < scalar(@words)) && ($token_content ne $simple_word_form));
334 # } else {
335 # warn "(5aa) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . " / " . $token->getType . " (word $word_idx / token $doc_idx)\n";
336 # if (($word_idx < scalar(@words)) && (index($words[$word_idx], $token_content))) {
337 # warn "current token is inside Seg word\n";
338 # warn scalar(@Word2tokens) . "\n";
339 # # if ($document->getAnnotations->getTokenLevel->getElements->[$doc_idx]->isSep) {
340 # # $word_form = $token->getContent;
341 # # $word_type ||= (($token->getType ne "sep") && ($token->getType ne "symb"));
342 # # $word_start = $token->getId;
343 # # push @Word2tokens, $token;
344 # # $word_idx++;
345 # # }
346 # # warn "Usually it's foreign words\n";
347 # }
348  
349 # warn "(5a) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . " / " . $token->getType . " (word $word_idx / token $doc_idx)\n";
350 # while(($doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements})) &&
351 # ($document->getAnnotations->getTokenLevel->getElements->[$doc_idx]->isSep)) {
352 # $doc_idx++;
353 # }
354 # if (Encode::decode("iso-8859-1", $words[$word_idx], Encode::FB_DEFAULT) =~ /^\s+$/o) {
355 # while(($doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements})) &&
356 # (!$document->getAnnotations->getTokenLevel->getElements->[$doc_idx]->isSep)) {
357 # $doc_idx++;
358 # warn "(5b) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . " / " . $token->getType . " (word $word_idx / token $doc_idx)\n";
359 # }
360 # $word_idx++;
361 # }
362 # warn "(5c) -- $isInNE " . $words[$word_idx] . " / " . $token->getContent . " / " . $token->getType . " (word $word_idx / token $doc_idx)\n";
363 }
364 }
365 # warn "out\n";
366 @Word2tokens = ();
367 $IsNE = 0;
368 $word_shift = 0;
369 $word_form = "";
370 $word_type = 0;
371  
372 $isInNE = 0;
373  
374 }
375 }
376 }
377 }
378 }
379  
380  
381 }
382  
383 sub run {
384 my ($self, $documentSet) = @_;
385  
386 # Set variables according the the configuration
387 $self->_documentSet($documentSet);
388  
389 warn "[LOG] UK Word segmentizer... \n";
390  
391 warn "*** TODO: check if the level exists\n";
392  
393 # Generating the input for one document
394 $self->_inputUKWS;
395  
396 # Running the uk segmentizer
397 my $command_line = $self->_processUKSW;
398  
399 if (defined $command_line) {
400 # Parsing the output
401 $self->_outputParsing;
402  
403 # Put log information
404 my $information = { 'software_name' => 'uk word segmentizer',
405 'comments' => 'Word Segmentation\n',
406 'command_line' => "$command_line",
407 'list_modified_level' => ['word_level'],
408 };
409
410 $self->_log($information);
411  
412 my $document;
413 foreach $document (@{$documentSet}) {
414 $document->getAnnotations->addLogProcessing(
415 Lingua::Ogmios::Annotations::LogProcessing->new(
416 { 'comments' => 'Found ' . $document->getAnnotations->getWordLevel->getSize . ' words \n',
417 }
418 )
419 );
420 }
421 }
422 if (($self->_position eq "last") && ($self->_no_standard_output)) {
423 warn "print no standard output\n";
424 }
425 warn "[LOG] done\n";
426 }
427  
428 1;
429  
430 __END__