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

filter: Statementstmt 0.0% 0 / 547 Branchbran 0.0% 0 / 142 Conditioncond 0.0% 0 / 72 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 787 SCAR 85.7
CC
72
Cov
0%
_outputParsing
930.0
run
930.0
_makeCRFINPUT
72.0
CRAP 5256.0
1 package Lingua::Ogmios::NLPWrappers::YaTeA;
2  
3  
4 our $VERSION='0.1';
5  
6 use Lingua::Ogmios::NLPWrappers::Wrapper;
7 use Lingua::Ogmios::Annotations::SemanticUnit;
8 use Lingua::Ogmios::Annotations::SemanticFeatures;
9 use Lingua::Ogmios::Annotations::Phrase;
10  
11 use Lingua::Ogmios::Annotations::SyntacticRelation;
12  
13 use UNIVERSAL;
14 use Scalar::Util qw(blessed);
15  
16 use Encode qw(:fallbacks);;
17  
18 use XML::Entities;
19  
20 use File::Basename;
21  
22 # use File::Path 'remove_tree';
23  
24 use strict;
25 use warnings;
26  
27 our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper);
28  
29 sub new {
30 my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output, $out_stream) = @_;
31
32 warn "[LOG] Creating a wrapper of the " . $config->comments . "\n";
33
34 my $lang;
35 my $lang2;
36 my $YaTeA = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output, $out_stream);
37  
38 if (defined $YaTeA->_config->configuration->{'CONFIG'}) {
39 foreach $lang (keys %{$YaTeA->_config->configuration->{'CONFIG'}}) {
40 if ($lang =~ /language=([\w]+)/io) {
41 $lang2 = $1;
42 # $YaTeA->_setOption($lang2, "ONLY_SYNTACTIC_RELATIONS", "only_synt_rel", 0);
43 }
44 $YaTeA->_setGeneralOption("PRINTOUTPUTSECTION", "print_output_section", 0);
45 }
46 }
47  
48
49  
50 $YaTeA->_input_filename($tmpfile_prefix . ".YaTeA.in");
51 $YaTeA->_output_filename(dirname($tmpfile_prefix)); # . ".YaTeA.out");
52 $YaTeA->_output_hash({'phrase_set' => undef});
53  
54 unlink($YaTeA->_input_filename);
55
56 return($YaTeA);
57 }
58  
59 sub _inputYaTeA {
60 my ($self) = @_;
61  
62 warn "[LOG] Making input\n";
63  
64 $self->getTimer->startsLap("making input");
65
66 $self->_printTreeTaggerFormatOutput($self->_input_filename);
67  
68 $self->getTimer->_printTimesBySteps;
69 warn "[LOG] done\n";
70 }
71  
72 sub _processYaTeA {
73 my ($self, $lang) = @_;
74
75 $self->getTimer->startsLap("Term extraction");
76 warn "[LOG] " . $self->_config->comments . "\n";
77  
78
79 $lang = $self->_documentSet->[0]->getAnnotations->getLanguage;
80  
81 my $perlModule = $self->_config->commands($lang)->{PerlModule};
82  
83 eval "require $perlModule";
84 if ($@) {
85 warn $@ . "\n";
86 die "Problem while loading perlModule $perlModule - Abort\n\n";
87 } else {
88 $self->getTimer->lapStartUserTimeBySteps;
89 warn "Run term extraction Module $perlModule\n";
90  
91 eval "require $perlModule" . "::Corpus";
92 if ($@) {
93 warn $@ . "\n";
94 die "Problem while loading perlModule $perlModule - Abort\n\n";
95 }
96 my $configFile = $self->_config->configFile($lang)->{"YATEACONF"};
97
98 warn "Loading $configFile\n";
99  
100 my %config_yatea = Lingua::YaTeA::load_config($configFile);
101 my $yatea = Lingua::YaTeA->new($config_yatea{"OPTIONS"}, \%config_yatea);
102  
103 $self->_output_hash({'yatea' => $yatea});
104
105  
106 $yatea->getOptionSet->addOption("output-path", $self->_output_filename);
107  
108 my $corpus_path = $self->_input_filename;
109  
110 my $corpus = Lingua::YaTeA::Corpus->new($corpus_path,$yatea->getOptionSet,$yatea->getMessageSet);
111  
112 $self->_output_hash()->{'corpus'} = $corpus;
113  
114 my $sentence_boundary = $yatea->getOptionSet->getSentenceBoundary;
115 my $document_boundary = $yatea->getOptionSet->getDocumentBoundary;
116  
117 $yatea->loadTestifiedTerms(\$Lingua::YaTeA::process_counter,$corpus,$sentence_boundary,$document_boundary,$yatea->getOptionSet->MatchTypeValue,$yatea->getMessageSet,$yatea->getOptionSet->getDisplayLanguage);
118  
119 print STDERR $Lingua::YaTeA::process_counter++ . ") " . ($yatea->getMessageSet->getMessage('LOAD_CORPUS')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\n";
120  
121 $corpus->read($sentence_boundary,$document_boundary,$yatea->getFSSet,$yatea->getTestifiedTermSet,$yatea->getOptionSet->MatchTypeValue,$yatea->getMessageSet,$yatea->getOptionSet->getDisplayLanguage, $yatea->getOptionSet->getLanguage);
122
123 my $phrase_set = Lingua::YaTeA::PhraseSet->new;
124  
125 $self->_output_hash()->{'phrase_set'} = $phrase_set;
126
127 print STDERR $Lingua::YaTeA::process_counter++ . ") " . ($yatea->getMessageSet->getMessage('CHUNKING')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\n";
128  
129 $corpus->chunk($phrase_set,$sentence_boundary,$document_boundary,$yatea->getChunkingDataSet,$yatea->getFSSet,$yatea->getTagSet,$yatea->getParsingPatternSet,$yatea->getTestifiedTermSet,$yatea->getOptionSet);
130 $phrase_set->printChunkingStatistics($yatea->getMessageSet,$yatea->getOptionSet->getDisplayLanguage);
131  
132 $phrase_set->sortUnparsed;
133
134 print STDERR $Lingua::YaTeA::process_counter++ . ") " . ($yatea->getMessageSet->getMessage('PARSING')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\n";
135
136 $phrase_set->parseProgressively($yatea->getTagSet,$yatea->getOptionSet->getParsingDirection,$yatea->getParsingPatternSet,$yatea->getChunkingDataSet,$corpus->getLexicon,$corpus->getSentenceSet,$yatea->getMessageSet,$yatea->getOptionSet->getDisplayLanguage,\*STDERR);
137
138 $phrase_set->addTermCandidates($yatea->getOptionSet);
139 $corpus->termWeighting($phrase_set->getTermCandidates,\*STDERR);
140  
141 $corpus->termFiltering($phrase_set->getTermCandidates,\*STDERR, $yatea->getOptionSet->getOption("termFiltering")->getValue);
142 $corpus->selectOnTermListStyle($phrase_set->getTermCandidates, $yatea->getOptionSet->getTermListStyle,\*STDERR);
143  
144 print STDERR $Lingua::YaTeA::process_counter++ . ") " . ($yatea->getMessageSet->getMessage('RESULTS')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\n";
145
146 print STDERR "\t-" . ($yatea->getMessageSet->getMessage('DISPLAY_RAW')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\'". $corpus->getOutputFileSet->getFile('debug')->getPath . "'\n";
147 $phrase_set->printPhrases(FileHandle->new(">" . $corpus->getOutputFileSet->getFile('debug')->getPath));
148 $phrase_set->printUnparsable($corpus->getOutputFileSet->getFile('unparsable'));
149 $phrase_set->printUnparsed($corpus->getOutputFileSet->getFile('unparsed'));
150
151 print STDERR "\t-" . ($yatea->getMessageSet->getMessage('DISPLAY_TERM_LIST')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\'". $corpus->getOutputFileSet->getFile('termList')->getPath . "'\n";
152 $phrase_set->printTermList($corpus->getOutputFileSet->getFile('termList'),$yatea->getOptionSet->getTermListStyle, undef, $yatea->getOptionSet->getOption("sorting_weight")->getValue, $yatea->getOptionSet->getOption("termFiltering")->getValue);
153  
154 print STDERR "\t-" . ($yatea->getMessageSet->getMessage('DISPLAY_TC_XML')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\'". $corpus->getOutputFileSet->getFile('candidates')->getPath . "'\n";
155 # to remove
156 # $phrase_set->printTermCandidatesXML($corpus->getOutputFileSet->getFile("candidates"), $yatea->getTagSet);
157  
158  
159 warn "\n";
160 $self->getTimer->lapEndUserTimeBySteps;
161 }
162  
163 $self->getTimer->_printTimesBySteps;
164 warn "[LOG]\n";
165 return($perlModule);
166 }
167  
168 sub _outputParsing {
169 my ($self) = @_;
170  
171 warn "[LOG] . Parsing output Array\n";
172  
173 my $phrase_set = $self->_output_hash()->{'phrase_set'};
174  
175 my $term_candidate;
176 my $if;
177 my $pos;
178 my $lf;
179 my $term;
180  
181 my $oldTermUnitId;
182  
183 my $canonical_form;
184 my $termUnit;
185 my $semanticFeaturesTermUnit;
186  
187 my $document;
188  
189 my $yatea = $self->_output_hash()->{'yatea'};
190  
191 my $tagset = $yatea->getTagSet;
192  
193 my %YateaTermOcc2OgmiosSemUnits;
194 my $sem_unit_id;
195  
196 my $occurrence;
197  
198 my $weight;
199  
200 my $sent_id;
201 my $term_offset;
202 my $doc;
203 my $token;
204 my $word;
205 my $sent_offset = 0;
206 my $to;
207 my $term_content = "";
208 my $term_content2;
209 my $term2;
210 my $token_offset;
211 my $phrase;
212 my $head_occ;
213 my $modifier_occ;
214 my $syntacticRelation;
215  
216 my %compositeWeights = ('idf' => 1,
217 'tf' => 1,
218 'tf-idf' => 1,
219 );
220 my @wperdoc;
221 my $eltWperDoc;
222 my $docW;
223 my $valueW;
224  
225 $self->getTimer->startsLap("output Parsing - term recording");
226  
227 # if ($self->{"only_synt_rel"}->{$self->_documentSet->[0]->getAnnotations->getLanguage} == 0) {
228 foreach $term_candidate (values(%{$phrase_set->getTermCandidates}))
229 {
230 $self->getTimer->startsLapByCategory('YaTeA');
231 ($if,$pos,$lf) = $term_candidate->buildLinguisticInfos($tagset);
232 $self->getTimer->endsLapByCategory('YaTeA');
233  
234 # warn "==> $if : $pos : $lf\n";
235 # work around for a yatea bug
236 $if =~ s/\' /\'/go;
237 $if =~ s/ \- /-/go;
238  
239 $self->getTimer->startsLapByCategory('XML::Entities');
240 $term = XML::Entities::decode('all',$if);
241 $self->getTimer->endsLapByCategory('XML::Entities');
242  
243 $self->getTimer->startsLapByCategory('processTerm');
244 $canonical_form = $lf;
245 $canonical_form =~ s/_/ /go;
246 $self->getTimer->endsLapByCategory('processTerm');
247  
248 $self->getTimer->startsLapByCategory('weights');
249 my %weights_gen;
250 foreach $weight ($term_candidate->getWeightNames) {
251 $weights_gen{$weight} = $term_candidate->getWeight($weight);
252 }
253 $weights_gen{'relevance'} = ( 1 * $term_candidate->isTerm);
254 $self->getTimer->endsLapByCategory('weights');
255  
256  
257 foreach $occurrence (@{$term_candidate->getOccurrences})
258 {
259 $sent_id = $occurrence->getSentence->getInDocID;
260 $term_offset = $occurrence->getStartChar;
261 $document = $self->_documentSet()->[$occurrence->getDocument->getID ];
262 $doc = $occurrence->getDocument->getID;
263  
264 $self->getTimer->startsLapByCategory('weights');
265 my %weights;
266 foreach $weight (keys %weights_gen) {
267 # warn "==> $weight\n";
268 if (exists $compositeWeights{lc($weight)}) {
269 @wperdoc = split /;/, $weights_gen{$weight};
270 foreach $eltWperDoc (@wperdoc) {
271 ($docW, $valueW) = split /:/, $eltWperDoc;
272 if ($doc == $docW) {
273 $weights{$weight} = $valueW;
274 }
275 }
276 # $weights{$weight} = $weights_gen{$weight};
277 } else {
278 $weights{$weight} = $weights_gen{$weight};
279 }
280 }
281 $self->getTimer->endsLapByCategory('weights');
282
283 $token = $document->getAnnotations->getSentenceLevel->getElements->[$sent_id - 1]->{'refid_start_token'};
284  
285 $sent_offset = 0;
286 # warn "---> $term_offset\n";
287 while ($sent_offset < $term_offset) {
288 # warn "> " . $token->getContent . "\n";
289 if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
290 $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0];
291 $sent_offset += length($word->getForm) + 1;
292 my @refs = @{$word->getReference};
293 $token = $refs[$#refs]->next;
294 # warn $word->getForm . " : $sent_offset\n";
295 } else {
296 if (!($token->isSep)) {
297 if ($token->getContent =~ /[\x{2019}\x{2032}']/go) {
298 # warn "-------------\n";
299 $sent_offset--;
300 }
301 $sent_offset += length($token->getContent) + 1;
302 }
303 $token = $token->next;
304 }
305 }
306 # warn "out : $sent_offset\n";
307 $term_content = "";
308 my @TermTokens;
309 $to = $token->getFrom + length($term);
310 while ((defined $token->next) && ($token->isSep)) {
311 $token = $token->next;
312 $to = $token->getFrom + length($term);
313 }
314 do {
315 $term_content .= $token->getContent;
316 push @TermTokens, $token;
317
318 $token = $token->next;
319 $term_content2 = $term_content;
320 $term_content2 =~ s/_/ /go;
321 if ($term_content2 =~ s/([ \n\t]+)/ /go) { $to += length($1);}
322 if ($term_content2 =~ s/^( +)//go) {$to += length($1);};
323 if ($term_content2 =~ s/([ \n\t]+)$//go) {$to += length($1);}
324 $term2 = $term;
325 $term2 =~ s/_/ /go;
326
327 } while((defined $token) && ((lc($term_content2) ne lc($term2)) && ($token->getTo <= $to)));
328  
329  
330 $termUnit = $document->getAnnotations->getSemanticUnitLevel->getElementByStartEndTokens($TermTokens[0], $TermTokens[$#TermTokens]);
331  
332 $self->getTimer->startsLapByCategory('creation/branching objects');
333 # warn $termUnit->getForm . "(0 OK - $if)\n";
334 $semanticFeaturesTermUnit = undef;
335 $oldTermUnitId = -1;
336 if (defined $termUnit) {
337 # warn "=> " . $termUnit->getForm . " already exists\n";
338 if ($termUnit->isNamedEntity) {
339 # Keep it like this for the moment, when have time, convert in Term
340 } elsif ($termUnit->isTerm) {
341 # warn $termUnit->getForm . "(0 OK - $if)\n";
342 $oldTermUnitId = $termUnit->getId;
343 # merge weights
344 # %weights
345 # $termUnit->weights
346 ########################################################################
347 if (defined $termUnit->weights) {
348 foreach $weight (keys %weights) {
349 # foreach $weight (keys %{$termUnit->weights}) {
350 if (!defined $termUnit->weight($weight)) {
351 $termUnit->weight($weight,$weights{$weight});
352 }
353 }
354 # $weights{'relevance'} = $termUnit->weight("relevance");
355 # $termUnit->weights(\%weights);
356 }
357 if ((defined $canonical_form) && (!defined $termUnit->canonical_form)) {$termUnit->canonical_form($canonical_form)};
358 }
359 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'documentID'} = $occurrence->getDocument->getID;
360 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'offset_shift'} = $sent_offset;
361 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'sentence'} = $sent_id;
362 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'type'} = $termUnit->reference_name;
363 if ($termUnit->reference_name eq 'list_refid_token') {
364 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'list_refid_token'} = undef;
365 } else { #if ($termUnit->reference_name eq 'refid_word') {
366 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{$termUnit->reference_name} = $termUnit->reference;
367 }
368 # } elsif ($termUnit->reference_name eq 'refid_phrase'){
369 # $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'refid_phrase'} = $termUnit->reference;
370 # } els
371 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'semunit'} = $sem_unit_id;
372  
373 ########################################################################
374 # warn "term $term already exists $oldTermUnitId\n";
375 # if ($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $termUnit->getId)) {
376 # $semanticFeaturesTermUnit = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $termUnit->getId)->[0];
377 # }
378 } elsif (lc($term_content2) eq lc($term2)) {
379 # TODO recode XML entities before sending to the term tagger
380 ## Word ?
381 # warn "*** New term: $term2\n";
382 $token_offset = $TermTokens[0];
383 my @TermWords;
384 do {
385 if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token_offset->getId)) {
386 ## search the last word
387 push @TermWords, $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token_offset->getId)->[0];
388 $token_offset = $TermWords[$#TermWords]->getLastToken;
389 }
390 $token_offset = $token_offset->next;
391 } while((defined $token_offset) && ((!defined $TermTokens[$#TermTokens]->next) || ($token_offset->getTo < $TermTokens[$#TermTokens]->next->getTo)));
392
393  
394 if ((scalar(@TermTokens) == 0) || (scalar(@TermWords) == 0) ||
395 ((defined $TermTokens[$#TermTokens]->next) && ($TermTokens[$#TermTokens]->getTo != $TermWords[$#TermWords]->getLastToken->getTo))) {
396 @TermWords = ();
397 }
398  
399 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'documentID'} = $occurrence->getDocument->getID;
400 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'offset_shift'} = $sent_offset;
401 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'sentence'} = $sent_id;
402  
403 # $self->getTimer->startsLapByCategory('creation/branching objects');
404  
405 if (scalar @TermWords == 0) {
406 # term is a token list
407 # if (!defined $termUnit) {
408 $termUnit = Lingua::Ogmios::Annotations::SemanticUnit->newTerm(
409 {'form' => $term_content,
410 'list_refid_token' => \@TermTokens,
411 "weights" => \%weights
412 });
413 # } else {
414 # # warn "term $term already exists. Skip creation\n";
415 # $weights{'relevance'} = $termUnit->weight("relevance");
416 # $termUnit->weights(\%weights);
417 # $semanticFeaturesTermUnit = undef;
418 # }
419 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'type'} = 'list_refid_token';
420 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'list_refid_token'} = undef;
421 }
422 if (scalar @TermWords == 1) {
423 # term is a word
424 # if (defined $termUnit) {
425 # if (!$termUnit->isNamedEntity) {
426 # # warn "term $term already exists. remove previous term and merge related information\n";
427 # # warn "\t" . $termUnit->getId . "\n";
428 # # warn $termUnit->getForm . "(OK)\n";
429 # $weights{'relevance'} = $termUnit->weight("relevance");
430 # if (!defined $semanticFeaturesTermUnit) {
431 # warn "semantic features not found\n";
432 # }
433 # $document->getAnnotations->delSemanticUnit($termUnit);
434 # }
435 # }
436 $termUnit = Lingua::Ogmios::Annotations::SemanticUnit->newTerm(
437 {'form' => $term_content,
438 'refid_word' => $TermWords[0],
439 "weights" => \%weights
440 });
441 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'type'} = 'refid_word';
442 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'refid_word'} = $TermWords[0];
443 }
444 if (scalar @TermWords > 1) {
445 # term is a phrase
446 $phrase = Lingua::Ogmios::Annotations::Phrase->new(
447 { 'refid_word' => \@TermWords,
448 'form' => $term_content,
449 }
450 );
451 $document->getAnnotations->addPhrase($phrase);
452  
453 # if (defined $termUnit) {
454 # # warn "term $term already exists. remove previous term and merge related information\n";
455 # $weights{'relevance'} = $termUnit->weight("relevance");
456 # $document->getAnnotations->delSemanticUnit($termUnit);
457 # }
458  
459 $termUnit = Lingua::Ogmios::Annotations::SemanticUnit->newTerm(
460 {'form' => $term_content,
461 'refid_phrase' => $phrase,
462 "weights" => \%weights
463 });
464 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'type'} = 'refid_phrase';
465 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'refid_phrase'} = $phrase;
466 }
467  
468 if ((defined $canonical_form) && (!defined $termUnit->canonical_form)) {$termUnit->canonical_form($canonical_form)};
469 if (defined $termUnit) {
470 $sem_unit_id = $document->getAnnotations->addSemanticUnit($termUnit);
471 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'semunit'} = $sem_unit_id;
472 }
473 # if (defined $semanticFeaturesTermUnit) {
474 # # warn "reconnect semantic features to the right term $sem_unit_id (old: $oldTermUnitId)\n";
475 # $document->getAnnotations->getSemanticFeaturesLevel->delElementFromIndex($oldTermUnitId, "refid_semantic_unit", $semanticFeaturesTermUnit);
476 # $semanticFeaturesTermUnit->refid_semantic_unit($sem_unit_id);
477 # $document->getAnnotations->getSemanticFeaturesLevel->addElementToIndex($semanticFeaturesTermUnit, "refid_semantic_unit");
478 # $document->getAnnotations->delSemanticUnit($oldTermUnit);
479 # }
480 # }
481 } else {
482 # warn "term content ($term_content) doesn't match with the search term $term (term offset: $term_offset : sent_id: $sent_id : doc: $doc -- " . $document->getId . ")\n";
483 }
484 $self->getTimer->endsLapByCategory('creation/branching objects');
485 }
486 }
487 $self->getTimer->_printTimeByCategory(0);
488 $self->getTimer->_printTimesBySteps;
489 # }
490 $self->getTimer->startsLap("output Parsing - syntactic relation recording");
491 warn "Add syntactic relations\n";
492 # Second foreach to add syntactic relations
493  
494 foreach $term_candidate (values(%{$phrase_set->getTermCandidates}))
495 {
496 ($if,$pos,$lf) = $term_candidate->buildLinguisticInfos($tagset);
497  
498 # warn "term: $if\n";
499 if((blessed($term_candidate)) &&($term_candidate->isa('Lingua::YaTeA::MultiWordTermCandidate'))) {
500 foreach $occurrence (@{$term_candidate->getOccurrences}) {
501 if (exists ($YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID})) {
502 if (($head_occ = $self->_getOccFromTermOcc($term_candidate->getRootHead->getKey, $phrase_set, $occurrence->getStartChar, $occurrence->getEndChar, $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}, $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'sentence'})) == -1) {
503 warn "Occurrence not found (H)\n";
504 }
505 # if ($head_occ != -1) {
506 # warn $head_occ->{"documentID"} . "\n";
507 # warn "head_occ: $head_occ\n";
508 # warn "\t" . $head_occ->{$head_occ->{'type'}}->getId . "\n";
509 # warn "\t" . $head_occ->{$head_occ->{'type'}}->getForm . "\n";
510 # }
511 # $term_refid_head = $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$head_occ}->{'semunit'};
512  
513 if (($modifier_occ = $self->_getOccFromTermOcc($term_candidate->getRootModifier->getKey, $phrase_set, $occurrence->getStartChar, $occurrence->getEndChar, $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}, $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'sentence'})) == -1) {
514 warn "Occurrence not found (M)\n";
515 }
516 # if ($modifier_occ != -1) {
517 # warn $modifier_occ->{"documentID"} . "\n";
518 # warn "modifier_occ: $modifier_occ\n";
519 # warn "\t" . $modifier_occ->{$modifier_occ->{'type'}}->getId . "\n";
520 # warn "\t" . $modifier_occ->{$modifier_occ->{'type'}}->getForm . "\n";
521 # }
522 # $term_refid_modifier = $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$modifier_occ}->{'semunit'};
523  
524 if (($head_occ == -1) || ($modifier_occ == -1)) {
525 warn "head and/or Modifier not found\n";
526 warn "\t Term " . $term_candidate->buildLinguisticInfos($tagset) . "\n";
527 } else {
528 # warn "head_occ: $head_occ\n";
529 # warn "\t" . $head_occ->{$head_occ->{'type'}}->getId . "\n";
530 # warn "\t" . $head_occ->{$head_occ->{'type'}}->getForm . "\n";
531 # warn "modifier_occ: $modifier_occ\n";
532 # warn "\t" . $modifier_occ->{$modifier_occ->{'type'}}->getId . "\n";
533 # warn "\t" . $modifier_occ->{$modifier_occ->{'type'}}->getForm . "\n";
534  
535 $document = $self->_documentSet->[$head_occ->{"documentID"}];
536  
537 # warn $head_occ->{$head_occ->{'type'}} . " == " . $modifier_occ->{$modifier_occ->{'type'}} . "\n";
538 if ((!defined $head_occ->{'type'}) || (!defined $modifier_occ->{'type'}) ||
539 (!exists $head_occ->{$head_occ->{'type'}}) || (!exists $modifier_occ->{$modifier_occ->{'type'}}) ||
540 ($head_occ->{$head_occ->{'type'}} eq "") || ($modifier_occ->{$modifier_occ->{'type'}} eq "") ||
541 ($head_occ->{$head_occ->{'type'}} == $modifier_occ->{$modifier_occ->{'type'}})
542 ) {
543 warn "Something wrong: head and modifier are equals or undefined?\n";
544 warn "\t Term " . $term_candidate->buildLinguisticInfos($tagset) . "\n";
545 next;
546 }
547  
548  
549 # warn "Add Head_of\n";
550 # warn $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'type'} . "\n";
551 # warn $head_occ->{'type'} . "\n";
552 $syntacticRelation = Lingua::Ogmios::Annotations::SyntacticRelation->new(
553 { 'syntactic_relation_type' => 'Head_of',
554 $head_occ->{'type'} . '_head' => $head_occ->{$head_occ->{'type'}},
555 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'type'} . '_modifier' => $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{$YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'type'}},
556 }
557 );
558 $document->getAnnotations->addSyntacticRelation($syntacticRelation);
559  
560 # warn "Add Modifier_of\n";
561 $syntacticRelation = Lingua::Ogmios::Annotations::SyntacticRelation->new(
562 { 'syntactic_relation_type' => 'Modifier_of',
563 $modifier_occ->{'type'} . '_head' => $modifier_occ->{$modifier_occ->{'type'}},
564 $head_occ->{'type'} . '_modifier' => $head_occ->{$head_occ->{'type'}},
565 }
566 );
567 $document->getAnnotations->addSyntacticRelation($syntacticRelation);
568  
569 # warn "Add Component_in_modifier_position\n";
570 $syntacticRelation = Lingua::Ogmios::Annotations::SyntacticRelation->new(
571 { 'syntactic_relation_type' => 'Component_in_modifier_position',
572 $modifier_occ->{'type'} . '_head' => $modifier_occ->{$modifier_occ->{'type'}},
573 $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'type'} . '_modifier' => $YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{$YateaTermOcc2OgmiosSemUnits{$occurrence->getDocument->getID}->{$occurrence->getID}->{'type'}},
574 }
575 );
576 $document->getAnnotations->addSyntacticRelation($syntacticRelation);
577 # warn "done\n";
578 }
579 }
580 }
581 }
582 }
583 # $document->getAnnotations->getSemanticFeaturesLevel->printIndex("refid_semantic_unit");
584 $self->getTimer->_printTimesBySteps;
585  
586 warn "[LOG] done\n";
587 }
588  
589 sub _getOccFromTermOcc
590 {
591 my ($self, $termKey, $phrase_set, $start_char, $end_char, $ref_YateaTermOcc2OgmiosSemUnits, $sent_id) = @_;
592  
593 my @occurrences;
594 my $i;
595 my $term_candidate = $phrase_set->getTermCandidates->{$termKey};
596  
597 @occurrences = @{$term_candidate->getOccurrences};
598  
599 $i = 0;
600 while (($i<scalar(@occurrences)) && (($sent_id != $occurrences[$i]->getSentence->getInDocID) || ($start_char > $occurrences[$i]->getStartChar) || ($end_char < $occurrences[$i]->getStartChar))) {
601 $i++;
602 }
603 # warn "i: $i\n";
604  
605 if ($i < scalar @occurrences) {
606 if (defined $ref_YateaTermOcc2OgmiosSemUnits->{$occurrences[$i]->getID}) {
607 return($ref_YateaTermOcc2OgmiosSemUnits->{$occurrences[$i]->getID});
608 } else {
609 return(-1);
610 }
611 }
612 return(-1);
613 }
614  
615  
616 sub run {
617 my ($self, $documentSet) = @_;
618  
619 warn "*** TODO: check if the level exists\n";
620 # Set variables according the the configuration
621 my $output;
622 my @no_standard_output;
623 $self->_documentSet($documentSet);
624  
625 warn "[LOG] " . $self->_config->comments . " ... \n";
626  
627 $self->_inputYaTeA;
628  
629 my $command_line = $self->_processYaTeA;
630  
631 # warn "===> " . $self->_no_standard_output . "\n";
632 # warn "\t" . $self->_position . "\n";
633  
634 my $fh = *STDOUT;
635 if (defined($self->_out_stream)) {
636 $fh = $self->_out_stream;
637 }
638  
639 if (($self->_position eq "last") && ($self->_no_standard_output)) {
640 if (ref($self->_no_standard_output) eq "ARRAY") {
641 # warn $self->_no_standard_output . "\n";
642 @no_standard_output = @{$self->_no_standard_output};
643 } else {
644 push @no_standard_output, $self->_no_standard_output;
645 }
646 foreach $output (@no_standard_output) {
647 warn "print no standard output (" . $output . ")\n";
648 # warn $self->_output_hash()->{'yatea'} . "\n";
649  
650 if ($output eq "XML") {
651 warn "print XML output\n";
652 if ($self->{print_output_section}) {print $fh "==== XML ====\n";}
653 $self->_output_hash()->{'phrase_set'}->printTermCandidatesXML("stdout",$self->_output_hash()->{'yatea'}->getTagSet,$fh);
654 }
655 if ($output eq "DOT") {
656 warn "print dot output\n";
657 if ($self->{print_output_section}) {print $fh "==== DOT ====\n";}
658 $self->_output_hash()->{'phrase_set'}->printListTermCandidatesDot($self->_output_hash()->{'yatea'}->getTagSet,$fh);
659 # $self->_output_hash()->{'phrase_set'}->printTermCandidatesDot2("stdout",$self->_output_hash()->{'yatea'}->getTagSet);
660 }
661 if ($output eq "TOPICDEFINITION") {
662 if ($self->{print_output_section}) {print $fh "==== TOPICDEFINITION ====\n";}
663 warn "print topic definition : (" . scalar(@{$self->_documentSet()}) . ")\n";
664 my $line;
665 my @parts;
666 my @urls;
667 my @ids;
668 my $id;
669 foreach $line (@{$self->_output_hash()->{'phrase_set'}->getTopicDefinition("all", "autonomy")}) {
670 # warn "$line\n";
671 # $document = $self->_documentSet()->[$occurrence->getDocument->getID ];
672 @parts = split /:/, $line; # /
673 @ids = split /,/, pop @parts; # /
674 @urls = ();
675 foreach $id (@ids) {
676 # warn "-> $id\n";
677 # warn $self->getDocId2DocIndex->{$id} . "\n";
678 push @urls, @{$self->_documentSet()->[$self->getDocId2DocIndex->{$id}]->getAnnotations->getURLs};
679 }
680 print join(":", @parts);
681 print "|";
682 print join(" ", @urls);
683 print "\n";
684 }
685 # print "__END__\n";
686 }
687 if ($output eq "HTML") {
688 if ($self->{print_output_section}) {print $fh "==== HTML ====\n";}
689 $self->_outputParsing;
690 $self->HTMLoutput;
691 }
692 if ($output eq "HTML2") {
693 if ($self->{print_output_section}) {print $fh "==== HTML2 ====\n";}
694 warn "print HTML output\n";
695 $self->_output_hash()->{'corpus'}->printCandidatesAndUnparsedInCorpus(
696 $self->_output_hash()->{'phrase_set'}->getTermCandidates,
697 $self->_output_hash()->{'phrase_set'}->getUnparsable,
698 "stdout",
699 $self->_output_hash()->{'yatea'}->getOptionSet->getSentenceBoundary,
700 $self->_output_hash()->{'yatea'}->getOptionSet->getDocumentBoundary,
701 $self->_output_hash()->{'yatea'}->getOptionSet->getOption('COLOR_BLIND'),
702 $self->_output_hash()->{'yatea'}->getOptionSet->getOption('PARSED_COLOR'),
703 $self->_output_hash()->{'yatea'}->getOptionSet->getOption('UNPARSED_COLOR'),
704 );
705  
706 # $self->_output_hash()->{'corpus'}->getOutputFileSet->getFile('candidatesAndUnparsedInCorpus'),
707  
708 }
709 if ($output eq "TXT") {
710 if ($self->{print_output_section}) {print $fh "==== TXT ====\n";}
711 warn "==> print Text file in $fh\n";
712 $self->_output_hash()->{'phrase_set'}->printTermList('stdout',$self->_output_hash()->{'yatea'}->getOptionSet->getTermListStyle,$fh);
713 }
714 if ($output eq "TERMANDHEAD") {
715 if ($self->{print_output_section}) {print $fh "==== TERMANDHEAD ====\n";}
716 warn "print Term->Head output\n";
717 # mininal
718 $self->_output_hash()->{'phrase_set'}->printTermAndHeadList('stdout',$self->_output_hash()->{'yatea'}->getOptionSet->getTermListStyle,$fh,undef,undef);
719 }
720 if ($output eq "TERMANDROOTHEAD") {
721 # global
722 if ($self->{print_output_section}) {print $fh "==== TERMANROOTDHEAD ====\n";}
723 warn "print Term->RootHead output\n";
724 $self->_output_hash()->{'phrase_set'}->printTermAndRootHeadList('stdout',$self->_output_hash()->{'yatea'}->getOptionSet->getTermListStyle,$fh,undef,undef);
725 }
726 if ($output eq "TTG") {
727 if ($self->{print_output_section}) {print $fh "==== TTG ====\n";}
728 warn "print TTG output\n";
729 $self->_output_hash()->{'phrase_set'}->printTermCandidatesTTG('stdout',$self->_output_hash()->{'yatea'}->getOptionSet->getTTGStyle,$fh);
730 }
731 if ($output eq "FFTTG") {
732 if ($self->{print_output_section}) {print $fh "==== FFTTG ====\n";}
733 warn "print FF and TTG output on oneline\n";
734 $self->_output_hash()->{'phrase_set'}->printTermCandidatesFFandTTG('stdout',$self->_output_hash()->{'yatea'}->getOptionSet->getTTGStyle,$self->_output_hash()->{'yatea'}->getTagSet,$fh);
735 }
736 if ($output eq "TermAndComp") {
737 if ($self->{print_output_section}) {print $fh "==== TermAndComp ====\n";}
738 warn "print Terms and Components on oneline\n";
739 $self->_output_hash()->{'phrase_set'}->printTermCandidatesAndComponents('stdout',$self->_output_hash()->{'yatea'}->getOptionSet->getTTGStyle,$fh,$self->_output_hash()->{'yatea'}->getTagSet, undef);
740 }
741 if ($output eq "TAGGEDSENT") {
742 if ($self->{print_output_section}) {print $fh "==== TAGGEDSENT ====\n";}
743 warn "print Tagged Sentence output\n";
744 $self->_outputParsing;
745 $self->_makeTaggedSentences({}, $fh, "/", " ");
746 }
747 if ($output eq "CRFINPUT") {
748 warn "print Tagged Sentence output (CRFINPUT)\n";
749 $self->_outputParsing;
750 $self->_makeTaggedSentences({}, $fh, "\t", "\n");
751 }
752 if ($output eq "QALD") {
753 warn "print Tagged Sentence output (QALD)\n";
754 $self->_outputParsing;
755 $self->_QALDoutput(\*STDOUT);
756 }
757 }
758 } else {
759 # $self->_output_hash()->{'phrase_set'}->printTermCandidatesXML("stderr",$self->_output_hash()->{'yatea'}->getTagSet);
760 $self->_outputParsing;
761 }
762 # # Put log information
763 my $information = { 'software_name' => $self->_config->name,
764 'comments' => $self->_config->comments,
765 'command_line' => $command_line,
766 'list_modified_level' => ['phrase_level', 'semantic_unit_level', 'syntactic_relation_level'],
767 };
768 $self->_log($information);
769  
770 $self->getTimer->_printTimes;
771 $self->getTimer->_printTimesInLine($self->_config->name);
772  
773 warn "[LOG] done\n";
774 }
775  
776 sub _makeCRFINPUT {
777 my ($self, $fh) = @_;
778  
779 my $phrase_set = $self->_output_hash()->{'phrase_set'};
780  
781 my $term_candidate;
782 my $if;
783 my $pos;
784 my $lf;
785 my $term;
786 my $occurrence;
787 my $sent_id;
788 my $term_offset;
789 my $doc;
790 my $token;
791 my $word;
792 my $sent_offset = 0;
793 my $to;
794 my $term_content = "";
795 my $term_content2;
796 my $document;
797 my $yatea = $self->_output_hash()->{'yatea'};
798 my $tagset = $yatea->getTagSet;
799 my $term2;
800 my $termUnit;
801  
802 foreach $term_candidate (values(%{$phrase_set->getTermCandidates})) {
803 ($if,$pos,$lf) = $term_candidate->buildLinguisticInfos($tagset);
804  
805 warn "term: $if\n";
806 $term = XML::Entities::decode('all',$if);
807 warn "term: $if\n";
808 foreach $occurrence (@{$term_candidate->getOccurrences}) {
809 if ($occurrence->isMaximal) {
810 $sent_id = $occurrence->getSentence->getInDocID;
811 $term_offset = $occurrence->getStartChar;
812 $document = $self->_documentSet()->[$occurrence->getDocument->getID ];
813 $doc = $occurrence->getDocument->getID;
814 warn $occurrence->getStartChar . "\n";
815 warn $occurrence->getEndChar . "\n";
816 }
817 $token = $document->getAnnotations->getSentenceLevel->getElements->[$sent_id - 1]->{'refid_start_token'};
818  
819 $sent_offset = 0;
820 # warn "---> $term_offset\n";
821 while ($sent_offset < $term_offset) {
822 # warn "> " . $token->getContent . "\n";
823 if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
824 $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0];
825 $sent_offset += length($word->getForm) + 1;
826 my @refs = @{$word->getReference};
827 $token = $refs[$#refs]->next;
828 # warn $word->getForm . " : $sent_offset\n";
829 } else {
830 if (!($token->isSep)) {
831 if ($token->getContent =~ /[\x{2019}\x{2032}']/go) {
832 # warn "-------------\n";
833 $sent_offset--;
834 }
835 $sent_offset += length($token->getContent) + 1;
836 }
837 $token = $token->next;
838 }
839 }
840 # warn "out : $sent_offset\n";
841 $term_content = "";
842 my @TermTokens;
843 $to = $token->getFrom + length($term);
844 while ($token->isSep) {
845 $token = $token->next;
846 $to = $token->getFrom + length($term);
847 }
848 do {
849 $term_content .= $token->getContent;
850 push @TermTokens, $token;
851
852 $token = $token->next;
853 $term_content2 = $term_content;
854 $term_content2 =~ s/_/ /go;
855 if ($term_content2 =~ s/([ \n\t]+)/ /go) { $to += length($1);}
856 if ($term_content2 =~ s/^( +)//go) {$to += length($1);};
857 if ($term_content2 =~ s/([ \n\t]+)$//go) {$to += length($1);}
858 $term2 = $term;
859 $term2 =~ s/_/ /go;
860
861 } while((defined $token) && ((lc($term_content2) ne lc($term2)) && ($token->getTo <= $to)));
862  
863 warn "EVENT=\"" . $term . "\" " . $TermTokens[0]->getFrom . " " . $TermTokens[$#TermTokens]->getTo . "\n";
864 # $termUnit = $document->getAnnotations->getSemanticUnitLevel->getElementByStartEndTokens($TermTokens[0], $TermTokens[$#TermTokens]);
865 }
866 }
867 }
868  
869 1;
870  
871  
872 __END__