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

filter: Statementstmt 0.0% 0 / 338 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 491 CC 37 SCAR 44.7
CC
37
Cov
0%
_printStanfordParserFormatInput2
156.0
_outputStanfordParser
110.0
_printStanfordParserFormatInput
110.0
CRAP 87.4
1 package Lingua::Ogmios::NLPWrappers::StanfordParser;
2  
3  
4 our $VERSION='0.1';
5  
6  
7 use Lingua::Ogmios::NLPWrappers::Wrapper;
8 use Lingua::Ogmios::Annotations::SyntacticRelation;
9  
10 use strict;
11 use warnings;
12  
13 our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper Lingua::Ogmios::Annotations::SyntacticRelation);
14  
15 sub new {
16 my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output) = @_;
17  
18 warn "[LOG] Creating a wrapper of the StanfordParser\n";
19  
20  
21 my $StanfordParser = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output);
22  
23 $StanfordParser->_input_filename($tmpfile_prefix . ".StanfordParser.in");
24 $StanfordParser->_output_filename($tmpfile_prefix . ".StanfordParser.out");
25  
26 return($StanfordParser);
27  
28 }
29  
30 sub _processStanfordParser {
31 my ($self, $lang) = @_;
32  
33 warn "[LOG] StanfordParser\n";
34  
35 $lang = $self->_documentSet->[0]->getAnnotations->getLanguage;
36  
37 return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{STANFORDPARSER_CMD} . $self->_input_filename . ">" . $self->_output_filename)));
38  
39 warn "[LOG]\n";
40 }
41  
42 sub _inputStanfordParser {
43 my ($self) = @_;
44  
45 warn "[LOG] making StanfordParser input\n";
46  
47 $self->_printStanfordParserFormatInput2($self->_input_filename);
48
49 warn "[LOG] done\n";
50 }
51  
52  
53 sub _outputStanfordParser {
54 my ($self) = @_;
55  
56 warn "[LOG] . Parsing " . $self->_output_filename . "\n";
57  
58 # $self->{"ELTSBYSENT"}
59  
60 my $s;
61 my $r;
62 my $w1;
63 my $w2;
64 my $refW1;
65 my $refW2;
66  
67  
68 my $syntacticRelation;
69  
70 my $docId;
71 my $parsedDocs = $self->_parseSentences;
72  
73 foreach $docId (keys %$parsedDocs) {
74 my $parsedSentences = $parsedDocs->{$docId};
75 for($s = 0; $s < scalar(@$parsedSentences); $s++) {
76 # warn "s: $s\n";
77 for($r = 0; $r < scalar(@{$parsedSentences->[$s]}); $r++) {
78 # warn "r: $r\n";
79 # warn "rel: " . $parsedSentences->[$s]->[$r]->[0] . "\n";
80  
81 $w1 = $self->{"ELTSBYSENT"}->{$docId}->[$s]->[$parsedSentences->[$s]->[$r]->[1] - 1];
82 $w2 = $self->{"ELTSBYSENT"}->{$docId}->[$s]->[$parsedSentences->[$s]->[$r]->[2] - 1];
83  
84 # if ((ref($w1) eq "Lingua::Ogmios::Annotations::Word") && (ref($w2) eq "Lingua::Ogmios::Annotations::Word")) {
85 # # warn "add a syntactic relation between " . $w1->getForm . " and " . $w2->getForm . "(" . $parsedSentences->[$s]->[$r]->[0] . ")\n";
86 # $syntacticRelation = Lingua::Ogmios::Annotations::SyntacticRelation->new(
87 # { 'syntactic_relation_type' => $parsedSentences->[$s]->[$r]->[0],
88 # 'refid_word_head' => $w1,
89 # 'refid_word_modifier' => $w2,
90 # }
91 # );
92 # $self->getDocumentFromDocId($docId)->getAnnotations->addSyntacticRelation($syntacticRelation);
93 # # warn "Syntactic Relation: " . $syntacticRelation->getId . "\n";
94
95 # }
96 $refW1 = undef;
97 $refW2 = undef;
98 if (ref($w1) eq "Lingua::Ogmios::Annotations::Word") {
99 $refW1 = "refid_word_head";
100 } else {
101 if (ref($w1) eq "Lingua::Ogmios::Annotations::SemanticUnit") {
102 if (($w1->reference_name eq "refid_word") ||
103 ($w1->reference_name eq "refid_phrase")) {
104 $refW1 = $w1->reference_name . "_head";
105 $w1 = $w1->reference;
106 } elsif ($self->getDocumentFromDocId($docId)->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $w1->start_token->getId)) {
107 $refW1 = "refid_word_head";
108 $w1 = $self->getDocumentFromDocId($docId)->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $w1->start_token->getId)->[0];
109 }
110 }
111 }
112 if (ref($w2) eq "Lingua::Ogmios::Annotations::Word") {
113 $refW2 = "refid_word_modifier";
114 } else {
115 if (ref($w2) eq "Lingua::Ogmios::Annotations::SemanticUnit") {
116 if (($w2->reference_name eq "refid_word") ||
117 ($w2->reference_name eq "refid_phrase")) {
118 $refW2 = $w2->reference_name . "_modifier";
119 $w2 = $w2->reference;
120 } elsif ($self->getDocumentFromDocId($docId)->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $w2->start_token->getId)) {
121 $refW2 = "refid_word_modifier";
122 $w2 = $self->getDocumentFromDocId($docId)->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $w2->start_token->getId)->[0];
123 }
124 }
125 }
126  
127 if ((defined $refW1) && (defined $refW2)) {
128 # warn "add a syntactic relation between " . $w1->getForm . " and " . $w2->getForm . "(" . $parsedSentences->[$s]->[$r]->[0] . ")\n";
129 $syntacticRelation = Lingua::Ogmios::Annotations::SyntacticRelation->new(
130 { 'syntactic_relation_type' => $parsedSentences->[$s]->[$r]->[0],
131 $refW1 => $w1,
132 $refW2 => $w2,
133 }
134 );
135 $self->getDocumentFromDocId($docId)->getAnnotations->addSyntacticRelation($syntacticRelation);
136 # warn "Syntactic Relation: " . $syntacticRelation->getId . "\n";
137 }
138 }
139 }
140 }
141
142 warn "[LOG] done\n";
143 }
144  
145 sub _parseSentences {
146 my ($self) = @_;
147  
148 my %documents;
149  
150 my $line;
151 my @sentences;
152  
153 my $doc_id = undef;;
154 my $posDoc;
155 my $posComma;
156  
157 open FILE_OUT, $self->_output_filename or warn "Can't open the file " . $self->_output_filename;;
158 # binmode(FILE_OUT, ":utf8");
159  
160 my @sentenceTMP;
161  
162 while($line = <FILE_OUT>) {
163 chomp $line;
164  
165 if (($posDoc = index($line, "-DOCUMENT")) > 0) {
166 if (defined $doc_id) {
167 my @sents;
168 push @sents, @sentences;
169 $documents{$doc_id} = \@sents;
170 @sentences = ();
171 }
172 $posComma = rindex($line, ', ', $posDoc);
173 $doc_id = substr($line, $posComma + 2, $posDoc - $posComma - 2 );
174 warn "doc_id: $doc_id\n";
175 <FILE_OUT>;
176 } elsif ($line eq "") {
177 my @sentence;
178 push @sentence, @sentenceTMP;
179 push @sentences, \@sentence;
180 @sentenceTMP = ();
181 } else {
182 # push @sentenceTMP, $line;
183 push @sentenceTMP, $self->_parseLine($line);
184 }
185 }
186 if (defined $doc_id) {
187 warn "doc_id : $doc_id\n";
188 my @sents;
189 push @sents, @sentences;
190 $documents{$doc_id} = \@sents;
191 }
192  
193 close FILE_OUT;
194  
195 return(\%documents);
196  
197 }
198  
199 sub _parseLine {
200 my ($self, $line) = @_;
201  
202 my $posPar;
203 my $posComma;
204 my $posDash;
205  
206 my $synRel;
207 my $word1Num;
208 my $word2Num;
209 my $posQuote;
210  
211 my $shift = 0;
212 my @rel;
213  
214 # warn "$line\n";
215  
216 # find first (
217 $posPar = index($line, '(');
218 $synRel = substr($line, 0, $posPar);
219 # warn "$synRel\n";
220 push @rel, $synRel;
221  
222 # find Comma
223 $posComma = index($line, ', ', $posPar + 1);
224 $posDash = rindex($line, '-', $posComma);
225 $shift = 0;
226 # warn "(1) dash: $posDash\n";
227 if ((($posQuote = index($line, "'", $posDash)) > -1) && ($posQuote < $posComma)) {
228 $shift = 1;
229 }
230 # warn "(1) shift: $shift ($posQuote / $posComma)\n";
231 # warn "(2) dash: $posDash\n";
232 # warn "(1) search for " . ($posDash + 1) . " and " . ($posComma - $posDash - 1 - $shift) . "\n";
233 $word1Num = substr($line, $posDash + 1, $posComma - $posDash - 1 - $shift);
234 # warn ($word1Num - 1 + 1) . "\n";
235 # warn "$word1Num\n";
236 push @rel, $word1Num;
237  
238 # find )
239 $posPar = length($line) - 1;
240 $posDash = rindex($line, '-', $posPar);
241 $shift = 0;
242 # warn "(3) dash: $posDash\n";
243 if ((($posQuote = index($line, "'", $posDash)) > -1) && ($posQuote < $posPar)) {
244 $shift = 1;
245 }
246 # warn "(2) shift: $shift ($posQuote / $posComma)\n";
247 # warn "(4) dash: $posDash\n";
248 # warn "(2) search for " . ($posDash + 1) . " and " . ($posPar - $posDash - 1 - $shift) . "\n";
249  
250 $word2Num = substr($line, $posDash + 1, $posPar - $posDash - 1 - $shift);
251 # warn ($word2Num - 1 + 1) . "\n";
252 # warn "$word2Num\n";
253 push @rel, $word2Num;
254  
255 # warn "\n";
256 return(\@rel);
257 }
258  
259 sub run {
260 my ($self, $documentSet) = @_;
261  
262 warn "*** TODO: check if the level exists\n";
263 # Set variables according the the configuration
264  
265 $self->_documentSet($documentSet);
266  
267 warn "[LOG] " . $self->_config->comments . " ... \n";
268  
269 $self->_inputStanfordParser;
270  
271 my $command_line = $self->_processStanfordParser;
272  
273 $self->_outputStanfordParser;
274 # if ($self->_position eq "last") {
275 # # TODO
276  
277 # Put log information
278 my $information = { 'software_name' => $self->_config->name,
279 'comments' => $self->_config->comments,
280 'command_line' => $command_line,
281 'list_modified_level' => ['syntactic_relation_level'],
282 };
283 $self->_log($information);
284  
285 if (($self->_position eq "last") && ($self->_no_standard_output)) {
286 warn "print no standard output\n";
287 # $self->_outputStanfordParser;
288 }
289  
290 warn "[LOG] done\n";
291 }
292  
293 sub _printStanfordParserFormatInput {
294 my $self = shift;
295 my $filename = shift;
296 my $encoding = shift;
297  
298 my $document;
299 my $doc_idx;
300 my $token;
301 my $word;
302 my $lemma;
303 my $MS_features;
304 my @corpus_in_t;
305  
306 my @sentElt;
307  
308 my %corpusDocuments;
309  
310  
311 warn "[LOG] printing TreeTagger Like Output\n";
312  
313 foreach $document (@{$self->_documentSet}) {
314  
315 my @corpus_inRefElementSent;
316 $corpusDocuments{$document->getId} = \@corpus_inRefElementSent;
317 my @tmp = ($document->getId . "-DOCUMENT/NNP :", ".\n"); # , $document->getId
318 push @corpus_in_t, \@tmp;
319
320 for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) {
321 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
322 if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
323 $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0];
324 $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0];
325 my $wordform = $word->getForm;
326 $wordform =~ s/[\t\n]/ /gos;
327 $wordform =~ s/ +/_/gos;
328  
329 my $MS_f = $MS_features->syntactic_category;
330 if ($MS_f eq "NE") {
331 $MS_f = "NN";
332 }
333 my @tmp = ($wordform, $MS_f); # , $lemma->canonical_form
334  
335 push @corpus_in_t, \@tmp;
336  
337 push @sentElt, $word;
338  
339 $doc_idx += $word->getReferenceSize - 1;
340 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
341
342 } else {
343 if (!($token->isSep)) {
344 my @tmp = ($token->getContent, $token->getContent); # , $token->getContent
345 push @corpus_in_t, \@tmp;
346 push @sentElt, $token;
347 }
348 }
349 if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) {
350 if ($token->isSymb) {
351 if (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) {
352 my @tmp = ($corpus_in_t[$#corpus_in_t]->[0] , ".\n"); # , $corpus_in_t[$#corpus_in_t]->[0]
353 $corpus_in_t[$#corpus_in_t] = \@tmp;
354 } else {
355 my @tmp = ($token->getContent, ".\n"); # , $token->getContent
356 push @corpus_in_t, \@tmp;
357 push @sentElt, $token;
358 }
359 } else {
360 my @tmp = (".", ".\n"); # , "."
361 push @corpus_in_t, \@tmp;
362 push @sentElt, "";
363 }
364 my @senttmp;
365 push @senttmp, @sentElt;
366 push @corpus_inRefElementSent, \@senttmp;
367 @sentElt = ();
368 }
369 }
370 }
371  
372 $self->{"ELTSBYSENT"} = \%corpusDocuments;
373  
374 warn "\tOpenning " . $filename . "\n";
375  
376 open FILE_IN, ">" . $filename or die "can't open " . $filename . "\n";
377  
378 my $str_out;
379  
380 my $word_ref;
381 foreach $word_ref (@corpus_in_t) {
382 if (scalar(@$word_ref) == 2) {
383 # Encode::encode("iso-8859-1", join("\n",@corpus_in_t), Encode::FB_DEFAULT);
384 if ((!defined $encoding) || (uc($encoding) eq "UTF-8")) {
385 $str_out = Encode::encode("UTF-8", join("/",@$word_ref)) . " ";
386 $str_out =~ s/\n /\n/o;
387  
388 print FILE_IN $str_out;
389 } else {
390 if ((defined $encoding) && (uc($encoding) eq "LATIN1")) {
391 # print FILE_IN Encode::encode("iso-8859-1", join("\t",@$word_ref), Encode::FB_DEFAULT) . "\n";
392 $str_out = Encode::encode("iso-8859-1", join("/",@$word_ref)) . " ";
393 $str_out =~ s/\n /\n/o;
394  
395 print FILE_IN $str_out;
396 } else {
397 warn "[WRAPPER LOG] Unknown enconding charset\n";
398 }
399 }
400 }
401 }
402  
403 close FILE_IN;
404 warn "[LOG] done\n";
405 }
406  
407 sub _printStanfordParserFormatInput2 {
408 my $self = shift;
409 my $filename = shift;
410 my $encoding = shift;
411  
412 my $document;
413 my $doc_idx;
414 my $token;
415 my $word;
416 my $lemma;
417 my $MS_features;
418 my @corpus_in_t;
419  
420 my @sentElt;
421  
422 my %corpusDocuments;
423  
424 my $terms;
425  
426 warn "[LOG] printing TreeTagger Like Output\n";
427  
428 foreach $document (@{$self->_documentSet}) {
429  
430 my @corpus_inRefElementSent;
431 $corpusDocuments{$document->getId} = \@corpus_inRefElementSent;
432 my @tmp = ($document->getId . "-DOCUMENT/NNP :", ".\n"); # , $document->getId
433 push @corpus_in_t, \@tmp;
434
435 for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) {
436 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
437 $terms = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token);
438 if (scalar(@{$terms}) > 0) {
439 my $termform = $terms->[0]->getForm;
440 $termform =~ s/[\t\n]/ /gos;
441 $termform =~ s/ +/_/gos;
442
443 my @tmp = ($termform, "NN"); # , $lemma->canonical_form
444  
445 push @corpus_in_t, \@tmp;
446 push @sentElt, $terms->[0];
447  
448 # warn "$termform" . $terms->[0]->getReference . "\n";
449  
450 $doc_idx += $terms->[0]->getReferenceTokenSize - 1;
451 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
452 } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
453 $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0];
454 $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0];
455 my $wordform = $word->getForm;
456 $wordform =~ s/[\t\n]/ /gos;
457 $wordform =~ s/ +/_/gos;
458  
459 my $MS_f = $MS_features->syntactic_category;
460 if ($MS_f eq "NE") {
461 $MS_f = "NN";
462 }
463 my @tmp = ($wordform, $MS_f); # , $lemma->canonical_form
464  
465 push @corpus_in_t, \@tmp;
466 push @sentElt, $word;
467  
468 $doc_idx += $word->getReferenceSize - 1;
469 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
470
471 } else {
472 if (!($token->isSep)) {
473 my @tmp = ($token->getContent, $token->getContent); # , $token->getContent
474 push @corpus_in_t, \@tmp;
475 push @sentElt, $token;
476 }
477 }
478 # warn "doc_idx $doc_idx / " . scalar(@{$document->getAnnotations->getTokenLevel->getElements}) . "\n";
479 if ($doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements})) {
480 if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) {
481 if ($token->isSymb) {
482 if (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) {
483 my @tmp = ($corpus_in_t[$#corpus_in_t]->[0] , ".\n"); # , $corpus_in_t[$#corpus_in_t]->[0]
484 $corpus_in_t[$#corpus_in_t] = \@tmp;
485 } else {
486 my @tmp = ($token->getContent, ".\n"); # , $token->getContent
487 push @corpus_in_t, \@tmp;
488 push @sentElt, $token;
489 }
490 } else {
491 my @tmp = (".", ".\n"); # , "."
492 push @corpus_in_t, \@tmp;
493 push @sentElt, "";
494 }
495  
496 my @senttmp;
497 push @senttmp, @sentElt;
498 push @corpus_inRefElementSent, \@senttmp;
499 @sentElt = ();
500 }
501 } else {
502 my @tmp = (".", ".\n"); # , "."
503 push @corpus_in_t, \@tmp;
504 push @sentElt, "";
505
506 my @senttmp;
507 push @senttmp, @sentElt;
508 push @corpus_inRefElementSent, \@senttmp;
509 @sentElt = ();
510 }
511 }
512 }
513  
514 $self->{"ELTSBYSENT"} = \%corpusDocuments;
515  
516 warn "\tOpenning " . $filename . "\n";
517  
518 open FILE_IN, ">" . $filename or die "can't open " . $filename . "\n";
519  
520 my $str_out;
521  
522 my $word_ref;
523 foreach $word_ref (@corpus_in_t) {
524 if (scalar(@$word_ref) == 2) {
525 # Encode::encode("iso-8859-1", join("\n",@corpus_in_t), Encode::FB_DEFAULT);
526 if ((!defined $encoding) || (uc($encoding) eq "UTF-8")) {
527 $str_out = Encode::encode("UTF-8", join("/",@$word_ref)) . " ";
528 $str_out =~ s/\n /\n/o;
529  
530 print FILE_IN $str_out;
531 } else {
532 if ((defined $encoding) && (uc($encoding) eq "LATIN1")) {
533 # print FILE_IN Encode::encode("iso-8859-1", join("\t",@$word_ref), Encode::FB_DEFAULT) . "\n";
534 $str_out = Encode::encode("iso-8859-1", join("/",@$word_ref)) . " ";
535 $str_out =~ s/\n /\n/o;
536  
537 print FILE_IN $str_out;
538 } else {
539 warn "[WRAPPER LOG] Unknown enconding charset\n";
540 }
541 }
542 }
543 }
544  
545 close FILE_IN;
546 warn "[LOG] done\n";
547 }
548  
549  
550 1;
551  
552 __END__