« blib/lib/Lingua/Ogmios/Annotations.pm
↑ Summary
blib/lib/Lingua/Ogmios/Annotations/SyntacticRelation.pm »
| 1 | package Lingua::Ogmios::NLPWrappers::Faster; | ||
| 2 | |||
| 3 | |||
| 4 | our $VERSION='0.1'; | ||
| 5 | |||
| 6 | use Lingua::Ogmios::NLPWrappers::Wrapper; | ||
| 7 | use Lingua::Ogmios::Annotations::DomainSpecificRelation; | ||
| 8 | use Lingua::Ogmios::Annotations::SemanticUnit; | ||
| 9 | use Data::Dumper; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | |||
| 15 | our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper); | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output) = @_; | ||
| 19 | my $lang; | ||
| 20 | my $lang2; | ||
| 21 | |||
| 22 | warn "[LOG] Creating a wrapper of the " . $config->comments . "\n"; | ||
| 23 | |||
| 24 | my $Faster = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output); | ||
| 25 | |||
| 26 | if (defined $Faster->_config->configuration->{'CONFIG'}) { | ||
| 27 | foreach $lang (keys %{$Faster->_config->configuration->{'CONFIG'}}) { | ||
| 28 | if ($lang =~ /language=([\w]+)/io) { | ||
| 29 | $lang2 = $1; | ||
| 30 | $Faster->_setOption($lang2, "MODE", "mode", "free"); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | $Faster->_FASTRTMP_dir($tmpfile_prefix . ".FASTER"); | ||
| 35 | $Faster->_input_filename($Faster->_FASTRTMP_dir . "/Faster.in"); | ||
| 36 | $Faster->_output_filename($Faster->_FASTRTMP_dir . "/Faster.out"); | ||
| 37 | |||
| 38 | $Faster->_output_hash({'term_idx' => {}}); | ||
| 39 | # $Faster->_output_hash({'sentence_idx' => {}}); | ||
| 40 | |||
| 41 | $Faster->_Corpus_filename($Faster->_FASTRTMP_dir . "/corpus_faster.in"); | ||
| 42 | $Faster->_tmp_filename($Faster->_FASTRTMP_dir . "/faster.tmp"); | ||
| 43 | $Faster->_ConvertCorpus_filename($Faster->_FASTRTMP_dir . "/corpus.fas"); | ||
| 44 | $Faster->_MetaRulesSW_filename($Faster->_FASTRTMP_dir . "/terms.R.w"); | ||
| 45 | $Faster->_MetaRulesTRM_filename($Faster->_FASTRTMP_dir . "/terms.R.t"); | ||
| 46 | |||
| 47 | mkdir $Faster->_FASTRTMP_dir; | ||
| 48 | |||
| 49 | return($Faster); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _FASTRTMP_dir { | ||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | $self->{"FASTRTMP_DIR"} = shift if @_; | ||
| 56 | |||
| 57 | return($self->{"FASTRTMP_DIR"}); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _Corpus_filename { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | $self->{"Corpus_filename"} = shift if @_; | ||
| 64 | |||
| 65 | return($self->{"Corpus_filename"}); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _tmp_filename { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | $self->{"tmp_filename"} = shift if @_; | ||
| 72 | |||
| 73 | return($self->{"tmp_filename"}); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _ConvertCorpus_filename { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | $self->{"convertCorpus_filename"} = shift if @_; | ||
| 80 | |||
| 81 | return($self->{"convertCorpus_filename"}); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _MetaRulesSW_filename { | ||
| 85 | my $self = shift; | ||
| 86 | |||
| 87 | $self->{"MetaRulesSW_filename"} = shift if @_; | ||
| 88 | |||
| 89 | return($self->{"MetaRulesSW_filename"}); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _MetaRulesTRM_filename { | ||
| 93 | my $self = shift; | ||
| 94 | |||
| 95 | $self->{"MetaRulesTRM_filename"} = shift if @_; | ||
| 96 | |||
| 97 | return($self->{"MetaRulesTRM_filename"}); | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | sub _exec_command { | ||
| 102 | |||
| 103 | my ($self, $command) = @_; | ||
| 104 | |||
| 105 | return($self->SUPER::_exec_command($command, | ||
| 106 | {'FASTRBIN' => $self->_config->configuration->{'FASTRBIN'}, | ||
| 107 | 'FASTRLIB' => $self->_config->configuration->{'FASTRLIB'}, | ||
| 108 | 'FASTRTMP' => $self->_FASTRTMP_dir, | ||
| 109 | })); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _processFaster { | ||
| 113 | my ($self, $lang) = @_; | ||
| 114 | |||
| 115 | my $command_line; | ||
| 116 | my $return_command_line; | ||
| 117 | |||
| 118 | warn "[LOG] " . $self->_config->comments . "\n"; | ||
| 119 | |||
| 120 | $lang = $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 121 | |||
| 122 | # $self->setConvertCorpus_cmd($self->getConfig->{'NLP_tools'}->{'FASTER'}->{'COMMANDS'}->{"language=$language"}->{'CONVERTCORPUS_CMD'}); | ||
| 123 | # $self->setMakeMetaRulesSW_cmd($self->getConfig->{'NLP_tools'}->{'FASTER'}->{'COMMANDS'}->{"language=$language"}->{'MAKEMETARULES_SW_CMD'}); | ||
| 124 | # $self->setMakeMetaRulesTRM_cmd($self->getConfig->{'NLP_tools'}->{'FASTER'}->{'COMMANDS'}->{"language=$language"}->{'MAKEMETARULES_TRM_CMD'}); | ||
| 125 | # $self->setFastrbinary_cmd($self->getConfig->{'NLP_tools'}->{'FASTER'}->{'COMMANDS'}->{"language=$language"}->{'FASTR'}); | ||
| 126 | |||
| 127 | $command_line = $self->_defineCommandLine($self->_config->commands($lang)->{'CONVERTCORPUS_CMD'}, | ||
| 128 | $self->_Corpus_filename, | ||
| 129 | $self->_ConvertCorpus_filename); | ||
| 130 | |||
| 131 | $return_command_line .= $self->_exec_command($command_line); | ||
| 132 | $return_command_line .= "; "; | ||
| 133 | |||
| 134 | # $mode = $self->{'mode'}->{$document->getAnnotations->getLanguage}; | ||
| 135 | |||
| 136 | $command_line = $self->_defineCommandLine($self->_config->commands($lang)->{'MAKEMETARULES_SW_CMD'}, $self->_input_filename, $self->_MetaRulesSW_filename); | ||
| 137 | $return_command_line .= $self->_exec_command($command_line); | ||
| 138 | $return_command_line .= "; "; | ||
| 139 | |||
| 140 | # # warn "[FASTER LOG] Postprocessing single word rules\n"; | ||
| 141 | |||
| 142 | my $line; | ||
| 143 | my $pos; | ||
| 144 | |||
| 145 | |||
| 146 | if ($lang eq 'EN') { | ||
| 147 | open SWR, $self->_MetaRulesSW_filename or warn "No such file " . $self->_MetaRulesSW_filename; | ||
| 148 | while ((defined(<SWR>)) && ($line = <SWR>) && (($pos=index($line, "Word 'be' : <cat> = V")) == -1)) {}; | ||
| 149 | close SWR; | ||
| 150 | if ($pos == -1) { | ||
| 151 | open SWR, ">>" . $self->_MetaRulesSW_filename or die "No such file " . $self->_MetaRulesSW_filename; | ||
| 152 | print SWR "Word 'be' : <cat> = V.\n"; | ||
| 153 | close SWR; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | $command_line = $self->_defineCommandLine($self->_config->commands($lang)->{'MAKEMETARULES_TRM_CMD'}, $self->_input_filename, $self->_MetaRulesTRM_filename); | ||
| 159 | $return_command_line .= $self->_exec_command($command_line); | ||
| 160 | $return_command_line .= "; "; | ||
| 161 | |||
| 162 | my $command_gen = $self->_config->commands($lang)->{'FASTR'} . " -C " . $self->_config->configuration->{'FASTREMPTYCONF'}; | ||
| 163 | |||
| 164 | warn "command_gen: $command_gen\n"; | ||
| 165 | $command_line = $self->_defineCommandLine($command_gen . " -z"); | ||
| 166 | warn "[FASTER LOG] command line : $command_line\n"; | ||
| 167 | $return_command_line .= $self->_exec_command($command_line); | ||
| 168 | $return_command_line .= "; "; | ||
| 169 | |||
| 170 | $command_gen = $self->_config->commands($lang)->{'FASTR'} . " -C " . $self->_config->configFile($lang)->{"FASTRCONF"}; | ||
| 171 | $command_line = $self->_defineCommandLine($command_gen . " -c " . $self->_MetaRulesSW_filename); | ||
| 172 | warn "[FASTER LOG] command line : $command_line\n"; | ||
| 173 | $return_command_line .= $self->_exec_command($command_line); | ||
| 174 | $return_command_line .= "; "; | ||
| 175 | |||
| 176 | $command_line = $self->_defineCommandLine($command_gen . " -c " . $self->_MetaRulesTRM_filename); | ||
| 177 | warn "[FASTER LOG] command line : $command_line\n"; | ||
| 178 | $return_command_line .= $self->_exec_command($command_line); | ||
| 179 | $return_command_line .= "; "; | ||
| 180 | |||
| 181 | $command_line = $self->_defineCommandLine("(" . $command_gen . " -i | " . $command_gen . | ||
| 182 | " -s " . $self->_input_filename . " 2 ) < " . | ||
| 183 | $self->_ConvertCorpus_filename . " > " . $self->_output_filename); | ||
| 184 | warn "[FASTER LOG] command line : $command_line\n"; | ||
| 185 | warn $self->_config->configuration->{'FASTRBIN'} . "\n"; | ||
| 186 | $return_command_line .= $self->_exec_command($command_line); | ||
| 187 | $return_command_line .= "; "; | ||
| 188 | |||
| 189 | warn "[LOG]\n"; | ||
| 190 | return($return_command_line); | ||
| 191 | } | ||
| 192 | |||
| 193 | sub _inputFaster { | ||
| 194 | my ($self) = @_; | ||
| 195 | |||
| 196 | my $lang = $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 197 | my $doc_idx; | ||
| 198 | my $document; | ||
| 199 | my $semanticUnit; | ||
| 200 | |||
| 201 | my $term_id; | ||
| 202 | my $term_idx; | ||
| 203 | my $term_lemma = ""; | ||
| 204 | my $term_postag = ""; | ||
| 205 | my $term_inflectedform = ""; | ||
| 206 | |||
| 207 | my $command_line; | ||
| 208 | my $return_command_line; | ||
| 209 | |||
| 210 | my $line; | ||
| 211 | |||
| 212 | my $word = ""; | ||
| 213 | my $lemma = ""; | ||
| 214 | my $postag = ""; | ||
| 215 | |||
| 216 | my @corpus_in_t; | ||
| 217 | |||
| 218 | warn "[LOG] Making input\n"; | ||
| 219 | |||
| 220 | warn "[LOG] Corpus\n"; | ||
| 221 | $self->_printTreeTaggerFormatOutput($self->_Corpus_filename, "LATIN1"); | ||
| 222 | |||
| 223 | warn "[LOG] Term List\n"; | ||
| 224 | warn "\tmode: " . $self->{'mode'}->{$lang} . "\n"; | ||
| 225 | |||
| 226 | if ($self->{'mode'}->{uc($lang)} eq "FREE") { | ||
| 227 | # if (!defined $self->_config->configFile($lang)->{"CONTROLLED_TERMLIST"}) { | ||
| 228 | warn "Making term list for free indexing\n"; | ||
| 229 | |||
| 230 | $term_idx = 0; | ||
| 231 | foreach $document (@{$self->_documentSet}) { | ||
| 232 | warn $document->getId . "\n"; | ||
| 233 | for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElements});$doc_idx++) { | ||
| 234 | $semanticUnit = $document->getAnnotations->getSemanticUnitLevel->getElements->[$doc_idx]; | ||
| 235 | $term_idx++; | ||
| 236 | |||
| 237 | $term_lemma = undef; | ||
| 238 | $term_postag = undef; | ||
| 239 | |||
| 240 | if ($semanticUnit->isTerm) { | ||
| 241 | $self->_output_hash()->{'term_idx'}->{$term_idx} = $semanticUnit; | ||
| 242 | $term_id = sprintf("%05d", $term_idx); | ||
| 243 | if ($semanticUnit->reference_name eq "refid_word") { | ||
| 244 | $term_lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $semanticUnit->reference->getId)->[0]->canonical_form; | ||
| 245 | $term_postag = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $semanticUnit->reference->getId)->[0]->syntactic_category; | ||
| 246 | } | ||
| 247 | |||
| 248 | if ($semanticUnit->reference_name eq "refid_phrase") { | ||
| 249 | ($term_lemma, $term_postag) = $self->_getPhraseInfo($document, $semanticUnit->reference); | ||
| 250 | } | ||
| 251 | |||
| 252 | if ($semanticUnit->reference_name eq "list_refid_token") { | ||
| 253 | warn "term $term_id is a list_refid_token\n"; | ||
| 254 | warn "Not done yet!\n" | ||
| 255 | } | ||
| 256 | |||
| 257 | if (defined $term_lemma) { | ||
| 258 | my @tmp = ($term_id, $term_lemma, $term_postag); | ||
| 259 | push @corpus_in_t, \@tmp; | ||
| 260 | } | ||
| 261 | |||
| 262 | } | ||
| 263 | } | ||
| 264 | } | ||
| 265 | warn "\tOpenning " . $self->_input_filename . "\n"; | ||
| 266 | |||
| 267 | open FILE_IN, ">" . $self->_input_filename or die "can't open " . $self->_input_filename . "\n"; | ||
| 268 | |||
| 269 | my $word_ref; | ||
| 270 | foreach $word_ref (@corpus_in_t) { | ||
| 271 | |||
| 272 | if ($word_ref->[0] =~ s/ /_/go) { | ||
| 273 | $word_ref->[2] =~ s/ /_/go; | ||
| 274 | } | ||
| 275 | print FILE_IN Encode::encode("iso-8859-1", join("\t",@$word_ref)) . "\n"; | ||
| 276 | } | ||
| 277 | |||
| 278 | close FILE_IN; | ||
| 279 | } elsif (defined $self->_config->configuration->{"RESOURCE"}->{"language=". uc($lang) }->{"CONTROLLED_TERMLIST"}) { | ||
| 280 | warn "Making term list for controlled indexing\n"; | ||
| 281 | if ( ! -f $self->_config->configuration->{"RESOURCE"}->{"language=". uc($lang)}->{"CONTROLLED_TERMLIST"}) { | ||
| 282 | die "No such file " . $self->_config->configuration->{"RESOURCE"}->{"language=". uc($lang)}->{"CONTROLLED_TERMLIST"} . "\n"; | ||
| 283 | } | ||
| 284 | |||
| 285 | $command_line = $self->_defineCommandLine($self->_config->commands($lang)->{'CONVERTTERMLIST_CMD'}, | ||
| 286 | $self->_config->configuration->{"RESOURCE"}->{"language=". uc($lang)}->{"CONTROLLED_TERMLIST"}, | ||
| 287 | $self->_tmp_filename); | ||
| 288 | |||
| 289 | warn "[FASTER LOG] command line : $command_line\n"; | ||
| 290 | $return_command_line .= $self->_exec_command($command_line); | ||
| 291 | $return_command_line .= "; "; | ||
| 292 | |||
| 293 | $term_idx = 0; | ||
| 294 | open TMPFILE, $self->_tmp_filename or die "No such file " . $self->_tmp_filename . "\n"; | ||
| 295 | binmode(TMPFILE, ":encoding(latin9)"); | ||
| 296 | |||
| 297 | warn "\tOpenning " . $self->_input_filename . "\n"; | ||
| 298 | open FILE_IN, ">" . $self->_input_filename or die "can't open " . $self->_input_filename . "\n"; | ||
| 299 | # binmode(FILE_IN, ":utf8"); | ||
| 300 | my $word_ref; | ||
| 301 | |||
| 302 | while($line = <TMPFILE>) { | ||
| 303 | $term_idx++; | ||
| 304 | $term_id = sprintf("%05d", ($term_idx - 1)); | ||
| 305 | print FILE_IN Encode::encode("iso-8859-1", join("\t",($term_id,$line))); | ||
| 306 | } | ||
| 307 | close FILE_IN; | ||
| 308 | close TMPFILE; | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | warn "[LOG] done\n"; | ||
| 313 | } | ||
| 314 | |||
| 315 | sub _getPhraseInfo { | ||
| 316 | my ($self, $document, $phrase) = @_; | ||
| 317 | |||
| 318 | my @term_lemmas; | ||
| 319 | my @term_postags; | ||
| 320 | my $term_lemma; | ||
| 321 | my $term_postag; | ||
| 322 | my $item; | ||
| 323 | |||
| 324 | |||
| 325 | foreach $item (@{$phrase->list_refid_components}) { | ||
| 326 | # warn "\tterm is " . $item . "\n"; | ||
| 327 | if (ref($item) eq "Lingua::Ogmios::Annotations::Word") { | ||
| 328 | #getElementFromIndex | ||
| 329 | #$term_lemma = $document->getAnnotations->getLemmaLevel $SemanticUnit | ||
| 330 | push @term_lemmas, $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $item->getId)->[0]->canonical_form ; | ||
| 331 | push @term_postags, $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $item->getId)->[0]->syntactic_category; | ||
| 332 | } | ||
| 333 | if (ref($item) eq "Lingua::Ogmios::Annotations::Phrase") { | ||
| 334 | ($term_lemma, $term_postag) = $self->_getphraseInfo($document,$item); | ||
| 335 | push @term_lemmas, $term_lemma; | ||
| 336 | push @term_postags, $term_postag; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | $term_lemma = join(" ", @term_lemmas); | ||
| 340 | $term_postag = join(" ", @term_postags); | ||
| 341 | |||
| 342 | return($term_lemma, $term_postag); | ||
| 343 | } | ||
| 344 | |||
| 345 | sub _outputParsing { | ||
| 346 | my ($self) = @_; | ||
| 347 | |||
| 348 | my $line; | ||
| 349 | my $sentence_id; | ||
| 350 | my $term; | ||
| 351 | my $term_id; | ||
| 352 | my $term_variante; | ||
| 353 | my $numeric_information; | ||
| 354 | my $MSrelation_type; | ||
| 355 | |||
| 356 | my $doc_idx; | ||
| 357 | my $sent_idx; | ||
| 358 | my $term_variante_clean; | ||
| 359 | my $term_variante_unit; | ||
| 360 | my $term_unit; | ||
| 361 | |||
| 362 | my $document; | ||
| 363 | |||
| 364 | my $domainSpecificRelation; | ||
| 365 | # my $term_unit; | ||
| 366 | |||
| 367 | warn "[LOG] . Parsing output Array\n"; | ||
| 368 | |||
| 369 | open FASTEROUTPUT, $self->_output_filename or warn "No such file " . $self->_output_filename; | ||
| 370 | # binmode(FASTEROUTPUT, ":utf8"); | ||
| 371 | |||
| 372 | while($line = <FASTEROUTPUT>) { | ||
| 373 | chomp $line; | ||
| 374 | # warn "$line\n"; | ||
| 375 | if ($line =~ /\tX[^\t]+$/) { | ||
| 376 | warn "=> $line\n"; | ||
| 377 | ($sentence_id, $term, $term_id, $term_variante, $numeric_information, $MSrelation_type) = split / ?\t ?/, $line; | ||
| 378 | # 1. check if the term_variante is already identify as a term | ||
| 379 | # 2. (if no) create a new term | ||
| 380 | $sentence_id += 0; | ||
| 381 | $term_id += 0; | ||
| 382 | $term_variante =~ s/^\s+//og; | ||
| 383 | |||
| 384 | # warn "$term_id\n"; | ||
| 385 | # warn "$sentence_id\n"; | ||
| 386 | |||
| 387 | $term_variante_clean = $term_variante; | ||
| 388 | $term_variante_clean =~ s/\[[^\]]+\]//og; | ||
| 389 | |||
| 390 | # warn "Find the document/sentence\n"; | ||
| 391 | $doc_idx = 0; | ||
| 392 | $sent_idx = $sentence_id; | ||
| 393 | while(($doc_idx <= $#{$self->_documentSet}) && ($sent_idx > $self->_documentSet->[$doc_idx]->getAnnotations->getSentenceLevel->getSize)) { | ||
| 394 | warn "\t(sent_idx: $sent_idx, doc_idx = $doc_idx)\n"; | ||
| 395 | warn "\t(" . $self->_documentSet->[$doc_idx]->getAnnotations->getSentenceLevel->getSize . ")\n"; | ||
| 396 | $sent_idx -= $self->_documentSet->[$doc_idx]->getAnnotations->getSentenceLevel->getSize; | ||
| 397 | $doc_idx++; | ||
| 398 | } | ||
| 399 | warn "sent_idx: $sent_idx, doc_idx = $doc_idx\n"; | ||
| 400 | |||
| 401 | $document = $self->_documentSet->[$doc_idx]; | ||
| 402 | warn $document->getId . "\n"; | ||
| 403 | |||
| 404 | # warn "$term_variante_clean exists ?\n\n"; | ||
| 405 | $term_variante_unit = undef; | ||
| 406 | my $sent = $document->getAnnotations->getSentenceLevel->getElement($sent_idx); | ||
| 407 | # warn "sentence: $sent\n"; | ||
| 408 | # warn "start token SENT: " . $sent->refid_start_token . "\n"; | ||
| 409 | # warn "end token SENT: " . $sent->refid_end_token . "\n"; | ||
| 410 | # warn "\t" . $sent->getForm . "\n\n"; | ||
| 411 | if ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("form", $term_variante_clean)) { | ||
| 412 | |||
| 413 | my $i = 0; | ||
| 414 | do { | ||
| 415 | $term_variante_unit = $document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("form", $term_variante_clean)->[$i]; | ||
| 416 | |||
| 417 | # warn "start token TU: " . $term_variante_unit->start_token . "\n"; | ||
| 418 | # warn "end token TU: " . $term_variante_unit->end_token . "\n"; | ||
| 419 | $i++; | ||
| 420 | } while((!(($sent->refid_start_token->getFrom <= $term_variante_unit->start_token->getFrom) && | ||
| 421 | ($term_variante_unit->end_token->getTo <= $sent->refid_end_token->getFrom))) && | ||
| 422 | ($i < scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("form", $term_variante_clean)}))); | ||
| 423 | if (!(($sent->refid_start_token->getFrom <= $term_variante_unit->start_token->getFrom) && | ||
| 424 | ($term_variante_unit->end_token->getTo <= $sent->refid_end_token->getFrom))) { | ||
| 425 | $term_variante_unit = undef; | ||
| 426 | # } else { | ||
| 427 | # warn "Term variante already exists $term_variante_unit\n"; | ||
| 428 | } | ||
| 429 | # warn "End\n"; | ||
| 430 | } | ||
| 431 | if (!defined $term_variante_unit) { | ||
| 432 | # Creation of a new term | ||
| 433 | # warn "$term_variante_clean ($term_variante) doesn't exists\n"; | ||
| 434 | # warn "building the token list\n"; | ||
| 435 | |||
| 436 | my @TV_words = split /\[[^\]]+\]/, $term_variante; | ||
| 437 | # warn "TV words: " . join(":", @TV_words) . "\n"; | ||
| 438 | my $j; | ||
| 439 | for($j = 2;$j < scalar(@TV_words);$j+=2) { | ||
| 440 | $TV_words[$j] =~ s/^ //o; | ||
| 441 | } | ||
| 442 | # warn "\n" . $sent->getForm . "\n"; | ||
| 443 | my $word; | ||
| 444 | my $i = 0; | ||
| 445 | my $term_content; | ||
| 446 | my @TermWords; | ||
| 447 | do { | ||
| 448 | do { | ||
| 449 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("form", $TV_words[0])->[$i]; | ||
| 450 | $i++; | ||
| 451 | if (!defined $word) { | ||
| 452 | $sent_idx++; | ||
| 453 | $sent = $document->getAnnotations->getSentenceLevel->getElement($sent_idx); | ||
| 454 | $i = 0; | ||
| 455 | } else { | ||
| 456 | # warn "word: $word" . $word->getForm . "\n"; | ||
| 457 | } | ||
| 458 | } while((!defined $word) || ((defined $sent) && (!(($sent->refid_start_token->getFrom <= $word->start_token->getFrom) && | ||
| 459 | ($word->end_token->getTo <= $sent->refid_end_token->getFrom))) && | ||
| 460 | ($i < scalar($document->getAnnotations->getWordLevel->getElementFromIndex("form", $TV_words[0]))))); | ||
| 461 | # if ((defined $word) && (!defined $sent)) { | ||
| 462 | # $sent_idx--; | ||
| 463 | # $sent = $document->getAnnotations->getSentenceLevel->getElement($sent_idx); | ||
| 464 | # warn "\t" . $sent->getForm . "\n"; | ||
| 465 | # warn "word: $word" . $word->getForm . "\n"; | ||
| 466 | # } | ||
| 467 | $term_content = $word->getForm; | ||
| 468 | @TermWords = (); | ||
| 469 | # (defined $sent) && | ||
| 470 | if ((($sent->refid_start_token->getFrom <= $word->start_token->getFrom) && | ||
| 471 | ($word->end_token->getTo <= $sent->refid_end_token->getFrom))) { | ||
| 472 | push @TermWords, $word; | ||
| 473 | $j = 2; # first word already found | ||
| 474 | do { | ||
| 475 | $word = $word->next; | ||
| 476 | $term_content .= " " . $word->getForm; | ||
| 477 | push @TermWords, $word; | ||
| 478 | $j+=2; | ||
| 479 | } while (($TV_words[$j - 2] eq $word->getForm) && ($j < scalar(@TV_words))); | ||
| 480 | } | ||
| 481 | } while(((!(($sent->refid_start_token->getFrom <= $word->start_token->getFrom) && | ||
| 482 | ($word->end_token->getTo <= $sent->refid_end_token->getFrom)))) && | ||
| 483 | ($term_content ne $term_variante_clean)); | ||
| 484 | # warn "term content: $term_content\n"; | ||
| 485 | if (scalar(@TermWords) < 2) { | ||
| 486 | # warn "Strange: multiword term is recognize has a word\n;"; | ||
| 487 | } | ||
| 488 | if ($term_content eq $term_variante_clean) { | ||
| 489 | my $phrase = Lingua::Ogmios::Annotations::Phrase->new( | ||
| 490 | { 'refid_word' => \@TermWords, | ||
| 491 | 'form' => $term_content, | ||
| 492 | } | ||
| 493 | ); | ||
| 494 | $document->getAnnotations->addPhrase($phrase); | ||
| 495 | $term_variante_unit = Lingua::Ogmios::Annotations::SemanticUnit->newTerm( | ||
| 496 | {'form' => $term_content, | ||
| 497 | 'refid_phrase' => $phrase, | ||
| 498 | 'canonical_form' => $term, | ||
| 499 | }); | ||
| 500 | $document->getAnnotations->addSemanticUnit($term_variante_unit); | ||
| 501 | } | ||
| 502 | } | ||
| 503 | |||
| 504 | if (defined $term_variante_unit) { | ||
| 505 | warn "Add a domain specific relation $term_id\n"; | ||
| 506 | $term_variante_unit->canonical_form($term); | ||
| 507 | if (exists $self->_output_hash()->{'term_idx'}->{$term_id}) { | ||
| 508 | my @tmp = ($self->_output_hash()->{'term_idx'}->{$term_id}, $term_variante_unit); | ||
| 509 | |||
| 510 | # warn join(" : ", @tmp) . "\n"; | ||
| 511 | |||
| 512 | $domainSpecificRelation = Lingua::Ogmios::Annotations::DomainSpecificRelation->new( | ||
| 513 | {'domain_specific_relation_type' => $MSrelation_type, | ||
| 514 | 'list_refid_semantic_unit' => \@tmp, | ||
| 515 | }); | ||
| 516 | # foreach my $field (@{$document->getAnnotations->getDomainSpecificRelationLevel->getIndexfields}) { | ||
| 517 | # warn "$field\n"; | ||
| 518 | # my $value = $domainSpecificRelation->_getField($field); | ||
| 519 | # warn ref($value) . "\n"; | ||
| 520 | # # warn join(" : ", @$value) . "\n"; | ||
| 521 | # } | ||
| 522 | # warn "add relation\n"; | ||
| 523 | $document->getAnnotations->addDomainSpecificRelation($domainSpecificRelation); | ||
| 524 | # warn "end\n\n"; | ||
| 525 | } else { | ||
| 526 | # warn "term: $term\n"; | ||
| 527 | # $document->getAnnotations->getSemanticUnitLevel->printIndex("form"); | ||
| 528 | # warn scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("form", $term)}) . "\n"; | ||
| 529 | foreach $term_unit (@{$document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("form", $term)}) { | ||
| 530 | my @tmp = ($term_unit, $term_variante_unit); | ||
| 531 | |||
| 532 | # warn join(" : ", @tmp) . "\n"; | ||
| 533 | |||
| 534 | $domainSpecificRelation = Lingua::Ogmios::Annotations::DomainSpecificRelation->new( | ||
| 535 | {'domain_specific_relation_type' => $MSrelation_type, | ||
| 536 | 'list_refid_semantic_unit' => \@tmp, | ||
| 537 | }); | ||
| 538 | $document->getAnnotations->addDomainSpecificRelation($domainSpecificRelation); | ||
| 539 | |||
| 540 | } | ||
| 541 | # warn "$line not a term\n"; | ||
| 542 | } | ||
| 543 | } | ||
| 544 | } | ||
| 545 | } | ||
| 546 | close FASTEROUTPUT; | ||
| 547 | |||
| 548 | warn "[LOG] done\n"; | ||
| 549 | } | ||
| 550 | |||
| 551 | sub run { | ||
| 552 | my ($self, $documentSet) = @_; | ||
| 553 | |||
| 554 | warn "*** TODO: check if the level exists\n"; | ||
| 555 | # Set variables according the the configuration | ||
| 556 | |||
| 557 | $self->_documentSet($documentSet); | ||
| 558 | |||
| 559 | warn "[LOG] " . $self->_config->comments . " ... \n"; | ||
| 560 | |||
| 561 | $self->_inputFaster; | ||
| 562 | |||
| 563 | my $command_line = $self->_processFaster; | ||
| 564 | |||
| 565 | if (($self->_position eq "last") && ($self->_no_standard_output)) { | ||
| 566 | warn "print no standard output\n"; | ||
| 567 | |||
| 568 | if (($self->_no_standard_output eq "TXT") || ($self->_no_standard_output == 5)){ | ||
| 569 | $self->textoutput; | ||
| 570 | } | ||
| 571 | } else { | ||
| 572 | $self->_outputParsing; | ||
| 573 | } | ||
| 574 | # # Put log information | ||
| 575 | |||
| 576 | my $information = { 'software_name' => $self->_config->name, | ||
| 577 | 'comments' => $self->_config->comments, | ||
| 578 | 'command_line' => $command_line, | ||
| 579 | 'list_modified_level' => ['phrase_level', 'semantic_unit_level', 'domain_specific_relation_level'], | ||
| 580 | }; | ||
| 581 | $self->_log($information); | ||
| 582 | |||
| 583 | |||
| 584 | warn "[LOG] done\n"; | ||
| 585 | } | ||
| 586 | |||
| 587 | sub textoutput { | ||
| 588 | my ($self) = @_; | ||
| 589 | |||
| 590 | my $line; | ||
| 591 | my $sentence_id; | ||
| 592 | my $term; | ||
| 593 | my $term_id; | ||
| 594 | my $term_variante; | ||
| 595 | my $numeric_information; | ||
| 596 | my $MSrelation_type; | ||
| 597 | my $term_variante_clean; | ||
| 598 | |||
| 599 | binmode(STDOUT, ":utf8"); | ||
| 600 | open FASTEROUTPUT, $self->_output_filename or warn "No such file " . $self->_output_filename; | ||
| 601 | binmode(FASTEROUTPUT, ":encoding(latin1)"); | ||
| 602 | |||
| 603 | while($line = <FASTEROUTPUT>) { | ||
| 604 | chomp $line; | ||
| 605 | # warn "$line\n"; | ||
| 606 | if ($line =~ /\tX[^\t]+$/) { | ||
| 607 | # warn "=> $line\n"; | ||
| 608 | ($sentence_id, $term, $term_id, $term_variante, $numeric_information, $MSrelation_type) = split /\t/, $line; | ||
| 609 | $term_variante =~ s/^\s+//og; | ||
| 610 | $term_variante_clean = $term_variante; | ||
| 611 | $term_variante_clean =~ s/\[[^\]]+\]//og; | ||
| 612 | |||
| 613 | print "$term\t$term_variante_clean\t$MSrelation_type\n"; | ||
| 614 | } | ||
| 615 | } | ||
| 616 | |||
| 617 | } | ||
| 618 | |||
| 619 | 1; | ||
| 620 | |||
| 621 | |||
| 622 | __END__ |