| 1 | package Lingua::Ogmios::NLPWrappers::Wrapper; | ||
| 2 | |||
| 3 | |||
| 4 | our $VERSION='0.1'; | ||
| 5 | |||
| 6 | use utf8; | ||
| 7 | |||
| 8 | use Config::General; | ||
| 9 | |||
| 10 | use Data::Dumper; | ||
| 11 | |||
| 12 | use Lingua::Ogmios::NLPWrappers::PatternParser; | ||
| 13 | |||
| 14 | use Lingua::Ogmios::Annotations::Phrase; | ||
| 15 | use Lingua::Ogmios::Annotations::SemanticUnit; | ||
| 16 | use Lingua::Ogmios::Annotations::SemanticFeatures; | ||
| 17 | use Lingua::Ogmios::Annotations::LogProcessing; | ||
| 18 | use Lingua::Ogmios::Timer; | ||
| 19 | use Lingua::Ogmios::LearningDataSet; | ||
| 20 | use Lingua::Ogmios::LearningDataSet::Attribute; | ||
| 21 | use Lingua::Ogmios::LearningDataSet::Data; | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use warnings; | ||
| 25 | sub new { | ||
| 26 | my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output, $out_stream) = @_; | ||
| 27 | |||
| 28 | my $wrapper = { | ||
| 29 | "config" => $config, | ||
| 30 | "tmpfile_prefix" => $tmpfile_prefix, | ||
| 31 | "documentSet" => undef, | ||
| 32 | "input_filename" => undef, | ||
| 33 | "tmp_filename" => undef, | ||
| 34 | "output_filename" => undef, | ||
| 35 | "logfile" => $logfile, | ||
| 36 | "position" => $position, # position in the NL line processing | ||
| 37 | "no_standard_output" => $no_standard_output, | ||
| 38 | "timer" => Lingua::Ogmios::Timer->new(), | ||
| 39 | "currentDocument" => undef, | ||
| 40 | "out_stream" => $out_stream, | ||
| 41 | }; | ||
| 42 | |||
| 43 | bless $wrapper, $class; | ||
| 44 | |||
| 45 | $wrapper->{"timer"}->start; | ||
| 46 | |||
| 47 | # die "You call the 'new' method of the wrapper class base\n | ||
| 48 | # You should define a 'new' method for your wrapper\n"; | ||
| 49 | |||
| 50 | return($wrapper); | ||
| 51 | } | ||
| 52 | |||
| 53 | # sub _logfile { | ||
| 54 | # my ($self) = @_; | ||
| 55 | |||
| 56 | # die "You call the '_logfile' method of the wrapper class base\n | ||
| 57 | # You should define a '_logfile' method for your wrapper\n"; | ||
| 58 | # } | ||
| 59 | |||
| 60 | |||
| 61 | sub run { | ||
| 62 | my ($self, $documentSet) = @_; | ||
| 63 | |||
| 64 | die "You call the 'rum' method of the wrapper class base\n | ||
| 65 | You should define a 'run' method for your wrapper\n"; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _defineCommandLine { | ||
| 69 | my ($self, $cmd, $input, $output) = @_; | ||
| 70 | |||
| 71 | my $command_line; | ||
| 72 | |||
| 73 | $command_line = "("; | ||
| 74 | $command_line .= $cmd; | ||
| 75 | $command_line .= ") "; | ||
| 76 | if (defined $input) { | ||
| 77 | $command_line .= " < "; | ||
| 78 | $command_line .= $input; | ||
| 79 | } | ||
| 80 | if (defined $output) { | ||
| 81 | $command_line .= " > "; | ||
| 82 | $command_line .= $output; | ||
| 83 | } | ||
| 84 | $command_line .= " 2>> " . $self->_logfile; | ||
| 85 | warn "[LOG] command line : $command_line\n"; | ||
| 86 | return($command_line); | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | sub _exec_command { | ||
| 91 | |||
| 92 | my ($self, $command, $env_vars) = @_; | ||
| 93 | my $cmd_withEnvVar = ""; | ||
| 94 | my $var; | ||
| 95 | |||
| 96 | |||
| 97 | foreach $var (keys %$env_vars) { | ||
| 98 | $cmd_withEnvVar .= "export " . $var . "=" . $env_vars->{$var} . " ; "; | ||
| 99 | } | ||
| 100 | $cmd_withEnvVar .= $command; | ||
| 101 | # warn "Running $cmd_withEnvVar\n"; | ||
| 102 | `$cmd_withEnvVar`; | ||
| 103 | |||
| 104 | return($cmd_withEnvVar); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub _log { | ||
| 108 | my ($self, $information) = @_; | ||
| 109 | |||
| 110 | my $document; | ||
| 111 | foreach $document (@{$self->_documentSet}) { | ||
| 112 | $document->getAnnotations->addLogProcessing(Lingua::Ogmios::Annotations::LogProcessing->new($information)); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | sub _input_filename { | ||
| 117 | my $self = shift; | ||
| 118 | |||
| 119 | $self->{"input_filename"} = shift if @_; | ||
| 120 | |||
| 121 | return($self->{"input_filename"}); | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | sub _tmp_filename { | ||
| 126 | my $self = shift; | ||
| 127 | |||
| 128 | $self->{"input_filename"} = shift if @_; | ||
| 129 | |||
| 130 | return($self->{"input_filename"}); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _output_filename { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | $self->{"output_filename"} = shift if @_; | ||
| 137 | |||
| 138 | return($self->{"output_filename"}); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _out_stream { | ||
| 142 | my $self = shift; | ||
| 143 | |||
| 144 | $self->{"out_stream"} = shift if @_; | ||
| 145 | |||
| 146 | return($self->{"out_stream"}); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _input_array { | ||
| 150 | my $self = shift; | ||
| 151 | |||
| 152 | $self->{"input_array"} = shift if @_; | ||
| 153 | |||
| 154 | return($self->{"input_array"}); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub _output_array { | ||
| 158 | my $self = shift; | ||
| 159 | |||
| 160 | $self->{"output_array"} = shift if @_; | ||
| 161 | |||
| 162 | return($self->{"output_array"}); | ||
| 163 | } | ||
| 164 | |||
| 165 | sub _addedElements_array { | ||
| 166 | my $self = shift; | ||
| 167 | |||
| 168 | $self->{"addedElements_array"} = shift if @_; | ||
| 169 | |||
| 170 | return($self->{"addedElements_array"}); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub _input_hash { | ||
| 174 | my $self = shift; | ||
| 175 | |||
| 176 | $self->{"input_hash"} = shift if @_; | ||
| 177 | |||
| 178 | return($self->{"input_hash"}); | ||
| 179 | } | ||
| 180 | |||
| 181 | sub _output_hash { | ||
| 182 | my $self = shift; | ||
| 183 | |||
| 184 | $self->{"output_hash"} = shift if @_; | ||
| 185 | |||
| 186 | return($self->{"output_hash"}); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub _config { | ||
| 190 | my $self = shift; | ||
| 191 | |||
| 192 | $self->{"config"} = shift if @_; | ||
| 193 | |||
| 194 | return($self->{"config"}); | ||
| 195 | |||
| 196 | } | ||
| 197 | sub _position { | ||
| 198 | my $self = shift; | ||
| 199 | |||
| 200 | $self->{"position"} = shift if @_; | ||
| 201 | |||
| 202 | return($self->{"position"}); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub _no_standard_output { | ||
| 206 | my $self = shift; | ||
| 207 | |||
| 208 | $self->{"no_standard_output"} = shift if @_; | ||
| 209 | |||
| 210 | return($self->{"no_standard_output"}); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub _tmpfile_prefix { | ||
| 214 | my $self = shift; | ||
| 215 | |||
| 216 | $self->{"tmpfile_prefix"} = shift if @_; | ||
| 217 | |||
| 218 | # warn "tmpfile_prefix:" . $self->{"tmpfile_prefix"} . "\n"; | ||
| 219 | |||
| 220 | return($self->{"tmpfile_prefix"}); | ||
| 221 | |||
| 222 | } | ||
| 223 | |||
| 224 | sub _logfile { | ||
| 225 | my $self = shift; | ||
| 226 | |||
| 227 | $self->{"logfile"} = shift if @_; | ||
| 228 | |||
| 229 | return($self->{"logfile"}); | ||
| 230 | } | ||
| 231 | |||
| 232 | sub _documentSet { | ||
| 233 | my $self = shift; | ||
| 234 | |||
| 235 | if (@_) { | ||
| 236 | $self->{'documentSet'} = shift; | ||
| 237 | |||
| 238 | $self->setDocIndex2DocId; | ||
| 239 | $self->setDocId2DocIndex; | ||
| 240 | } | ||
| 241 | return($self->{'documentSet'}); | ||
| 242 | } | ||
| 243 | |||
| 244 | sub setDocIndex2DocId { | ||
| 245 | |||
| 246 | my ($self) = @_; | ||
| 247 | |||
| 248 | my $i; | ||
| 249 | my @DocIndex2DocId; | ||
| 250 | |||
| 251 | for($i=0;$i < scalar(@{$self->{'documentSet'}}); $i++) { | ||
| 252 | $DocIndex2DocId[$i] = $self->{'documentSet'}->[$i]->getId; | ||
| 253 | } | ||
| 254 | |||
| 255 | $self->{DocIndex2DocId} = \@DocIndex2DocId; | ||
| 256 | } | ||
| 257 | |||
| 258 | sub setDocId2DocIndex { | ||
| 259 | |||
| 260 | my ($self) = @_; | ||
| 261 | |||
| 262 | my $i; | ||
| 263 | my %DocId2DocIndex; | ||
| 264 | |||
| 265 | # warn "In setDocId2Index\n"; | ||
| 266 | |||
| 267 | for($i=0;$i < scalar(@{$self->{'documentSet'}}); $i++) { | ||
| 268 | # warn $self->{'documentSet'}->[$i]->getId . " => $i\n"; | ||
| 269 | $DocId2DocIndex{$self->{'documentSet'}->[$i]->getId} = $i; | ||
| 270 | } | ||
| 271 | |||
| 272 | $self->{DocId2DocIndex} = \%DocId2DocIndex; | ||
| 273 | |||
| 274 | } | ||
| 275 | |||
| 276 | sub getDocId2DocIndex { | ||
| 277 | |||
| 278 | my ($self) = @_; | ||
| 279 | |||
| 280 | return($self->{DocId2DocIndex}); | ||
| 281 | } | ||
| 282 | |||
| 283 | sub getDocIndex2DocId { | ||
| 284 | |||
| 285 | my ($self) = @_; | ||
| 286 | |||
| 287 | return($self->{DocIndex2DocId}); | ||
| 288 | } | ||
| 289 | |||
| 290 | sub getDocumentFromDocId { | ||
| 291 | |||
| 292 | my ($self, $docId) = @_; | ||
| 293 | |||
| 294 | return($self->_documentSet->[$self->getDocId2DocIndex->{$docId}]); | ||
| 295 | |||
| 296 | } | ||
| 297 | |||
| 298 | sub getDocumentFromRank { | ||
| 299 | |||
| 300 | my ($self, $rank) = @_; | ||
| 301 | |||
| 302 | return($self->_documentSet->[$rank]); | ||
| 303 | |||
| 304 | } | ||
| 305 | |||
| 306 | sub _getLemmatisedSentence { | ||
| 307 | my ($self, $document, $refid_start_token, $refid_end_token) = @_; | ||
| 308 | |||
| 309 | my $word; | ||
| 310 | my $lemma; | ||
| 311 | |||
| 312 | my $sentLemma = "";; | ||
| 313 | my $token = $refid_start_token; | ||
| 314 | |||
| 315 | my $token_prec = 0; | ||
| 316 | my %offsets; | ||
| 317 | |||
| 318 | if (defined $token) { | ||
| 319 | do { | ||
| 320 | # warn $token->getContent . "\n"; | ||
| 321 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 322 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 323 | $lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 324 | # $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 325 | # warn $word->getFrom . " : " . $lemma->canonical_form . " : " . length($sentLemma) . "($sentLemma)\n"; | ||
| 326 | $offsets{length($sentLemma)} = $word->reference; | ||
| 327 | if (defined $lemma) { | ||
| 328 | $sentLemma .= $lemma->canonical_form; | ||
| 329 | } else { | ||
| 330 | $sentLemma .= $word->getForm; | ||
| 331 | } | ||
| 332 | if (index($word->getForm, "'") == length($word->getForm) - 1) { | ||
| 333 | $sentLemma .= " "; | ||
| 334 | } | ||
| 335 | $token = $word->end_token; | ||
| 336 | $token_prec = 0; | ||
| 337 | } else { | ||
| 338 | $offsets{length($sentLemma)} = [$token]; | ||
| 339 | $sentLemma .= $token->getContent; | ||
| 340 | $token_prec = 1; | ||
| 341 | } | ||
| 342 | if (defined $token) { # { | ||
| 343 | $token = $token->next; | ||
| 344 | } | ||
| 345 | |||
| 346 | # warn "=> " . length($sentLemma) . " ($sentLemma)\n"; | ||
| 347 | } while((defined $token) && (!($token->previous->equals($refid_end_token)))); | ||
| 348 | } | ||
| 349 | # warn (length($sentLemma) - length($token->previous->getContent)) . "\n"; | ||
| 350 | # warn scalar(@{$offsets{length($sentLemma) - length($token->previous->getContent)}}) . " -- \n"; | ||
| 351 | # warn $offsets{length($sentLemma) - length($token->previous->getContent)}->[0]->getContent . "\n"; | ||
| 352 | # warn "-> $token_prec\n"; | ||
| 353 | # warn join(";", sort({$a <=> $b} keys %offsets)) . "\n"; | ||
| 354 | if (($token_prec == 0) && ($document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $refid_end_token->getId))) { | ||
| 355 | # warn "Add empty\n"; | ||
| 356 | $offsets{length($sentLemma)} = []; | ||
| 357 | } | ||
| 358 | # warn join(";", sort({$a <=> $b} keys %offsets)) . "\n"; | ||
| 359 | # warn join(";", values %offsets) . "\n"; | ||
| 360 | |||
| 361 | # if ((defined $token) && ($token->isSymb)) { | ||
| 362 | # if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 363 | # $offsets{length($sentLemma)} = $token; | ||
| 364 | # $sentLemma .= $token->getContent; | ||
| 365 | # } | ||
| 366 | # } | ||
| 367 | # $sentLemma =~ s/ +/ /go; | ||
| 368 | return($sentLemma, \%offsets); | ||
| 369 | } | ||
| 370 | |||
| 371 | |||
| 372 | sub _printTreeTaggerFormatOutput { | ||
| 373 | my $self = shift; | ||
| 374 | my $filename = shift; | ||
| 375 | my $encoding = shift; | ||
| 376 | my $printDocId = shift; | ||
| 377 | |||
| 378 | my $fh_out; | ||
| 379 | my $document; | ||
| 380 | my $doc_idx; | ||
| 381 | my $token; | ||
| 382 | my $word; | ||
| 383 | my $lemma; | ||
| 384 | my $MS_features; | ||
| 385 | my @corpus_in_t; | ||
| 386 | |||
| 387 | warn "[LOG] printing TreeTagger Like Output\n"; | ||
| 388 | |||
| 389 | my $i = 0; | ||
| 390 | foreach $document (@{$self->_documentSet}) { | ||
| 391 | warn "$i : " . $document->getId . "\n"; | ||
| 392 | $i++; | ||
| 393 | my @tmp; | ||
| 394 | if ((!defined $printDocId) || ($printDocId)) { | ||
| 395 | @tmp = ($document->getId, "DOCUMENT", $document->getId); | ||
| 396 | push @corpus_in_t, \@tmp; | ||
| 397 | } | ||
| 398 | |||
| 399 | for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) { | ||
| 400 | # warn "doc_idx: $doc_idx\n"; | ||
| 401 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 402 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 403 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 404 | $lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 405 | $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 406 | my $wordform = $word->getForm; | ||
| 407 | $wordform =~ s/[\t\n]/ /gos; | ||
| 408 | $wordform =~ s/ +/ /gos; | ||
| 409 | |||
| 410 | my @tmp = ($wordform, $MS_features->syntactic_category, $lemma->canonical_form); | ||
| 411 | |||
| 412 | push @corpus_in_t, \@tmp; | ||
| 413 | |||
| 414 | $doc_idx += $word->getReferenceSize - 1; | ||
| 415 | # warn "doc_idx (new value): $doc_idx\n"; | ||
| 416 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 417 | |||
| 418 | } else { | ||
| 419 | if (!($token->isSep)) { | ||
| 420 | if ($token->getContent =~ /[\x{2019}\x{2032}']/go) { | ||
| 421 | $corpus_in_t[$#corpus_in_t]->[0] .= $token->getContent; | ||
| 422 | } else { | ||
| 423 | my @tmp = ($token->getContent, $token->getContent, $token->getContent); | ||
| 424 | push @corpus_in_t, \@tmp; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | } | ||
| 428 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 429 | if ($token->isSymb) { | ||
| 430 | if (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) { | ||
| 431 | my @tmp = ($corpus_in_t[$#corpus_in_t]->[0] , "SENT", $corpus_in_t[$#corpus_in_t]->[0]); | ||
| 432 | $corpus_in_t[$#corpus_in_t] = \@tmp; | ||
| 433 | } else { | ||
| 434 | my @tmp = ($token->getContent, "SENT", $token->getContent); | ||
| 435 | push @corpus_in_t, \@tmp; | ||
| 436 | } | ||
| 437 | } else { | ||
| 438 | my @tmp = (".", "SENT", "."); | ||
| 439 | push @corpus_in_t, \@tmp; | ||
| 440 | } | ||
| 441 | } | ||
| 442 | } | ||
| 443 | } | ||
| 444 | |||
| 445 | warn "\tOpenning " . $filename . "\n"; | ||
| 446 | if ($filename ne "stdout") { | ||
| 447 | open $fh_out, ">". $filename or die "can't open " . $filename . "\n"; | ||
| 448 | } else { | ||
| 449 | $fh_out = \*STDOUT; | ||
| 450 | } | ||
| 451 | |||
| 452 | |||
| 453 | my $word_ref; | ||
| 454 | foreach $word_ref (@corpus_in_t) { | ||
| 455 | |||
| 456 | if ($word_ref->[0] =~ s/ /_/go) { | ||
| 457 | $word_ref->[2] =~ s/ /_/go; | ||
| 458 | } | ||
| 459 | |||
| 460 | if ($word_ref->[0] =~ s/[\x{2019}\x{2032}]/\'/go) { | ||
| 461 | $word_ref->[1] =~ s/[\x{2019}\x{2032}]/\'/go; | ||
| 462 | $word_ref->[2] =~ s/[\x{2019}\x{2032}]/\'/go; | ||
| 463 | } | ||
| 464 | |||
| 465 | # Encode::encode("iso-8859-1", join("\n",@corpus_in_t), Encode::FB_DEFAULT); | ||
| 466 | if ((!defined $encoding) || (uc($encoding) eq "UTF-8")) { | ||
| 467 | print $fh_out Encode::encode("UTF-8", join("\t",@$word_ref)) . "\n"; | ||
| 468 | } else { | ||
| 469 | if ((defined $encoding) && (uc($encoding) eq "LATIN1")) { | ||
| 470 | # print FILE_IN Encode::encode("iso-8859-1", join("\t",@$word_ref), Encode::FB_DEFAULT) . "\n"; | ||
| 471 | print $fh_out Encode::encode("iso-8859-1", join("\t",@$word_ref)) . "\n"; | ||
| 472 | } else { | ||
| 473 | warn "[WRAPPER LOG] Unknown enconding charset\n"; | ||
| 474 | } | ||
| 475 | } | ||
| 476 | } | ||
| 477 | |||
| 478 | close $fh_out; | ||
| 479 | warn "[LOG] done\n"; | ||
| 480 | } | ||
| 481 | |||
| 482 | |||
| 483 | sub _createTermFromStartEndTokens { | ||
| 484 | my ($self, $document, $token_start, $token_end) = @_; | ||
| 485 | |||
| 486 | my @tokens; | ||
| 487 | |||
| 488 | my $term_content = ""; | ||
| 489 | |||
| 490 | my $token = $token_start; | ||
| 491 | |||
| 492 | # warn "start_token: " . $token->getFrom . " (" . $token->getContent . ")\n"; | ||
| 493 | $term_content = $token->getContent; | ||
| 494 | |||
| 495 | push @tokens, $token; | ||
| 496 | while((defined $token) && (!$token->equals($token_end))) { | ||
| 497 | $token = $token->next; | ||
| 498 | $term_content .= $token->getContent; | ||
| 499 | push @tokens, $token; | ||
| 500 | } | ||
| 501 | # warn "term_content: $term_content\n"; | ||
| 502 | return($self->_createTerm($document, \@tokens, $term_content)); | ||
| 503 | |||
| 504 | } | ||
| 505 | |||
| 506 | sub _changeSemanticFeatures { | ||
| 507 | my ($self, $document, $oldTerm1, $oldTerm2, $newTerm) = @_; | ||
| 508 | |||
| 509 | my $semf; | ||
| 510 | |||
| 511 | if (defined $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $oldTerm1->getId)->[0]) { | ||
| 512 | if ((defined $oldTerm2) && (defined $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $oldTerm2->getId)->[0])) { | ||
| 513 | my $semFeatures = Lingua::Ogmios::Annotations::SemanticFeatures->new( | ||
| 514 | { 'semantic_category' => $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $self->_getLargerTerm([$oldTerm1, $oldTerm2])->getId)->[0]->semantic_category, | ||
| 515 | 'refid_semantic_unit' => $newTerm->getId, | ||
| 516 | }); | ||
| 517 | $document->getAnnotations->addSemanticFeatures($semFeatures); | ||
| 518 | |||
| 519 | |||
| 520 | } else { | ||
| 521 | my $semFeatures = Lingua::Ogmios::Annotations::SemanticFeatures->new( | ||
| 522 | { 'semantic_category' => $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $oldTerm1->getId)->[0]->semantic_category, | ||
| 523 | 'refid_semantic_unit' => $newTerm->getId, | ||
| 524 | }); | ||
| 525 | $document->getAnnotations->addSemanticFeatures($semFeatures); | ||
| 526 | } | ||
| 527 | } else { | ||
| 528 | if ((defined $oldTerm2) && (defined $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $oldTerm2->getId)->[0])) { | ||
| 529 | my $semFeatures = Lingua::Ogmios::Annotations::SemanticFeatures->new( | ||
| 530 | { 'semantic_category' => $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $oldTerm2->getId)->[0]->semantic_category, | ||
| 531 | 'refid_semantic_unit' => $newTerm->getId, | ||
| 532 | }); | ||
| 533 | $document->getAnnotations->addSemanticFeatures($semFeatures); | ||
| 534 | } | ||
| 535 | } | ||
| 536 | } | ||
| 537 | |||
| 538 | sub _getLargerTerm { | ||
| 539 | my ($self, $terms) = @_; | ||
| 540 | |||
| 541 | my $largerTerm; | ||
| 542 | my $tmpTerm; | ||
| 543 | |||
| 544 | $largerTerm = $terms->[0]; | ||
| 545 | |||
| 546 | foreach $tmpTerm (@$terms) { | ||
| 547 | if (!$tmpTerm->equals($largerTerm)) { | ||
| 548 | if (($tmpTerm->start_token->getFrom < $largerTerm->start_token->getFrom ) || | ||
| 549 | ($largerTerm->end_token->getTo < $tmpTerm->end_token->getTo)) { | ||
| 550 | $largerTerm = $tmpTerm | ||
| 551 | } | ||
| 552 | } | ||
| 553 | } | ||
| 554 | return($largerTerm); | ||
| 555 | } | ||
| 556 | |||
| 557 | |||
| 558 | |||
| 559 | sub _createTerm { | ||
| 560 | my ($self, $document, $termTokens, $term_content, $termType) = @_; | ||
| 561 | |||
| 562 | my $token_offset = $termTokens->[0]; | ||
| 563 | my @termWords; | ||
| 564 | |||
| 565 | my $termUnit; | ||
| 566 | |||
| 567 | $termUnit = $document->getAnnotations->getSemanticUnitLevel->getElementByStartEndTokens($termTokens->[0], $termTokens->[scalar(@$termTokens) - 1]); | ||
| 568 | |||
| 569 | if (!defined $termUnit) { | ||
| 570 | do { | ||
| 571 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token_offset->getId)) { | ||
| 572 | ## search the last word | ||
| 573 | push @termWords, $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token_offset->getId)->[0]; | ||
| 574 | $token_offset = $termWords[$#termWords]->getLastToken; | ||
| 575 | } | ||
| 576 | $token_offset = $token_offset->next; | ||
| 577 | } while((defined $token_offset) && ($token_offset->getTo < $termTokens->[$#$termTokens]->next->getTo)); | ||
| 578 | |||
| 579 | if ((scalar @termWords != 0) && | ||
| 580 | (($termTokens->[$#$termTokens]->getTo != $termWords[$#termWords]->getLastToken->getTo) || | ||
| 581 | ($termTokens->[0]->getFrom != $termWords[0]->start_token->getFrom))) { | ||
| 582 | @termWords = (); | ||
| 583 | } | ||
| 584 | if (scalar @termWords == 0) { | ||
| 585 | # term is a token list | ||
| 586 | my @localTermTokens = @$termTokens; | ||
| 587 | if ((!defined($termType)) || ($termType eq "term")) { | ||
| 588 | $termUnit = Lingua::Ogmios::Annotations::SemanticUnit->newTerm( | ||
| 589 | {'form' => $term_content, | ||
| 590 | 'list_refid_token' => \@localTermTokens, | ||
| 591 | }); | ||
| 592 | # warn "create Term (a)\n"; | ||
| 593 | } else { | ||
| 594 | if ($termType eq "named_entity") { | ||
| 595 | $termUnit = Lingua::Ogmios::Annotations::SemanticUnit->newNamedEntity( | ||
| 596 | {'form' => $term_content, | ||
| 597 | 'list_refid_token' => \@localTermTokens, | ||
| 598 | 'named_entity_type' => "", | ||
| 599 | }); | ||
| 600 | # warn "create Named Entity (a)\n"; | ||
| 601 | } | ||
| 602 | } | ||
| 603 | } | ||
| 604 | if (scalar @termWords == 1) { | ||
| 605 | # term is a word | ||
| 606 | $term_content = $termWords[0]->getForm; | ||
| 607 | if ((!defined($termType)) || ($termType eq "term")) { | ||
| 608 | $termUnit = Lingua::Ogmios::Annotations::SemanticUnit->newTerm( | ||
| 609 | {'form' => $term_content, | ||
| 610 | 'refid_word' => $termWords[0], | ||
| 611 | }); | ||
| 612 | # warn "create Term (b)\n"; | ||
| 613 | } else { | ||
| 614 | if ($termType eq "named_entity") { | ||
| 615 | $termUnit = Lingua::Ogmios::Annotations::SemanticUnit->newNamedEntity( | ||
| 616 | {'form' => $term_content, | ||
| 617 | 'refid_word' => $termWords[0], | ||
| 618 | 'named_entity_type' => "", | ||
| 619 | }); | ||
| 620 | # warn "create Named Entity (b)\n"; | ||
| 621 | } | ||
| 622 | } | ||
| 623 | } | ||
| 624 | if (scalar @termWords > 1) { | ||
| 625 | # term is a phrase | ||
| 626 | my $phrase = Lingua::Ogmios::Annotations::Phrase->new( | ||
| 627 | { 'refid_word' => \@termWords, | ||
| 628 | 'form' => $term_content, | ||
| 629 | } | ||
| 630 | ); | ||
| 631 | |||
| 632 | $document->getAnnotations->addPhrase($phrase); | ||
| 633 | if ((!defined($termType)) || ($termType eq "term")) { | ||
| 634 | $termUnit = Lingua::Ogmios::Annotations::SemanticUnit->newTerm( | ||
| 635 | {'form' => $term_content, | ||
| 636 | 'refid_phrase' => $phrase, | ||
| 637 | }); | ||
| 638 | # warn "create Term (c)\n"; | ||
| 639 | } else { | ||
| 640 | if ($termType eq "named_entity") { | ||
| 641 | $termUnit = Lingua::Ogmios::Annotations::SemanticUnit->newNamedEntity( | ||
| 642 | {'form' => $term_content, | ||
| 643 | 'refid_phrase' => $phrase, | ||
| 644 | 'named_entity_type' => "", | ||
| 645 | }); | ||
| 646 | # warn "create Named Entity (c)\n"; | ||
| 647 | } | ||
| 648 | } | ||
| 649 | } | ||
| 650 | } else { | ||
| 651 | warn "$term_content already exists\n"; | ||
| 652 | |||
| 653 | } | ||
| 654 | return($termUnit); | ||
| 655 | } | ||
| 656 | |||
| 657 | sub HTMLoutput { | ||
| 658 | my $self = shift; | ||
| 659 | |||
| 660 | my $document; | ||
| 661 | my $doc_token_idx; | ||
| 662 | my $token; | ||
| 663 | my $word; | ||
| 664 | my $term; | ||
| 665 | my $phrase; | ||
| 666 | my $doc_str; | ||
| 667 | my $i; | ||
| 668 | my $phraseWords; | ||
| 669 | my $termToken; | ||
| 670 | my %NEtypeColor; | ||
| 671 | my $sentId; | ||
| 672 | my %colorTypes; | ||
| 673 | # my %TermtypeColor = ("test", "0000FF", "problem", "FF0000", "treatment" , "00FF00") ; | ||
| 674 | |||
| 675 | my $TermtypeColor = $self->_loadColors; # OR ("test", "0000FF", "problem", "FF0000", "treatment" , "00FF00") ; | ||
| 676 | |||
| 677 | # %NEtypeColor = %$TermtypeColor; | ||
| 678 | |||
| 679 | my $semf; | ||
| 680 | my $idColor; | ||
| 681 | |||
| 682 | my $color = "FFCBDB"; | ||
| 683 | |||
| 684 | |||
| 685 | my $headerStart = << 'EOH'; | ||
| 686 | <html> | ||
| 687 | <head> | ||
| 688 | <title>Tagged term candidates</title> | ||
| 689 | <link href="i2b2Style.css" rel="stylesheet" type="text/css"/> | ||
| 690 | <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="infobox.js"></SCRIPT> | ||
| 691 | EOH | ||
| 692 | |||
| 693 | my $headerEnd = << 'EOH'; | ||
| 694 | </head><body> | ||
| 695 | EOH | ||
| 696 | |||
| 697 | |||
| 698 | my $footer = << 'EOF'; | ||
| 699 | </body></html> | ||
| 700 | EOF | ||
| 701 | |||
| 702 | foreach $document (@{$self->_documentSet}) { | ||
| 703 | $doc_str = ""; | ||
| 704 | for($doc_token_idx = 0; $doc_token_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_token_idx++) { | ||
| 705 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_token_idx]; | ||
| 706 | |||
| 707 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_start_token", $token->getId)) { | ||
| 708 | $sentId = $document->getAnnotations->getSentenceLevel->getElementFromIndex("refid_start_token", $token->getId)->[0]->getId; | ||
| 709 | $doc_str .= "<div id=\"" . $document->getId . "-$sentId" . "\" style=\"display:block\">\n"; | ||
| 710 | } | ||
| 711 | $color = "FFCBDB"; | ||
| 712 | # warn $token->getContent . "\n"; | ||
| 713 | # if ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 714 | if ($document->getAnnotations->getSemanticUnitLevel->existsElementByToken($token)) { | ||
| 715 | $term = $self->_getLargerTerm($document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)); | ||
| 716 | # warn "TermForm: " . $term->getForm . "\n"; | ||
| 717 | if ($term->isNamedEntity) { | ||
| 718 | $idColor = $self->_getIdColor($term->NEtype, \%colorTypes); | ||
| 719 | |||
| 720 | } else { | ||
| 721 | if ($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $term->getId)) { | ||
| 722 | $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]; | ||
| 723 | if (defined $semf) { | ||
| 724 | $idColor = $self->_getIdColor($semf->first_node_first_semantic_category, \%colorTypes); | ||
| 725 | } | ||
| 726 | } | ||
| 727 | } | ||
| 728 | $doc_str .= $self->_genHTMLTagSemUnit_start($idColor); | ||
| 729 | $termToken = $term->start_token; | ||
| 730 | while(!$termToken->equals($token)) { | ||
| 731 | $termToken = $termToken->next; | ||
| 732 | } | ||
| 733 | while(!$termToken->equals($term->end_token)) { | ||
| 734 | $doc_str .= $termToken->getContent; | ||
| 735 | $termToken = $termToken->next; | ||
| 736 | } | ||
| 737 | if (defined $termToken) { | ||
| 738 | $doc_str .= $termToken->getContent; | ||
| 739 | } | ||
| 740 | $doc_str .= $self->_genHTMLTagSemUnit_end; | ||
| 741 | # warn "Size: " . $term->getReferenceTokenSize . " $doc_str\n"; | ||
| 742 | $doc_token_idx += $term->getReferenceTokenSize - 1; | ||
| 743 | # next; | ||
| 744 | # } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 745 | # $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 746 | # warn "passe" . $word->getForm . "\n"; | ||
| 747 | # if ($document->getAnnotations->getPhraseLevel->existsElementFromIndex("refid_word", $word->getId)) { | ||
| 748 | # $phrase = $document->getAnnotations->getPhraseLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 749 | # if ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("refid_phrase", $phrase->getId)) { | ||
| 750 | # $term = $document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("refid_phrase", $phrase->getId)->[0]; | ||
| 751 | # if ($term->isNamedEntity) { | ||
| 752 | # $idColor = $self->_getIdColor($term->NEtype, \%colorTypes); | ||
| 753 | # } else { | ||
| 754 | # if ($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $term->getId)) { | ||
| 755 | # $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]; | ||
| 756 | # $idColor = $self->_getIdColor($semf->first_node_first_semantic_category, \%colorTypes); | ||
| 757 | # } | ||
| 758 | # } | ||
| 759 | # $doc_str .= $self->_genHTMLTagSemUnit_start($idColor); | ||
| 760 | # $i = 0; | ||
| 761 | # $phraseWords = $phrase->reference; | ||
| 762 | # while(($i < scalar(@$phraseWords)) && (!$word->equals($phraseWords->[$i]))) { | ||
| 763 | # $i++; | ||
| 764 | # } | ||
| 765 | # while($i < scalar(@$phraseWords)) { | ||
| 766 | # $doc_str .= $phraseWords->[$i]->getForm; | ||
| 767 | # $i++; | ||
| 768 | # } | ||
| 769 | # $doc_str .= $self->_genHTMLTagSemUnit_end; | ||
| 770 | # $doc_token_idx += $phrase->getReferenceSize - 1; | ||
| 771 | # # next; | ||
| 772 | # } | ||
| 773 | # } else { | ||
| 774 | # if ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("refid_word", $word->getId)) { | ||
| 775 | # $term = $document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 776 | # if ($term->isNamedEntity) { | ||
| 777 | # $idColor = $self->_getIdColor($term->NEtype, \%colorTypes); | ||
| 778 | # } else { | ||
| 779 | # if ($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $term->getId)) { | ||
| 780 | # $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]; | ||
| 781 | |||
| 782 | # $idColor = $self->_getIdColor($semf->first_node_first_semantic_category, \%colorTypes); | ||
| 783 | |||
| 784 | # } | ||
| 785 | # } | ||
| 786 | # $doc_str .= $self->_genHTMLTagSemUnit($idColor, $term->getForm); | ||
| 787 | # $doc_token_idx += $word->getReferenceSize - 1; | ||
| 788 | # # next; | ||
| 789 | # } | ||
| 790 | # } | ||
| 791 | } else { | ||
| 792 | $doc_str .= $token->getContent; | ||
| 793 | } | ||
| 794 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 795 | $doc_str .= "<\/div>\n<p/>\n"; | ||
| 796 | } | ||
| 797 | } | ||
| 798 | $doc_str =~ s/\n/\n<P>\n/go; | ||
| 799 | |||
| 800 | print "$headerStart\n"; | ||
| 801 | |||
| 802 | print "$headerEnd\n"; | ||
| 803 | print '<DIV ID="infodiv" STYLE="position:absolute; visibility:hidden; z-index:20; top:0px; left:0px;"></DIV>' . "\n"; | ||
| 804 | print '<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">' . "\n"; | ||
| 805 | print "maketip('Terms','Semantic type','Untagged Terms');\n"; | ||
| 806 | |||
| 807 | foreach my $Termtype (sort {$colorTypes{$b} cmp $colorTypes{$a}} keys %colorTypes) { | ||
| 808 | $idColor = $colorTypes{$Termtype}; | ||
| 809 | print "maketip('" . $idColor . "','Semantic type','" . $Termtype . "');\n"; | ||
| 810 | } | ||
| 811 | |||
| 812 | print '</SCRIPT>' . "\n"; | ||
| 813 | print "<BR><B>document ID: " . $document->getId . "</B><BR>\n"; | ||
| 814 | print "<HR>\n"; | ||
| 815 | print "colors: <BR>\n<ul>\n"; | ||
| 816 | $color = "FFCBDB"; # 0000FF | ||
| 817 | print "<li><a class=\"Terms\">Terms</a></li>\n"; | ||
| 818 | foreach my $Termtype (sort {$colorTypes{$b} cmp $colorTypes{$a}} keys %colorTypes) { | ||
| 819 | $idColor = $colorTypes{$Termtype}; | ||
| 820 | print "<li><a class=\"" . $idColor . "\">$Termtype = $idColor</a></li>\n"; | ||
| 821 | } | ||
| 822 | |||
| 823 | print "</ul>\n<HR>\n"; | ||
| 824 | print $doc_str; | ||
| 825 | print "\n<HR>\n"; | ||
| 826 | } | ||
| 827 | print "\n$footer\n" | ||
| 828 | } | ||
| 829 | |||
| 830 | sub HTMLoutputOld { | ||
| 831 | my $self = shift; | ||
| 832 | |||
| 833 | my $document; | ||
| 834 | my $doc_token_idx; | ||
| 835 | my $token; | ||
| 836 | my $word; | ||
| 837 | my $term; | ||
| 838 | my $phrase; | ||
| 839 | my $doc_str; | ||
| 840 | my $i; | ||
| 841 | my $phraseWords; | ||
| 842 | my $termToken; | ||
| 843 | my %NEtypeColor; | ||
| 844 | |||
| 845 | # my %TermtypeColor = ("test", "0000FF", "problem", "FF0000", "treatment" , "00FF00") ; | ||
| 846 | |||
| 847 | my $TermtypeColor = $self->_loadColors; # OR ("test", "0000FF", "problem", "FF0000", "treatment" , "00FF00") ; | ||
| 848 | |||
| 849 | # %NEtypeColor = %$TermtypeColor; | ||
| 850 | |||
| 851 | my $semf; | ||
| 852 | my $color = "FFCBDB"; | ||
| 853 | |||
| 854 | my $header = << 'EOH'; | ||
| 855 | <html> | ||
| 856 | <head> | ||
| 857 | <title>Tagged term candidates</title> | ||
| 858 | </head><body> | ||
| 859 | EOH | ||
| 860 | |||
| 861 | my $footer = << 'EOF'; | ||
| 862 | </body></html> | ||
| 863 | EOF | ||
| 864 | |||
| 865 | print "$header\n"; | ||
| 866 | foreach $document (@{$self->_documentSet}) { | ||
| 867 | $doc_str = ""; | ||
| 868 | for($doc_token_idx = 0; $doc_token_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_token_idx++) { | ||
| 869 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_token_idx]; | ||
| 870 | $color = "FFCBDB"; | ||
| 871 | if ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 872 | $term = $document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 873 | if ($term->isNamedEntity) { | ||
| 874 | $color = $self->_determinedColor($term->NEtype, $TermtypeColor); | ||
| 875 | |||
| 876 | } else { | ||
| 877 | if ($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $term->getId)) { | ||
| 878 | $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]; | ||
| 879 | $color = $self->_determinedColor($semf->first_node_first_semantic_category, $TermtypeColor); | ||
| 880 | } | ||
| 881 | } | ||
| 882 | # $doc_str .= "<B><FONT COLOR =\"$color\">" . $term->getForm . "</FONT></B>"; | ||
| 883 | $doc_str .= "<B><FONT COLOR =\"$color\">"; | ||
| 884 | # $doc_str .= $term->getForm; | ||
| 885 | $termToken = $term->start_token; | ||
| 886 | while(!$termToken->equals($token)) { | ||
| 887 | $termToken = $termToken->next; | ||
| 888 | } | ||
| 889 | while(!$termToken->equals($term->end_token)) { | ||
| 890 | $doc_str .= $termToken->getContent; | ||
| 891 | $termToken = $termToken->next; | ||
| 892 | } | ||
| 893 | if (defined $termToken) { | ||
| 894 | $doc_str .= $termToken->getContent; | ||
| 895 | } | ||
| 896 | $doc_str .= "</FONT></B>"; | ||
| 897 | $doc_token_idx += $term->getReferenceSize - 1; | ||
| 898 | next; | ||
| 899 | } | ||
| 900 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 901 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 902 | if ($document->getAnnotations->getPhraseLevel->existsElementFromIndex("refid_word", $word->getId)) { | ||
| 903 | $phrase = $document->getAnnotations->getPhraseLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 904 | if ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("refid_phrase", $phrase->getId)) { | ||
| 905 | $term = $document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("refid_phrase", $phrase->getId)->[0]; | ||
| 906 | if ($term->isNamedEntity) { | ||
| 907 | $color = $self->_determinedColor($term->NEtype, $TermtypeColor); | ||
| 908 | } else { | ||
| 909 | if ($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $term->getId)) { | ||
| 910 | $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]; | ||
| 911 | $color = $self->_determinedColor($semf->first_node_first_semantic_category, $TermtypeColor); | ||
| 912 | } | ||
| 913 | } | ||
| 914 | # $doc_str .= "<B><FONT COLOR =\"$color\">" . $term->getForm . "</FONT></B>"; | ||
| 915 | $doc_str .= "<B><FONT COLOR =\"$color\">"; | ||
| 916 | $i = 0; | ||
| 917 | $phraseWords = $phrase->reference; | ||
| 918 | while(($i < scalar(@$phraseWords)) && (!$word->equals($phraseWords->[$i]))) { | ||
| 919 | $i++; | ||
| 920 | } | ||
| 921 | while($i < scalar(@$phraseWords)) { | ||
| 922 | $doc_str .= $phraseWords->[$i]->getForm; | ||
| 923 | $i++; | ||
| 924 | } | ||
| 925 | $doc_str .= "</FONT></B>"; | ||
| 926 | $doc_token_idx += $phrase->getReferenceSize - 1; | ||
| 927 | next; | ||
| 928 | } | ||
| 929 | } else { | ||
| 930 | if ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("refid_word", $word->getId)) { | ||
| 931 | $term = $document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 932 | if ($term->isNamedEntity) { | ||
| 933 | $color = $self->_determinedColor($term->NEtype, $TermtypeColor); | ||
| 934 | } else { | ||
| 935 | if ($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $term->getId)) { | ||
| 936 | $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]; | ||
| 937 | |||
| 938 | # $color = $self->_determinedColor("medication", $TermtypeColor); | ||
| 939 | $color = $self->_determinedColor($semf->first_node_first_semantic_category, $TermtypeColor); | ||
| 940 | |||
| 941 | } | ||
| 942 | } | ||
| 943 | $doc_str .= "<B><FONT COLOR =\"$color\">" . $term->getForm . "</FONT></B>"; | ||
| 944 | $doc_token_idx += $word->getReferenceSize - 1; | ||
| 945 | next; | ||
| 946 | } | ||
| 947 | } | ||
| 948 | } | ||
| 949 | $doc_str .= $token->getContent; | ||
| 950 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 951 | $doc_str .= "\n<p/>\n"; | ||
| 952 | } | ||
| 953 | } | ||
| 954 | $doc_str =~ s/\n/\n<P>\n/go; | ||
| 955 | |||
| 956 | # warn $document->getId . "\n"; | ||
| 957 | print "<BR><B>document ID: " . $document->getId . "</B><BR>\n"; | ||
| 958 | print "<HR>\n"; | ||
| 959 | print "colors: <BR>\n<ul>\n"; | ||
| 960 | # foreach my $NEtype (keys %NEtypeColor) { | ||
| 961 | # $color = $NEtypeColor{$NEtype}; | ||
| 962 | # print "<LI><b><FONT COLOR=\"" . $color . "\">$NEtype = $color</FONT></b></LI>\n"; | ||
| 963 | # } | ||
| 964 | $color = "FFCBDB"; # 0000FF | ||
| 965 | print "<LI><b><FONT COLOR=\"" . $color . "\">Terms = $color</FONT></b></LI>\n"; | ||
| 966 | foreach my $Termtype (sort {$TermtypeColor->{$b} cmp $TermtypeColor->{$a}} keys %$TermtypeColor) { | ||
| 967 | $color = $TermtypeColor->{$Termtype}; | ||
| 968 | print "<LI><b><FONT COLOR=\"" . $color . "\">$Termtype = $color</FONT></b></LI>\n"; | ||
| 969 | } | ||
| 970 | |||
| 971 | print "</ul>\n<HR>\n"; | ||
| 972 | print $doc_str; | ||
| 973 | print "\n<HR>\n"; | ||
| 974 | } | ||
| 975 | print "\n$footer\n" | ||
| 976 | } | ||
| 977 | |||
| 978 | sub _loadColors { | ||
| 979 | my ($self) = @_; | ||
| 980 | |||
| 981 | my $cg = new Config::General('-ConfigFile' => $self->_config->configuration->{'RESOURCE'}->{"language=EN"}->{'COLORFILE'}, | ||
| 982 | '-InterPolateVars' => 1, | ||
| 983 | '-InterPolateEnv' => 1 | ||
| 984 | ); | ||
| 985 | |||
| 986 | my %colors = $cg->getall; | ||
| 987 | |||
| 988 | return(\%colors); | ||
| 989 | } | ||
| 990 | |||
| 991 | |||
| 992 | sub _genHTMLTagSemUnit_start { | ||
| 993 | my ($self, $idColor) = @_; | ||
| 994 | |||
| 995 | # onMouseOver="tip('alpha')" onMouseOut="untip()" | ||
| 996 | # <a href="#"style="text-decoration:none" onMouseOver="tip('alpha')" onMouseOut="untip()"> | ||
| 997 | |||
| 998 | |||
| 999 | my $str = "<a class=\"$idColor\" href='#' onMouseOver=\"tip('$idColor')\" onMouseOut=\"untip()\">"; | ||
| 1000 | return($str); | ||
| 1001 | } | ||
| 1002 | |||
| 1003 | sub _genHTMLTagSemUnit_end { | ||
| 1004 | my ($self) = @_; | ||
| 1005 | |||
| 1006 | my $str = "</a>"; | ||
| 1007 | return($str); | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | sub _genHTMLTagSemUnit { | ||
| 1011 | my ($self, $idColor, $term_str) = @_; | ||
| 1012 | |||
| 1013 | my $str; | ||
| 1014 | |||
| 1015 | $str = $self->_genHTMLTagSemUnit_start($idColor); | ||
| 1016 | $str .= $term_str; | ||
| 1017 | $str .= $self->_genHTMLTagSemUnit_end; | ||
| 1018 | |||
| 1019 | return($str); | ||
| 1020 | } | ||
| 1021 | |||
| 1022 | |||
| 1023 | sub _getIdColor { | ||
| 1024 | my ($self, $type, $typeColor) = @_ ; | ||
| 1025 | |||
| 1026 | my $color; | ||
| 1027 | |||
| 1028 | # warn "$type\n"; | ||
| 1029 | if (!exists $typeColor->{$type}) { | ||
| 1030 | $typeColor->{$type} = lc($type); | ||
| 1031 | # $typeColor->{$type} =~ s/\-/_/go; | ||
| 1032 | } else { | ||
| 1033 | # $color = $typeColor->{$type} | ||
| 1034 | } | ||
| 1035 | return($typeColor->{$type}); | ||
| 1036 | |||
| 1037 | } | ||
| 1038 | |||
| 1039 | sub _determinedColor { | ||
| 1040 | my ($self, $type, $typeColor) = @_ ; | ||
| 1041 | |||
| 1042 | my $color; | ||
| 1043 | |||
| 1044 | if (!exists $typeColor->{$type}) { | ||
| 1045 | # $color = "FF" . sprintf("%X", (ord(substr($type, 0, 1)) % 16)) . "0" . sprintf("%X", (ord(substr($type, 1, 1)) % 16)) . "0"; | ||
| 1046 | $color = "FFCBDB"; | ||
| 1047 | $typeColor->{$type} = $color; | ||
| 1048 | } else { | ||
| 1049 | $color = $typeColor->{$type}; | ||
| 1050 | } | ||
| 1051 | return($color); | ||
| 1052 | # return("FFCBDB"); | ||
| 1053 | } | ||
| 1054 | |||
| 1055 | |||
| 1056 | sub _getEndForm { | ||
| 1057 | |||
| 1058 | my ($self, $nextTerm, $document, $end_form, $endTokens) = @_; | ||
| 1059 | |||
| 1060 | my $token; | ||
| 1061 | |||
| 1062 | $token = $nextTerm->end_token; | ||
| 1063 | while ((defined $token->next) && | ||
| 1064 | (($token->next->isSep) || ($token->next->isSymb)) && | ||
| 1065 | # (index($token->getContent, ")") == -1) && | ||
| 1066 | (!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) | ||
| 1067 | ) { | ||
| 1068 | $token = $token->next; | ||
| 1069 | push @$endTokens, $token; | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | $token = $$endTokens[$#$endTokens]; | ||
| 1073 | while((scalar(@$endTokens) > 0) && ($token->isSep)) { | ||
| 1074 | pop @$endTokens; | ||
| 1075 | $token = $$endTokens[$#$endTokens]; | ||
| 1076 | } | ||
| 1077 | foreach $token (@$endTokens) { | ||
| 1078 | $$end_form .= $token->getContent; | ||
| 1079 | } | ||
| 1080 | return($$endTokens[$#$endTokens]); | ||
| 1081 | } | ||
| 1082 | |||
| 1083 | |||
| 1084 | # Take into account the sem tag | ||
| 1085 | sub _semanticUnitMerging { | ||
| 1086 | my ($self, $document, $firstTerm, $secondTerm, $sepForm, $sepTokens, $endForm, $endTokens) = @_; | ||
| 1087 | |||
| 1088 | my @Term2tokens; | ||
| 1089 | my $token; | ||
| 1090 | my $term_form; | ||
| 1091 | my $id; | ||
| 1092 | my $newTerm; | ||
| 1093 | |||
| 1094 | # warn "Merging " . $firstTerm->getForm . " : " . $firstTerm->type . "\n"; | ||
| 1095 | |||
| 1096 | # if (defined $secondTerm) { | ||
| 1097 | # warn " with " . $secondTerm->getForm . " : " . $secondTerm->type. "\n"; | ||
| 1098 | # } | ||
| 1099 | # if (defined $endForm) { | ||
| 1100 | # warn " with $endForm\n"; | ||
| 1101 | # } | ||
| 1102 | # warn "Merging " . $firstTerm->getForm . " and " . $secondTerm->getForm . "\n"; | ||
| 1103 | |||
| 1104 | # Merge Term if same type | ||
| 1105 | # warn "in _semanticUnitMerging\n"; | ||
| 1106 | |||
| 1107 | # warn $firstTerm->reference_name . "\n"; | ||
| 1108 | # warn $firstTerm->reference . "\n"; | ||
| 1109 | |||
| 1110 | $token = $firstTerm->start_token; | ||
| 1111 | push @Term2tokens, $token; | ||
| 1112 | while(!$token->equals($firstTerm->end_token)) { | ||
| 1113 | $token = $token->next; | ||
| 1114 | push @Term2tokens, $token; | ||
| 1115 | }; | ||
| 1116 | $term_form = $firstTerm->getForm; | ||
| 1117 | |||
| 1118 | if (defined $sepForm) { | ||
| 1119 | push @Term2tokens, @$sepTokens; | ||
| 1120 | $term_form .= $sepForm; | ||
| 1121 | $token = $sepTokens->[$#$sepTokens]; | ||
| 1122 | } else { | ||
| 1123 | $token = $firstTerm->end_token; | ||
| 1124 | } | ||
| 1125 | if (defined $secondTerm) { | ||
| 1126 | while(!($token->equals($secondTerm->end_token))) { | ||
| 1127 | $token = $token->next; | ||
| 1128 | push @Term2tokens, $token; | ||
| 1129 | $term_form .= $token->getContent; | ||
| 1130 | } | ||
| 1131 | } | ||
| 1132 | |||
| 1133 | if (defined $endForm) { | ||
| 1134 | push @Term2tokens, @$endTokens; | ||
| 1135 | $term_form .= $endForm; | ||
| 1136 | } | ||
| 1137 | |||
| 1138 | $newTerm = $self->_createTerm($document, \@Term2tokens, $term_form, $firstTerm->type); | ||
| 1139 | if ($newTerm->getId == -1) { | ||
| 1140 | # Add semantic Modification | ||
| 1141 | $id = $document->getAnnotations->addSemanticUnit($newTerm); | ||
| 1142 | # warn "\tnewTerm: " . $newTerm->getForm . " ($id)\n"; | ||
| 1143 | my $semf; | ||
| 1144 | if ($firstTerm->isTerm) { | ||
| 1145 | if (defined $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $firstTerm->getId)->[0]) { | ||
| 1146 | my $semFeatures = Lingua::Ogmios::Annotations::SemanticFeatures->new( | ||
| 1147 | { 'semantic_category' => [@{$document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $firstTerm->getId)->[0]->semantic_category}], | ||
| 1148 | 'refid_semantic_unit' => $newTerm->getId, | ||
| 1149 | }); | ||
| 1150 | $document->getAnnotations->addSemanticFeatures($semFeatures); | ||
| 1151 | } | ||
| 1152 | # if (!$firstTerm->equals($newTerm)) { | ||
| 1153 | # warn "del1a\n"; | ||
| 1154 | # $document->getAnnotations->delSemanticUnit($firstTerm); | ||
| 1155 | # } | ||
| 1156 | } elsif ($firstTerm->isNamedEntity) { | ||
| 1157 | # warn $firstTerm->NEtype . "\n"; | ||
| 1158 | $newTerm->NEtype($firstTerm->NEtype); | ||
| 1159 | # if (!$firstTerm->equals($newTerm)) { | ||
| 1160 | # $document->getAnnotations->delSemanticUnit($firstTerm); | ||
| 1161 | # } | ||
| 1162 | } | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | if (!$firstTerm->equals($newTerm)) { | ||
| 1166 | # warn "del1b\n"; | ||
| 1167 | $document->getAnnotations->delSemanticUnit($firstTerm); | ||
| 1168 | } | ||
| 1169 | if ((defined $secondTerm) && ($secondTerm->isTerm)) { | ||
| 1170 | if (!$secondTerm->equals($newTerm)) { | ||
| 1171 | # warn "del2\n"; | ||
| 1172 | $document->getAnnotations->delSemanticUnit($secondTerm); | ||
| 1173 | } | ||
| 1174 | } | ||
| 1175 | return($newTerm); | ||
| 1176 | |||
| 1177 | } | ||
| 1178 | |||
| 1179 | |||
| 1180 | sub _loadResource { | ||
| 1181 | my ($self, $resourceRC, $name) = @_; | ||
| 1182 | |||
| 1183 | # warn "$resourceRC : $name : " . $resourceRC->{$name} . "\n"; | ||
| 1184 | |||
| 1185 | my $cg = new Config::General('-ConfigFile' => $resourceRC->{$name}, | ||
| 1186 | '-InterPolateVars' => 1, | ||
| 1187 | '-InterPolateEnv' => 1 | ||
| 1188 | ); | ||
| 1189 | |||
| 1190 | my %resource = $cg->getall; | ||
| 1191 | |||
| 1192 | |||
| 1193 | |||
| 1194 | # foreach my $type (keys %resource) { | ||
| 1195 | # warn "$type : " . $resource{$type} . "\n**\n"; | ||
| 1196 | # foreach my $elt (keys %{$resource{$type}}) { | ||
| 1197 | # warn "\t" . $elt . " : " . $resource{$type}->{$elt} . "\n"; | ||
| 1198 | # } | ||
| 1199 | # } | ||
| 1200 | $self->{$name} = \%resource; | ||
| 1201 | } | ||
| 1202 | |||
| 1203 | sub _loadRCFile { | ||
| 1204 | my ($self, $file, $fields) = @_; | ||
| 1205 | |||
| 1206 | my $field; | ||
| 1207 | my $cg = new Config::General('-ConfigFile' => $file, | ||
| 1208 | '-InterPolateVars' => 1, | ||
| 1209 | '-InterPolateEnv' => 1 | ||
| 1210 | ); | ||
| 1211 | |||
| 1212 | my %resource = $cg->getall; | ||
| 1213 | |||
| 1214 | foreach $field (@$fields) { | ||
| 1215 | |||
| 1216 | if (exists $resource{$field}) { | ||
| 1217 | $self->{$field} = $resource{$field}; | ||
| 1218 | $self->{$field} = $resource{$field}; | ||
| 1219 | } else { | ||
| 1220 | warn "*** $field not found ***\n"; | ||
| 1221 | } | ||
| 1222 | } | ||
| 1223 | } | ||
| 1224 | |||
| 1225 | |||
| 1226 | sub LoadMorphoSemanticMarkResource { | ||
| 1227 | my ($self, $fieldGroup) = @_; | ||
| 1228 | |||
| 1229 | if (defined $self->_config->configuration->{'RESOURCE'}->{"language=EN"}->{"AFFIXLIST"}) { | ||
| 1230 | my $filename = $self->_config->configuration->{'RESOURCE'}->{"language=EN"}->{"AFFIXLIST"}; | ||
| 1231 | |||
| 1232 | warn "Reading file containing marks $filename\n"; | ||
| 1233 | |||
| 1234 | my $affix; | ||
| 1235 | my $type; | ||
| 1236 | |||
| 1237 | my %problems_lexical; | ||
| 1238 | my %tests_lexical; | ||
| 1239 | my %treatments_lexical; | ||
| 1240 | |||
| 1241 | my %problems_morphologic; | ||
| 1242 | my %tests_morphologic; | ||
| 1243 | my %treatments_morphologic; | ||
| 1244 | |||
| 1245 | my $line; | ||
| 1246 | my $field; | ||
| 1247 | |||
| 1248 | foreach $field (keys %{$self->{$fieldGroup}}) { | ||
| 1249 | my %tmp; | ||
| 1250 | $self->{"RESOURCES"}->{$field . "_lexical"} = \%tmp; | ||
| 1251 | my %tmp2; | ||
| 1252 | $self->{"RESOURCES"}->{$field . "_adjectival"} = \%tmp; | ||
| 1253 | my %tmp3; | ||
| 1254 | $self->{"RESOURCES"}->{$field . "_morphological"} = \%tmp2; | ||
| 1255 | } | ||
| 1256 | |||
| 1257 | open (AFFIXFILE, $filename) || die "file not found ($filename)\n"; | ||
| 1258 | |||
| 1259 | while ($line = <AFFIXFILE>) { | ||
| 1260 | chomp $line; | ||
| 1261 | if (($line !~ /\s*#/o) && ($line !~ /^\s*$/o)) { | ||
| 1262 | ($affix, $type) = split / : *: /, $line; # / | ||
| 1263 | # warn "affix: $affix ; type: $type\n"; | ||
| 1264 | if (exists $self->{$fieldGroup}->{$type}) { | ||
| 1265 | $self->{"RESOURCES"}->{$type . "_lexical"}->{$affix} = "l"; | ||
| 1266 | } elsif (($type =~ s/\-a$//) &&(exists $self->{$fieldGroup}->{$type})) { | ||
| 1267 | $self->{"RESOURCES"}->{$type . "_adjectival"}->{$affix} = "a"; | ||
| 1268 | } elsif ($type =~ /\-(m(-[ps])?)$/) { | ||
| 1269 | $self->{"RESOURCES"}->{$` . "_morphological"}->{$affix} = "$1"; | ||
| 1270 | } else { | ||
| 1271 | warn "type not found\n"; | ||
| 1272 | } | ||
| 1273 | } | ||
| 1274 | } | ||
| 1275 | } | ||
| 1276 | } | ||
| 1277 | |||
| 1278 | |||
| 1279 | sub _addSemanticFeature { | ||
| 1280 | my ($self, $termUnit, $semtag, $document) = @_; | ||
| 1281 | |||
| 1282 | my $semFeatures; | ||
| 1283 | |||
| 1284 | if (defined $semtag) { | ||
| 1285 | my @semtags; | ||
| 1286 | if (ref($semtag) ne "ARRAY") { | ||
| 1287 | @semtags = split /\//, $semtag; | ||
| 1288 | } else { | ||
| 1289 | @semtags = @$semtag; | ||
| 1290 | } | ||
| 1291 | my @list_semtags; | ||
| 1292 | push @list_semtags, \@semtags; | ||
| 1293 | # TODO Check if the semtag already exists in order to avoid to create one another | ||
| 1294 | $semFeatures = Lingua::Ogmios::Annotations::SemanticFeatures->new( | ||
| 1295 | { 'semantic_category' => \@list_semtags, | ||
| 1296 | 'refid_semantic_unit' => $termUnit->getId, | ||
| 1297 | }); | ||
| 1298 | $document->getAnnotations->addSemanticFeatures($semFeatures); | ||
| 1299 | } else { | ||
| 1300 | warn "Semtag not defined\n"; | ||
| 1301 | } | ||
| 1302 | |||
| 1303 | } | ||
| 1304 | |||
| 1305 | sub _split_RE { | ||
| 1306 | my ($self, $RE) = @_; | ||
| 1307 | |||
| 1308 | my $pos_start = 0; | ||
| 1309 | my $pos = 0; | ||
| 1310 | my $str; | ||
| 1311 | my $i; | ||
| 1312 | my @pattern_tab; | ||
| 1313 | |||
| 1314 | my ($pattern, $rel_type) = split /\|\|/, $RE; | ||
| 1315 | |||
| 1316 | push @pattern_tab, $rel_type; | ||
| 1317 | do { | ||
| 1318 | $pos = index($pattern, '@', $pos_start); | ||
| 1319 | if ($pos > -1) { | ||
| 1320 | if ($pos != 0) { | ||
| 1321 | $str = substr($pattern, $pos_start, $pos - $pos_start); | ||
| 1322 | my %tmp = ("mark" => $str); | ||
| 1323 | push @pattern_tab, \%tmp; | ||
| 1324 | } | ||
| 1325 | $str = substr($pattern, $pos+1, 2); | ||
| 1326 | |||
| 1327 | my %tmp; | ||
| 1328 | if (exists $self->{"SEMANTICTYPES"}->{$str}) { | ||
| 1329 | %tmp = ("semtype" => $str); | ||
| 1330 | } | ||
| 1331 | if (!exists $self->{"SEMANTICTYPES"}->{$str}) { | ||
| 1332 | %tmp = ("postag" => $str); | ||
| 1333 | } | ||
| 1334 | push @pattern_tab, \%tmp ; | ||
| 1335 | $pos_start = $pos + 3; | ||
| 1336 | } | ||
| 1337 | } while(($pos > -1) && ($pos < length($pattern))); | ||
| 1338 | $str = substr($pattern, $pos_start); | ||
| 1339 | |||
| 1340 | if ($str ne "") { | ||
| 1341 | my %tmp = ("mark" => $str); | ||
| 1342 | push @pattern_tab, \%tmp; | ||
| 1343 | } | ||
| 1344 | |||
| 1345 | for($i = 1; $i < scalar(@pattern_tab);$i++) { | ||
| 1346 | $pattern = $pattern_tab[$i]; | ||
| 1347 | } | ||
| 1348 | return(\@pattern_tab); | ||
| 1349 | } | ||
| 1350 | |||
| 1351 | |||
| 1352 | |||
| 1353 | sub patternSearch { | ||
| 1354 | my ($self, $function_pointer, $args) = @_; | ||
| 1355 | |||
| 1356 | my $document; | ||
| 1357 | my $sentence; | ||
| 1358 | my $sentenceLemma; | ||
| 1359 | my $sentenceLemma2; | ||
| 1360 | # my $sentenceForm; | ||
| 1361 | my $REtype; | ||
| 1362 | my $RELtype; | ||
| 1363 | my $pattern; | ||
| 1364 | |||
| 1365 | my $sentence_start_token; | ||
| 1366 | my $sentence_end_token; | ||
| 1367 | |||
| 1368 | my $i; | ||
| 1369 | my $j; | ||
| 1370 | my $regexp; | ||
| 1371 | |||
| 1372 | my $offset_refs; | ||
| 1373 | |||
| 1374 | my $wordLimit = 10; | ||
| 1375 | my $POSTAGwordLimit = 10; | ||
| 1376 | my $tokenLimit; | ||
| 1377 | |||
| 1378 | my $semfGuessing = 0; | ||
| 1379 | |||
| 1380 | my $word; | ||
| 1381 | my $domainSpecificRelation; | ||
| 1382 | |||
| 1383 | my $start_mark_token; | ||
| 1384 | my $end_mark_token; | ||
| 1385 | |||
| 1386 | my $found; | ||
| 1387 | my @terms; | ||
| 1388 | my $term; | ||
| 1389 | |||
| 1390 | # warn "pattternSearch\n"; | ||
| 1391 | |||
| 1392 | foreach $document (@{$self->_documentSet}) { | ||
| 1393 | foreach $sentence (@{$document->getAnnotations->getSentenceLevel->getElements}) { | ||
| 1394 | $sentence_start_token = $sentence->refid_start_token; | ||
| 1395 | $sentence_end_token = $sentence->refid_end_token; | ||
| 1396 | |||
| 1397 | # warn "==> " . $sentence->getForm . "\n"; | ||
| 1398 | ($sentenceLemma, $offset_refs) = $self->_getLemmatisedSentence($document, $sentence_start_token, $sentence_end_token); | ||
| 1399 | foreach $REtype (keys %{$self->{'Resources'}}) { | ||
| 1400 | # warn "REType: $REtype\n"; | ||
| 1401 | $RELtype = $self->{'Resources'}->{$REtype}->[2]; | ||
| 1402 | for($i=0; $i < scalar(@{$self->{'Resources'}->{$REtype}->[4]});$i++) { | ||
| 1403 | # warn "==>?PATTERN: " . $self->{'Resources'}->{$REtype}->[1]->[$i] . "\n"; | ||
| 1404 | $pattern = $self->{'Resources'}->{$REtype}->[4]->[$i]; | ||
| 1405 | # foreach $pattern (@{$self->{'Resources'}->{$REtype}->[4]}) { | ||
| 1406 | # warn "$pattern\n"; | ||
| 1407 | # warn "relation type: " . $pattern->[0] . "\n"; | ||
| 1408 | $tokenLimit = $sentence_start_token; | ||
| 1409 | $start_mark_token = $sentence_start_token; | ||
| 1410 | $end_mark_token = $sentence_end_token; | ||
| 1411 | # warn "+++>" . scalar(@{$pattern}) . "\n"; | ||
| 1412 | @terms = (); | ||
| 1413 | # $sentenceLemma2 = $sentenceLemma; | ||
| 1414 | # $sentenceLemma =~ /^/go; | ||
| 1415 | |||
| 1416 | $sentenceLemma =~ /^/go; | ||
| 1417 | $found = 0; | ||
| 1418 | for($j = 1; $j < scalar(@{$pattern}); $j++) { | ||
| 1419 | if (exists $pattern->[$j]->{'mark'}) { | ||
| 1420 | # warn "mark ($j): " . $pattern->[$j]->{'mark'} . "\n"; | ||
| 1421 | $regexp = $pattern->[$j]->{'mark'}; | ||
| 1422 | |||
| 1423 | # warn "SENTENCE => $sentenceLemma\n"; | ||
| 1424 | if ($sentenceLemma =~ /$regexp/gcsi) { | ||
| 1425 | # warn "=============\n"; | ||
| 1426 | # warn "SENTENCE => $sentenceForm\n"; | ||
| 1427 | # warn "SENTENCE => $sentenceLemma\n"; | ||
| 1428 | # warn "PATTERN: " . $self->{'Resources'}->{$REtype}->[1]->[$i] . "\n"; | ||
| 1429 | # warn "\tMATCH $& " . (length($`)) . "\n"; | ||
| 1430 | $start_mark_token = undef; | ||
| 1431 | if (exists $offset_refs->{length($`)}) { | ||
| 1432 | # warn "$` " . length($`) . " / " . $offset_refs->{length($`)}->getContent . "\n"; | ||
| 1433 | $start_mark_token = $offset_refs->{length($`)}; | ||
| 1434 | } | ||
| 1435 | # warn "$' " . (length($` . $&)) . "\n"; # ' # | ||
| 1436 | $end_mark_token = undef; | ||
| 1437 | if (exists $offset_refs->{length($` . $&)}) { | ||
| 1438 | # warn "$' " . (length($` . $&)) . " / " . $offset_refs->{length($` . $&)}->getContent . "\n"; # ' # | ||
| 1439 | $end_mark_token = $offset_refs->{length($` . $&)}; | ||
| 1440 | $end_mark_token = $end_mark_token->previous; | ||
| 1441 | } elsif (exists $offset_refs->{length($` . $&) - 1}) { | ||
| 1442 | # warn ">>>$' " . (length($` . $&) - 1) . " / " . $offset_refs->{length($` . $&) - 1}->getContent . "\n"; # ' # | ||
| 1443 | $end_mark_token = $offset_refs->{length($` . $&) - 1}; | ||
| 1444 | $end_mark_token = $end_mark_token->previous; | ||
| 1445 | } | ||
| 1446 | |||
| 1447 | # if ((defined $start_mark_token) && ()) { | ||
| 1448 | |||
| 1449 | if ((defined $end_mark_token) && ($j > 1)) { | ||
| 1450 | # if ((defined $start_mark_token) && (defined $end_mark_token) && ($j > 1)) { | ||
| 1451 | if (exists $pattern->[$j - 1]->{'semtype'}) { | ||
| 1452 | if ($term = $self->_searchConceptBefore($document, $tokenLimit, $sentence_end_token, $pattern->[$j - 1]->{"semtype"}, $end_mark_token, $wordLimit, $semfGuessing)) { | ||
| 1453 | # warn "Found term " . $term->getId . " / " . $term->getForm . " and store it\n"; | ||
| 1454 | push @terms, $term; | ||
| 1455 | $tokenLimit = $end_mark_token; | ||
| 1456 | # $tokenLimit = $start_mark_token; | ||
| 1457 | } else { | ||
| 1458 | # warn "concept not found\n"; | ||
| 1459 | # warn "pattern not complete\n"; | ||
| 1460 | #$j = scalar(@{$pattern}) + 1 ; | ||
| 1461 | last; | ||
| 1462 | } | ||
| 1463 | } else { | ||
| 1464 | # warn "(3) pattern not complete\n"; | ||
| 1465 | #$j = scalar(@{$pattern}) + 1 ; | ||
| 1466 | last; | ||
| 1467 | } | ||
| 1468 | } else { | ||
| 1469 | # warn "(2) pattern not complete\n"; | ||
| 1470 | #$j = scalar(@{$pattern}) + 1 ; | ||
| 1471 | # if ($j > 1) { | ||
| 1472 | last; | ||
| 1473 | # } | ||
| 1474 | } | ||
| 1475 | } else { | ||
| 1476 | # warn "(1) pattern not complete\n"; | ||
| 1477 | #$j = scalar(@{$pattern}) + 1 ; | ||
| 1478 | last; | ||
| 1479 | } | ||
| 1480 | } elsif (exists $pattern->[$j]->{'semtype'}) { | ||
| 1481 | # warn "semtype ($j)\n"; | ||
| 1482 | } elsif (exists $pattern->[$j]->{'postag'}) { | ||
| 1483 | # POSTAG | ||
| 1484 | # warn "postag ($j)\n"; | ||
| 1485 | if ($word = $self->_searchPOSTagBefore($document, $tokenLimit, $sentence_end_token, $pattern->[$j]->{"postag"}, $end_mark_token, $POSTAGwordLimit)) { | ||
| 1486 | # warn "found postag ($word)\n"; | ||
| 1487 | } else { | ||
| 1488 | # warn "postag not found\n"; | ||
| 1489 | last; | ||
| 1490 | } | ||
| 1491 | } | ||
| 1492 | } | ||
| 1493 | # warn "j = $j\n"; | ||
| 1494 | # warn "\t" . scalar(@{$pattern}) . "\n"; | ||
| 1495 | if ($j == scalar(@{$pattern})) { | ||
| 1496 | if ((defined $end_mark_token) && (exists $pattern->[$j - 1]->{'semtype'})) { | ||
| 1497 | $found = 0; | ||
| 1498 | if ($term = $self->_searchConceptAfter($document, $tokenLimit, $sentence_end_token, $pattern->[$j - 1]->{"semtype"}, $end_mark_token, $wordLimit, $semfGuessing)) { | ||
| 1499 | # warn "Found term " . $term->getId . " / " . $term->getForm . " and store it\n"; | ||
| 1500 | push @terms, $term; | ||
| 1501 | $found = 1; | ||
| 1502 | } | ||
| 1503 | } elsif (exists $pattern->[$j - 1]->{'postag'}) { | ||
| 1504 | # POSTAG | ||
| 1505 | # warn "postag ($j)\n"; | ||
| 1506 | if ($word = $self->_searchPOSTagAfter($document, $tokenLimit, $sentence_end_token, $pattern->[$j - 1]->{"postag"}, $end_mark_token, $POSTAGwordLimit)) { | ||
| 1507 | # warn "found postag ($word)\n"; | ||
| 1508 | } else { | ||
| 1509 | # warn "postag not found\n"; | ||
| 1510 | last; | ||
| 1511 | } | ||
| 1512 | |||
| 1513 | } | ||
| 1514 | if ($found == 1) { | ||
| 1515 | # $tokenLimit = $start_mark_token; | ||
| 1516 | # warn "\tFind a relation (" . (scalar(@terms)) . ")\n"; | ||
| 1517 | # warn "\tCreation of the relation " . $pattern->[0] . "\n"; | ||
| 1518 | |||
| 1519 | # warn "$self\n"; | ||
| 1520 | &$function_pointer($document, $pattern->[0], \@terms, $args); | ||
| 1521 | # $self->_addRelation($document, $pattern->[0], \@terms); | ||
| 1522 | } | ||
| 1523 | } | ||
| 1524 | } | ||
| 1525 | } | ||
| 1526 | } | ||
| 1527 | } | ||
| 1528 | # warn "Done\n"; | ||
| 1529 | } | ||
| 1530 | |||
| 1531 | sub _searchConceptBefore { | ||
| 1532 | my ($self, $document, $sentence_start_token, $sentence_end_token, $semtypeAcr, $start_mark_token, $tokenLimit, $semfGuessing) = @_; | ||
| 1533 | |||
| 1534 | |||
| 1535 | my $token = $start_mark_token; | ||
| 1536 | my $term; | ||
| 1537 | |||
| 1538 | my $windowSize = 0; | ||
| 1539 | |||
| 1540 | my %words; | ||
| 1541 | |||
| 1542 | my $semtype = $self->{"SEMANTICTYPES"}->{$semtypeAcr}; | ||
| 1543 | |||
| 1544 | # warn "in _searchConceptBefore (" . $start_mark_token->getFrom . ") " . $sentence_start_token->getId . " $semtype\n"; | ||
| 1545 | |||
| 1546 | do { | ||
| 1547 | my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 1548 | if (scalar(@words) > 0) { | ||
| 1549 | $words{$words[0]->getId}++; | ||
| 1550 | $windowSize = scalar(keys %words); | ||
| 1551 | } | ||
| 1552 | # warn "(0) Id: " . $token->getId . " token: " . $token->getContent . " ( $windowSize / $tokenLimit)\n"; | ||
| 1553 | # warn "\t" . scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) . "\n"; | ||
| 1554 | # foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 1555 | # while((defined $token) && (($token->isSep) && (!$token->equals($sentence_start_token))) ) { | ||
| 1556 | # $token = $token->previous; | ||
| 1557 | # } ; | ||
| 1558 | |||
| 1559 | # if (defined($token)) { | ||
| 1560 | # warn "(a) Id: " . $token->getId . "token: " . $token->getContent . " ( $tokenLimit / $windowSize)\n"; | ||
| 1561 | # warn "\t" . scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) . "\n"; | ||
| 1562 | # $windowSize++; | ||
| 1563 | |||
| 1564 | if (scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) > 0) { | ||
| 1565 | # warn "---\n"; | ||
| 1566 | if ($semfGuessing) { | ||
| 1567 | # # $term = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)->[0]; | ||
| 1568 | |||
| 1569 | if (($term->isTerm) && (!defined($term->getSemanticFeatureFC($document)))) { | ||
| 1570 | # warn "Guessing $semtype\n"; | ||
| 1571 | $self->_addSemanticFeature($term, $semtype, $document); | ||
| 1572 | } | ||
| 1573 | } else { | ||
| 1574 | $term = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)->[0]; | ||
| 1575 | if ($term->isTerm) { | ||
| 1576 | # warn 'Term: ' . $term->getForm . "\n"; | ||
| 1577 | # warn "Semf: " . $term->getSemanticFeatureFC($document) . "\n"; | ||
| 1578 | if ($term->getSemanticFeatureFC($document) eq $semtype) { | ||
| 1579 | # warn "found concept with the right semtype ($semtype)\n"; | ||
| 1580 | return($term); | ||
| 1581 | } | ||
| 1582 | } | ||
| 1583 | } | ||
| 1584 | } | ||
| 1585 | # if (!$token->equals($sentence_start_token)) { | ||
| 1586 | $token = $token->previous; | ||
| 1587 | # } | ||
| 1588 | # warn "(b) Id: " . $token->getId . "token: " . $token->getContent . " ( $tokenLimit / $windowSize)\n"; | ||
| 1589 | } while(($windowSize < $tokenLimit) && (defined $token) && ($token->getTo >= $sentence_start_token->getFrom)); | ||
| 1590 | # warn "+++\n"; | ||
| 1591 | return(0); | ||
| 1592 | } | ||
| 1593 | |||
| 1594 | |||
| 1595 | sub _searchConceptAfter { | ||
| 1596 | my ($self, $document, $sentence_start_token, $sentence_end_token, $semtypeAcr, $start_mark_token, $tokenLimit, $semfGuessing) = @_; | ||
| 1597 | |||
| 1598 | |||
| 1599 | my $token = $start_mark_token; | ||
| 1600 | my $term; | ||
| 1601 | |||
| 1602 | my $word; | ||
| 1603 | |||
| 1604 | my $windowSize = 0; | ||
| 1605 | my $semtype = $self->{"SEMANTICTYPES"}->{$semtypeAcr}; | ||
| 1606 | my %words; | ||
| 1607 | |||
| 1608 | # warn "in _searchConceptAfter (" . $start_mark_token->getFrom . ") " . $sentence_start_token->getId . " $semtype\n"; | ||
| 1609 | # warn "in _searchConceptAfter (" . $start_mark_token->getFrom . ")\n"; | ||
| 1610 | |||
| 1611 | do { | ||
| 1612 | |||
| 1613 | my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 1614 | if (scalar(@words) > 0) { | ||
| 1615 | $words{$words[0]->getId}++; | ||
| 1616 | $windowSize = scalar(keys %words); | ||
| 1617 | } | ||
| 1618 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 1619 | |||
| 1620 | |||
| 1621 | # # while((defined $token) && (($token->isSep) && (!$token->equals($sentence_end_token))) ) { | ||
| 1622 | # # $token = $token->next; | ||
| 1623 | # # } ; | ||
| 1624 | # # if ((defined($token)) && (!$token->equals($sentence_end_token))) { | ||
| 1625 | # # # warn "(a) Id: " . $token->getId . "token: " . $token->getContent . " ( $tokenLimit / $windowSize)\n"; | ||
| 1626 | # # $windowSize++; | ||
| 1627 | # # if (scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) > 0) { | ||
| 1628 | # # # warn "---\n"; | ||
| 1629 | if ($semfGuessing) { | ||
| 1630 | # # $term = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)->[0]; | ||
| 1631 | if (($term->isTerm) && (!defined($term->getSemanticFeatureFC($document)))) { | ||
| 1632 | # warn "Guessing $semtype\n"; | ||
| 1633 | $self->_addSemanticFeature($term, $semtype, $document); | ||
| 1634 | } | ||
| 1635 | } else { | ||
| 1636 | # # $term = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)->[0]; | ||
| 1637 | # warn 'Term: ' . $term->getForm . "\n"; | ||
| 1638 | # warn "Semf: " . $term->getSemanticFeatureFC($document) . "\n"; | ||
| 1639 | if ($term->isTerm) { | ||
| 1640 | if ($term->getSemanticFeatureFC($document) eq $semtype) { | ||
| 1641 | # warn "found concept with the right semtype ($semtype)\n"; | ||
| 1642 | return($term); | ||
| 1643 | } | ||
| 1644 | } | ||
| 1645 | } | ||
| 1646 | } | ||
| 1647 | # if (!$token->equals($sentence_end_token)) { | ||
| 1648 | $token = $token->next; | ||
| 1649 | # } | ||
| 1650 | # warn "(b) Id: " . $token->getId . "token: " . $token->getContent . " ( $tokenLimit / $windowSize)\n"; | ||
| 1651 | } while(($windowSize < $tokenLimit) && (defined $token) && ($token->getFrom <= $sentence_end_token->getTo)); | ||
| 1652 | |||
| 1653 | return(0); | ||
| 1654 | } | ||
| 1655 | |||
| 1656 | ######################################################################## | ||
| 1657 | |||
| 1658 | sub _searchPOSTagBefore { | ||
| 1659 | my ($self, $document, $sentence_start_token, $sentence_end_token, $POSTagAcr, $start_mark_token, $tokenLimit) = @_; | ||
| 1660 | |||
| 1661 | |||
| 1662 | my $token = $start_mark_token; | ||
| 1663 | my $word; | ||
| 1664 | |||
| 1665 | my $windowSize = 0; | ||
| 1666 | |||
| 1667 | # warn "in _searchPOSTagBefore (" . $start_mark_token->getFrom . ")\n"; | ||
| 1668 | |||
| 1669 | my $POSTag; | ||
| 1670 | if (defined $self->{"POSTAGS"}->{$POSTagAcr}) { | ||
| 1671 | $POSTag = $self->{"POSTAGS"}->{$POSTagAcr}; | ||
| 1672 | } else { | ||
| 1673 | $POSTag = $POSTagAcr; | ||
| 1674 | } | ||
| 1675 | # my $semtype = $self->{"SEMANTICTYPES"}->{$POSTag}; | ||
| 1676 | |||
| 1677 | do { | ||
| 1678 | while((defined $token) && (($token->isSep) && (!$token->equals($sentence_start_token))) ) { | ||
| 1679 | $token = $token->previous; | ||
| 1680 | } ; | ||
| 1681 | |||
| 1682 | if ((defined($token))# && (!$token->equals($sentence_start_token)) | ||
| 1683 | ) { | ||
| 1684 | # warn "token: " . $token->getContent . "\n"; | ||
| 1685 | # warn "\t" . scalar(@{$document->getAnnotations->getWordLevel->getElementByToken($token)}) . "\n"; | ||
| 1686 | $windowSize++; | ||
| 1687 | |||
| 1688 | if (scalar(@{$document->getAnnotations->getWordLevel->getElementByToken($token)}) > 0) { | ||
| 1689 | $word = $document->getAnnotations->getWordLevel->getElementByToken($token)->[0]; | ||
| 1690 | if ($word->getMorphoSyntacticFeatures($document)->syntactic_category eq $POSTag) { | ||
| 1691 | # warn "found word with rigt POSTAG ($POSTag)\n"; | ||
| 1692 | return($word); | ||
| 1693 | } | ||
| 1694 | } | ||
| 1695 | if (!$token->equals($sentence_start_token)) { | ||
| 1696 | $token = $token->previous; | ||
| 1697 | } | ||
| 1698 | # $token = $token->previous; | ||
| 1699 | # warn "token: " . $token->getContent . "\n"; | ||
| 1700 | } | ||
| 1701 | } while(($windowSize < $tokenLimit) && (defined $token) && (!$token->equals($sentence_start_token))); | ||
| 1702 | |||
| 1703 | return(0); | ||
| 1704 | |||
| 1705 | } | ||
| 1706 | |||
| 1707 | |||
| 1708 | sub _searchPOSTagAfter { | ||
| 1709 | my ($self, $document, $sentence_start_token, $sentence_end_token, $POSTagAcr, $start_mark_token, $tokenLimit) = @_; | ||
| 1710 | |||
| 1711 | |||
| 1712 | my $token = $start_mark_token; | ||
| 1713 | my $word; | ||
| 1714 | |||
| 1715 | my $windowSize = 0; | ||
| 1716 | my $POSTag; | ||
| 1717 | if (defined $self->{"POSTAGS"}->{$POSTagAcr}) { | ||
| 1718 | $POSTag = $self->{"POSTAGS"}->{$POSTagAcr}; | ||
| 1719 | } else { | ||
| 1720 | $POSTag = $POSTagAcr; | ||
| 1721 | } | ||
| 1722 | # my $POSTag = $self->{"POSTAGS"}->{$POSTagAcr}; | ||
| 1723 | # my $semtype = $self->{"SEMANTICTYPES"}->{$semtypeAcr}; | ||
| 1724 | |||
| 1725 | # warn "in _searchPOSTagAfter (" . $start_mark_token->getFrom . ")\n"; | ||
| 1726 | |||
| 1727 | do { | ||
| 1728 | while((defined $token) && (($token->isSep) && (!$token->equals($sentence_end_token))) ) { | ||
| 1729 | $token = $token->next; | ||
| 1730 | } ; | ||
| 1731 | if ((defined($token)) && (!$token->equals($sentence_end_token))) { | ||
| 1732 | $windowSize++; | ||
| 1733 | if (scalar(@{$document->getAnnotations->getWordLevel->getElementByToken($token)}) > 0) { | ||
| 1734 | $word = $document->getAnnotations->getWordLevel->getElementByToken($token)->[0]; | ||
| 1735 | if ($word->getMorphoSyntacticFeatures($document)->syntactic_category eq $POSTag) { | ||
| 1736 | return($word); | ||
| 1737 | } | ||
| 1738 | } | ||
| 1739 | if (!$token->equals($sentence_end_token)) { | ||
| 1740 | $token = $token->next; | ||
| 1741 | } | ||
| 1742 | } | ||
| 1743 | } while(($windowSize < $tokenLimit) && (defined $token) && (!$token->equals($sentence_end_token))); | ||
| 1744 | |||
| 1745 | return(0); | ||
| 1746 | } | ||
| 1747 | |||
| 1748 | |||
| 1749 | ######################################################################## | ||
| 1750 | ######################################################################## | ||
| 1751 | |||
| 1752 | sub _searchConceptBefore1 { | ||
| 1753 | my ($self, $document, $sentence_start_token, $sentence_end_token, $semtypeAcr, $start_mark_token, $tokenLimit, $semfGuessing) = @_; | ||
| 1754 | |||
| 1755 | |||
| 1756 | my $token = $start_mark_token; | ||
| 1757 | my $term; | ||
| 1758 | |||
| 1759 | my $windowSize = 0; | ||
| 1760 | |||
| 1761 | # warn "in _searchConceptBefore (" . $start_mark_token->getFrom . ")\n"; | ||
| 1762 | |||
| 1763 | my $semtype = $self->{"SEMANTICTYPES"}->{$semtypeAcr}; | ||
| 1764 | |||
| 1765 | do { | ||
| 1766 | while((defined $token) && (($token->isSep) && (!$token->equals($sentence_start_token))) ) { | ||
| 1767 | $token = $token->previous; | ||
| 1768 | } ; | ||
| 1769 | |||
| 1770 | if ((defined($token)) && (!$token->equals($sentence_start_token))) { | ||
| 1771 | # warn "token: " . $token->getContent . "\n"; | ||
| 1772 | # warn "\t" . scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) . "\n"; | ||
| 1773 | $windowSize++; | ||
| 1774 | |||
| 1775 | if (scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) > 0) { | ||
| 1776 | if ($semfGuessing) { | ||
| 1777 | $term = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)->[0]; | ||
| 1778 | |||
| 1779 | if (($term->isTerm) && (!defined($term->getSemanticFeatureFC($document)))) { | ||
| 1780 | # warn "Guessing $semtype\n"; | ||
| 1781 | $self->_addSemanticFeature($term, $semtype, $document); | ||
| 1782 | } | ||
| 1783 | } else { | ||
| 1784 | $term = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)->[0]; | ||
| 1785 | if ($term->isTerm) { | ||
| 1786 | if ($term->getSemanticFeatureFC($document) eq $semtype) { | ||
| 1787 | # warn "found concept with the right semtype ($semtype)\n"; | ||
| 1788 | return($term); | ||
| 1789 | } | ||
| 1790 | } | ||
| 1791 | } | ||
| 1792 | } | ||
| 1793 | $token = $token->previous; | ||
| 1794 | # warn "token: " . $token->getContent . "\n"; | ||
| 1795 | } | ||
| 1796 | } while(($windowSize < $tokenLimit) && (defined $token) && (!$token->equals($sentence_start_token))); | ||
| 1797 | |||
| 1798 | return(0); | ||
| 1799 | |||
| 1800 | } | ||
| 1801 | |||
| 1802 | |||
| 1803 | sub _searchConceptAfter1 { | ||
| 1804 | my ($self, $document, $sentence_start_token, $sentence_end_token, $semtypeAcr, $start_mark_token, $tokenLimit, $semfGuessing) = @_; | ||
| 1805 | |||
| 1806 | |||
| 1807 | my $token = $start_mark_token; | ||
| 1808 | my $term; | ||
| 1809 | |||
| 1810 | my $windowSize = 0; | ||
| 1811 | my $semtype = $self->{"SEMANTICTYPES"}->{$semtypeAcr}; | ||
| 1812 | |||
| 1813 | # warn "in _searchConceptAfter (" . $start_mark_token->getFrom . ")\n"; | ||
| 1814 | |||
| 1815 | do { | ||
| 1816 | while((defined $token) && (($token->isSep) && (!$token->equals($sentence_end_token))) ) { | ||
| 1817 | $token = $token->next; | ||
| 1818 | } ; | ||
| 1819 | if ((defined($token)) && (!$token->equals($sentence_end_token))) { | ||
| 1820 | $windowSize++; | ||
| 1821 | if (scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) > 0) { | ||
| 1822 | if ($semfGuessing) { | ||
| 1823 | $term = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)->[0]; | ||
| 1824 | if (($term->isTerm) && (!defined($term->getSemanticFeatureFC($document)))) { | ||
| 1825 | # warn "Guessing $semtype\n"; | ||
| 1826 | $self->_addSemanticFeature($term, $semtype, $document); | ||
| 1827 | } | ||
| 1828 | } else { | ||
| 1829 | $term = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)->[0]; | ||
| 1830 | if ($term->isTerm) { | ||
| 1831 | if ($term->getSemanticFeatureFC($document) eq $semtype) { | ||
| 1832 | return($term); | ||
| 1833 | } | ||
| 1834 | } | ||
| 1835 | } | ||
| 1836 | } | ||
| 1837 | $token = $token->next; | ||
| 1838 | } | ||
| 1839 | } while(($windowSize < $tokenLimit) && (defined $token) && (!$token->equals($sentence_end_token))); | ||
| 1840 | |||
| 1841 | return(0); | ||
| 1842 | } | ||
| 1843 | |||
| 1844 | ######################################################################## | ||
| 1845 | |||
| 1846 | sub _searchPOSTagBefore1 { | ||
| 1847 | my ($self, $document, $sentence_start_token, $sentence_end_token, $POSTagAcr, $start_mark_token, $tokenLimit) = @_; | ||
| 1848 | |||
| 1849 | |||
| 1850 | my $token = $start_mark_token; | ||
| 1851 | my $word; | ||
| 1852 | |||
| 1853 | my $windowSize = 0; | ||
| 1854 | |||
| 1855 | # warn "in _searchPOSTagBefore (" . $start_mark_token->getFrom . ")\n"; | ||
| 1856 | |||
| 1857 | my $POSTag; | ||
| 1858 | if (defined $self->{"POSTAGS"}->{$POSTagAcr}) { | ||
| 1859 | $POSTag = $self->{"POSTAGS"}->{$POSTagAcr}; | ||
| 1860 | } else { | ||
| 1861 | $POSTag = $POSTagAcr; | ||
| 1862 | } | ||
| 1863 | # my $semtype = $self->{"SEMANTICTYPES"}->{$POSTag}; | ||
| 1864 | |||
| 1865 | do { | ||
| 1866 | while((defined $token) && (($token->isSep) && (!$token->equals($sentence_start_token))) ) { | ||
| 1867 | $token = $token->previous; | ||
| 1868 | } ; | ||
| 1869 | |||
| 1870 | if ((defined($token)) && (!$token->equals($sentence_start_token))) { | ||
| 1871 | # warn "token: " . $token->getContent . "\n"; | ||
| 1872 | # warn "\t" . scalar(@{$document->getAnnotations->getWordLevel->getElementByToken($token)}) . "\n"; | ||
| 1873 | $windowSize++; | ||
| 1874 | |||
| 1875 | if (scalar(@{$document->getAnnotations->getWordLevel->getElementByToken($token)}) > 0) { | ||
| 1876 | $word = $document->getAnnotations->getWordLevel->getElementByToken($token)->[0]; | ||
| 1877 | if ($word->getMorphoSyntacticFeatures($document) eq $POSTag) { | ||
| 1878 | # warn "found word with rigt POSTAG ($POSTag)\n"; | ||
| 1879 | return($word); | ||
| 1880 | } | ||
| 1881 | } | ||
| 1882 | $token = $token->previous; | ||
| 1883 | # warn "token: " . $token->getContent . "\n"; | ||
| 1884 | } | ||
| 1885 | } while(($windowSize < $tokenLimit) && (defined $token) && (!$token->equals($sentence_start_token))); | ||
| 1886 | |||
| 1887 | return(0); | ||
| 1888 | |||
| 1889 | } | ||
| 1890 | |||
| 1891 | |||
| 1892 | sub _searchPOSTagAfter1 { | ||
| 1893 | my ($self, $document, $sentence_start_token, $sentence_end_token, $POSTagAcr, $start_mark_token, $tokenLimit) = @_; | ||
| 1894 | |||
| 1895 | |||
| 1896 | my $token = $start_mark_token; | ||
| 1897 | my $word; | ||
| 1898 | |||
| 1899 | my $windowSize = 0; | ||
| 1900 | my $POSTag; | ||
| 1901 | if (defined $self->{"POSTAGS"}->{$POSTagAcr}) { | ||
| 1902 | $POSTag = $self->{"POSTAGS"}->{$POSTagAcr}; | ||
| 1903 | } else { | ||
| 1904 | $POSTag = $POSTagAcr; | ||
| 1905 | } | ||
| 1906 | do { | ||
| 1907 | while((defined $token) && (($token->isSep) && (!$token->equals($sentence_end_token))) ) { | ||
| 1908 | $token = $token->next; | ||
| 1909 | } ; | ||
| 1910 | if ((defined($token)) && (!$token->equals($sentence_end_token))) { | ||
| 1911 | $windowSize++; | ||
| 1912 | if (scalar(@{$document->getAnnotations->getWordLevel->getElementByToken($token)}) > 0) { | ||
| 1913 | $word = $document->getAnnotations->getWordLevel->getElementByToken($token)->[0]; | ||
| 1914 | if ($word->getMorphoSyntacticFeatures($document) eq $POSTag) { | ||
| 1915 | return($word); | ||
| 1916 | } | ||
| 1917 | } | ||
| 1918 | $token = $token->next; | ||
| 1919 | } | ||
| 1920 | } while(($windowSize < $tokenLimit) && (defined $token) && (!$token->equals($sentence_end_token))); | ||
| 1921 | |||
| 1922 | return(0); | ||
| 1923 | } | ||
| 1924 | |||
| 1925 | sub loadSynPatterns | ||
| 1926 | { | ||
| 1927 | my ($self) = @_; | ||
| 1928 | |||
| 1929 | # $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 1930 | |||
| 1931 | if (defined $self->_config->configuration->{'RESOURCE'}->{"language=EN"}->{"PATTERNSSYN"}) { | ||
| 1932 | # warn "**** FILE value to CHANGE *****"; | ||
| 1933 | # open FILE, "/home/thierry/Recherche/Challenges/I2B2-2010/Resources/structure_syntaxique-nf.txt" or die "No such file\n"; | ||
| 1934 | # open FILE, "/export/home/limbio/hamon/Research/Challenges/I2B2-2010/Resources/structure_syntaxique-test.txt"; | ||
| 1935 | |||
| 1936 | if (!open FILE, $self->_config->configuration->{'RESOURCE'}->{"language=EN"}->{"PATTERNSSYN"}) { | ||
| 1937 | warn "No such file" . $self->_config->configuration->{'RESOURCE'}->{"language=EN"}->{"PATTERNSSYN"} ."\n"; | ||
| 1938 | return(0); | ||
| 1939 | } | ||
| 1940 | } else { | ||
| 1941 | warn "No Pattern FileName specified\n"; | ||
| 1942 | return(0); | ||
| 1943 | } | ||
| 1944 | |||
| 1945 | my $parser = Lingua::Ogmios::NLPWrappers::PatternParser->new(); | ||
| 1946 | |||
| 1947 | $parser->YYData->{PPRS} = $self; | ||
| 1948 | $parser->YYData->{FH} = \*FILE; | ||
| 1949 | |||
| 1950 | $parser->YYParse(yylex => \&Lingua::Ogmios::NLPWrappers::PatternParser::_Lexer, yyerror => \&Lingua::Ogmios::NLPWrappers::PatternParser::_Error); | ||
| 1951 | |||
| 1952 | close FILE; | ||
| 1953 | } | ||
| 1954 | |||
| 1955 | sub addSynPattern { | ||
| 1956 | my ($self, $node_set, $action, $distance) = @_; | ||
| 1957 | |||
| 1958 | my @nodes = @{$node_set}; | ||
| 1959 | push @{$self->{'SYN_PATTERNS'}}, {'pattern' => \@nodes, | ||
| 1960 | 'action' => $action, | ||
| 1961 | 'distance' => $distance}; | ||
| 1962 | } | ||
| 1963 | |||
| 1964 | sub getSynPatterns { | ||
| 1965 | my ($self) = @_; | ||
| 1966 | |||
| 1967 | return $self->{SYN_PATTERNS}; | ||
| 1968 | } | ||
| 1969 | |||
| 1970 | sub synPatternSearch { | ||
| 1971 | my ($self, $function_pointer, $args) = @_; | ||
| 1972 | |||
| 1973 | warn "start synPatternSearch\n"; | ||
| 1974 | |||
| 1975 | $self->{"SYN_PATTERNS"} = []; | ||
| 1976 | |||
| 1977 | if ($self->loadSynPatterns() == 0) { | ||
| 1978 | return(0); | ||
| 1979 | } | ||
| 1980 | |||
| 1981 | my $document; | ||
| 1982 | my $sentence; | ||
| 1983 | |||
| 1984 | my $sentence_start_token; | ||
| 1985 | my $sentence_end_token; | ||
| 1986 | |||
| 1987 | my $pattern; | ||
| 1988 | my $synPattern; | ||
| 1989 | |||
| 1990 | my $term; | ||
| 1991 | my $word; | ||
| 1992 | |||
| 1993 | my $sentenceLemma; | ||
| 1994 | my $sentenceLemma2; | ||
| 1995 | |||
| 1996 | my $REtype; | ||
| 1997 | my $RELtype; | ||
| 1998 | |||
| 1999 | my $i; | ||
| 2000 | |||
| 2001 | my $semfGuessing = 0; | ||
| 2002 | my $wordSize = 5; | ||
| 2003 | |||
| 2004 | my $j; | ||
| 2005 | my $regexp; | ||
| 2006 | |||
| 2007 | my $offset_refs; | ||
| 2008 | |||
| 2009 | my $POSTAGwordLimit = 10; | ||
| 2010 | my $tokenLimit; | ||
| 2011 | |||
| 2012 | my $domainSpecificRelation; | ||
| 2013 | |||
| 2014 | my $start_token; | ||
| 2015 | my $start_token_origin; | ||
| 2016 | my $end_mark_token; | ||
| 2017 | |||
| 2018 | my $found; | ||
| 2019 | my @terms; | ||
| 2020 | |||
| 2021 | my $word_re_start_token; | ||
| 2022 | my $word_re_end_token; | ||
| 2023 | |||
| 2024 | my $element; | ||
| 2025 | my $selectedSentences; | ||
| 2026 | |||
| 2027 | # print Dumper($self->{SYN_PATTERNS}); | ||
| 2028 | |||
| 2029 | # warn "start Search\n"; | ||
| 2030 | |||
| 2031 | foreach $document (@{$self->_documentSet}) { | ||
| 2032 | foreach $synPattern (@{$self->getSynPatterns}) { | ||
| 2033 | # $pattern = $synPattern->{'pattern'}; | ||
| 2034 | foreach $sentence (@{$document->getAnnotations->getSentenceLevel->getElements}) { | ||
| 2035 | $selectedSentences->{$sentence->refid_start_token} = {'sentence' => $sentence, | ||
| 2036 | 'first_item' => [] | ||
| 2037 | }; | ||
| 2038 | } | ||
| 2039 | # print STDERR "Select sentences\n"; | ||
| 2040 | # print STDERR Dumper $synPattern->{'pattern'}; | ||
| 2041 | $selectedSentences = $self->_selectSentencesFromPattern($synPattern->{'pattern'}, $selectedSentences, $document, 0); | ||
| 2042 | |||
| 2043 | # print STDERR "================\n"; | ||
| 2044 | foreach $sentence (values %$selectedSentences) { | ||
| 2045 | # print "sentenceID: " . $sentence->{'sentence'}->getId . "\n"; | ||
| 2046 | # print STDERR "Scanning sentence " . $sentence->{'sentence'}->getId . "\n"; | ||
| 2047 | foreach $element (@{$sentence->{'first_item'}}) { | ||
| 2048 | # print STDERR '>>>' . $element->getForm . "\n"; | ||
| 2049 | |||
| 2050 | $start_token = $element->start_token; | ||
| 2051 | # if ($synPattern->{'pattern'}->[0]->{'type'} eq "word_re") { | ||
| 2052 | # $start_token = $sentence->{'sentence'}->refid_start_token; | ||
| 2053 | # } | ||
| 2054 | $sentence_end_token = $sentence->{'sentence'}->refid_end_token; | ||
| 2055 | $wordSize = $synPattern->{'distance'}; | ||
| 2056 | |||
| 2057 | my @terms; | ||
| 2058 | $self->_searchPattern($document, \$start_token, $sentence_end_token, $synPattern, $wordSize, $semfGuessing, 0, \@terms, $function_pointer, $args); | ||
| 2059 | |||
| 2060 | } | ||
| 2061 | # print STDERR "-----\n"; | ||
| 2062 | } | ||
| 2063 | # print STDERR "************************\n"; | ||
| 2064 | } | ||
| 2065 | } | ||
| 2066 | # warn "end Search\n"; | ||
| 2067 | return (0); | ||
| 2068 | } | ||
| 2069 | |||
| 2070 | sub _searchPattern { | ||
| 2071 | my ($self, $document, $start_token, $sentence_end_token, $synPattern, $wordSize, $semfGuessing, $level, $terms, $function_pointer, $args) = @_; | ||
| 2072 | |||
| 2073 | my $word; | ||
| 2074 | my $term; | ||
| 2075 | my $word_re_start_token; | ||
| 2076 | my $word_re_end_token; | ||
| 2077 | my $found = 0; | ||
| 2078 | my $i; | ||
| 2079 | |||
| 2080 | my $pattern = $synPattern->{"pattern"}; | ||
| 2081 | |||
| 2082 | my $syndep_start_token = $$start_token; | ||
| 2083 | |||
| 2084 | # my @terms; | ||
| 2085 | |||
| 2086 | # my $start_token_orig = $$start_token; | ||
| 2087 | |||
| 2088 | my @patternElements; | ||
| 2089 | |||
| 2090 | # print STDERR "level = $level\n"; | ||
| 2091 | |||
| 2092 | # print STDERR Dumper $pattern; | ||
| 2093 | |||
| 2094 | |||
| 2095 | # if ((scalar(@$pattern) == 1) && ($pattern->[0]->{'type'} eq "chunk")) { | ||
| 2096 | # $level = -1; | ||
| 2097 | # } | ||
| 2098 | |||
| 2099 | for($i = 0; $i < scalar(@$pattern); $i++) { | ||
| 2100 | # print "i = $i (" . $pattern->[$i]->{'type'} . ")\n"; | ||
| 2101 | # print "$$start_token\n"; | ||
| 2102 | # print $$start_token->getId . " : " . $$start_token->getFrom . "\n"; | ||
| 2103 | if (!defined $$start_token) { | ||
| 2104 | $found = 0; | ||
| 2105 | last; | ||
| 2106 | } | ||
| 2107 | if ($pattern->[$i]->{'type'} eq "semtype") { | ||
| 2108 | # print "i = $i (" . $pattern->[$i]->{'type'} . ")\n"; | ||
| 2109 | if ($term = $self->_searchSemType($document, $$start_token, $sentence_end_token, $pattern->[$i]->{"semtype"}, $wordSize, $semfGuessing)) { | ||
| 2110 | # print STDERR "found " . $term->getForm . " : " . $term->end_token->getId . "(SEMTYPE)\n"; | ||
| 2111 | push @patternElements, $term; | ||
| 2112 | # temporary | ||
| 2113 | if ($level == 0) { | ||
| 2114 | push @$terms, $term; | ||
| 2115 | } | ||
| 2116 | $$start_token = $term->end_token->next; | ||
| 2117 | $found = 1; | ||
| 2118 | # next; | ||
| 2119 | } else { | ||
| 2120 | # print "term not found\n"; | ||
| 2121 | $found = 0; | ||
| 2122 | last; | ||
| 2123 | } | ||
| 2124 | } | ||
| 2125 | if ($pattern->[$i]->{'type'} eq "postag") { | ||
| 2126 | # print "i = $i (" . $pattern->[$i]->{'type'} . ")\n"; | ||
| 2127 | if ($word = $self->_searchPOSTAG($document, $$start_token, $sentence_end_token, $pattern->[$i]->{"postag"}, $wordSize)) { | ||
| 2128 | # print STDERR "found " . $word->getForm . "(POSTAG)\n"; | ||
| 2129 | push @patternElements, $word; | ||
| 2130 | $$start_token = $word->end_token->next; | ||
| 2131 | $found = 1; | ||
| 2132 | # next; | ||
| 2133 | } else { | ||
| 2134 | # print "postag not found\n"; | ||
| 2135 | $found = 0; | ||
| 2136 | last; | ||
| 2137 | } | ||
| 2138 | } | ||
| 2139 | if ($pattern->[$i]->{'type'} eq "word") { | ||
| 2140 | if ($word = $self->_searchWord($document, $$start_token, $sentence_end_token, $pattern->[$i]->{"word"}, $wordSize)) { | ||
| 2141 | # print STDERR "found " . $word->getForm . " " . $word->start_token->getFrom . " (WORD)\n"; | ||
| 2142 | push @patternElements, $word; | ||
| 2143 | $$start_token = $word->end_token->next; | ||
| 2144 | $found = 1; | ||
| 2145 | # next; | ||
| 2146 | } else { | ||
| 2147 | # print "word not found\n"; | ||
| 2148 | $found = 0; | ||
| 2149 | last; | ||
| 2150 | } | ||
| 2151 | } | ||
| 2152 | if ($pattern->[$i]->{'type'} eq "word_re") { | ||
| 2153 | # $$start_token = $start_token_orig; | ||
| 2154 | ($word_re_start_token, $word_re_end_token) = $self->_searchWordRE($document, $$start_token, $sentence_end_token, $pattern->[$i]->{"word_re"}, $wordSize); | ||
| 2155 | if ((defined $word_re_start_token) && (defined $word_re_end_token)) { | ||
| 2156 | # print STDERR "found " . $word_re_end_token->getContent . "(WORDRE)\n"; | ||
| 2157 | ## push @patternElements, $term; | ||
| 2158 | $$start_token = $word_re_end_token->next; | ||
| 2159 | $found = 1; | ||
| 2160 | # next; | ||
| 2161 | } else { | ||
| 2162 | # print "word not found\n"; | ||
| 2163 | $found = 0; | ||
| 2164 | last; | ||
| 2165 | } | ||
| 2166 | } | ||
| 2167 | if ($pattern->[$i]->{'type'} eq "term") { | ||
| 2168 | if ($term = $self->_searchTerm($document, $$start_token, $sentence_end_token, $wordSize)) { | ||
| 2169 | # print STDERR "found " . $word->getForm . "(TERM)\n"; | ||
| 2170 | push @patternElements, $term; | ||
| 2171 | $$start_token = $term->end_token->next; | ||
| 2172 | $found = 1; | ||
| 2173 | # next; | ||
| 2174 | } else { | ||
| 2175 | # print "word not found\n"; | ||
| 2176 | $found = 0; | ||
| 2177 | last; | ||
| 2178 | } | ||
| 2179 | } | ||
| 2180 | if ($pattern->[$i]->{'type'} eq "termContent") { | ||
| 2181 | if ($term = $self->_searchTermContent($document, $$start_token, $sentence_end_token, $pattern->[$i]->{"termContent"}, $wordSize)) { | ||
| 2182 | # print STDERR "found " . $word->getForm . "(TERMCONTENT)\n"; | ||
| 2183 | push @patternElements, $term; | ||
| 2184 | $$start_token = $term->end_token->next; | ||
| 2185 | $found = 1; | ||
| 2186 | # next; | ||
| 2187 | } else { | ||
| 2188 | # print "word not found\n"; | ||
| 2189 | $found = 0; | ||
| 2190 | last; | ||
| 2191 | } | ||
| 2192 | } | ||
| 2193 | if ($pattern->[$i]->{'type'} eq "chunk") { | ||
| 2194 | # print "i = $i (" . $pattern->[$i]->{'type'} . ")\n"; | ||
| 2195 | if ($found = $self->_searchPattern($document, $start_token, $sentence_end_token, {'pattern' => $pattern->[$i]->{"chunk"}}, $wordSize, $semfGuessing, $level + 1, $terms, $function_pointer, $args)) { | ||
| 2196 | ## push @patternElements, $term; | ||
| 2197 | $found = 1; | ||
| 2198 | # print STDERR "chunk found $found\n"; | ||
| 2199 | } else { | ||
| 2200 | # print "chunk not found\n"; | ||
| 2201 | $found = 0; | ||
| 2202 | last; | ||
| 2203 | } | ||
| 2204 | } | ||
| 2205 | if ($pattern->[$i]->{'type'} eq "syndep") { | ||
| 2206 | if (scalar (@patternElements) > 0) { | ||
| 2207 | $syndep_start_token = $patternElements[0]->start_token; | ||
| 2208 | } | ||
| 2209 | |||
| 2210 | if ($self->_searchSynDep($document, $syndep_start_token, $sentence_end_token, $pattern->[$i]->{'syndep'}, $wordSize, \@patternElements, $terms)) { | ||
| 2211 | # print STDERR "syndep found\n"; | ||
| 2212 | $found = 1; | ||
| 2213 | } else { | ||
| 2214 | # print "i = $i (" . $pattern->[$i]->{'type'} . ")\n"; | ||
| 2215 | $found = 0; | ||
| 2216 | last; | ||
| 2217 | } | ||
| 2218 | } | ||
| 2219 | } | ||
| 2220 | # print STDERR "($level - $found)\n"; | ||
| 2221 | if (($level == 0) && ($found == 1)) { # ($i == scalar(@$pattern)) { | ||
| 2222 | # if ($found == 1) { # ($i == scalar(@$pattern)) { | ||
| 2223 | # print STDERR "PATTERN FOUND ($level - $found) - " . $synPattern->{"action"} . "\n"; | ||
| 2224 | # my @tmp = split m!/!, $synPattern->{"action"}; | ||
| 2225 | &$function_pointer($document, $synPattern->{"action"}, $terms, $args); | ||
| 2226 | # &$function_pointer($document, $synPattern->{"action"}, \@terms, $args); | ||
| 2227 | } | ||
| 2228 | return($found); | ||
| 2229 | } | ||
| 2230 | |||
| 2231 | sub _searchSynDep { | ||
| 2232 | my ($self, $document, $start_token, $end_token, $syndep, $WindowSize, $patternElements, $terms) = @_; | ||
| 2233 | |||
| 2234 | my $token = $start_token; | ||
| 2235 | my $synrel; | ||
| 2236 | my $currentWindowSize = 0; | ||
| 2237 | my $phrase; | ||
| 2238 | |||
| 2239 | if (scalar(@$terms) == 0) { | ||
| 2240 | return(0); | ||
| 2241 | } | ||
| 2242 | |||
| 2243 | my $term_start_token = $terms->[0]->start_token; | ||
| 2244 | my $term_end_token = $terms->[0]->end_token; | ||
| 2245 | |||
| 2246 | my %postags = ("DT" => 1, "IN" => 1); | ||
| 2247 | |||
| 2248 | # warn "====> " . $terms->[0]->getForm . "\n"; | ||
| 2249 | |||
| 2250 | if (ref($terms->[0]) eq "Lingua::Ogmios::Annotations::SemanticUnit") { | ||
| 2251 | if (($terms->[0]->reference_name eq "refid_word") || | ||
| 2252 | ($terms->[0]->reference_name eq "refid_phrase")) { | ||
| 2253 | $phrase = $terms->[0]->reference; | ||
| 2254 | } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $terms->[0]->start_token->getId)) { | ||
| 2255 | $phrase = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $terms->[0]->start_token->getId)->[0]; | ||
| 2256 | } | ||
| 2257 | } | ||
| 2258 | |||
| 2259 | # warn "phrase: " . $phrase->getForm . "\n"; | ||
| 2260 | |||
| 2261 | foreach $synrel (@{$document->getAnnotations->getSyntacticRelationLevel->getElementFromIndex('syntactic_relation_type', $syndep)}) { | ||
| 2262 | # foreach $synrel (@{$document->getAnnotations->getSyntacticRelationLevel->getElementFromIndex2('refid_head', $phrase)}) { | ||
| 2263 | # foreach $synrel (@{$document->getAnnotations->getSyntacticRelationLevel->getElements}) { | ||
| 2264 | # if ($synrel->syntactic_relation_type eq $syndep) { | ||
| 2265 | # print $start_token->getFrom . " <= " . $synrel->refid_modifier->[0]->start_token->getFrom . "\n"; | ||
| 2266 | # print $synrel->refid_modifier->[0]->end_token->getTo . " <= " . $end_token->getTo . "\n"; | ||
| 2267 | |||
| 2268 | # print STDERR "syndep\n"; | ||
| 2269 | |||
| 2270 | # my %tmp = (ref($synrel->refid_head->[0]) . $synrel->refid_head->[0]->getId => $synrel->refid_head->[0]); | ||
| 2271 | # my @elementChunk = $self->_getChunkFromElement($document, $synrel->refid_modifier->[0], $synrel->refid_head->[0], \%tmp); | ||
| 2272 | # my $elt; | ||
| 2273 | # warn "===\n"; | ||
| 2274 | # foreach $elt (keys %tmp) { | ||
| 2275 | # print STDERR "\t$elt : " . $tmp{$elt}->getForm . "\n"; | ||
| 2276 | # } | ||
| 2277 | |||
| 2278 | # ? check if the head of syndep is in the found elements at the level n-1 | ||
| 2279 | # my $i = -1; | ||
| 2280 | # my $refPatElmt; | ||
| 2281 | # for($i = 0; $i < scalar(@$patternelments); $i++) { | ||
| 2282 | # if ((($refPatElmt eq "Lingua::Ogmios::Annotations::Word" ) || | ||
| 2283 | # ($refPatElmt eq "Lingua::Ogmios::Annotations::Phrase" )) && | ||
| 2284 | # (!exists $tmp{$refPatElmt . $patternelments->[$i]->getId})) { | ||
| 2285 | # return(0); | ||
| 2286 | # } | ||
| 2287 | # } | ||
| 2288 | # return(1); | ||
| 2289 | |||
| 2290 | # do { | ||
| 2291 | # $i++; | ||
| 2292 | # if ($i < scalar(@$patternElements)) { | ||
| 2293 | # $refPatElmt = ref($patternElements->[$i]); | ||
| 2294 | # print STDERR "($i)" . $refPatElmt . $patternElements->[$i]->getId . "\n"; | ||
| 2295 | # if ($refPatElmt eq "Lingua::Ogmios::Annotations::Word" ) { | ||
| 2296 | # print STDERR "\t" . $patternElements->[$i]->getMorphoSyntacticFeatures($document)->syntactic_category . "\n"; | ||
| 2297 | # } | ||
| 2298 | # } | ||
| 2299 | # } while(($i < scalar(@$patternElements)) && | ||
| 2300 | # (($refPatElmt eq "Lingua::Ogmios::Annotations::Word" ) || | ||
| 2301 | # ($refPatElmt eq "Lingua::Ogmios::Annotations::Phrase" )) && | ||
| 2302 | # ((($refPatElmt eq "Lingua::Ogmios::Annotations::Word" ) && | ||
| 2303 | # (exists $postags{$patternElements->[$i]->getMorphoSyntacticFeatures($document)->syntactic_category})) || | ||
| 2304 | # (exists $tmp{$refPatElmt . $patternElements->[$i]->getId}))); | ||
| 2305 | # warn "i = $i\n"; | ||
| 2306 | # if ($i == scalar(@$patternElements)) { | ||
| 2307 | # print STDERR "==>FOUND (" . $terms->[0]->getForm . ")\n"; | ||
| 2308 | # print "==>FOUND (" . $terms->[0]->getForm . ")\n"; | ||
| 2309 | # return(1); | ||
| 2310 | # } | ||
| 2311 | |||
| 2312 | # search in syndep is in the found elements | ||
| 2313 | if (($start_token->getFrom <= $synrel->refid_modifier->[0]->start_token->getFrom) && | ||
| 2314 | ($synrel->refid_modifier->[0]->end_token->getTo <= $end_token->getTo)) { | ||
| 2315 | |||
| 2316 | # # if ((($start_token->getFrom <= $synrel->refid_modifier->[0]->start_token->getFrom) && | ||
| 2317 | # # ($synrel->refid_modifier->[0]->end_token->getTo <= $end_token->getTo)) && | ||
| 2318 | # # (($term_start_token->getFrom <= $synrel->refid_head->[0]->start_token->getFrom) && | ||
| 2319 | # # ($synrel->refid_head->[0]->end_token->getTo <= $term_end_token->getTo))) { | ||
| 2320 | |||
| 2321 | # print STDERR "==>FOUND (" . $terms->[0]->getForm . ")\n"; | ||
| 2322 | # print "==>FOUND (" . $terms->[0]->getForm . ")\n"; | ||
| 2323 | return(1); | ||
| 2324 | |||
| 2325 | # check if all the the elements are in the chunk | ||
| 2326 | |||
| 2327 | } | ||
| 2328 | } | ||
| 2329 | # warn "syndep not found\n"; | ||
| 2330 | # print "search Concept\n"; | ||
| 2331 | |||
| 2332 | # do { | ||
| 2333 | # my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 2334 | # if (scalar(@words) > 0) { | ||
| 2335 | # $words{$words[0]->getId}++; | ||
| 2336 | # $currentWindowSize = scalar(keys %words); | ||
| 2337 | # } | ||
| 2338 | # foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 2339 | # if ($semfGuessing) { | ||
| 2340 | # if (($term->isTerm) && (!defined($term->getSemanticFeatureFC($document)))) { | ||
| 2341 | # $self->_addSemanticFeature($term, $semtype, $document); | ||
| 2342 | # } | ||
| 2343 | # } else { | ||
| 2344 | # if ($term->isTerm) { | ||
| 2345 | # if ($term->getSemanticFeatureFC($document) eq $semtype) { | ||
| 2346 | # return($term); | ||
| 2347 | # } | ||
| 2348 | # } | ||
| 2349 | # } | ||
| 2350 | # } | ||
| 2351 | # $token = $token->next; | ||
| 2352 | # } while(($currentWindowSize < $WindowSize) && (defined $token) && ($token->getFrom <= $end_token->getTo)); | ||
| 2353 | |||
| 2354 | return(0); | ||
| 2355 | } | ||
| 2356 | |||
| 2357 | sub _searchSynDep2 { | ||
| 2358 | my ($self, $document, $start_token, $end_token, $syndep, $WindowSize, $patternElements, $terms) = @_; | ||
| 2359 | |||
| 2360 | my $token = $start_token; | ||
| 2361 | my $synrel; | ||
| 2362 | my $currentWindowSize = 0; | ||
| 2363 | my $phrase; | ||
| 2364 | |||
| 2365 | my $term_start_token = $terms->[0]->start_token; | ||
| 2366 | my $term_end_token = $terms->[0]->end_token; | ||
| 2367 | |||
| 2368 | my %postags = ("DT" => 1, "IN" => 1); | ||
| 2369 | |||
| 2370 | # warn "====> " . $terms->[0]->getForm . "\n"; | ||
| 2371 | |||
| 2372 | if (ref($terms->[0]) eq "Lingua::Ogmios::Annotations::SemanticUnit") { | ||
| 2373 | if (($terms->[0]->reference_name eq "refid_word") || | ||
| 2374 | ($terms->[0]->reference_name eq "refid_phrase")) { | ||
| 2375 | $phrase = $terms->[0]->reference; | ||
| 2376 | } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $terms->[0]->start_token->getId)) { | ||
| 2377 | $phrase = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $terms->[0]->start_token->getId)->[0]; | ||
| 2378 | } | ||
| 2379 | } | ||
| 2380 | |||
| 2381 | warn "phrase: " . $phrase->getForm . "\n"; | ||
| 2382 | |||
| 2383 | # foreach $synrel (@{$document->getAnnotations->getSyntacticRelationLevel->getElementFromIndex('syntactic_relation_type', $syndep)}) { | ||
| 2384 | foreach $synrel (@{$document->getAnnotations->getSyntacticRelationLevel->getElementFromIndex2('refid_head', $phrase)}) { | ||
| 2385 | # foreach $synrel (@{$document->getAnnotations->getSyntacticRelationLevel->getElements}) { | ||
| 2386 | # if ($synrel->syntactic_relation_type eq $syndep) { | ||
| 2387 | # print $start_token->getFrom . " <= " . $synrel->refid_modifier->[0]->start_token->getFrom . "\n"; | ||
| 2388 | # print $synrel->refid_modifier->[0]->end_token->getTo . " <= " . $end_token->getTo . "\n"; | ||
| 2389 | |||
| 2390 | print STDERR "syndep\n"; | ||
| 2391 | |||
| 2392 | my %tmp = (ref($synrel->refid_head->[0]) . $synrel->refid_head->[0]->getId => $synrel->refid_head->[0]); | ||
| 2393 | my @elementChunk = $self->_getChunkFromElement($document, $synrel->refid_modifier->[0], $synrel->refid_head->[0], \%tmp); | ||
| 2394 | my $elt; | ||
| 2395 | warn "===\n"; | ||
| 2396 | foreach $elt (keys %tmp) { | ||
| 2397 | print STDERR "\t$elt : " . $tmp{$elt}->getForm . "\n"; | ||
| 2398 | } | ||
| 2399 | |||
| 2400 | # ? check if the head of syndep is in the found elements at the level n-1 | ||
| 2401 | my $i = -1; | ||
| 2402 | my $refPatElmt; | ||
| 2403 | # for($i = 0; $i < scalar(@$patternelments); $i++) { | ||
| 2404 | # if ((($refPatElmt eq "Lingua::Ogmios::Annotations::Word" ) || | ||
| 2405 | # ($refPatElmt eq "Lingua::Ogmios::Annotations::Phrase" )) && | ||
| 2406 | # (!exists $tmp{$refPatElmt . $patternelments->[$i]->getId})) { | ||
| 2407 | # return(0); | ||
| 2408 | # } | ||
| 2409 | # } | ||
| 2410 | # return(1); | ||
| 2411 | do { | ||
| 2412 | $i++; | ||
| 2413 | if ($i < scalar(@$patternElements)) { | ||
| 2414 | $refPatElmt = ref($patternElements->[$i]); | ||
| 2415 | print STDERR "($i)" . $refPatElmt . $patternElements->[$i]->getId . "\n"; | ||
| 2416 | if ($refPatElmt eq "Lingua::Ogmios::Annotations::Word" ) { | ||
| 2417 | print STDERR "\t" . $patternElements->[$i]->getMorphoSyntacticFeatures($document)->syntactic_category . "\n"; | ||
| 2418 | } | ||
| 2419 | } | ||
| 2420 | } while(($i < scalar(@$patternElements)) && | ||
| 2421 | (($refPatElmt eq "Lingua::Ogmios::Annotations::Word" ) || | ||
| 2422 | ($refPatElmt eq "Lingua::Ogmios::Annotations::Phrase" )) && | ||
| 2423 | ((($refPatElmt eq "Lingua::Ogmios::Annotations::Word" ) && | ||
| 2424 | (exists $postags{$patternElements->[$i]->getMorphoSyntacticFeatures($document)->syntactic_category})) || | ||
| 2425 | (exists $tmp{$refPatElmt . $patternElements->[$i]->getId}))); | ||
| 2426 | warn "i = $i\n"; | ||
| 2427 | if ($i == scalar(@$patternElements)) { | ||
| 2428 | print STDERR "==>FOUND (" . $terms->[0]->getForm . ")\n"; | ||
| 2429 | print "==>FOUND (" . $terms->[0]->getForm . ")\n"; | ||
| 2430 | return(1); | ||
| 2431 | } | ||
| 2432 | |||
| 2433 | # search in syndep is in the found elements | ||
| 2434 | # if (($start_token->getFrom <= $synrel->refid_modifier->[0]->start_token->getFrom) && | ||
| 2435 | # ($synrel->refid_modifier->[0]->end_token->getTo <= $end_token->getTo)) { | ||
| 2436 | |||
| 2437 | # # if ((($start_token->getFrom <= $synrel->refid_modifier->[0]->start_token->getFrom) && | ||
| 2438 | # # ($synrel->refid_modifier->[0]->end_token->getTo <= $end_token->getTo)) && | ||
| 2439 | # # (($term_start_token->getFrom <= $synrel->refid_head->[0]->start_token->getFrom) && | ||
| 2440 | # # ($synrel->refid_head->[0]->end_token->getTo <= $term_end_token->getTo))) { | ||
| 2441 | |||
| 2442 | # print STDERR "==>FOUND (" . $terms->[0]->getForm . ")\n"; | ||
| 2443 | # print "==>FOUND (" . $terms->[0]->getForm . ")\n"; | ||
| 2444 | # return(1); | ||
| 2445 | |||
| 2446 | # # check if all the the elements are in the chunk | ||
| 2447 | |||
| 2448 | # } | ||
| 2449 | } | ||
| 2450 | warn "syndep not found\n"; | ||
| 2451 | # print "search Concept\n"; | ||
| 2452 | |||
| 2453 | # do { | ||
| 2454 | # my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 2455 | # if (scalar(@words) > 0) { | ||
| 2456 | # $words{$words[0]->getId}++; | ||
| 2457 | # $currentWindowSize = scalar(keys %words); | ||
| 2458 | # } | ||
| 2459 | # foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 2460 | # if ($semfGuessing) { | ||
| 2461 | # if (($term->isTerm) && (!defined($term->getSemanticFeatureFC($document)))) { | ||
| 2462 | # $self->_addSemanticFeature($term, $semtype, $document); | ||
| 2463 | # } | ||
| 2464 | # } else { | ||
| 2465 | # if ($term->isTerm) { | ||
| 2466 | # if ($term->getSemanticFeatureFC($document) eq $semtype) { | ||
| 2467 | # return($term); | ||
| 2468 | # } | ||
| 2469 | # } | ||
| 2470 | # } | ||
| 2471 | # } | ||
| 2472 | # $token = $token->next; | ||
| 2473 | # } while(($currentWindowSize < $WindowSize) && (defined $token) && ($token->getFrom <= $end_token->getTo)); | ||
| 2474 | |||
| 2475 | return(0); | ||
| 2476 | } | ||
| 2477 | |||
| 2478 | sub _searchSemType { | ||
| 2479 | my ($self, $document, $start_token, $end_token, $semtypeAcr, $WindowSize, $semfGuessing) = @_; | ||
| 2480 | |||
| 2481 | my $token = $start_token; | ||
| 2482 | my $term; | ||
| 2483 | my $word; | ||
| 2484 | my $currentWindowSize = 0; | ||
| 2485 | my $semtype = $self->{"SEMANTICTYPES"}->{$semtypeAcr}; | ||
| 2486 | my %words; | ||
| 2487 | |||
| 2488 | # print STDERR "search SemType\n"; | ||
| 2489 | |||
| 2490 | do { | ||
| 2491 | my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 2492 | if (scalar(@words) > 0) { | ||
| 2493 | $words{$words[0]->getId}++; | ||
| 2494 | $currentWindowSize = scalar(keys %words); | ||
| 2495 | } | ||
| 2496 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 2497 | if ($semfGuessing) { | ||
| 2498 | if (($term->isTerm) && (!defined($term->getSemanticFeatureFC($document)))) { | ||
| 2499 | $self->_addSemanticFeature($term, $semtype, $document); | ||
| 2500 | } | ||
| 2501 | } else { | ||
| 2502 | if ($term->isTerm) { | ||
| 2503 | if ($term->getSemanticFeatureFC($document) eq $semtype) { | ||
| 2504 | return($term); | ||
| 2505 | } | ||
| 2506 | } | ||
| 2507 | } | ||
| 2508 | } | ||
| 2509 | $token = $token->next; | ||
| 2510 | } while(($currentWindowSize < $WindowSize) && (defined $token) && ($token->getFrom <= $end_token->getTo)); | ||
| 2511 | |||
| 2512 | return(0); | ||
| 2513 | } | ||
| 2514 | |||
| 2515 | sub _searchTermContent { | ||
| 2516 | my ($self, $document, $start_token, $end_token, $termcontent, $WindowSize) = @_; | ||
| 2517 | |||
| 2518 | my $token = $start_token; | ||
| 2519 | my $term; | ||
| 2520 | my $word; | ||
| 2521 | my $currentWindowSize = 0; | ||
| 2522 | my %words; | ||
| 2523 | |||
| 2524 | # print STDERR "search term content\n"; | ||
| 2525 | |||
| 2526 | do { | ||
| 2527 | my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 2528 | if (scalar(@words) > 0) { | ||
| 2529 | $words{$words[0]->getId}++; | ||
| 2530 | $currentWindowSize = scalar(keys %words); | ||
| 2531 | } | ||
| 2532 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 2533 | if ($term->isTerm) { | ||
| 2534 | if ($term->getForm eq $termcontent) { | ||
| 2535 | return($term); | ||
| 2536 | } | ||
| 2537 | } | ||
| 2538 | } | ||
| 2539 | $token = $token->next; | ||
| 2540 | } while(($currentWindowSize < $WindowSize) && (defined $token) && ($token->getFrom <= $end_token->getTo)); | ||
| 2541 | |||
| 2542 | return(0); | ||
| 2543 | } | ||
| 2544 | |||
| 2545 | sub _searchTerm { | ||
| 2546 | my ($self, $document, $start_token, $end_token, $WindowSize) = @_; | ||
| 2547 | |||
| 2548 | my $token = $start_token; | ||
| 2549 | my $term; | ||
| 2550 | my $word; | ||
| 2551 | my $currentWindowSize = 0; | ||
| 2552 | my %words; | ||
| 2553 | |||
| 2554 | # print STDERR "search Term\n"; | ||
| 2555 | |||
| 2556 | do { | ||
| 2557 | my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 2558 | if (scalar(@words) > 0) { | ||
| 2559 | $words{$words[0]->getId}++; | ||
| 2560 | $currentWindowSize = scalar(keys %words); | ||
| 2561 | } | ||
| 2562 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 2563 | if ($term->isTerm) { | ||
| 2564 | return($term); | ||
| 2565 | } | ||
| 2566 | } | ||
| 2567 | $token = $token->next; | ||
| 2568 | } while(($currentWindowSize < $WindowSize) && (defined $token) && ($token->getFrom <= $end_token->getTo)); | ||
| 2569 | |||
| 2570 | return(0); | ||
| 2571 | } | ||
| 2572 | |||
| 2573 | sub _searchPOSTAG { | ||
| 2574 | my ($self, $document, $start_token, $end_token, $POSTAG, $WindowSize) = @_; | ||
| 2575 | |||
| 2576 | my $token = $start_token; | ||
| 2577 | my $currentWindowSize = 0; | ||
| 2578 | my %words; | ||
| 2579 | |||
| 2580 | # print STDERR "search POSTAG ($POSTAG - $WindowSize)\n"; | ||
| 2581 | do { | ||
| 2582 | # print $token->getContent . "\n"; | ||
| 2583 | my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 2584 | if (scalar(@words) > 0) { | ||
| 2585 | $words{$words[0]->getId}++; | ||
| 2586 | $currentWindowSize = scalar(keys %words); | ||
| 2587 | # print STDERR "word : " . $words[0]->getMorphoSyntacticFeatures($document) . "\n"; | ||
| 2588 | # print STDERR "word : " . $words[0]->getMorphoSyntacticFeatures($document)->syntactic_category . "\n"; | ||
| 2589 | |||
| 2590 | if ($words[0]->getMorphoSyntacticFeatures($document)->syntactic_category eq $POSTAG) { | ||
| 2591 | return($words[0]); | ||
| 2592 | } | ||
| 2593 | } | ||
| 2594 | $token = $token->next; | ||
| 2595 | } while(($currentWindowSize < $WindowSize) && (defined $token) && ($token->getFrom <= $end_token->getTo)); | ||
| 2596 | |||
| 2597 | return(0); | ||
| 2598 | } | ||
| 2599 | |||
| 2600 | sub _searchWordRE { | ||
| 2601 | my ($self, $document, $start_token, $end_token, $wordRegExp, $WindowSize) = @_; | ||
| 2602 | |||
| 2603 | my $sentenceLemma; | ||
| 2604 | my $offset_refs; | ||
| 2605 | # print STDERR "in wordRE: $wordRegExp (" . $start_token->getContent . ")\n"; | ||
| 2606 | |||
| 2607 | my $word_re_start_token = undef; | ||
| 2608 | my $word_re_end_token = undef; | ||
| 2609 | my $currentWindowSize = 0; | ||
| 2610 | my %words; | ||
| 2611 | my $token = $start_token; | ||
| 2612 | |||
| 2613 | do { | ||
| 2614 | my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 2615 | if (scalar(@words) > 0) { | ||
| 2616 | $words{$words[0]->getId}++; | ||
| 2617 | $currentWindowSize = scalar(keys %words); | ||
| 2618 | } | ||
| 2619 | $token = $token->next; | ||
| 2620 | } while(($currentWindowSize < $WindowSize) && (defined $token) && ($token->getFrom <= $end_token->getTo)); | ||
| 2621 | |||
| 2622 | |||
| 2623 | ($sentenceLemma, $offset_refs) = $self->_getLemmatisedSentence($document, $start_token, $token->previous); | ||
| 2624 | |||
| 2625 | # print STDERR $sentenceLemma . ";\n"; | ||
| 2626 | |||
| 2627 | while(($sentenceLemma =~ /$wordRegExp/gcsi) && (!defined $word_re_end_token)){ | ||
| 2628 | # print STDERR "match\n"; | ||
| 2629 | $word_re_start_token = undef; | ||
| 2630 | if (exists $offset_refs->{length($`)}) { | ||
| 2631 | $word_re_start_token = $offset_refs->{length($`)}; | ||
| 2632 | } | ||
| 2633 | $word_re_end_token = undef; | ||
| 2634 | if (exists $offset_refs->{length($` . $&)}) { | ||
| 2635 | $word_re_end_token = $offset_refs->{length($` . $&)}; | ||
| 2636 | $word_re_end_token = $word_re_end_token->previous; | ||
| 2637 | } elsif (exists $offset_refs->{length($` . $&) - 1}) { | ||
| 2638 | $word_re_end_token = $offset_refs->{length($` . $&) - 1}; | ||
| 2639 | $word_re_end_token = $word_re_end_token->previous; | ||
| 2640 | } | ||
| 2641 | |||
| 2642 | } | ||
| 2643 | |||
| 2644 | return($word_re_start_token, $word_re_end_token); | ||
| 2645 | } | ||
| 2646 | sub _searchWord { | ||
| 2647 | my ($self, $document, $start_token, $end_token, $wordForm, $WindowSize) = @_; | ||
| 2648 | |||
| 2649 | my $token = $start_token; | ||
| 2650 | my $currentWindowSize = 0; | ||
| 2651 | my %words; | ||
| 2652 | |||
| 2653 | # print STDERR "search Word\n"; | ||
| 2654 | |||
| 2655 | do { | ||
| 2656 | # print $token->getContent . "\n"; | ||
| 2657 | my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 2658 | if (scalar(@words) > 0) { | ||
| 2659 | $words{$words[0]->getId}++; | ||
| 2660 | $currentWindowSize = scalar(keys %words); | ||
| 2661 | if ((lc($words[0]->getForm) eq lc($wordForm)) || (lc($words[0]->getLemma($document)->canonical_form) eq lc($wordForm))) { | ||
| 2662 | return($words[0]); | ||
| 2663 | } | ||
| 2664 | } | ||
| 2665 | $token = $token->next; | ||
| 2666 | } while(($currentWindowSize < $WindowSize) && (defined $token) && ($token->getFrom <= $end_token->getTo)); | ||
| 2667 | |||
| 2668 | return(0); | ||
| 2669 | } | ||
| 2670 | |||
| 2671 | sub _selectSentencesFromPattern { | ||
| 2672 | my ($self, $pattern, $selectedSentences, $document, $level) = @_; | ||
| 2673 | |||
| 2674 | # print STDERR "===\n"; | ||
| 2675 | # my $pattern = $synPattern->{"pattern"}; | ||
| 2676 | my $i; | ||
| 2677 | my $sentence; | ||
| 2678 | for($i = 0; $i < scalar(@$pattern); $i++) { | ||
| 2679 | # print STDERR "i: $i " . $pattern->[$i]->{'type'} . "\n"; | ||
| 2680 | # foreach $sentence (values %$selectedSentences) { | ||
| 2681 | # print STDERR "sentenceID: " . $sentence->{'sentence'}->getId . "\n"; | ||
| 2682 | # } | ||
| 2683 | # selection of the sentence containing $pattern->[$i] (Semtype) according to %selectedSentences | ||
| 2684 | if ($pattern->[$i]->{'type'} eq "semtype") { | ||
| 2685 | $selectedSentences = $self->_selectSentencesFromSemType($pattern->[$i]->{'semtype'}, $selectedSentences, $i + $level, $document); | ||
| 2686 | } | ||
| 2687 | # selection of the sentence containing $pattern->[$i] (termcontent) according to %selectedSentences | ||
| 2688 | if ($pattern->[$i]->{'type'} eq "termcontent") { | ||
| 2689 | $selectedSentences = $self->_selectSentencesFromTermContent($pattern->[$i]->{'termcontent'}, $selectedSentences, $i + $level, $document); | ||
| 2690 | } | ||
| 2691 | # selection of the sentence containing $pattern->[$i] (term) according to %selectedSentences | ||
| 2692 | if ($pattern->[$i]->{'type'} eq "term") { | ||
| 2693 | $selectedSentences = $self->_selectSentencesFromTerm($selectedSentences, $i + $level, $document); | ||
| 2694 | } | ||
| 2695 | # selection of the sentence containing $pattern->[$i] (postag) according to %selectedSentences | ||
| 2696 | if ($pattern->[$i]->{'type'} eq "postag") { | ||
| 2697 | $selectedSentences = $self->_selectSentencesFromPOSTAG($pattern->[$i]->{'postag'}, $selectedSentences, $i + $level, $document); | ||
| 2698 | } | ||
| 2699 | # selection of the sentence containing $pattern->[$i] (word) according to %selectedSentences | ||
| 2700 | if ($pattern->[$i]->{'type'} eq "word") { | ||
| 2701 | $selectedSentences = $self->_selectSentencesFromWord($pattern->[$i]->{'word'}, $selectedSentences, $i + $level, $document); | ||
| 2702 | } | ||
| 2703 | |||
| 2704 | # selection of the sentence containing $pattern->[$i] (word_re) according to %selectedSentences | ||
| 2705 | if ($pattern->[$i]->{'type'} eq "word_re") { | ||
| 2706 | $selectedSentences = $self->_selectSentencesFromWordRE($pattern->[$i]->{'word_re'}, $selectedSentences, $i + $level, $document); | ||
| 2707 | } | ||
| 2708 | |||
| 2709 | # selection of the sentence containing $pattern->[$i] (syndep) according to %selectedSentences | ||
| 2710 | if ($pattern->[$i]->{'type'} eq "syndep") { | ||
| 2711 | $selectedSentences = $self->_selectSentencesFromSynDep($pattern->[$i]->{'syndep'}, $selectedSentences, $i + $level, $document); | ||
| 2712 | } | ||
| 2713 | # if chunk, go inside the chunk | ||
| 2714 | if ($pattern->[$i]->{'type'} eq "chunk") { | ||
| 2715 | $selectedSentences = $self->_selectSentencesFromPattern($pattern->[$i]->{'chunk'}, $selectedSentences, $document, $level + 1); | ||
| 2716 | } | ||
| 2717 | # foreach $sentence (values %$selectedSentences) { | ||
| 2718 | # print STDERR "sentenceID: " . $sentence->{'sentence'}->getId . "\n"; | ||
| 2719 | # # print STDERR Dumper($sentence->{'first_item'}); | ||
| 2720 | # } | ||
| 2721 | } | ||
| 2722 | # print STDERR "----\n"; | ||
| 2723 | return($selectedSentences); | ||
| 2724 | } | ||
| 2725 | |||
| 2726 | # selection of the sentence containing $pattern->[$i] (Semtype) according to %selectedSentences | ||
| 2727 | |||
| 2728 | sub _selectSentencesFromSemType { | ||
| 2729 | my ($self, $semtypeAcr, $selectedSentences, $i, $document) = @_; | ||
| 2730 | |||
| 2731 | my %newSelectedSentences; | ||
| 2732 | my $term; | ||
| 2733 | my $sentence; | ||
| 2734 | my $semtype = $self->{"SEMANTICTYPES"}->{$semtypeAcr}; | ||
| 2735 | |||
| 2736 | # print STDERR "select semtype: $semtype\n"; | ||
| 2737 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElements}) { | ||
| 2738 | # if ($term->isTerm) { | ||
| 2739 | # print STDERR " " . $term->getForm . "\n"; | ||
| 2740 | # print STDERR "FC: " . $term->getSemanticFeatureFC($document) . "\n"; | ||
| 2741 | # } | ||
| 2742 | if (($term->isTerm) && ((defined($term->getSemanticFeatureFC($document))) && ($term->getSemanticFeatureFC($document) eq $semtype))) { | ||
| 2743 | foreach $sentence (values %$selectedSentences) { | ||
| 2744 | if (($sentence->{'sentence'}->refid_start_token->getFrom <= $term->start_token->getFrom) && | ||
| 2745 | ($term->end_token->getTo <= $sentence->{'sentence'}->refid_end_token->getTo)) { | ||
| 2746 | # print STDERR "==>sentenceID: " . $sentence->{'sentence'}->getId . "$term (" . $term->getForm . ")\n"; | ||
| 2747 | if (($i == 0) || (scalar(@{$sentence->{'first_item'}}) == 0)) { | ||
| 2748 | # if ($i == 0) { | ||
| 2749 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'sentence'} = $sentence->{'sentence'}; | ||
| 2750 | push @{$newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'first_item'}}, $term; | ||
| 2751 | } else { | ||
| 2752 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token} = $sentence; | ||
| 2753 | } | ||
| 2754 | } | ||
| 2755 | } | ||
| 2756 | } | ||
| 2757 | } | ||
| 2758 | $selectedSentences = \%newSelectedSentences; | ||
| 2759 | return(\%newSelectedSentences); | ||
| 2760 | } | ||
| 2761 | |||
| 2762 | # selection of the sentence containing $pattern->[$i] (termcontent) according to %selectedSentences | ||
| 2763 | |||
| 2764 | sub _selectSentencesFromTermContent { | ||
| 2765 | my ($self, $termcontent, $selectedSentences, $i, $document) = @_; | ||
| 2766 | |||
| 2767 | my %newSelectedSentences; | ||
| 2768 | my $term; | ||
| 2769 | my $sentence; | ||
| 2770 | |||
| 2771 | |||
| 2772 | # print STDERR "select termcontent: $termcontent\n"; | ||
| 2773 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElements}) { | ||
| 2774 | # if ($term->isTerm) { | ||
| 2775 | # print "FC: " . $term->getSemanticFeatureFC($document) . "\n"; | ||
| 2776 | # } | ||
| 2777 | if (($term->isTerm) && ($term->getForm eq $termcontent)) { | ||
| 2778 | foreach $sentence (values %$selectedSentences) { | ||
| 2779 | if (($sentence->{'sentence'}->refid_start_token->getFrom <= $term->start_token->getFrom) && | ||
| 2780 | ($term->end_token->getTo <= $sentence->{'sentence'}->refid_end_token->getTo)) { | ||
| 2781 | # print "==>sentenceID: " . $sentence->{'sentence'}->getId . "\n"; | ||
| 2782 | if (($i == 0) || (scalar(@{$sentence->{'first_item'}}) == 0)) { | ||
| 2783 | # if ($i == 0) { | ||
| 2784 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'sentence'} = $sentence->{'sentence'}; | ||
| 2785 | push @{$newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'first_item'}}, $term; | ||
| 2786 | } else { | ||
| 2787 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token} = $sentence; | ||
| 2788 | } | ||
| 2789 | } | ||
| 2790 | } | ||
| 2791 | } | ||
| 2792 | } | ||
| 2793 | $selectedSentences = \%newSelectedSentences; | ||
| 2794 | return(\%newSelectedSentences); | ||
| 2795 | } | ||
| 2796 | |||
| 2797 | # selection of the sentence containing $pattern->[$i] (term) according to %selectedSentences | ||
| 2798 | |||
| 2799 | sub _selectSentencesFromTerm { | ||
| 2800 | my ($self, $selectedSentences, $i, $document) = @_; | ||
| 2801 | |||
| 2802 | my %newSelectedSentences; | ||
| 2803 | my $term; | ||
| 2804 | my $sentence; | ||
| 2805 | |||
| 2806 | # print STDERR "select term exists ?\n"; | ||
| 2807 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElements}) { | ||
| 2808 | # if ($term->isTerm) { | ||
| 2809 | # print "FC: " . $term->getSemanticFeatureFC($document) . "\n"; | ||
| 2810 | # } | ||
| 2811 | if ($term->isTerm) { | ||
| 2812 | foreach $sentence (values %$selectedSentences) { | ||
| 2813 | if (($sentence->{'sentence'}->refid_start_token->getFrom <= $term->start_token->getFrom) && | ||
| 2814 | ($term->end_token->getTo <= $sentence->{'sentence'}->refid_end_token->getTo)) { | ||
| 2815 | # print "==>sentenceID: " . $sentence->{'sentence'}->getId . "\n"; | ||
| 2816 | if (($i == 0) || (scalar(@{$sentence->{'first_item'}}) == 0)) { | ||
| 2817 | # if ($i == 0) { | ||
| 2818 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'sentence'} = $sentence->{'sentence'}; | ||
| 2819 | push @{$newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'first_item'}}, $term; | ||
| 2820 | } else { | ||
| 2821 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token} = $sentence; | ||
| 2822 | } | ||
| 2823 | } | ||
| 2824 | } | ||
| 2825 | } | ||
| 2826 | } | ||
| 2827 | $selectedSentences = \%newSelectedSentences; | ||
| 2828 | return(\%newSelectedSentences); | ||
| 2829 | } | ||
| 2830 | |||
| 2831 | # selection of the sentence containing $pattern->[$i] (postag) according to %selectedSentences | ||
| 2832 | |||
| 2833 | sub _selectSentencesFromPOSTAG { | ||
| 2834 | my ($self, $postag, $selectedSentences, $i, $document) = @_; | ||
| 2835 | |||
| 2836 | my %newSelectedSentences; | ||
| 2837 | my $word; | ||
| 2838 | my $sentence; | ||
| 2839 | |||
| 2840 | # print STDERR "select postag; $postag\n"; | ||
| 2841 | |||
| 2842 | foreach $word (@{$document->getAnnotations->getWordLevel->getElements}) { | ||
| 2843 | if ($word->getMorphoSyntacticFeatures($document)->syntactic_category eq $postag) { | ||
| 2844 | foreach $sentence (values %$selectedSentences) { | ||
| 2845 | if (($sentence->{'sentence'}->refid_start_token->getFrom <= $word->start_token->getFrom) && | ||
| 2846 | ($word->end_token->getTo <= $sentence->{'sentence'}->refid_end_token->getTo)) { | ||
| 2847 | if (($i == 0) || (scalar(@{$sentence->{'first_item'}}) == 0)) { | ||
| 2848 | # if ($i == 0) { | ||
| 2849 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'sentence'} = $sentence->{'sentence'}; | ||
| 2850 | push @{$newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'first_item'}}, $word; | ||
| 2851 | } else { | ||
| 2852 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token} = $sentence; | ||
| 2853 | } | ||
| 2854 | } | ||
| 2855 | } | ||
| 2856 | } | ||
| 2857 | } | ||
| 2858 | $selectedSentences = \%newSelectedSentences; | ||
| 2859 | return(\%newSelectedSentences); | ||
| 2860 | } | ||
| 2861 | |||
| 2862 | # selection of the sentence containing $pattern->[$i] (word) according to %selectedSentences | ||
| 2863 | |||
| 2864 | sub _selectSentencesFromWord { | ||
| 2865 | my ($self, $wordForm, $selectedSentences, $i, $document) = @_; | ||
| 2866 | |||
| 2867 | my %newSelectedSentences; | ||
| 2868 | my $word; | ||
| 2869 | my $sentence; | ||
| 2870 | |||
| 2871 | # print STDERR "select word: $wordForm\n"; | ||
| 2872 | |||
| 2873 | foreach $word (@{$document->getAnnotations->getWordLevel->getElements}) { | ||
| 2874 | if ((lc($word->getForm) eq lc($wordForm)) || (lc($word->getLemma($document)->canonical_form) eq lc($wordForm))) { | ||
| 2875 | # warn "\t> " . $word->getForm . "\n"; | ||
| 2876 | foreach $sentence (values %$selectedSentences) { | ||
| 2877 | # print STDERR "\tsentenceID: " . $sentence->{'sentence'}->getId . "\n"; | ||
| 2878 | if (($sentence->{'sentence'}->refid_start_token->getFrom <= $word->start_token->getFrom) && | ||
| 2879 | ($word->end_token->getTo <= $sentence->{'sentence'}->refid_end_token->getTo)) { | ||
| 2880 | if (($i == 0) || (scalar(@{$sentence->{'first_item'}}) == 0)) { | ||
| 2881 | # if ($i == 0) { | ||
| 2882 | # warn "go there (1)\n"; | ||
| 2883 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'sentence'} = $sentence->{'sentence'}; | ||
| 2884 | push @{$newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'first_item'}}, $word; | ||
| 2885 | } else { | ||
| 2886 | # warn "go there (2)\n"; | ||
| 2887 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token} = $sentence; | ||
| 2888 | } | ||
| 2889 | } | ||
| 2890 | } | ||
| 2891 | } | ||
| 2892 | } | ||
| 2893 | $selectedSentences = \%newSelectedSentences; | ||
| 2894 | return(\%newSelectedSentences); | ||
| 2895 | } | ||
| 2896 | |||
| 2897 | |||
| 2898 | # selection of the sentence containing $pattern->[$i] (word_re) according to %selectedSentences | ||
| 2899 | |||
| 2900 | sub _selectSentencesFromWordRE { | ||
| 2901 | my ($self, $wordRegExp, $selectedSentences, $i, $document) = @_; | ||
| 2902 | |||
| 2903 | my %newSelectedSentences; | ||
| 2904 | my $word; | ||
| 2905 | my $sentence; | ||
| 2906 | my $sentenceLemma; | ||
| 2907 | my $offset_refs; | ||
| 2908 | my $word_re_start_token; | ||
| 2909 | |||
| 2910 | # print STDERR "select word_re: $wordRegExp\n"; | ||
| 2911 | |||
| 2912 | foreach $sentence (values %$selectedSentences) { | ||
| 2913 | # print STDERR "\tsentenceID: " . $sentence->{'sentence'}->getId . "\n"; | ||
| 2914 | ($sentenceLemma, $offset_refs) = $self->_getLemmatisedSentence($document, $sentence->{'sentence'}->refid_start_token, $sentence->{'sentence'}->refid_end_token); | ||
| 2915 | |||
| 2916 | # print STDERR "\t$sentenceLemma\n"; | ||
| 2917 | |||
| 2918 | $word_re_start_token = undef; | ||
| 2919 | while($sentenceLemma =~ /$wordRegExp/gcsi){ | ||
| 2920 | # print STDERR "MATCH\n"; | ||
| 2921 | $word_re_start_token = undef; | ||
| 2922 | if (exists $offset_refs->{length($`)}) { | ||
| 2923 | $word_re_start_token = $offset_refs->{length($`)}; | ||
| 2924 | } | ||
| 2925 | |||
| 2926 | } | ||
| 2927 | if (defined $word_re_start_token) { | ||
| 2928 | $word = $document->getAnnotations->getWordLevel->getElementByToken($word_re_start_token)->[0]; | ||
| 2929 | |||
| 2930 | # if (($sentence->{'sentence'}->refid_start_token->getFrom <= $word->start_token->getFrom) && | ||
| 2931 | # ($word->end_token->getTo <= $sentence->{'sentence'}->refid_end_token->getTo)) { | ||
| 2932 | if (($i == 0) || (scalar(@{$sentence->{'first_item'}}) == 0)) { | ||
| 2933 | # if ($i == 0) { | ||
| 2934 | # warn "go there (1)\n"; | ||
| 2935 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'sentence'} = $sentence->{'sentence'}; | ||
| 2936 | push @{$newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'first_item'}}, $word; | ||
| 2937 | } else { | ||
| 2938 | # warn "go there (2)\n"; | ||
| 2939 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token} = $sentence; | ||
| 2940 | } | ||
| 2941 | # } | ||
| 2942 | } | ||
| 2943 | } | ||
| 2944 | $selectedSentences = \%newSelectedSentences; | ||
| 2945 | return(\%newSelectedSentences); | ||
| 2946 | } | ||
| 2947 | |||
| 2948 | |||
| 2949 | |||
| 2950 | # selection of the sentence containing $pattern->[$i] (syndep) according to %selectedSentences | ||
| 2951 | |||
| 2952 | sub _selectSentencesFromSynDep { | ||
| 2953 | my ($self, $syndep, $selectedSentences, $i, $document) = @_; | ||
| 2954 | |||
| 2955 | my %newSelectedSentences; | ||
| 2956 | my $synrel; | ||
| 2957 | my $sentence; | ||
| 2958 | |||
| 2959 | # print STDERR "select syndep: $syndep\n"; | ||
| 2960 | |||
| 2961 | foreach $synrel (@{$document->getAnnotations->getSyntacticRelationLevel->getElementFromIndex('syntactic_relation_type', $syndep)}) { | ||
| 2962 | # foreach $synrel (@{$document->getAnnotations->getSyntacticRelationLevel->getElements}) { | ||
| 2963 | # if ($synrel->syntactic_relation_type eq $syndep) { | ||
| 2964 | foreach $sentence (values %$selectedSentences) { | ||
| 2965 | if (($sentence->{'sentence'}->refid_start_token->getFrom <= $synrel->refid_head->[0]->start_token->getFrom) && | ||
| 2966 | ($synrel->refid_head->[0]->end_token->getTo <= $sentence->{'sentence'}->refid_end_token->getTo)) { | ||
| 2967 | if (($i == 0) || (scalar(@{$sentence->{'first_item'}}) == 0)) { | ||
| 2968 | # if ($i == 0) { | ||
| 2969 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'sentence'} = $sentence->{'sentence'}; | ||
| 2970 | if ($synrel->refid_head->[0]->start_token->getFrom < $synrel->refid_modifier->[0]->start_token->getFrom) { | ||
| 2971 | push @{$newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'first_item'}}, $synrel->refid_head->[0]; | ||
| 2972 | } else { | ||
| 2973 | push @{$newSelectedSentences{$sentence->{'sentence'}->refid_start_token}->{'first_item'}}, $synrel->refid_modifier->[0]; | ||
| 2974 | } | ||
| 2975 | } else { | ||
| 2976 | $newSelectedSentences{$sentence->{'sentence'}->refid_start_token} = $sentence; | ||
| 2977 | } | ||
| 2978 | } | ||
| 2979 | } | ||
| 2980 | # } | ||
| 2981 | } | ||
| 2982 | $selectedSentences = \%newSelectedSentences; | ||
| 2983 | return(\%newSelectedSentences); | ||
| 2984 | } | ||
| 2985 | |||
| 2986 | |||
| 2987 | sub _parseTreeTaggerFormatOutput { | ||
| 2988 | my ($self, $output_filename, $replace) = @_; | ||
| 2989 | # ($self->_output_filename); | ||
| 2990 | |||
| 2991 | my $line; | ||
| 2992 | my @TreeTaggerOutput; | ||
| 2993 | |||
| 2994 | my $doc_idx; | ||
| 2995 | my $word_idx; | ||
| 2996 | my $document; | ||
| 2997 | |||
| 2998 | my $word; | ||
| 2999 | my $token; | ||
| 3000 | |||
| 3001 | my $posInWord; | ||
| 3002 | my $posInLemma; | ||
| 3003 | |||
| 3004 | my $substringBefore; | ||
| 3005 | my $substringAfter; | ||
| 3006 | |||
| 3007 | my $MSFeatures; | ||
| 3008 | my $Lemma; | ||
| 3009 | |||
| 3010 | warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 3011 | |||
| 3012 | open FILE_OUT, $output_filename or warn "Can't open the file " . $output_filename;; | ||
| 3013 | # binmode(FILE_OUT, ":utf8"); | ||
| 3014 | |||
| 3015 | while($line = <FILE_OUT>) { | ||
| 3016 | chomp $line; | ||
| 3017 | # if ($line ne "") { | ||
| 3018 | # my @tmp = split /[\t\|]/, $line; | ||
| 3019 | my @tmp = split /\t| ?\| ?/, $line; #/ | ||
| 3020 | # warn "$line : " . join(':', @tmp) . "\n"; | ||
| 3021 | if (scalar(@tmp) >= 3) { | ||
| 3022 | if ($tmp[1] ne "DOCUMENT") { | ||
| 3023 | # work around some strange tagging | ||
| 3024 | if (!defined $tmp[1]) { | ||
| 3025 | $tmp[1] = "SYM"; | ||
| 3026 | } | ||
| 3027 | if (!defined $tmp[2]) { | ||
| 3028 | $tmp[2] = $tmp[0]; | ||
| 3029 | } | ||
| 3030 | push @TreeTaggerOutput, \@tmp; | ||
| 3031 | } | ||
| 3032 | } else { | ||
| 3033 | # if (scalar(@tmp) != 0) { | ||
| 3034 | push @TreeTaggerOutput, \@tmp; | ||
| 3035 | # } | ||
| 3036 | } | ||
| 3037 | # } | ||
| 3038 | |||
| 3039 | } | ||
| 3040 | close FILE_OUT; | ||
| 3041 | |||
| 3042 | # warn "TT output size: " . scalar(@TreeTaggerOutput) . "\n"; | ||
| 3043 | |||
| 3044 | # warn "\n\n"; | ||
| 3045 | my $TreeTaggerOutput_idx = 0; | ||
| 3046 | |||
| 3047 | foreach $document (@{$self->_documentSet}) { | ||
| 3048 | # warn $document->getId . "\n"; | ||
| 3049 | for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) { | ||
| 3050 | # if ((!defined $TreeTaggerOutput[$TreeTaggerOutput_idx]) || (scalar(@{$TreeTaggerOutput[$TreeTaggerOutput_idx]}) == 0)) { | ||
| 3051 | # $TreeTaggerOutput_idx++; | ||
| 3052 | # } | ||
| 3053 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 3054 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 3055 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 3056 | |||
| 3057 | # warn $word->getForm . " (" . $word->getId . ") " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] . " : " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] . " : " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] . "\n"; | ||
| 3058 | |||
| 3059 | # Correct POSTag if it's named entity | ||
| 3060 | |||
| 3061 | if ($word->isNE) { | ||
| 3062 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] = "NP"; | ||
| 3063 | # warn "Remove _ if it's a complex word i.e. a named entity\n"; | ||
| 3064 | |||
| 3065 | # Remove _ if it's a complex word i.e. a named entity | ||
| 3066 | |||
| 3067 | $posInLemma = 0; | ||
| 3068 | do { | ||
| 3069 | if ((($posInLemma = index($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], "_"), $posInLemma) != -1) && | ||
| 3070 | ((($posInWord = index($TreeTaggerOutput[$TreeTaggerOutput_idx]->[0], "_", $posInLemma)) == -1) || | ||
| 3071 | ($posInLemma != $posInWord))) { | ||
| 3072 | # warn "lemma to correct : " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] . "\n"; | ||
| 3073 | $substringBefore = substr($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], 0, $posInLemma); | ||
| 3074 | $substringAfter = substr($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], $posInLemma + 1); | ||
| 3075 | |||
| 3076 | # warn "New lemma : $substringBefore $substringAfter ($posInLemma)\n"; | ||
| 3077 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] = "$substringBefore $substringAfter"; | ||
| 3078 | $posInLemma++; | ||
| 3079 | } | ||
| 3080 | } while (($posInLemma != -1) && ($posInLemma != $posInWord) && ($posInLemma < length($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2]))); | ||
| 3081 | } | ||
| 3082 | |||
| 3083 | # work around some strange tagging | ||
| 3084 | # warn "TreeTaggerOutput_idx: $TreeTaggerOutput_idx\n"; | ||
| 3085 | if ((defined $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1]) && ($TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] eq "SENT")) { | ||
| 3086 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] = "NP"; | ||
| 3087 | } | ||
| 3088 | |||
| 3089 | # if (scalar(@{$TreeTaggerOutput[$TreeTaggerOutput_idx]}) >= 0) { | ||
| 3090 | if ($replace == 1) { | ||
| 3091 | # Correction bug | ||
| 3092 | my $ttg_syntactic_category = $word->getMorphoSyntacticFeatures($document)->syntactic_category; | ||
| 3093 | my $ttg_lemma = $word->getLemma($document)->canonical_form; | ||
| 3094 | |||
| 3095 | # if (! defined $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0]) { | ||
| 3096 | # warn "(0) " . join(';', @{$TreeTaggerOutput[$TreeTaggerOutput_idx]}) . " $ttg_lemma\n"; | ||
| 3097 | # } | ||
| 3098 | # if (scalar(@{$TreeTaggerOutput[$TreeTaggerOutput_idx]}) == 0) { | ||
| 3099 | # @{$TreeTaggerOutput[$TreeTaggerOutput_idx]} = ($ttg_lemma, $ttg_syntactic_category, $ttg_lemma); | ||
| 3100 | # } | ||
| 3101 | |||
| 3102 | |||
| 3103 | if ((defined $ttg_syntactic_category) && (defined $ttg_lemma) && | ||
| 3104 | (defined ($TreeTaggerOutput[$TreeTaggerOutput_idx]->[0])) && | ||
| 3105 | ($TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] =~ /^:Vm\-\-/o)) { | ||
| 3106 | # warn "Passe ici\n"; | ||
| 3107 | if ($ttg_syntactic_category eq "ADJ") { | ||
| 3108 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] = "ADJ:A---s--"; | ||
| 3109 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] = $ttg_lemma; | ||
| 3110 | } else { | ||
| 3111 | # warn "et ici\n"; | ||
| 3112 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] = "NOM:Nc-s--"; | ||
| 3113 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] = $ttg_lemma; | ||
| 3114 | } | ||
| 3115 | } | ||
| 3116 | if ((defined $ttg_syntactic_category) && (defined $ttg_lemma)){ | ||
| 3117 | |||
| 3118 | # if (! defined $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1]) { | ||
| 3119 | # warn "(1) " . join(';', @{$TreeTaggerOutput[$TreeTaggerOutput_idx]}) . " $ttg_lemma\n"; | ||
| 3120 | # } | ||
| 3121 | #(scalar(@{$TreeTaggerOutput[$TreeTaggerOutput_idx]}) == 0) || | ||
| 3122 | |||
| 3123 | if ((!defined $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1]) || | ||
| 3124 | ($TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] eq "")) { | ||
| 3125 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] = $ttg_syntactic_category; | ||
| 3126 | # $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] = $FF; | ||
| 3127 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] = $ttg_lemma; | ||
| 3128 | } | ||
| 3129 | # if (! defined $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2]) { | ||
| 3130 | # warn "(2) " . join(';', @{$TreeTaggerOutput[$TreeTaggerOutput_idx]}) . " $ttg_lemma\n"; | ||
| 3131 | # } | ||
| 3132 | if ((!defined $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2]) || ($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] eq "\n")) { | ||
| 3133 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] = $ttg_lemma; | ||
| 3134 | } | ||
| 3135 | } | ||
| 3136 | $word->getMorphoSyntacticFeatures($document)->syntactic_category($TreeTaggerOutput[$TreeTaggerOutput_idx]->[1]); | ||
| 3137 | |||
| 3138 | $word->getLemma($document)->canonical_form($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2]); | ||
| 3139 | |||
| 3140 | } else { | ||
| 3141 | |||
| 3142 | $MSFeatures = Lingua::Ogmios::Annotations::MorphosyntacticFeatures->new( | ||
| 3143 | {'refid_word' => $word, | ||
| 3144 | 'syntactic_category' => $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1], | ||
| 3145 | }); | ||
| 3146 | $document->getAnnotations->addMorphosyntacticFeatures($MSFeatures); | ||
| 3147 | |||
| 3148 | $Lemma = Lingua::Ogmios::Annotations::Lemma->new( | ||
| 3149 | {'refid_word' => $word, | ||
| 3150 | 'canonical_form' => $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], | ||
| 3151 | }); | ||
| 3152 | $document->getAnnotations->addLemma($Lemma); | ||
| 3153 | } | ||
| 3154 | $doc_idx += $word->getReferenceSize - 1; | ||
| 3155 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 3156 | # } | ||
| 3157 | $TreeTaggerOutput_idx++; | ||
| 3158 | |||
| 3159 | } else { | ||
| 3160 | if ((!($token->isSep)) && ($token->getContent !~ /[\x{2019}\x{2032}']/go)) { | ||
| 3161 | # if (!($token->isSep)) { | ||
| 3162 | $TreeTaggerOutput_idx++; | ||
| 3163 | # warn $token->getContent . " (" . $token->getId . ") " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] . "\n"; | ||
| 3164 | } | ||
| 3165 | } | ||
| 3166 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 3167 | if ($token->isSymb) { | ||
| 3168 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 3169 | |||
| 3170 | # } else { | ||
| 3171 | $TreeTaggerOutput_idx++; | ||
| 3172 | # warn $token->getContent . " (" . $token->getId . ") " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] . " 0 \n"; | ||
| 3173 | } | ||
| 3174 | } else { | ||
| 3175 | $TreeTaggerOutput_idx++; | ||
| 3176 | # warn ". ( 000 ) " . $TreeTaggerOutput[$TreeTaggerOutput_idx++]->[0] . "\n"; | ||
| 3177 | } | ||
| 3178 | } | ||
| 3179 | # if (scalar(@{$TreeTaggerOutput[$TreeTaggerOutput_idx]}) == 0) { | ||
| 3180 | # $TreeTaggerOutput_idx++; | ||
| 3181 | # } | ||
| 3182 | |||
| 3183 | } | ||
| 3184 | } | ||
| 3185 | |||
| 3186 | } | ||
| 3187 | |||
| 3188 | sub propagateSemanticFeatures { | ||
| 3189 | my ($self) = @_; | ||
| 3190 | |||
| 3191 | my $document; | ||
| 3192 | my $syntactic_relation; | ||
| 3193 | my $head_term; | ||
| 3194 | my $term; | ||
| 3195 | |||
| 3196 | warn "[LOG] propagateSemanticFeatures\n"; | ||
| 3197 | |||
| 3198 | foreach $document (@{$self->_documentSet}) { | ||
| 3199 | foreach $syntactic_relation (@{$document->getAnnotations->getSyntacticRelationLevel->getElements}) { | ||
| 3200 | # warn $syntactic_relation->getId . "\n"; | ||
| 3201 | # warn $syntactic_relation->syntactic_relation_type . "\n"; | ||
| 3202 | if ($syntactic_relation->syntactic_relation_type eq "Head_of") { | ||
| 3203 | # warn "\t" . $syntactic_relation->refid_head->[0] . "\n"; | ||
| 3204 | # warn "\t" . $syntactic_relation->reference_name_head . "\n"; | ||
| 3205 | $head_term = $document->getAnnotations->getSemanticUnitLevel->getElementFromIndex($syntactic_relation->reference_name_head, $syntactic_relation->refid_head->[0]->getId)->[0]; | ||
| 3206 | # warn "===\n"; | ||
| 3207 | # warn "\t" . $syntactic_relation->refid_modifier->[0] . "\n"; | ||
| 3208 | # warn "\t" . $syntactic_relation->reference_name_modifier . "\n"; | ||
| 3209 | $term = $document->getAnnotations->getSemanticUnitLevel->getElementFromIndex($syntactic_relation->reference_name_modifier, $syntactic_relation->refid_modifier->[0]->getId)->[0]; | ||
| 3210 | # warn "===\n"; | ||
| 3211 | |||
| 3212 | # warn "head = " . $head_term->getForm . "\n"; | ||
| 3213 | # warn "head = " . $head_term->getId . "\n"; | ||
| 3214 | # warn "term = " . $term->getForm . "\n"; | ||
| 3215 | # warn "term = " . $term->getId . "\n"; | ||
| 3216 | if (($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $head_term->getId)) && | ||
| 3217 | (!($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $term->getId)))) { | ||
| 3218 | # $document->getAnnotations->getSemanticFeaturesLevel->addSemanticFeature | ||
| 3219 | # warn "head = " . $head_term->getId . "\n"; | ||
| 3220 | # warn "term = " . $term->getId . "\n"; | ||
| 3221 | # warn "semf = " . $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $head_term->getId)->[0]->getId . "\n"; | ||
| 3222 | # warn "add a semantic Feature to the term " . $term->getId . "(" . $term->getForm . ")\n"; | ||
| 3223 | $self->_addSemanticFeature($term, $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $head_term->getId)->[0]->semantic_category, $document); | ||
| 3224 | # _addSemanticFeature { | ||
| 3225 | # my ($self, $termUnit, $semtag, $document) = @_; | ||
| 3226 | } | ||
| 3227 | } | ||
| 3228 | } | ||
| 3229 | } | ||
| 3230 | |||
| 3231 | warn "done\n"; | ||
| 3232 | } | ||
| 3233 | |||
| 3234 | sub _addRelation { | ||
| 3235 | # my $self; | ||
| 3236 | |||
| 3237 | # warn join(" : ", (caller(1))) . "\n"; | ||
| 3238 | |||
| 3239 | # warn $_[0] . " : " . __PACKAGE__ . " : " . ref($_[0]) . "\n"; | ||
| 3240 | |||
| 3241 | # if (index($_[0], __PACKAGE__) == 0){ | ||
| 3242 | # $self = shift; | ||
| 3243 | # } | ||
| 3244 | my ($self, $document, $reltype, $list_refid) = @_; | ||
| 3245 | |||
| 3246 | my $domainSpecificRelation; | ||
| 3247 | my @tmpTerms; | ||
| 3248 | my $i; | ||
| 3249 | my $rel; | ||
| 3250 | my $rel1; | ||
| 3251 | my $rel2; | ||
| 3252 | my %hrel1; | ||
| 3253 | |||
| 3254 | push @tmpTerms, @$list_refid; | ||
| 3255 | |||
| 3256 | # warn $tmpTerms[0]->getForm . "\n"; | ||
| 3257 | # warn $tmpTerms[1]->getForm . "\n"; | ||
| 3258 | |||
| 3259 | $i = 0; | ||
| 3260 | if (!$tmpTerms[0]->equals($tmpTerms[1])) { | ||
| 3261 | if ((defined $document->getAnnotations->getDomainSpecificRelationLevel->getElementFromIndex("list_refid_semantic_unit", $tmpTerms[0]->getId)) && | ||
| 3262 | (defined $document->getAnnotations->getDomainSpecificRelationLevel->getElementFromIndex("list_refid_semantic_unit", $tmpTerms[1]->getId))){ | ||
| 3263 | $rel1 = $document->getAnnotations->getDomainSpecificRelationLevel->getElementFromIndex("list_refid_semantic_unit", $tmpTerms[0]->getId); | ||
| 3264 | $rel2 = $document->getAnnotations->getDomainSpecificRelationLevel->getElementFromIndex("list_refid_semantic_unit", $tmpTerms[1]->getId); | ||
| 3265 | |||
| 3266 | foreach $rel (@$rel1) { | ||
| 3267 | $hrel1{$rel} = 0; | ||
| 3268 | } | ||
| 3269 | $i = 0; | ||
| 3270 | while(($i < scalar(@$rel2)) && (!exists($hrel1{$rel2->[$i]}))) { | ||
| 3271 | $i++; | ||
| 3272 | } | ||
| 3273 | } | ||
| 3274 | if (((!defined $rel2) || ($i == scalar(@$rel2))) ) { | ||
| 3275 | # warn "add a relation\n"; | ||
| 3276 | $domainSpecificRelation = Lingua::Ogmios::Annotations::DomainSpecificRelation->new( | ||
| 3277 | {'domain_specific_relation_type' => $reltype, | ||
| 3278 | 'list_refid_semantic_unit' => \@tmpTerms, | ||
| 3279 | }); | ||
| 3280 | $document->getAnnotations->addDomainSpecificRelation($domainSpecificRelation); | ||
| 3281 | return($domainSpecificRelation); | ||
| 3282 | } else { | ||
| 3283 | warn "Relationalready exists\n"; | ||
| 3284 | return(undef); | ||
| 3285 | } | ||
| 3286 | } else { | ||
| 3287 | warn "term1 == term2\n"; | ||
| 3288 | return(undef); | ||
| 3289 | } | ||
| 3290 | } | ||
| 3291 | |||
| 3292 | sub getTimer { | ||
| 3293 | my ($self) = @_; | ||
| 3294 | |||
| 3295 | return($self->{'timer'}); | ||
| 3296 | } | ||
| 3297 | |||
| 3298 | sub currentDocument { | ||
| 3299 | my $self = shift; | ||
| 3300 | |||
| 3301 | $self->{'currentDocument'} = shift if @_; | ||
| 3302 | return $self->{'currentDocument'}; | ||
| 3303 | |||
| 3304 | } | ||
| 3305 | |||
| 3306 | sub _loadStopWords { | ||
| 3307 | my ($self, $filename, $lang, $name) = @_; | ||
| 3308 | my %resource; | ||
| 3309 | |||
| 3310 | if (defined $self->{$name}) { | ||
| 3311 | %resource = %{$self->{$name}->{$lang}}; | ||
| 3312 | } | ||
| 3313 | |||
| 3314 | my $line; | ||
| 3315 | if (!(open FILE, $filename)) { | ||
| 3316 | warn "No such file " . $filename . "\n"; | ||
| 3317 | return(-1); | ||
| 3318 | } | ||
| 3319 | while ($line = <FILE>) { | ||
| 3320 | chomp $line; | ||
| 3321 | if (($line !~ /^\s*$/o) && ($line !~ /^\s*\#$/o)){ | ||
| 3322 | $resource{$line} = 0; | ||
| 3323 | } | ||
| 3324 | |||
| 3325 | } | ||
| 3326 | close FILE; | ||
| 3327 | |||
| 3328 | $self->{$name}->{$lang} = \%resource; | ||
| 3329 | } | ||
| 3330 | |||
| 3331 | sub _loadList { | ||
| 3332 | my ($self, $filename, $lang, $name) = @_; | ||
| 3333 | my @resource; | ||
| 3334 | |||
| 3335 | if (defined $self->{$name}) { | ||
| 3336 | @resource = @{$self->{$name}->{$lang}}; | ||
| 3337 | } | ||
| 3338 | |||
| 3339 | my $line; | ||
| 3340 | if (!(open FILE, $filename)) { | ||
| 3341 | warn "No such file " . $filename . "\n"; | ||
| 3342 | return(-1); | ||
| 3343 | } | ||
| 3344 | while ($line = <FILE>) { | ||
| 3345 | chomp $line; | ||
| 3346 | if (($line !~ /^\s*$/o) && ($line !~ /^\s*\#$/o)){ | ||
| 3347 | push @resource, $line; | ||
| 3348 | } | ||
| 3349 | |||
| 3350 | } | ||
| 3351 | close FILE; | ||
| 3352 | |||
| 3353 | $self->{$name}->{$lang} = \@resource; | ||
| 3354 | } | ||
| 3355 | |||
| 3356 | |||
| 3357 | sub _setOption { | ||
| 3358 | my ($self, $lang2, $optionRC, $option, $defaultValue, $defaultType) = @_; | ||
| 3359 | my $item; | ||
| 3360 | |||
| 3361 | # warn "==> $lang2\n"; | ||
| 3362 | my $lang = "language=" . uc($lang2); | ||
| 3363 | |||
| 3364 | |||
| 3365 | if (defined $self->_config->configuration->{'CONFIG'}->{$lang}->{$optionRC}) { | ||
| 3366 | |||
| 3367 | # warn $self->_config->configuration->{'CONFIG'}->{$lang}->{$optionRC} . "\n"; | ||
| 3368 | if (ref($self->_config->configuration->{'CONFIG'}->{$lang}->{$optionRC}) eq "ARRAY") { | ||
| 3369 | $self->{$option}->{$lang2} = {}; | ||
| 3370 | foreach $item (@{$self->_config->configuration->{'CONFIG'}->{$lang}->{$optionRC}}) { | ||
| 3371 | $self->{$option}->{$lang2}->{$item}++; | ||
| 3372 | } | ||
| 3373 | } else { | ||
| 3374 | if ((defined $defaultType) && ($defaultType eq "HASH")) { | ||
| 3375 | $self->{$option}->{$lang2}->{$self->_config->configuration->{'CONFIG'}->{$lang}->{$optionRC}}++; | ||
| 3376 | } else { | ||
| 3377 | # warn "ok ($optionRC)\n"; | ||
| 3378 | $self->{$option}->{$lang2} = $self->_config->configuration->{'CONFIG'}->{$lang}->{$optionRC}; | ||
| 3379 | } | ||
| 3380 | } | ||
| 3381 | } else { | ||
| 3382 | $self->{$option}->{$lang2} = $defaultValue; | ||
| 3383 | } | ||
| 3384 | } | ||
| 3385 | |||
| 3386 | sub _setGeneralOption { | ||
| 3387 | my ($self, $optionRC, $option, $defaultValue, $defaultType) = @_; | ||
| 3388 | my $item; | ||
| 3389 | |||
| 3390 | # warn "==> $lang2\n"; | ||
| 3391 | |||
| 3392 | |||
| 3393 | if (defined $self->_config->configuration->{'CONFIG'}->{$optionRC}) { | ||
| 3394 | |||
| 3395 | # warn $self->_config->configuration->{'CONFIG'}->{$lang}->{$optionRC} . "\n"; | ||
| 3396 | if (ref($self->_config->configuration->{'CONFIG'}->{$optionRC}) eq "ARRAY") { | ||
| 3397 | $self->{$option} = {}; | ||
| 3398 | foreach $item (@{$self->_config->configuration->{'CONFIG'}->{$optionRC}}) { | ||
| 3399 | $self->{$option}->{$item}++; | ||
| 3400 | } | ||
| 3401 | } else { | ||
| 3402 | if ((defined $defaultType) && ($defaultType eq "HASH")) { | ||
| 3403 | $self->{$option}->{$self->_config->configuration->{'CONFIG'}->{$optionRC}}++; | ||
| 3404 | } else { | ||
| 3405 | # warn "ok ($optionRC)\n"; | ||
| 3406 | $self->{$option} = $self->_config->configuration->{'CONFIG'}->{$optionRC}; | ||
| 3407 | } | ||
| 3408 | } | ||
| 3409 | } else { | ||
| 3410 | $self->{$option} = $defaultValue; | ||
| 3411 | } | ||
| 3412 | } | ||
| 3413 | |||
| 3414 | sub _getCanonicalForm { | ||
| 3415 | my ($self, $term, $document) = @_; | ||
| 3416 | |||
| 3417 | my $canForm; | ||
| 3418 | my $elmt; | ||
| 3419 | |||
| 3420 | # warn ">> " . $term->getForm . "\n"; | ||
| 3421 | if (!defined $term->canonical_form) { | ||
| 3422 | if ($term->reference_name eq "refid_word") { | ||
| 3423 | $canForm = $term->reference->getLemma($document)->canonical_form; | ||
| 3424 | } elsif ($term->reference_name eq "refid_phrase") { | ||
| 3425 | $canForm = ""; | ||
| 3426 | foreach $elmt ($term->reference->getElementList) { | ||
| 3427 | # warn "$elmt\n"; | ||
| 3428 | if (ref($elmt) eq "Lingua::Ogmios::Annotations::Word") { | ||
| 3429 | $canForm .= $elmt->getLemma($document)->canonical_form . " "; | ||
| 3430 | } else { | ||
| 3431 | $canForm .= $elmt->getContent; | ||
| 3432 | } | ||
| 3433 | } | ||
| 3434 | $canForm =~ s/ +$//go; | ||
| 3435 | } else { | ||
| 3436 | foreach $elmt (@{$term->reference}) { | ||
| 3437 | $canForm .= $elmt->getContent; | ||
| 3438 | } | ||
| 3439 | } | ||
| 3440 | } else { | ||
| 3441 | $canForm = $term->canonical_form; | ||
| 3442 | # warn "===> $canForm\n"; | ||
| 3443 | } | ||
| 3444 | # warn ">> " . $term->getForm . ": $canForm\n"; | ||
| 3445 | return($canForm); | ||
| 3446 | } | ||
| 3447 | |||
| 3448 | sub _getSyntacticCategory { | ||
| 3449 | my ($self, $term, $document) = @_; | ||
| 3450 | |||
| 3451 | my $syntCat; | ||
| 3452 | my $elmt; | ||
| 3453 | |||
| 3454 | if ($term->reference_name eq "refid_word") { | ||
| 3455 | $syntCat = $term->reference->getMorphoSyntacticFeatures($document)->syntactic_category; | ||
| 3456 | } elsif ($term->reference_name eq "refid_phrase") { | ||
| 3457 | $syntCat = ""; | ||
| 3458 | foreach $elmt ($term->reference->getElementList) { | ||
| 3459 | if (ref($elmt) eq "Lingua::Ogmios::Annotations::Word") { | ||
| 3460 | $syntCat .= $elmt->getMorphoSyntacticFeatures($document)->syntactic_category . " "; | ||
| 3461 | } else { | ||
| 3462 | $syntCat .= $elmt->getContent; | ||
| 3463 | } | ||
| 3464 | } | ||
| 3465 | $syntCat =~ s/ +$//go; | ||
| 3466 | } else { | ||
| 3467 | foreach $elmt (@{$term->reference}) { | ||
| 3468 | $syntCat .= $elmt->getContent; | ||
| 3469 | } | ||
| 3470 | } | ||
| 3471 | return($syntCat); | ||
| 3472 | } | ||
| 3473 | |||
| 3474 | sub _printCRFInput { | ||
| 3475 | my ($self, $fh, $fields) = @_; | ||
| 3476 | |||
| 3477 | $self->_makeTaggedSentencesCRF("", $fh, "\t", "\n", $fields); | ||
| 3478 | } | ||
| 3479 | |||
| 3480 | sub _getValidation { | ||
| 3481 | my ($self, $SemUnit) = @_; | ||
| 3482 | |||
| 3483 | return('-'); | ||
| 3484 | |||
| 3485 | } | ||
| 3486 | sub _makeTaggedSentencesCRF { | ||
| 3487 | my ($self, $sentences, $fh, $sepInfo, $sepUnit, $fields) = @_; | ||
| 3488 | |||
| 3489 | if (!defined $fh) { | ||
| 3490 | $fh = *STDOUT; | ||
| 3491 | } | ||
| 3492 | |||
| 3493 | my $document; | ||
| 3494 | my $doc_idx; | ||
| 3495 | my $token; | ||
| 3496 | my $word; | ||
| 3497 | my $lemma; | ||
| 3498 | my $MS_features; | ||
| 3499 | my $SemUnit; | ||
| 3500 | my @corpus_in_t; | ||
| 3501 | my $canonical_form; | ||
| 3502 | my $taggedSentence; | ||
| 3503 | my $i; | ||
| 3504 | my $semf; | ||
| 3505 | my $wordform; | ||
| 3506 | my $lang; | ||
| 3507 | my $lineNb; | ||
| 3508 | # my $validatedTerms; | ||
| 3509 | my $terms; | ||
| 3510 | my $numberOfWeights; | ||
| 3511 | my $validation = 0; | ||
| 3512 | my @weights; | ||
| 3513 | my @emptyWeights; | ||
| 3514 | my $sortedFields = [sort {$fields->{$a}->{'ORDER'} <=> $fields->{$b}->{'ORDER'}} keys(%$fields)]; | ||
| 3515 | my $weight; | ||
| 3516 | |||
| 3517 | if (exists $fields->{'WEIGHTS'}) { | ||
| 3518 | # Get number of weigts | ||
| 3519 | # my @terms = @{$self->_documentSet->[0]->getAnnotations->getSemanticUnitLevel->getElements}; | ||
| 3520 | # $i = 0; | ||
| 3521 | # while(($i < scalar(@terms)) && (!$terms[$i]->isTerm)) { $i++; }; | ||
| 3522 | # if ($i < scalar(@terms)) { | ||
| 3523 | # $numberOfWeights = $terms[$i]->numberOfWeights; | ||
| 3524 | # } else { | ||
| 3525 | # $numberOfWeights = 0; | ||
| 3526 | # } | ||
| 3527 | $numberOfWeights = scalar(@{$fields->{'WEIGHTS'}->{'LIST'}}); | ||
| 3528 | warn "Number of weights: $numberOfWeights\n"; | ||
| 3529 | # warn " List of weights: " . join(':', keys(%{$terms[$i]->weights})) . "\n"; | ||
| 3530 | for($i=0;$i < $numberOfWeights; $i++) { | ||
| 3531 | $emptyWeights[$i] = -1; | ||
| 3532 | } | ||
| 3533 | } | ||
| 3534 | |||
| 3535 | $self->getTimer->startsLap("making tagged sentences"); | ||
| 3536 | warn "[LOG] printing Tagged Sentences\n"; | ||
| 3537 | |||
| 3538 | # warn "open " . $self->_input_filename . "(1)\n"; | ||
| 3539 | |||
| 3540 | # open FILEINPUT, ">:utf8", $self->_input_filename or die "No such file " . $self->_input_filename; | ||
| 3541 | |||
| 3542 | for ($i = 0; $i < scalar(@{$self->_documentSet}); $i++) { | ||
| 3543 | $self->getTimer->startsLapByCategory('document'); | ||
| 3544 | |||
| 3545 | $document = $self->_documentSet->[$i]; | ||
| 3546 | $lang = $document->getAnnotations->getLanguage; | ||
| 3547 | |||
| 3548 | # if ((exists $fields->{"VALIDATION"}) && (defined $keywordProperty)) { | ||
| 3549 | # # if (!defined $validatedTerms) { | ||
| 3550 | # $keywords = $self->_getCorrectKeywords($document, $keywordProperty); | ||
| 3551 | # # } | ||
| 3552 | # } | ||
| 3553 | # warn "$keywordProperty: " . join(":", %$validatedTerms) . "\n"; | ||
| 3554 | |||
| 3555 | # if (defined $termField) { | ||
| 3556 | |||
| 3557 | # } | ||
| 3558 | |||
| 3559 | $self->getTimer->startsLapByCategory('token'); | ||
| 3560 | |||
| 3561 | $token = $document->getAnnotations->getTokenLevel->getElements->[0]; | ||
| 3562 | |||
| 3563 | $self->getTimer->endsLapByCategory('token'); | ||
| 3564 | while(defined ($token)) { | ||
| 3565 | $self->getTimer->startsLapByCategory('token2'); | ||
| 3566 | $terms = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token); | ||
| 3567 | |||
| 3568 | if (scalar(@$terms) > 0) { | ||
| 3569 | $self->getTimer->startsLapByCategory('semUnit'); | ||
| 3570 | |||
| 3571 | $self->getTimer->startsLapByCategory('LargerSemUnit'); | ||
| 3572 | $SemUnit = $self->_getLargerTerm($terms); | ||
| 3573 | $self->getTimer->endsLapByCategory('LargerSemUnit'); | ||
| 3574 | |||
| 3575 | # warn "OK\n"; | ||
| 3576 | if ($SemUnit->isNamedEntity) { | ||
| 3577 | $canonical_form = $self->_getCanonicalForm($SemUnit, $document); | ||
| 3578 | # if (!defined $SemUnit->canonical_form) { | ||
| 3579 | # $canonical_form = ""; | ||
| 3580 | # } else { | ||
| 3581 | # $canonical_form = $SemUnit->canonical_form; | ||
| 3582 | # } | ||
| 3583 | @weights = (); | ||
| 3584 | foreach $weight (@{$fields->{'WEIGHTS'}->{'LIST'}}) { | ||
| 3585 | if ($weight eq "position") { | ||
| 3586 | push @weights, $SemUnit->start_token->getFrom; | ||
| 3587 | } elsif ($weight eq "wordLength") { | ||
| 3588 | push @weights, $SemUnit->getReferenceWordSize; | ||
| 3589 | } else { | ||
| 3590 | push @weights, -1; | ||
| 3591 | } | ||
| 3592 | } | ||
| 3593 | |||
| 3594 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 3595 | "ID" => $SemUnit->getId, | ||
| 3596 | "POSTAG" => "named_entity", | ||
| 3597 | "LM" => $canonical_form, | ||
| 3598 | "SEMTAG" => $SemUnit->NEtype, | ||
| 3599 | "DOCID" => $document->getId, | ||
| 3600 | "WEIGHTS" => [@weights], | ||
| 3601 | "VALIDATION" => "-", | ||
| 3602 | }, $sortedFields); | ||
| 3603 | |||
| 3604 | } elsif ($SemUnit->isTerm) { | ||
| 3605 | $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $SemUnit->getId)->[0]; | ||
| 3606 | # warn "OK2"; | ||
| 3607 | if (defined $semf) { | ||
| 3608 | # warn "OK2b"; | ||
| 3609 | $canonical_form = $self->_getCanonicalForm($SemUnit, $document); | ||
| 3610 | # if (!defined $SemUnit->canonical_form) { | ||
| 3611 | # $canonical_form = ""; | ||
| 3612 | # } else { | ||
| 3613 | # $canonical_form = $SemUnit->canonical_form; | ||
| 3614 | # } | ||
| 3615 | $validation = $self->_getValidation($SemUnit, $document); | ||
| 3616 | my $weight; | ||
| 3617 | @weights = (); | ||
| 3618 | foreach $weight (@{$fields->{'WEIGHTS'}->{'LIST'}}) { | ||
| 3619 | # warn $weight . "\n"; | ||
| 3620 | if ($SemUnit->existsWeight($weight)) { | ||
| 3621 | push @weights, $SemUnit->weight($weight); | ||
| 3622 | } elsif ($weight eq "position") { | ||
| 3623 | push @weights, $SemUnit->start_token->getFrom; | ||
| 3624 | } elsif ($weight eq "wordLength") { | ||
| 3625 | push @weights, $SemUnit->getReferenceWordSize; | ||
| 3626 | } else { | ||
| 3627 | push @weights, 0; | ||
| 3628 | } | ||
| 3629 | |||
| 3630 | } | ||
| 3631 | |||
| 3632 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 3633 | "ID" => $SemUnit->getId, | ||
| 3634 | "POSTAG" => "term", | ||
| 3635 | "LM" => $canonical_form, | ||
| 3636 | "SEMTAG" => $semf->first_node_first_semantic_category, | ||
| 3637 | "DOCID" => $document->getId, | ||
| 3638 | "WEIGHTS" => [@weights], | ||
| 3639 | "VALIDATION" => $validation, | ||
| 3640 | }, $sortedFields); | ||
| 3641 | $self->{'ML_termlist'}->{$lineNb} = [$i, $SemUnit]; | ||
| 3642 | # warn "$lineNb: " . $SemUnit->getForm . "\n"; | ||
| 3643 | } else { | ||
| 3644 | # warn "OK2c"; | ||
| 3645 | $canonical_form = $self->_getCanonicalForm($SemUnit, $document); | ||
| 3646 | # if (!defined $SemUnit->canonical_form) { | ||
| 3647 | # $canonical_form = ""; | ||
| 3648 | # } else { | ||
| 3649 | # $canonical_form = $SemUnit->canonical_form; | ||
| 3650 | # } | ||
| 3651 | # warn "OK\n"; | ||
| 3652 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 3653 | "ID" => $SemUnit->getId, | ||
| 3654 | "POSTAG" => "term", | ||
| 3655 | "LM" => $canonical_form, | ||
| 3656 | "SEMTAG" => "NOSEMTAG", | ||
| 3657 | "DOCID" => $document->getId, | ||
| 3658 | "WEIGHTS" => [@emptyWeights], | ||
| 3659 | "VALIDATION" => "-", | ||
| 3660 | }, $sortedFields); | ||
| 3661 | } | ||
| 3662 | } | ||
| 3663 | $token = $SemUnit->end_token; | ||
| 3664 | $self->getTimer->endsLapByCategory('semUnit'); | ||
| 3665 | } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 3666 | $self->getTimer->startsLapByCategory('word'); | ||
| 3667 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 3668 | $lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 3669 | $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 3670 | $wordform = $word->getForm; | ||
| 3671 | $wordform =~ s/[\t\n]/ /gos; | ||
| 3672 | $wordform =~ s/ +/ /gos; | ||
| 3673 | |||
| 3674 | @weights = (); | ||
| 3675 | foreach $weight (@{$fields->{'WEIGHTS'}->{'LIST'}}) { | ||
| 3676 | if ($weight eq "position") { | ||
| 3677 | push @weights, $word->start_token->getFrom; | ||
| 3678 | } elsif ($weight eq "wordLength") { | ||
| 3679 | push @weights, $word->getReferenceSize; | ||
| 3680 | } else { | ||
| 3681 | push @weights, -1; | ||
| 3682 | } | ||
| 3683 | } | ||
| 3684 | |||
| 3685 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $wordform, | ||
| 3686 | "ID" => $word->getId, | ||
| 3687 | "POSTAG" => $MS_features->syntactic_category, | ||
| 3688 | "LM" => $lemma->canonical_form, | ||
| 3689 | "SEMTAG" => "NOSEMTAG", | ||
| 3690 | "DOCID" => $document->getId, | ||
| 3691 | "WEIGHTS" => [@weights], | ||
| 3692 | "VALIDATION" => "-", | ||
| 3693 | }, $sortedFields); | ||
| 3694 | |||
| 3695 | $token = $word->end_token; | ||
| 3696 | |||
| 3697 | $self->getTimer->endsLapByCategory('word'); | ||
| 3698 | } else { | ||
| 3699 | $self->getTimer->startsLapByCategory('separator'); | ||
| 3700 | if ((!($token->isSep)) && (!($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)))){ | ||
| 3701 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $token->getContent, | ||
| 3702 | "ID" => $token->getId, | ||
| 3703 | "POSTAG" => $token->getContent, | ||
| 3704 | "LM" => $token->getContent, | ||
| 3705 | "SEMTAG" => "NOSEMTAG", | ||
| 3706 | "DOCID" => $document->getId, | ||
| 3707 | "WEIGHTS" => [@emptyWeights], | ||
| 3708 | "VALIDATION" => "-", | ||
| 3709 | }, $sortedFields); | ||
| 3710 | } | ||
| 3711 | $self->getTimer->endsLapByCategory('separator'); | ||
| 3712 | } | ||
| 3713 | $self->getTimer->startsLapByCategory('end sentence'); | ||
| 3714 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 3715 | if ($token->isSymb) { | ||
| 3716 | if (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) { | ||
| 3717 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $token->getContent, | ||
| 3718 | "ID" => $token->getId, | ||
| 3719 | "POSTAG" => "SENT", | ||
| 3720 | "LM" => $token->getContent, | ||
| 3721 | "SEMTAG" => "NOSEMTAG", | ||
| 3722 | "DOCID" => $document->getId, | ||
| 3723 | "WEIGHTS" => [@emptyWeights], | ||
| 3724 | "VALIDATION" => "-", | ||
| 3725 | }, $sortedFields); | ||
| 3726 | $self->_printTaggedSentence($fh, \@corpus_in_t, $sepInfo, $sepUnit); | ||
| 3727 | @corpus_in_t = (); | ||
| 3728 | } else { | ||
| 3729 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $token->getContent, | ||
| 3730 | "ID" => $token->getId, | ||
| 3731 | "POSTAG" => "SENT", | ||
| 3732 | "LM" => $token->getContent, | ||
| 3733 | "SEMTAG" => "NOSEMTAG", | ||
| 3734 | "DOCID" => $document->getId, | ||
| 3735 | "WEIGHTS" => [@emptyWeights], | ||
| 3736 | "VALIDATION" => "-", | ||
| 3737 | }, $sortedFields); | ||
| 3738 | $self->_printTaggedSentence($fh, \@corpus_in_t, $sepInfo, $sepUnit); | ||
| 3739 | @corpus_in_t = (); | ||
| 3740 | } | ||
| 3741 | } else { | ||
| 3742 | # my @tmp = (".", $token->getId, "SENT", "."); | ||
| 3743 | # push @corpus_in_t, \@tmp; | ||
| 3744 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => ".", | ||
| 3745 | "ID" => $token->getId, | ||
| 3746 | "POSTAG" => "SENT", | ||
| 3747 | "LM" => ".", | ||
| 3748 | "SEMTAG" => "NOSEMTAG", | ||
| 3749 | "DOCID" => $document->getId, | ||
| 3750 | "WEIGHTS" => [@emptyWeights], | ||
| 3751 | "VALIDATION" => "-", | ||
| 3752 | }, $sortedFields); | ||
| 3753 | $self->_printTaggedSentence($fh, \@corpus_in_t, $sepInfo, $sepUnit); | ||
| 3754 | @corpus_in_t = (); | ||
| 3755 | } | ||
| 3756 | $self->getTimer->endsLapByCategory('end sentence'); | ||
| 3757 | } | ||
| 3758 | $token = $token->next; | ||
| 3759 | $self->getTimer->endsLapByCategory('token2'); | ||
| 3760 | } | ||
| 3761 | $self->getTimer->endsLapByCategory('token'); | ||
| 3762 | |||
| 3763 | $self->getTimer->endsLapByCategory('document'); | ||
| 3764 | } | ||
| 3765 | # close FILEINPUT; | ||
| 3766 | $self->getTimer->_printTimeByCategory(0); | ||
| 3767 | $self->getTimer->_printTimesBySteps; | ||
| 3768 | warn "[LOG] done\n"; | ||
| 3769 | } | ||
| 3770 | |||
| 3771 | sub _QALDoutput { | ||
| 3772 | my ($self, $fh) = @_; | ||
| 3773 | |||
| 3774 | my $document; | ||
| 3775 | my $i; | ||
| 3776 | my $lang; | ||
| 3777 | my $sentence; | ||
| 3778 | my $SemUnit; | ||
| 3779 | my $semf; | ||
| 3780 | my $semfString; | ||
| 3781 | my %fields = ("IF" => 1, | ||
| 3782 | "POSTAG" => 2, | ||
| 3783 | "LM" => 3, | ||
| 3784 | # "ID" => 4, | ||
| 3785 | # "DOCID" => 5, | ||
| 3786 | # "SEMTAG" => 6, | ||
| 3787 | "OFFSET" => 4 | ||
| 3788 | ); | ||
| 3789 | |||
| 3790 | if (!defined $fh) { | ||
| 3791 | $fh = $self->_out_stream; | ||
| 3792 | if (!defined $fh) { | ||
| 3793 | $fh = \*STDOUT; | ||
| 3794 | } | ||
| 3795 | } | ||
| 3796 | |||
| 3797 | for ($i = 0; $i < scalar(@{$self->_documentSet}); $i++) { | ||
| 3798 | $self->getTimer->startsLapByCategory('document'); | ||
| 3799 | |||
| 3800 | $document = $self->_documentSet->[$i]; | ||
| 3801 | $lang = $document->getAnnotations->getLanguage; | ||
| 3802 | print $fh "DOC: " . $document->getId . "\n"; | ||
| 3803 | print $fh "language: $lang\n\n"; | ||
| 3804 | print $fh "sentence: \n"; | ||
| 3805 | print $fh "# sentence form (ended by _END_SENT_)\n"; | ||
| 3806 | foreach $sentence (@{$document->getAnnotations->getSentenceLevel->getElements}) { | ||
| 3807 | print $fh XML::Entities::decode('all', $sentence->getForm) . "\n_END_SENT_\n"; | ||
| 3808 | } | ||
| 3809 | print $fh "\n"; | ||
| 3810 | print $fh "word information:\n"; | ||
| 3811 | print $fh "# Inflected form<tab>POSTAG<tab>Lemma<tab>offset (ended by _END_POSTAG_)\n"; | ||
| 3812 | $self->_makeOneTaggedSentence($document, \*STDOUT, "\t", "\n", \%fields); | ||
| 3813 | print $fh "_END_POSTAG_\n"; | ||
| 3814 | print $fh "\n"; | ||
| 3815 | print $fh "semantic units:\n"; | ||
| 3816 | print $fh "# term form<tab>term canonical form<tab>semantic features<tab>offset start<tab>offset end<tab>negation indication (ended by _END_SEM_UNIT_)\n"; | ||
| 3817 | foreach $SemUnit (@{$document->getAnnotations->getSemanticUnitLevel->getElements}) { | ||
| 3818 | # warn "==> " . $SemUnit->getForm . "\n"; | ||
| 3819 | print $fh $SemUnit->getForm . "\t" . $self->_getCanonicalForm($SemUnit, $document) . "\t"; | ||
| 3820 | $semfString = ""; | ||
| 3821 | foreach $semf (@{$document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $SemUnit->getId)}) { | ||
| 3822 | $semfString .= $semf->toString . ":"; | ||
| 3823 | } | ||
| 3824 | chop($semfString); | ||
| 3825 | print $fh $semfString . "\t" . $SemUnit->start_token->getFrom . "\t" . $SemUnit->end_token->getTo . "\t"; | ||
| 3826 | if (defined $SemUnit->negation) { | ||
| 3827 | print $fh $SemUnit->negation; | ||
| 3828 | } | ||
| 3829 | print $fh "\n"; | ||
| 3830 | } | ||
| 3831 | print $fh "_END_SEM_UNIT_\n"; | ||
| 3832 | print $fh "\n_END_DOC_\n\n"; | ||
| 3833 | } | ||
| 3834 | } | ||
| 3835 | |||
| 3836 | sub _makeOneTaggedSentence { | ||
| 3837 | my ($self, $document, $fh, $sepInfo, $sepUnit, $fields) = @_; | ||
| 3838 | |||
| 3839 | # warn "in _makeOneTaggedSentence $sepInfo, $sepUnit\n"; | ||
| 3840 | |||
| 3841 | if (!defined $fh) { | ||
| 3842 | $fh = *STDOUT; | ||
| 3843 | } | ||
| 3844 | # my $printDocId = shift; | ||
| 3845 | if (!defined $fields) { | ||
| 3846 | $fields = {"IF" => 1, | ||
| 3847 | "POSTAG" => 2, | ||
| 3848 | "LM" => 3, | ||
| 3849 | "ID" => 4, | ||
| 3850 | "DOCID" => 5, | ||
| 3851 | "SEMTAG" => 6, | ||
| 3852 | "OFFSET" => 7, | ||
| 3853 | }; | ||
| 3854 | } | ||
| 3855 | my $doc_idx; | ||
| 3856 | my $token; | ||
| 3857 | my $word; | ||
| 3858 | my $lemma; | ||
| 3859 | my $MS_features; | ||
| 3860 | my $SemUnit; | ||
| 3861 | my @corpus_in_t; | ||
| 3862 | my $canonical_form; | ||
| 3863 | my $taggedSentence; | ||
| 3864 | my $id; | ||
| 3865 | my $lineNb = 0; | ||
| 3866 | my $semf; | ||
| 3867 | my $wordform; | ||
| 3868 | my $lang; | ||
| 3869 | my $terms = []; | ||
| 3870 | |||
| 3871 | $self->getTimer->startsLap("making tagged sentences"); | ||
| 3872 | warn "[LOG] printing Tagged Sentences\n"; | ||
| 3873 | |||
| 3874 | # warn "open " . $self->_input_filename . "(1)\n"; | ||
| 3875 | |||
| 3876 | # open FILEINPUT, ">:utf8", $self->_input_filename or die "No such file " . $self->_input_filename; | ||
| 3877 | |||
| 3878 | # for ($i = 0; $i < scalar(@{$self->_documentSet}); $i++) { | ||
| 3879 | $self->getTimer->startsLapByCategory('document'); | ||
| 3880 | $id = $document->getId; | ||
| 3881 | # $document = $self->_documentSet->[$i]; | ||
| 3882 | $lang = $document->getAnnotations->getLanguage; | ||
| 3883 | |||
| 3884 | $self->getTimer->startsLapByCategory('token'); | ||
| 3885 | |||
| 3886 | $token = $document->getAnnotations->getTokenLevel->getElements->[0]; | ||
| 3887 | |||
| 3888 | $self->getTimer->endsLapByCategory('token'); | ||
| 3889 | while(defined ($token)) { | ||
| 3890 | $self->getTimer->startsLapByCategory('token2'); | ||
| 3891 | if ((exists $fields->{"SEMTAG"}) && ($fields->{"SEMTAG"} > 0)) { | ||
| 3892 | $terms = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token); | ||
| 3893 | } else { | ||
| 3894 | $terms = []; | ||
| 3895 | } | ||
| 3896 | |||
| 3897 | if (scalar(@$terms) > 0) { | ||
| 3898 | $self->getTimer->startsLapByCategory('semUnit'); | ||
| 3899 | |||
| 3900 | $self->getTimer->startsLapByCategory('LargerSemUnit'); | ||
| 3901 | $SemUnit = $self->_getLargerTerm($terms); | ||
| 3902 | $self->getTimer->endsLapByCategory('LargerSemUnit'); | ||
| 3903 | |||
| 3904 | # warn "OK\n"; | ||
| 3905 | if ($SemUnit->isNamedEntity) { | ||
| 3906 | $canonical_form = $self->_getCanonicalForm($SemUnit, $document); | ||
| 3907 | # if (!defined $SemUnit->canonical_form) { | ||
| 3908 | # $canonical_form = ""; | ||
| 3909 | # } else { | ||
| 3910 | # $canonical_form = $SemUnit->canonical_form; | ||
| 3911 | # } | ||
| 3912 | # warn "OK1"; | ||
| 3913 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 3914 | "ID" => "S". $SemUnit->getId, | ||
| 3915 | "POSTAG" => "named_entity", | ||
| 3916 | "LM" => $canonical_form, | ||
| 3917 | "SEMTAG" => $SemUnit->NEtype, | ||
| 3918 | "DOCID" => $id, | ||
| 3919 | "OFFSET" => $SemUnit->start_token->getFrom, | ||
| 3920 | }, $fields); | ||
| 3921 | |||
| 3922 | } elsif ($SemUnit->isTerm) { | ||
| 3923 | $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $SemUnit->getId)->[0]; | ||
| 3924 | # warn "OK2"; | ||
| 3925 | if (defined $semf) { | ||
| 3926 | # warn "OK2b"; | ||
| 3927 | $canonical_form = $self->_getCanonicalForm($SemUnit, $document); | ||
| 3928 | # if (!defined $SemUnit->canonical_form) { | ||
| 3929 | # $canonical_form = ""; | ||
| 3930 | # } else { | ||
| 3931 | # $canonical_form = $SemUnit->canonical_form; | ||
| 3932 | # } | ||
| 3933 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 3934 | "ID" => "S". $SemUnit->getId, | ||
| 3935 | "POSTAG" => "term", | ||
| 3936 | "LM" => $canonical_form, | ||
| 3937 | "SEMTAG" => $semf->first_node_first_semantic_category, | ||
| 3938 | "DOCID" => $id, | ||
| 3939 | "OFFSET" => $SemUnit->start_token->getFrom, | ||
| 3940 | }, $fields); | ||
| 3941 | } else { | ||
| 3942 | # warn "OK2c"; | ||
| 3943 | $canonical_form = $self->_getCanonicalForm($SemUnit, $document); | ||
| 3944 | # if (!defined $SemUnit->canonical_form) { | ||
| 3945 | # $canonical_form = ""; | ||
| 3946 | # } else { | ||
| 3947 | # $canonical_form = $SemUnit->canonical_form; | ||
| 3948 | # } | ||
| 3949 | # warn "OK\n"; | ||
| 3950 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 3951 | "ID" => "S". $SemUnit->getId, | ||
| 3952 | "POSTAG" => "term", | ||
| 3953 | "LM" => $canonical_form, | ||
| 3954 | "SEMTAG" => "", | ||
| 3955 | "DOCID" => $id, | ||
| 3956 | "OFFSET" => $SemUnit->start_token->getFrom, | ||
| 3957 | }, $fields); | ||
| 3958 | } | ||
| 3959 | } | ||
| 3960 | $token = $SemUnit->end_token; | ||
| 3961 | $self->getTimer->endsLapByCategory('semUnit'); | ||
| 3962 | } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 3963 | $self->getTimer->startsLapByCategory('word'); | ||
| 3964 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 3965 | $lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 3966 | $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 3967 | $wordform = $word->getForm; | ||
| 3968 | $wordform =~ s/[\t\n]/ /gos; | ||
| 3969 | $wordform =~ s/ +/ /gos; | ||
| 3970 | |||
| 3971 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $wordform, | ||
| 3972 | "ID" => "W". $word->getId, | ||
| 3973 | "POSTAG" => $MS_features->syntactic_category, | ||
| 3974 | "LM" => $lemma->canonical_form, | ||
| 3975 | "SEMTAG" => "", | ||
| 3976 | "DOCID" => $id, | ||
| 3977 | "OFFSET" => $word->start_token->getFrom, | ||
| 3978 | }, $fields); | ||
| 3979 | |||
| 3980 | $token = $word->end_token; | ||
| 3981 | |||
| 3982 | $self->getTimer->endsLapByCategory('word'); | ||
| 3983 | } else { | ||
| 3984 | $self->getTimer->startsLapByCategory('separator'); | ||
| 3985 | if ((!($token->isSep)) && (!($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)))){ | ||
| 3986 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $token->getContent, | ||
| 3987 | "ID" => "T". $token->getId, | ||
| 3988 | "POSTAG" => $token->getContent, | ||
| 3989 | "LM" => $token->getContent, | ||
| 3990 | "SEMTAG" => "", | ||
| 3991 | "DOCID" => $id, | ||
| 3992 | "OFFSET" => $token->getFrom, | ||
| 3993 | }, $fields); | ||
| 3994 | } | ||
| 3995 | $self->getTimer->endsLapByCategory('separator'); | ||
| 3996 | } | ||
| 3997 | $self->getTimer->startsLapByCategory('end sentence'); | ||
| 3998 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 3999 | if ($token->isSymb) { | ||
| 4000 | if (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) { | ||
| 4001 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $token->getContent, | ||
| 4002 | "ID" => "T" . $token->getId, | ||
| 4003 | "POSTAG" => "SENT", | ||
| 4004 | "LM" => $token->getContent, | ||
| 4005 | "SEMTAG" => "", | ||
| 4006 | "DOCID" => $id, | ||
| 4007 | "OFFSET" => $token->getFrom, | ||
| 4008 | }, $fields); | ||
| 4009 | $self->_printTaggedSentence($fh, \@corpus_in_t, $sepInfo, $sepUnit); | ||
| 4010 | @corpus_in_t = (); | ||
| 4011 | } else { | ||
| 4012 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $token->getContent, | ||
| 4013 | "ID" => "T" . $token->getId, | ||
| 4014 | "POSTAG" => "SENT", | ||
| 4015 | "LM" => $token->getContent, | ||
| 4016 | "SEMTAG" => "", | ||
| 4017 | "DOCID" => $id, | ||
| 4018 | "OFFSET" => $token->getFrom, | ||
| 4019 | }, $fields); | ||
| 4020 | $self->_printTaggedSentence($fh, \@corpus_in_t, $sepInfo, $sepUnit); | ||
| 4021 | @corpus_in_t = (); | ||
| 4022 | } | ||
| 4023 | } else { | ||
| 4024 | # my @tmp = (".", $token->getId, "SENT", "."); | ||
| 4025 | # push @corpus_in_t, \@tmp; | ||
| 4026 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => ".", | ||
| 4027 | "ID" => "T" . $token->getId, | ||
| 4028 | "POSTAG" => "SENT", | ||
| 4029 | "LM" => ".", | ||
| 4030 | "SEMTAG" => "", | ||
| 4031 | "DOCID" => $id, | ||
| 4032 | "OFFSET" => $token->getFrom, | ||
| 4033 | }, $fields); | ||
| 4034 | $self->_printTaggedSentence($fh, \@corpus_in_t, $sepInfo, $sepUnit); | ||
| 4035 | @corpus_in_t = (); | ||
| 4036 | } | ||
| 4037 | $self->getTimer->endsLapByCategory('end sentence'); | ||
| 4038 | } | ||
| 4039 | $token = $token->next; | ||
| 4040 | $self->getTimer->endsLapByCategory('token2'); | ||
| 4041 | } | ||
| 4042 | $self->getTimer->endsLapByCategory('token'); | ||
| 4043 | |||
| 4044 | $self->getTimer->endsLapByCategory('document'); | ||
| 4045 | # } | ||
| 4046 | # close FILEINPUT; | ||
| 4047 | $self->getTimer->_printTimeByCategory(0); | ||
| 4048 | $self->getTimer->_printTimesBySteps; | ||
| 4049 | warn "[LOG] done\n"; | ||
| 4050 | } | ||
| 4051 | |||
| 4052 | sub _makeTaggedSentences { | ||
| 4053 | my ($self, $sentences, $fh, $sepInfo, $sepUnit, $fields) = @_; | ||
| 4054 | |||
| 4055 | # warn "in _makeTaggedSentences $sepInfo, $sepUnit\n"; | ||
| 4056 | |||
| 4057 | if (!defined $fh) { | ||
| 4058 | $fh = *STDOUT; | ||
| 4059 | } | ||
| 4060 | # my $printDocId = shift; | ||
| 4061 | if (!defined $fields) { | ||
| 4062 | $fields = {"IF" => 1, | ||
| 4063 | "POSTAG" => 2, | ||
| 4064 | "LM" => 3, | ||
| 4065 | "ID" => 4, | ||
| 4066 | "DOCID" => 5, | ||
| 4067 | "SEMTAG" => 6, | ||
| 4068 | # "OFFSET" => 7, | ||
| 4069 | }; | ||
| 4070 | } | ||
| 4071 | my $document; | ||
| 4072 | my $doc_idx; | ||
| 4073 | my $token; | ||
| 4074 | my $word; | ||
| 4075 | my $lemma; | ||
| 4076 | my $MS_features; | ||
| 4077 | my $SemUnit; | ||
| 4078 | my @corpus_in_t; | ||
| 4079 | my $canonical_form; | ||
| 4080 | my $taggedSentence; | ||
| 4081 | my $i; | ||
| 4082 | my $lineNb = 0; | ||
| 4083 | my $semf; | ||
| 4084 | my $wordform; | ||
| 4085 | my $lang; | ||
| 4086 | my $terms = []; | ||
| 4087 | |||
| 4088 | $self->getTimer->startsLap("making tagged sentences"); | ||
| 4089 | warn "[LOG] printing Tagged Sentences\n"; | ||
| 4090 | |||
| 4091 | # warn "open " . $self->_input_filename . "(1)\n"; | ||
| 4092 | |||
| 4093 | # open FILEINPUT, ">:utf8", $self->_input_filename or die "No such file " . $self->_input_filename; | ||
| 4094 | |||
| 4095 | for ($i = 0; $i < scalar(@{$self->_documentSet}); $i++) { | ||
| 4096 | $self->getTimer->startsLapByCategory('document0'); | ||
| 4097 | $document = $self->_documentSet->[$i]; | ||
| 4098 | |||
| 4099 | $self->_makeOneTaggedSentence($document, $fh, $sepInfo, $sepUnit, $fields); | ||
| 4100 | |||
| 4101 | $self->getTimer->endsLapByCategory('document0'); | ||
| 4102 | } | ||
| 4103 | # close FILEINPUT; | ||
| 4104 | $self->getTimer->_printTimeByCategory(0); | ||
| 4105 | $self->getTimer->_printTimesBySteps; | ||
| 4106 | warn "[LOG] done\n"; | ||
| 4107 | } | ||
| 4108 | |||
| 4109 | sub _makeTaggedSentences2 { | ||
| 4110 | my ($self, $sentences, $fh, $sepInfo, $sepUnit, $fields) = @_; | ||
| 4111 | |||
| 4112 | if (!defined $fh) { | ||
| 4113 | $fh = *STDOUT; | ||
| 4114 | } | ||
| 4115 | # my $printDocId = shift; | ||
| 4116 | if (!defined $fields) { | ||
| 4117 | $fields = {"IF" => 1, | ||
| 4118 | "POSTAG" => 2, | ||
| 4119 | "LM" => 3, | ||
| 4120 | "ID" => 4, | ||
| 4121 | "DOCID" => 5, | ||
| 4122 | "SEMTAG" => 6, | ||
| 4123 | "OFFSET" => 7, | ||
| 4124 | }; | ||
| 4125 | } | ||
| 4126 | my $document; | ||
| 4127 | my $doc_idx; | ||
| 4128 | my $token; | ||
| 4129 | my $word; | ||
| 4130 | my $lemma; | ||
| 4131 | my $MS_features; | ||
| 4132 | my $SemUnit; | ||
| 4133 | my @corpus_in_t; | ||
| 4134 | my $canonical_form; | ||
| 4135 | my $taggedSentence; | ||
| 4136 | my $i; | ||
| 4137 | my $lineNb = 0; | ||
| 4138 | my $semf; | ||
| 4139 | my $wordform; | ||
| 4140 | my $lang; | ||
| 4141 | my $terms = []; | ||
| 4142 | |||
| 4143 | $self->getTimer->startsLap("making tagged sentences"); | ||
| 4144 | warn "[LOG] printing Tagged Sentences\n"; | ||
| 4145 | |||
| 4146 | # warn "open " . $self->_input_filename . "(1)\n"; | ||
| 4147 | |||
| 4148 | # open FILEINPUT, ">:utf8", $self->_input_filename or die "No such file " . $self->_input_filename; | ||
| 4149 | |||
| 4150 | for ($i = 0; $i < scalar(@{$self->_documentSet}); $i++) { | ||
| 4151 | $self->getTimer->startsLapByCategory('document'); | ||
| 4152 | |||
| 4153 | $document = $self->_documentSet->[$i]; | ||
| 4154 | $lang = $document->getAnnotations->getLanguage; | ||
| 4155 | |||
| 4156 | $self->getTimer->startsLapByCategory('token'); | ||
| 4157 | |||
| 4158 | $token = $document->getAnnotations->getTokenLevel->getElements->[0]; | ||
| 4159 | |||
| 4160 | $self->getTimer->endsLapByCategory('token'); | ||
| 4161 | while(defined ($token)) { | ||
| 4162 | $self->getTimer->startsLapByCategory('token2'); | ||
| 4163 | if ((exists $fields->{"SEMTAG"}) && ($fields->{"SEMTAG"} > 0)) { | ||
| 4164 | $terms = $document->getAnnotations->getSemanticUnitLevel->getElementByToken($token); | ||
| 4165 | } else { | ||
| 4166 | $terms = []; | ||
| 4167 | } | ||
| 4168 | |||
| 4169 | if (scalar(@$terms) > 0) { | ||
| 4170 | $self->getTimer->startsLapByCategory('semUnit'); | ||
| 4171 | |||
| 4172 | $self->getTimer->startsLapByCategory('LargerSemUnit'); | ||
| 4173 | $SemUnit = $self->_getLargerTerm($terms); | ||
| 4174 | $self->getTimer->endsLapByCategory('LargerSemUnit'); | ||
| 4175 | |||
| 4176 | # warn "OK\n"; | ||
| 4177 | if ($SemUnit->isNamedEntity) { | ||
| 4178 | $canonical_form = $self->_getCanonicalForm($SemUnit, $document); | ||
| 4179 | # if (!defined $SemUnit->canonical_form) { | ||
| 4180 | # $canonical_form = ""; | ||
| 4181 | # } else { | ||
| 4182 | # $canonical_form = $SemUnit->canonical_form; | ||
| 4183 | # } | ||
| 4184 | # warn "OK1"; | ||
| 4185 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 4186 | "ID" => $SemUnit->getId, | ||
| 4187 | "POSTAG" => "named_entity", | ||
| 4188 | "LM" => $canonical_form, | ||
| 4189 | "SEMTAG" => $SemUnit->NEtype, | ||
| 4190 | "DOCID" => $i, | ||
| 4191 | "OFFSET" => $SemUnit->start_token->getFrom, | ||
| 4192 | }, $fields); | ||
| 4193 | |||
| 4194 | } elsif ($SemUnit->isTerm) { | ||
| 4195 | $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $SemUnit->getId)->[0]; | ||
| 4196 | # warn "OK2"; | ||
| 4197 | if (defined $semf) { | ||
| 4198 | # warn "OK2b"; | ||
| 4199 | $canonical_form = $self->_getCanonicalForm($SemUnit, $document); | ||
| 4200 | # if (!defined $SemUnit->canonical_form) { | ||
| 4201 | # $canonical_form = ""; | ||
| 4202 | # } else { | ||
| 4203 | # $canonical_form = $SemUnit->canonical_form; | ||
| 4204 | # } | ||
| 4205 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 4206 | "ID" => $SemUnit->getId, | ||
| 4207 | "POSTAG" => "term", | ||
| 4208 | "LM" => $canonical_form, | ||
| 4209 | "SEMTAG" => $semf->first_node_first_semantic_category, | ||
| 4210 | "DOCID" => $i, | ||
| 4211 | "OFFSET" => $SemUnit->start_token->getFrom, | ||
| 4212 | }, $fields); | ||
| 4213 | } else { | ||
| 4214 | # warn "OK2c"; | ||
| 4215 | $canonical_form = $self->_getCanonicalForm($SemUnit, $document); | ||
| 4216 | # if (!defined $SemUnit->canonical_form) { | ||
| 4217 | # $canonical_form = ""; | ||
| 4218 | # } else { | ||
| 4219 | # $canonical_form = $SemUnit->canonical_form; | ||
| 4220 | # } | ||
| 4221 | # warn "OK\n"; | ||
| 4222 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 4223 | "ID" => $SemUnit->getId, | ||
| 4224 | "POSTAG" => "term", | ||
| 4225 | "LM" => $canonical_form, | ||
| 4226 | "SEMTAG" => "", | ||
| 4227 | "DOCID" => $i, | ||
| 4228 | "OFFSET" => $SemUnit->start_token->getFrom, | ||
| 4229 | }, $fields); | ||
| 4230 | } | ||
| 4231 | } | ||
| 4232 | $token = $SemUnit->end_token; | ||
| 4233 | $self->getTimer->endsLapByCategory('semUnit'); | ||
| 4234 | } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 4235 | $self->getTimer->startsLapByCategory('word'); | ||
| 4236 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 4237 | $lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 4238 | $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 4239 | $wordform = $word->getForm; | ||
| 4240 | $wordform =~ s/[\t\n]/ /gos; | ||
| 4241 | $wordform =~ s/ +/ /gos; | ||
| 4242 | |||
| 4243 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $wordform, | ||
| 4244 | "ID" => $word->getId, | ||
| 4245 | "POSTAG" => $MS_features->syntactic_category, | ||
| 4246 | "LM" => $lemma->canonical_form, | ||
| 4247 | "SEMTAG" => "", | ||
| 4248 | "DOCID" => $i, | ||
| 4249 | "OFFSET" => $word->start_token->getFrom, | ||
| 4250 | }, $fields); | ||
| 4251 | |||
| 4252 | $token = $word->end_token; | ||
| 4253 | |||
| 4254 | $self->getTimer->endsLapByCategory('word'); | ||
| 4255 | } else { | ||
| 4256 | $self->getTimer->startsLapByCategory('separator'); | ||
| 4257 | if ((!($token->isSep)) && (!($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)))){ | ||
| 4258 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $token->getContent, | ||
| 4259 | "ID" => $token->getId, | ||
| 4260 | "POSTAG" => $token->getContent, | ||
| 4261 | "LM" => $token->getContent, | ||
| 4262 | "SEMTAG" => "", | ||
| 4263 | "DOCID" => $i, | ||
| 4264 | "OFFSET" => $token->getFrom, | ||
| 4265 | }, $fields); | ||
| 4266 | } | ||
| 4267 | $self->getTimer->endsLapByCategory('separator'); | ||
| 4268 | } | ||
| 4269 | $self->getTimer->startsLapByCategory('end sentence'); | ||
| 4270 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 4271 | if ($token->isSymb) { | ||
| 4272 | if (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) { | ||
| 4273 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $token->getContent, | ||
| 4274 | "ID" => $token->getId, | ||
| 4275 | "POSTAG" => "SENT", | ||
| 4276 | "LM" => $token->getContent, | ||
| 4277 | "SEMTAG" => "", | ||
| 4278 | "DOCID" => $i, | ||
| 4279 | "OFFSET" => $token->getFrom, | ||
| 4280 | }, $fields); | ||
| 4281 | $self->_printTaggedSentence($fh, \@corpus_in_t, $sepInfo, $sepUnit); | ||
| 4282 | @corpus_in_t = (); | ||
| 4283 | } else { | ||
| 4284 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => $token->getContent, | ||
| 4285 | "ID" => $token->getId, | ||
| 4286 | "POSTAG" => "SENT", | ||
| 4287 | "LM" => $token->getContent, | ||
| 4288 | "SEMTAG" => "", | ||
| 4289 | "DOCID" => $i, | ||
| 4290 | "OFFSET" => $token->getFrom, | ||
| 4291 | }, $fields); | ||
| 4292 | $self->_printTaggedSentence($fh, \@corpus_in_t, $sepInfo, $sepUnit); | ||
| 4293 | @corpus_in_t = (); | ||
| 4294 | } | ||
| 4295 | } else { | ||
| 4296 | # my @tmp = (".", $token->getId, "SENT", "."); | ||
| 4297 | # push @corpus_in_t, \@tmp; | ||
| 4298 | $self->_addInfosUnit($lang, \$lineNb, \@corpus_in_t, {"IF" => ".", | ||
| 4299 | "ID" => $token->getId, | ||
| 4300 | "POSTAG" => "SENT", | ||
| 4301 | "LM" => ".", | ||
| 4302 | "SEMTAG" => "", | ||
| 4303 | "DOCID" => $i, | ||
| 4304 | "OFFSET" => $token->getFrom, | ||
| 4305 | }, $fields); | ||
| 4306 | $self->_printTaggedSentence($fh, \@corpus_in_t, $sepInfo, $sepUnit); | ||
| 4307 | @corpus_in_t = (); | ||
| 4308 | } | ||
| 4309 | $self->getTimer->endsLapByCategory('end sentence'); | ||
| 4310 | } | ||
| 4311 | $token = $token->next; | ||
| 4312 | $self->getTimer->endsLapByCategory('token2'); | ||
| 4313 | } | ||
| 4314 | $self->getTimer->endsLapByCategory('token'); | ||
| 4315 | |||
| 4316 | $self->getTimer->endsLapByCategory('document'); | ||
| 4317 | } | ||
| 4318 | # close FILEINPUT; | ||
| 4319 | $self->getTimer->_printTimeByCategory(0); | ||
| 4320 | $self->getTimer->_printTimesBySteps; | ||
| 4321 | warn "[LOG] done\n"; | ||
| 4322 | } | ||
| 4323 | |||
| 4324 | |||
| 4325 | |||
| 4326 | sub _addInfosUnit { | ||
| 4327 | my ($self, $lang, $lineNb, $corpus, $infoUnit, $fields) = @_; | ||
| 4328 | |||
| 4329 | $self->getTimer->startsLapByCategory('infoUnit'); | ||
| 4330 | my $field; | ||
| 4331 | my @tabinfos; | ||
| 4332 | |||
| 4333 | $$lineNb++; | ||
| 4334 | |||
| 4335 | foreach $field (sort {$fields->{$a} <=> $fields->{$b}} keys %$fields) { | ||
| 4336 | # warn "$field\n"; | ||
| 4337 | if (($fields->{$field} > 0) && (exists $infoUnit->{$field})) { | ||
| 4338 | if (ref($infoUnit->{$field}) eq "ARRAY") { | ||
| 4339 | push @tabinfos, @{$infoUnit->{$field}}; | ||
| 4340 | } else { | ||
| 4341 | # $self->_deft2arff($self->_arff2deft( | ||
| 4342 | if ((defined $self->{"JoinComplexTerm"}->{$lang}) && ($self->{"JoinComplexTerm"}->{$lang} == 1)) { | ||
| 4343 | push @tabinfos, $self->_deft2arff($infoUnit->{$field}); | ||
| 4344 | } else { | ||
| 4345 | push @tabinfos, $infoUnit->{$field}; | ||
| 4346 | } | ||
| 4347 | } | ||
| 4348 | } | ||
| 4349 | } | ||
| 4350 | |||
| 4351 | push @{$corpus}, \@tabinfos; | ||
| 4352 | |||
| 4353 | $self->getTimer->endsLapByCategory('infoUnit'); | ||
| 4354 | } | ||
| 4355 | |||
| 4356 | |||
| 4357 | sub _printTaggedSentence { | ||
| 4358 | my ($self, $fh, $corpus_in_t, $sepInfo, $sepUnit, $sepSent) = @_; | ||
| 4359 | my $encoding = "no"; | ||
| 4360 | |||
| 4361 | # warn "in _printTaggedSentence $sepInfo, $sepUnit\n"; | ||
| 4362 | |||
| 4363 | if (!defined $sepSent) { | ||
| 4364 | $sepSent = "\n"; | ||
| 4365 | } | ||
| 4366 | |||
| 4367 | $self->getTimer->startsLapByCategory('print Tagged Sentence'); | ||
| 4368 | my $taggedSentence = ""; | ||
| 4369 | my $word_ref; | ||
| 4370 | foreach $word_ref (@$corpus_in_t) { | ||
| 4371 | |||
| 4372 | if ((!defined $encoding) || (uc($encoding) eq "UTF-8")) { | ||
| 4373 | $taggedSentence .= Encode::encode("UTF-8", join($sepInfo, @$word_ref)); # . "\n"; | ||
| 4374 | } else { | ||
| 4375 | if ((defined $encoding) && (uc($encoding) eq "LATIN1")) { | ||
| 4376 | $taggedSentence .= Encode::encode("iso-8859-1", join($sepInfo, @$word_ref)); # . "\n"; | ||
| 4377 | } else { | ||
| 4378 | # warn "[WRAPPER LOG] Unknown enconding charset\n"; | ||
| 4379 | $taggedSentence .= join($sepInfo, @$word_ref); # . "\n"; | ||
| 4380 | } | ||
| 4381 | } | ||
| 4382 | $taggedSentence .= $sepInfo; | ||
| 4383 | if ($word_ref->[1] eq "SENT") { | ||
| 4384 | print $fh "$taggedSentence$sepUnit$sepSent"; | ||
| 4385 | $taggedSentence = ""; | ||
| 4386 | } else { | ||
| 4387 | $taggedSentence .= $sepUnit; | ||
| 4388 | } | ||
| 4389 | } | ||
| 4390 | $self->getTimer->endsLapByCategory('print Tagged Sentence'); | ||
| 4391 | } | ||
| 4392 | |||
| 4393 | sub addResourceSemRel { | ||
| 4394 | my ($self, $lang, $lang2, $type) = @_; | ||
| 4395 | |||
| 4396 | my $terminologyFilename; | ||
| 4397 | my $DistribThesaurusFilename; | ||
| 4398 | my $exceptionFilename; | ||
| 4399 | |||
| 4400 | $self->{"Resources"}->{$lang2}->{$type} = []; | ||
| 4401 | |||
| 4402 | # warn "==> $self / $lang / type\n"; | ||
| 4403 | # warn $self->_config->configuration->{'RESOURCE'}->{$lang}->{$type} . "\n"; | ||
| 4404 | if ($type eq "DISTRIBTHESAURUS") { | ||
| 4405 | if (ref($self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}) eq "ARRAY") { | ||
| 4406 | foreach $DistribThesaurusFilename (@{$self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}}) { | ||
| 4407 | warn "Adding (1) " . $DistribThesaurusFilename . "\n"; | ||
| 4408 | my %DistThesaurus; | ||
| 4409 | my %DistThesaurusIdx; | ||
| 4410 | push @{$self->{"Resources"}->{$lang2}->{$type}}, {'filename' => $DistribThesaurusFilename, | ||
| 4411 | 'type' => $type, | ||
| 4412 | 'thesaurus' => \%DistThesaurus, | ||
| 4413 | 'thesaurusIdx' => \%DistThesaurusIdx, | ||
| 4414 | }; | ||
| 4415 | $self->_loadThesaurus($DistribThesaurusFilename, \%DistThesaurus, \%DistThesaurusIdx); | ||
| 4416 | } | ||
| 4417 | } else { | ||
| 4418 | $DistribThesaurusFilename = $self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}; | ||
| 4419 | warn "Adding (2) " . $DistribThesaurusFilename . "\n"; | ||
| 4420 | my %DistThesaurus; | ||
| 4421 | my %DistThesaurusIdx; | ||
| 4422 | push @{$self->{"Resources"}->{$lang2}->{$type}}, {'filename' => $DistribThesaurusFilename, | ||
| 4423 | 'type' => $type, | ||
| 4424 | 'thesaurus' => \%DistThesaurus, | ||
| 4425 | 'thesaurusIdx' => \%DistThesaurusIdx, | ||
| 4426 | }; | ||
| 4427 | $self->_loadThesaurus($DistribThesaurusFilename, \%DistThesaurus, \%DistThesaurusIdx); | ||
| 4428 | } | ||
| 4429 | # $self->loadResource($lang, $name, $type); | ||
| 4430 | } | ||
| 4431 | |||
| 4432 | if ($type eq "EXCEPTIONLIST") { | ||
| 4433 | if (ref($self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}) eq "ARRAY") { | ||
| 4434 | foreach $exceptionFilename (@{$self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}}) { | ||
| 4435 | $self->_loadException($exceptionFilename, $lang2, 'exceptions'); | ||
| 4436 | } | ||
| 4437 | } else { | ||
| 4438 | $self->_loadException($self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}, $lang2, 'exceptions'); | ||
| 4439 | } | ||
| 4440 | } | ||
| 4441 | |||
| 4442 | if ($type eq "TERMINOLOGY") { | ||
| 4443 | if (ref($self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}) eq "ARRAY") { | ||
| 4444 | foreach $terminologyFilename (@{$self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}}) { | ||
| 4445 | warn "Adding (1) " . $terminologyFilename . "\n"; | ||
| 4446 | my %Terminology; | ||
| 4447 | my %TerminologyIdx; | ||
| 4448 | push @{$self->{"Resources"}->{$lang2}->{$type}}, {'filename' => $terminologyFilename, | ||
| 4449 | 'type' => $type, | ||
| 4450 | 'terminology' => \%Terminology, | ||
| 4451 | 'terminologyIdx' => \%TerminologyIdx, | ||
| 4452 | }; | ||
| 4453 | $self->_loadTerminology($terminologyFilename, \%Terminology, \%TerminologyIdx); | ||
| 4454 | } | ||
| 4455 | } else { | ||
| 4456 | $terminologyFilename = $self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}; | ||
| 4457 | warn "Adding (2) " . $terminologyFilename . "\n"; | ||
| 4458 | my %Terminology; | ||
| 4459 | my %TerminologyIdx; | ||
| 4460 | push @{$self->{"Resources"}->{$lang2}->{$type}}, {'filename' => $terminologyFilename, | ||
| 4461 | 'type' => $type, | ||
| 4462 | 'terminology' => \%Terminology, | ||
| 4463 | 'terminologyIdx' => \%TerminologyIdx, | ||
| 4464 | }; | ||
| 4465 | $self->_loadTerminology($terminologyFilename, \%Terminology, \%TerminologyIdx); | ||
| 4466 | } | ||
| 4467 | } | ||
| 4468 | |||
| 4469 | if ($type eq "SYNONYMY") { | ||
| 4470 | if (ref($self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}) eq "ARRAY") { | ||
| 4471 | foreach $terminologyFilename (@{$self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}}) { | ||
| 4472 | warn "Adding (1) " . $terminologyFilename . "\n"; | ||
| 4473 | my %Terminology; | ||
| 4474 | push @{$self->{"Resources"}->{$lang2}->{$type}}, {'filename' => $terminologyFilename, | ||
| 4475 | 'type' => $type, | ||
| 4476 | 'synonymy' => \%Terminology, | ||
| 4477 | # 'synonymyIdx' => \%TerminologyIdx, | ||
| 4478 | }; | ||
| 4479 | $self->_loadSynonymy($terminologyFilename, \%Terminology); | ||
| 4480 | } | ||
| 4481 | } else { | ||
| 4482 | $terminologyFilename = $self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}; | ||
| 4483 | warn "Adding (2) " . $terminologyFilename . "\n"; | ||
| 4484 | my %Terminology; | ||
| 4485 | push @{$self->{"Resources"}->{$lang2}->{$type}}, {'filename' => $terminologyFilename, | ||
| 4486 | 'type' => $type, | ||
| 4487 | 'synonymy' => \%Terminology, | ||
| 4488 | # 'synonymyIdx' => \%TerminologyIdx, | ||
| 4489 | }; | ||
| 4490 | $self->_loadSynonymy($terminologyFilename, \%Terminology); | ||
| 4491 | } | ||
| 4492 | |||
| 4493 | } | ||
| 4494 | if ($type eq "VDLMDIR") { | ||
| 4495 | my $vdlmDir = $self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}; | ||
| 4496 | warn "Adding (2) " . $vdlmDir . "\n"; | ||
| 4497 | my %DistThesaurus; | ||
| 4498 | my %DistThesaurusIdx; | ||
| 4499 | push @{$self->{"Resources"}->{$lang2}->{$type}}, {'filename' => $vdlmDir, | ||
| 4500 | 'type' => $type, | ||
| 4501 | 'thesaurus' => \%DistThesaurus, | ||
| 4502 | 'thesaurusIdx' => \%DistThesaurusIdx, | ||
| 4503 | }; | ||
| 4504 | $self->_loadVDLM($vdlmDir, \%DistThesaurus, \%DistThesaurusIdx); | ||
| 4505 | } | ||
| 4506 | if ($type eq "VDWDIR") { | ||
| 4507 | my $vdwDir = $self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}; | ||
| 4508 | warn "Adding (2) " . $vdwDir . "\n"; | ||
| 4509 | my %DistThesaurus; | ||
| 4510 | my %DistThesaurusIdx; | ||
| 4511 | push @{$self->{"Resources"}->{$lang2}->{$type}}, {'filename' => $vdwDir, | ||
| 4512 | 'type' => $type, | ||
| 4513 | 'thesaurus' => \%DistThesaurus, | ||
| 4514 | 'thesaurusIdx' => \%DistThesaurusIdx, | ||
| 4515 | }; | ||
| 4516 | $self->_loadVDLM($vdwDir, \%DistThesaurus, \%DistThesaurusIdx); | ||
| 4517 | } | ||
| 4518 | if ($type eq "SEMANTIC_RELATIONS_WITH_TYPE") { | ||
| 4519 | if (ref($self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}) eq "ARRAY") { | ||
| 4520 | foreach $terminologyFilename (@{$self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}}) { | ||
| 4521 | warn "Adding (1) " . $terminologyFilename . "\n"; | ||
| 4522 | my %Terminology; | ||
| 4523 | my %TerminologyIdx; | ||
| 4524 | push @{$self->{"Resources"}->{$lang2}->{$type}}, {'filename' => $terminologyFilename, | ||
| 4525 | 'type' => $type, | ||
| 4526 | 'thesaurus' => \%Terminology, | ||
| 4527 | 'thesaurusIdx' => \%TerminologyIdx, | ||
| 4528 | }; | ||
| 4529 | $self->_loadSemanticRelationsWithType($terminologyFilename, \%Terminology, \%TerminologyIdx); | ||
| 4530 | } | ||
| 4531 | } else { | ||
| 4532 | $terminologyFilename = $self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}; | ||
| 4533 | warn "Adding (2) " . $terminologyFilename . "\n"; | ||
| 4534 | my %Terminology; | ||
| 4535 | my %TerminologyIdx; | ||
| 4536 | push @{$self->{"Resources"}->{$lang2}->{$type}}, {'filename' => $terminologyFilename, | ||
| 4537 | 'type' => $type, | ||
| 4538 | 'thesaurus' => \%Terminology, | ||
| 4539 | 'thesaurusIdx' => \%TerminologyIdx, | ||
| 4540 | }; | ||
| 4541 | $self->_loadSemanticRelationsWithType($terminologyFilename, \%Terminology, \%TerminologyIdx); | ||
| 4542 | } | ||
| 4543 | } | ||
| 4544 | warn "Done\n"; | ||
| 4545 | } | ||
| 4546 | |||
| 4547 | |||
| 4548 | |||
| 4549 | sub _loadVDLM { | ||
| 4550 | my ($self, $vdlmDir, $DistThesaurus, $DistThesaurusIdx) = @_; | ||
| 4551 | |||
| 4552 | my @wordsInfos; | ||
| 4553 | my $wordInfo; | ||
| 4554 | my $word; | ||
| 4555 | my $dist; | ||
| 4556 | my $wordDist; | ||
| 4557 | |||
| 4558 | my $line; | ||
| 4559 | my %lemmaSet; | ||
| 4560 | |||
| 4561 | my $postag; | ||
| 4562 | my $id; | ||
| 4563 | my $lemma; | ||
| 4564 | my $rel; | ||
| 4565 | my $n; | ||
| 4566 | my $freq; | ||
| 4567 | my $nbNeighbour; | ||
| 4568 | my $nbcl; | ||
| 4569 | my $lid1; | ||
| 4570 | my $lid2; | ||
| 4571 | my $n1; | ||
| 4572 | my $n2; | ||
| 4573 | my $jacc; | ||
| 4574 | my $lin; | ||
| 4575 | my $j1; | ||
| 4576 | my $j2; | ||
| 4577 | |||
| 4578 | warn "Load lemma ($vdlmDir/upery-lemme.txt)\n"; | ||
| 4579 | open LEMMA, $vdlmDir . "/upery-lemme.txt" or die "no such file $vdlmDir/upery-lemme.txt\n"; | ||
| 4580 | $line = <LEMMA>; | ||
| 4581 | while($line = <LEMMA>) { | ||
| 4582 | chomp $line; | ||
| 4583 | ($id, $postag, $lemma, $rel, $n, $freq, $nbNeighbour, $nbcl) = split /\t/, $line; | ||
| 4584 | |||
| 4585 | $lemmaSet{$id} = {'lemma' => $lemma, | ||
| 4586 | 'postag' => $postag, | ||
| 4587 | 'freq' => $freq, | ||
| 4588 | }; | ||
| 4589 | } | ||
| 4590 | close LEMMA; | ||
| 4591 | |||
| 4592 | warn "load Prox ($vdlmDir/upery-prox.txt)\n"; | ||
| 4593 | |||
| 4594 | open PROX, $vdlmDir . "/upery-prox.txt" or die "no such file $vdlmDir/upery-prox.txt\n"; | ||
| 4595 | $line = <PROX>; | ||
| 4596 | while($line = <PROX>) { | ||
| 4597 | chomp $line; | ||
| 4598 | |||
| 4599 | ($lid1, $lid2, $n1, $n2, $a, $jacc, $lin, $j1, $j2) = split /\t/, $line; | ||
| 4600 | |||
| 4601 | $dist = $jacc; | ||
| 4602 | @wordsInfos = (); | ||
| 4603 | push @wordsInfos, [$lemmaSet{$lid2}->{'lemma'}, $lemmaSet{$lid2}->{'postag'}, $dist]; | ||
| 4604 | |||
| 4605 | $self->_addWord2Thesaurus($lemmaSet{$lid1}->{'lemma'}, $lemmaSet{$lid1}->{'postag'}, $lemmaSet{$lid1}->{'postag'} . "|" . $lemmaSet{$lid1}->{'lemma'}, $DistThesaurus, $DistThesaurusIdx, \@wordsInfos); | ||
| 4606 | |||
| 4607 | } | ||
| 4608 | close PROX; | ||
| 4609 | } | ||
| 4610 | |||
| 4611 | sub _addWord2Thesaurus { | ||
| 4612 | my ($self, $word, $postag, $wordInfo, $DistThesaurus, $DistThesaurusIdx, $wordsInfos) = @_; | ||
| 4613 | |||
| 4614 | my $wordInfoElt; | ||
| 4615 | |||
| 4616 | # warn "add $wordInfo ($word)\n"; | ||
| 4617 | |||
| 4618 | if (!exists $DistThesaurusIdx->{$word}) { | ||
| 4619 | $DistThesaurusIdx->{$word} = []; | ||
| 4620 | } | ||
| 4621 | push @{$DistThesaurusIdx->{$word}}, $wordInfo; | ||
| 4622 | |||
| 4623 | if (!exists $DistThesaurus->{$wordInfo}) { | ||
| 4624 | $DistThesaurus->{$wordInfo} = { 'word' => $word, | ||
| 4625 | 'postag' => $postag, | ||
| 4626 | 'neighbour' => [], | ||
| 4627 | }; | ||
| 4628 | } | ||
| 4629 | |||
| 4630 | foreach $wordInfoElt (@$wordsInfos) { | ||
| 4631 | push @{$DistThesaurus->{$wordInfo}->{'neighbour'}}, {'word' => $wordInfoElt->[0], | ||
| 4632 | 'postag' => $wordInfoElt->[1], | ||
| 4633 | 'dist' => $wordInfoElt->[2], | ||
| 4634 | 'type' => $wordInfoElt->[3], | ||
| 4635 | 'measure_name' => $wordInfoElt->[4], | ||
| 4636 | } | ||
| 4637 | } | ||
| 4638 | } | ||
| 4639 | |||
| 4640 | sub _loadSemanticRelationsWithType { | ||
| 4641 | my ($self, $semanticRelationsFilename, $semanticRelations, $semanticRelationsIdx) = @_; | ||
| 4642 | |||
| 4643 | my $line; | ||
| 4644 | my $T1IF; | ||
| 4645 | my $T2IF; | ||
| 4646 | my $T1LM; | ||
| 4647 | my $T2LM; | ||
| 4648 | my $postag; | ||
| 4649 | my $postag2; | ||
| 4650 | my $type; | ||
| 4651 | my $weight; | ||
| 4652 | my $measure_name; | ||
| 4653 | my @infos; | ||
| 4654 | my @wordsInfos2; | ||
| 4655 | my $i; | ||
| 4656 | my $j; | ||
| 4657 | |||
| 4658 | # warn "open $semanticRelationsFilename\n"; | ||
| 4659 | open SEMREL, "<:utf8", $semanticRelationsFilename or die "no such file $semanticRelationsFilename\n"; | ||
| 4660 | |||
| 4661 | while($line = <SEMREL>) { | ||
| 4662 | chomp $line; | ||
| 4663 | if (($line !~ /^\s*#/) && ($line !~ /^\s*$/) && ($line !~ /^\s*\-/)) { | ||
| 4664 | # warn "$line\n"; | ||
| 4665 | # ($T1IF, $T1LM, $T2IF, $T2LM, $type, $weight, $measure_name) = split / *[\|:] */, $line; | ||
| 4666 | @infos = split / *[\|:] */, $line; | ||
| 4667 | $postag = substr($infos[1], 0, 1); | ||
| 4668 | $postag2 = substr($infos[4], 0, 1); | ||
| 4669 | $type = $infos[6]; | ||
| 4670 | $measure_name = $infos[8]; | ||
| 4671 | $weight = $infos[7]; | ||
| 4672 | |||
| 4673 | for($i=0;$i < 3; $i+=2) { | ||
| 4674 | if ($infos[$i] !~ /^\s*$/) { | ||
| 4675 | for($j=3;$j < 6; $j+=2) { | ||
| 4676 | if ($infos[$j] !~ /^\s*$/) { | ||
| 4677 | @wordsInfos2 = (); | ||
| 4678 | push @wordsInfos2, [$infos[$j], $postag2, $weight, $type, $measure_name]; | ||
| 4679 | |||
| 4680 | $self->_addWord2Thesaurus($infos[$i], $postag, "$postag|". $infos[$i], $semanticRelations, $semanticRelationsIdx, \@wordsInfos2); | ||
| 4681 | |||
| 4682 | push @wordsInfos2, [$infos[$i], $postag, $weight, $type, $measure_name]; | ||
| 4683 | |||
| 4684 | $self->_addWord2Thesaurus($infos[$j], $postag2, "$postag2|". $infos[$j], $semanticRelations, $semanticRelationsIdx, \@wordsInfos2); | ||
| 4685 | # if (!exists $semanticRelations->{$infos[$i]}) { | ||
| 4686 | # $semanticRelations->{$infos[$i]} = []; | ||
| 4687 | # } | ||
| 4688 | # push @{$semanticRelations->{$infos[$i]}}, {'term' => $infos[$j], 'type' => $type, | ||
| 4689 | # 'weight'=> $weight, 'measure' => $measure_name}; | ||
| 4690 | # if (!exists $semanticRelations->{$infos[$j]}) { | ||
| 4691 | # $semanticRelations->{$infos[$j]} = []; | ||
| 4692 | # } | ||
| 4693 | # push @{$semanticRelations->{$infos[$j]}}, {'term' => $infos[$i], 'type' => $type . "_inv", | ||
| 4694 | # 'weight'=> $weight, 'measure' => $measure_name}; | ||
| 4695 | } | ||
| 4696 | } | ||
| 4697 | } | ||
| 4698 | } | ||
| 4699 | } | ||
| 4700 | } | ||
| 4701 | close SEMREL | ||
| 4702 | } | ||
| 4703 | |||
| 4704 | |||
| 4705 | sub _loadSemanticRelationsWithType2 { | ||
| 4706 | my ($self, $semanticRelationsFilename, $semanticRelations) = @_; | ||
| 4707 | |||
| 4708 | my $line; | ||
| 4709 | my $T1IF; | ||
| 4710 | my $T2IF; | ||
| 4711 | my $T1LM; | ||
| 4712 | my $T2LM; | ||
| 4713 | my $type; | ||
| 4714 | my $weight; | ||
| 4715 | my $measure_name; | ||
| 4716 | my @infos; | ||
| 4717 | my $i; | ||
| 4718 | my $j; | ||
| 4719 | |||
| 4720 | # warn "open $semanticRelationsFilename\n"; | ||
| 4721 | open SEMREL, "<:utf8", $semanticRelationsFilename or die "no such file $semanticRelationsFilename\n"; | ||
| 4722 | |||
| 4723 | while($line = <SEMREL>) { | ||
| 4724 | chomp $line; | ||
| 4725 | if (($line !~ /^\s*#/) && ($line !~ /^\s*$/) && ($line !~ /^\s*\-/)) { | ||
| 4726 | # warn "$line\n"; | ||
| 4727 | # ($T1IF, $T1LM, $T2IF, $T2LM, $type, $weight, $measure_name) = split / *[\|:] */, $line; | ||
| 4728 | @infos = split / *[\|:] */, $line; | ||
| 4729 | |||
| 4730 | for($i=0;$i < 2; $i++) { | ||
| 4731 | if ($infos[$i] !~ /^\s*$/) { | ||
| 4732 | for($j=2;$j < 4; $j++) { | ||
| 4733 | if ($infos[$j] !~ /^\s*$/) { | ||
| 4734 | if (!exists $semanticRelations->{$infos[$i]}) { | ||
| 4735 | $semanticRelations->{$infos[$i]} = []; | ||
| 4736 | } | ||
| 4737 | push @{$semanticRelations->{$infos[$i]}}, {'term' => $infos[$j], 'type' => $type, | ||
| 4738 | 'weight'=> $weight, 'measure' => $measure_name}; | ||
| 4739 | if (!exists $semanticRelations->{$infos[$j]}) { | ||
| 4740 | $semanticRelations->{$infos[$j]} = []; | ||
| 4741 | } | ||
| 4742 | push @{$semanticRelations->{$infos[$j]}}, {'term' => $infos[$i], 'type' => $type . "_inv", | ||
| 4743 | 'weight'=> $weight, 'measure' => $measure_name}; | ||
| 4744 | } | ||
| 4745 | } | ||
| 4746 | } | ||
| 4747 | } | ||
| 4748 | } | ||
| 4749 | } | ||
| 4750 | close SEMREL | ||
| 4751 | } | ||
| 4752 | |||
| 4753 | |||
| 4754 | |||
| 4755 | sub _loadSynonymy { | ||
| 4756 | my ($self, $synonymyFilename, $synonymy) = @_; | ||
| 4757 | |||
| 4758 | my $line; | ||
| 4759 | my $T1; | ||
| 4760 | my $T2; | ||
| 4761 | |||
| 4762 | # warn "open $synonymyFilename\n"; | ||
| 4763 | open SYNO, "<:utf8", $synonymyFilename or die "no such file $synonymyFilename\n"; | ||
| 4764 | |||
| 4765 | while($line = <SYNO>) { | ||
| 4766 | chomp $line; | ||
| 4767 | if (($line !~ /^\s*#/) && ($line !~ /^\s*$/) && ($line !~ /^\s*\-/)) { | ||
| 4768 | # warn "$line\n"; | ||
| 4769 | $line =~ s/__...=?//go; | ||
| 4770 | ($T1, $T2) = split / *[\|:] */, $line; | ||
| 4771 | if (!exists $synonymy->{$T1}) { | ||
| 4772 | $synonymy->{$T1} = []; | ||
| 4773 | } | ||
| 4774 | push @{$synonymy->{$T1}}, $T2; | ||
| 4775 | if (!exists $synonymy->{$T2}) { | ||
| 4776 | $synonymy->{$T2} = []; | ||
| 4777 | } | ||
| 4778 | push @{$synonymy->{$T2}}, $T1; | ||
| 4779 | } | ||
| 4780 | } | ||
| 4781 | close SYNO | ||
| 4782 | } | ||
| 4783 | |||
| 4784 | sub _loadException { | ||
| 4785 | my ($self, $resource, $lang, $type) = @_; | ||
| 4786 | |||
| 4787 | return($self->_loadWordList($resource, $lang, $type)); | ||
| 4788 | } | ||
| 4789 | |||
| 4790 | sub _loadWordList { | ||
| 4791 | my ($self, $resource, $lang, $type) = @_; | ||
| 4792 | my $line; | ||
| 4793 | my $filename; | ||
| 4794 | |||
| 4795 | if (defined $resource) { | ||
| 4796 | $self->{$type}->{$lang} = {}; | ||
| 4797 | if (ref($resource) eq "ARRAY") { | ||
| 4798 | foreach $filename (@$resource) { | ||
| 4799 | open EXCEPTION, "<:utf8", $filename or die "no such file $filename\n"; | ||
| 4800 | warn "read $filename\n"; | ||
| 4801 | while($line = <EXCEPTION>) { | ||
| 4802 | chomp $line; | ||
| 4803 | if (($line !~ /^\s*$/o) && ($line !~ /^\s*\#$/o)){ | ||
| 4804 | $self->{$type}->{$lang}->{$line}++; | ||
| 4805 | } | ||
| 4806 | } | ||
| 4807 | close EXCEPTION; | ||
| 4808 | } | ||
| 4809 | } else { | ||
| 4810 | $filename = $resource; | ||
| 4811 | open EXCEPTION, "<:utf8", $filename or die "no such file $filename\n"; | ||
| 4812 | warn "read $filename\n"; | ||
| 4813 | while($line = <EXCEPTION>) { | ||
| 4814 | chomp $line; | ||
| 4815 | if (($line !~ /^\s*$/o) && ($line !~ /^\s*\#$/o)){ | ||
| 4816 | $self->{$type}->{$lang}->{$line}++; | ||
| 4817 | } | ||
| 4818 | } | ||
| 4819 | close EXCEPTION; | ||
| 4820 | } | ||
| 4821 | } | ||
| 4822 | } | ||
| 4823 | |||
| 4824 | sub _loadCorrespondanceList { | ||
| 4825 | my ($self, $resource, $lang, $type) = @_; | ||
| 4826 | my $line; | ||
| 4827 | my $filename; | ||
| 4828 | my $val1; | ||
| 4829 | my $val2; | ||
| 4830 | |||
| 4831 | if (defined $resource) { | ||
| 4832 | $self->{$type}->{$lang} = {}; | ||
| 4833 | if (ref($resource) eq "ARRAY") { | ||
| 4834 | foreach $filename (@$resource) { | ||
| 4835 | open EXCEPTION, "<:utf8", $filename or die "no such file $filename\n"; | ||
| 4836 | while($line = <EXCEPTION>) { | ||
| 4837 | chomp $line; | ||
| 4838 | if (($line !~ /^\s*$/o) && ($line !~ /^\s*\#$/o)){ | ||
| 4839 | ($val1, $val2) = split /\s*:\s*/, $line; | ||
| 4840 | $self->{$type}->{$lang}->{$val1} = $val2; | ||
| 4841 | } | ||
| 4842 | } | ||
| 4843 | close EXCEPTION; | ||
| 4844 | } | ||
| 4845 | } else { | ||
| 4846 | $filename = $resource; | ||
| 4847 | open EXCEPTION, "<:utf8", $filename or die "no such file $filename\n"; | ||
| 4848 | while($line = <EXCEPTION>) { | ||
| 4849 | chomp $line; | ||
| 4850 | if (($line !~ /^\s*$/o) && ($line !~ /^\s*\#$/o)){ | ||
| 4851 | ($val1, $val2) = split /\s*:\s*/, $line; | ||
| 4852 | warn "$val1\n"; | ||
| 4853 | $self->{$type}->{$lang}->{$val1} = $val2; | ||
| 4854 | } | ||
| 4855 | } | ||
| 4856 | close EXCEPTION; | ||
| 4857 | } | ||
| 4858 | } | ||
| 4859 | } | ||
| 4860 | |||
| 4861 | sub _loadTerminology { | ||
| 4862 | my ($self, $terminologyFilename, $terminology, $terminologyIdx) = @_; | ||
| 4863 | my $line; | ||
| 4864 | my @termInfos; | ||
| 4865 | my @semCats; | ||
| 4866 | my $semCat; | ||
| 4867 | |||
| 4868 | |||
| 4869 | open TERMINO, "<:utf8", $terminologyFilename or die "no such file $terminologyFilename\n"; | ||
| 4870 | while($line = <TERMINO>) { | ||
| 4871 | chomp $line; | ||
| 4872 | if (($line !~ /^\s*#/) && ($line !~ /^\s*$/)) { | ||
| 4873 | @termInfos = split / ?[:|] ?/, $line; | ||
| 4874 | |||
| 4875 | if ((defined $termInfos[2]) && ($termInfos[2] ne "")) { | ||
| 4876 | @semCats = split /\//, $termInfos[2]; | ||
| 4877 | # if ($terminologyIdx->{$termInfos[0]}) { | ||
| 4878 | # $terminologyIdx->{$termInfos[0]} = []; | ||
| 4879 | # } | ||
| 4880 | # push @{$terminologyIdx->{$termInfos[0]}}, $termInfos[2]; | ||
| 4881 | $terminologyIdx->{$termInfos[0]}->{lc($termInfos[2])}++; | ||
| 4882 | $terminology->{lc($termInfos[2])}->{$termInfos[0]}++; | ||
| 4883 | |||
| 4884 | for $semCat (@semCats) { | ||
| 4885 | $terminologyIdx->{$termInfos[0]}->{lc($semCat)}++; | ||
| 4886 | $terminology->{lc($semCat)}->{$termInfos[0]}++; | ||
| 4887 | } | ||
| 4888 | } else { | ||
| 4889 | warn "Empty semantic category\n"; | ||
| 4890 | } | ||
| 4891 | } | ||
| 4892 | } | ||
| 4893 | close TERMINO; | ||
| 4894 | } | ||
| 4895 | |||
| 4896 | sub _selectTermInfos { | ||
| 4897 | my ($self, $termToSelect, $terminology, $terminologyIdx, $selectedTermsInTerminology) = @_; | ||
| 4898 | |||
| 4899 | my $semCat; | ||
| 4900 | |||
| 4901 | %$selectedTermsInTerminology = (); | ||
| 4902 | if (exists $terminologyIdx->{$termToSelect}) { | ||
| 4903 | foreach $semCat (keys %{$terminologyIdx->{$termToSelect}}) { | ||
| 4904 | $selectedTermsInTerminology->{$semCat} = $terminology->{$semCat}; | ||
| 4905 | } | ||
| 4906 | } | ||
| 4907 | } | ||
| 4908 | |||
| 4909 | sub _selectSynonyms { | ||
| 4910 | my ($self, $termToSelect, $synonymy, $selectedSynonyms) = @_; | ||
| 4911 | |||
| 4912 | my $synonym; | ||
| 4913 | |||
| 4914 | @$selectedSynonyms = (); | ||
| 4915 | if (exists $synonymy->{$termToSelect}) { | ||
| 4916 | # warn ".. found\n"; | ||
| 4917 | # foreach $synonym (@{$synonymy->{$termToSelect}}) { | ||
| 4918 | push @$selectedSynonyms, @{$synonymy->{$termToSelect}}; | ||
| 4919 | # } | ||
| 4920 | } | ||
| 4921 | } | ||
| 4922 | |||
| 4923 | |||
| 4924 | |||
| 4925 | sub _loadThesaurus { | ||
| 4926 | my ($self, $freDistThesaurusFilename, $freDistThesaurus, $freDistThesaurusIdx) = @_; | ||
| 4927 | |||
| 4928 | my @wordsInfos; | ||
| 4929 | my @wordsInfos2; | ||
| 4930 | my $wordInfo; | ||
| 4931 | my $wordInfoElt; | ||
| 4932 | my $postag; | ||
| 4933 | my $word; | ||
| 4934 | my $dist; | ||
| 4935 | my $postag2; | ||
| 4936 | my $word2; | ||
| 4937 | my $dist2; | ||
| 4938 | my $wordDist; | ||
| 4939 | my $line; | ||
| 4940 | |||
| 4941 | open THESAURUS, '<:utf8', $freDistThesaurusFilename or die "no such file $freDistThesaurusFilename\n"; | ||
| 4942 | |||
| 4943 | while($line = <THESAURUS>) { | ||
| 4944 | chomp $line; | ||
| 4945 | |||
| 4946 | @wordsInfos = split /\t/, $line; | ||
| 4947 | |||
| 4948 | $wordInfo = shift @wordsInfos; | ||
| 4949 | ($postag, $word) = split /\|/, $wordInfo; | ||
| 4950 | |||
| 4951 | @wordsInfos2 = (); | ||
| 4952 | foreach $wordInfoElt (@wordsInfos) { | ||
| 4953 | ($postag2, $wordDist) = split /\|/, $wordInfoElt; | ||
| 4954 | ($word2, $dist2) = split /:/, $wordDist; # / | ||
| 4955 | # push @{$freDistThesaurus->{$wordInfo}->{'neighbour'}}, {'word' => $word, | ||
| 4956 | # 'postag' => $postag, | ||
| 4957 | # 'dist' => $dist, | ||
| 4958 | # } | ||
| 4959 | push @wordsInfos2, [$word2, $postag2, $dist2]; | ||
| 4960 | } | ||
| 4961 | |||
| 4962 | $self->_addWord2Thesaurus($word, $postag, $wordInfo, $freDistThesaurus, $freDistThesaurusIdx, \@wordsInfos2); | ||
| 4963 | } | ||
| 4964 | close THESAURUS; | ||
| 4965 | } | ||
| 4966 | |||
| 4967 | sub _selectWordInfos { | ||
| 4968 | my ($self, $wordToSelect, $withPOStag, $freDistThesaurus, $freDistThesaurusIdx, $selectedWordsInThesaurus) = @_; | ||
| 4969 | |||
| 4970 | my $wordInfo; | ||
| 4971 | |||
| 4972 | %$selectedWordsInThesaurus = (); | ||
| 4973 | if (!$withPOStag) { | ||
| 4974 | if (exists $freDistThesaurusIdx->{$wordToSelect}) { | ||
| 4975 | foreach $wordInfo (@{$freDistThesaurusIdx->{$wordToSelect}}) { | ||
| 4976 | $selectedWordsInThesaurus->{$wordInfo} = $freDistThesaurus->{$wordInfo}; | ||
| 4977 | } | ||
| 4978 | } | ||
| 4979 | } else { | ||
| 4980 | # warn "$wordToSelect\n"; | ||
| 4981 | if (exists $freDistThesaurus->{$wordToSelect}) { | ||
| 4982 | $selectedWordsInThesaurus->{$wordToSelect} = $freDistThesaurus->{$wordToSelect}; | ||
| 4983 | } | ||
| 4984 | } | ||
| 4985 | } | ||
| 4986 | |||
| 4987 | |||
| 4988 | sub _mergeTerms { | ||
| 4989 | my ($self, $document) = @_; | ||
| 4990 | |||
| 4991 | my $term; | ||
| 4992 | my $token; | ||
| 4993 | my $semf; | ||
| 4994 | my $tmpSemf; | ||
| 4995 | my $nextSemf; | ||
| 4996 | |||
| 4997 | my $tmpTerm; | ||
| 4998 | my $end_form; | ||
| 4999 | my @endTokens; | ||
| 5000 | my $type; | ||
| 5001 | my @tmpTermList; | ||
| 5002 | my %tmpTermListH; | ||
| 5003 | my $lang = $document->getAnnotations->getLanguage; | ||
| 5004 | |||
| 5005 | my %medsep = ("," => 1); | ||
| 5006 | |||
| 5007 | my @termlist = @{$document->getAnnotations->getSemanticUnitLevel->getElements}; | ||
| 5008 | |||
| 5009 | warn "[LOG] Merge Terms\n"; | ||
| 5010 | |||
| 5011 | foreach $term (@termlist) { | ||
| 5012 | # warn "termForm: " . $term->getForm . "\n"; | ||
| 5013 | if ($document->getAnnotations->getSemanticUnitLevel->existsElement($term->getId)) { | ||
| 5014 | # nested Term ? | ||
| 5015 | $type = $term->type; | ||
| 5016 | $semf = ""; | ||
| 5017 | # test exists semantic tag ? | ||
| 5018 | if (($term->isTerm) && | ||
| 5019 | ($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $term->getId)) && | ||
| 5020 | (defined($document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]))) { | ||
| 5021 | if (!defined($document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0])) { | ||
| 5022 | warn $term->getForm . ": no defined semantic features\n"; | ||
| 5023 | } | ||
| 5024 | $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]->first_node_first_semantic_category; | ||
| 5025 | } elsif ($term->isNamedEntity) { | ||
| 5026 | $semf = $term->NEtype; | ||
| 5027 | } else { | ||
| 5028 | $semf = "NA"; | ||
| 5029 | } | ||
| 5030 | my $termToken = $term->start_token; | ||
| 5031 | @tmpTermList = (); | ||
| 5032 | %tmpTermListH = (); | ||
| 5033 | |||
| 5034 | foreach $tmpTerm (@{$document->getAnnotations->getSemanticUnitLevel->getElementByOffset($termToken->getFrom)}) { | ||
| 5035 | if ((!$tmpTerm->equals($term)) && ($tmpTerm->type eq $term->type) && | ||
| 5036 | ((($type eq "term") && (($semf eq "NA") || | ||
| 5037 | ($tmpTerm->SemanticFeatureFCEquals($document, $term->getSemanticFeatureFC($document))))) || | ||
| 5038 | (($type eq "named_entity") && ($tmpTerm->NEtype eq $term->NEtype))) | ||
| 5039 | ){ | ||
| 5040 | $tmpTermListH{$tmpTerm->getId} = $tmpTerm; | ||
| 5041 | } | ||
| 5042 | } | ||
| 5043 | while(!$termToken->equals($term->end_token)) { | ||
| 5044 | $termToken = $termToken->next; | ||
| 5045 | foreach $tmpTerm (@{$document->getAnnotations->getSemanticUnitLevel->getElementByOffset($termToken->getFrom)}) { | ||
| 5046 | if ((!$tmpTerm->equals($term)) && ($tmpTerm->type eq $term->type) && | ||
| 5047 | ((($type eq "term") && (($semf eq "NA") || | ||
| 5048 | ($tmpTerm->SemanticFeatureFCEquals($document, $term->getSemanticFeatureFC($document))))) || | ||
| 5049 | (($type eq "named_entity") && ($tmpTerm->NEtype eq $term->NEtype))) | ||
| 5050 | ) { | ||
| 5051 | $tmpTermListH{$tmpTerm->getId} = $tmpTerm; | ||
| 5052 | } | ||
| 5053 | } | ||
| 5054 | } | ||
| 5055 | foreach $tmpTerm (values %tmpTermListH) { | ||
| 5056 | # warn "==> " . $tmpTerm->getForm . "(" . $tmpTerm->getId . " -- $type / " . $tmpTerm->type . ")\n"; | ||
| 5057 | if (($document->getAnnotations->getSemanticUnitLevel->existsElement($term->getId)) && | ||
| 5058 | ($document->getAnnotations->getSemanticUnitLevel->existsElement($tmpTerm->getId)) && | ||
| 5059 | ($tmpTerm->type eq $type) | ||
| 5060 | ) { | ||
| 5061 | if (defined $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $tmpTerm->getId)->[0]) { | ||
| 5062 | $tmpSemf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $tmpTerm->getId)->[0]->first_node_first_semantic_category; | ||
| 5063 | } else { | ||
| 5064 | $tmpSemf = ""; | ||
| 5065 | } | ||
| 5066 | # warn " $semf / $tmpSemf\n"; | ||
| 5067 | if ($term->start_token->getFrom <= $tmpTerm->start_token->getFrom) { | ||
| 5068 | if ($tmpTerm->end_token->getTo <= $term->end_token->getTo) { | ||
| 5069 | if ($tmpSemf eq $semf) { # TO change to compare categories | ||
| 5070 | if ((defined $tmpTerm->canonical_form) && (!defined ($term->canonical_form))) { | ||
| 5071 | $term->canonical_form($tmpTerm->canonical_form); | ||
| 5072 | } | ||
| 5073 | $document->getAnnotations->delSemanticUnit($tmpTerm); | ||
| 5074 | # $document->getAnnotations->delSemanticFeaturesFromTermId($tmpTerm); | ||
| 5075 | } | ||
| 5076 | } else { | ||
| 5077 | # Merge Term if same type | ||
| 5078 | if ($tmpSemf eq $semf) { # TO change to compare categories | ||
| 5079 | $end_form = undef; | ||
| 5080 | @endTokens = (); | ||
| 5081 | # expansion | ||
| 5082 | if ($self->{"EXPAND_TERMS"}->{$lang} == 1) { | ||
| 5083 | if (index($tmpTerm->getForm, "(") > -1) { | ||
| 5084 | $self->_getEndForm($tmpTerm, $document, \$end_form, \@endTokens); | ||
| 5085 | } | ||
| 5086 | } | ||
| 5087 | |||
| 5088 | push @termlist, $self->_semanticUnitMerging($document, $term, $tmpTerm, undef, undef, $end_form, \@endTokens); | ||
| 5089 | } | ||
| 5090 | } | ||
| 5091 | } else { | ||
| 5092 | if (!defined $tmpSemf) { | ||
| 5093 | warn "tmpSemf not defined for " . $term->getForm . " / " . $tmpTerm->getForm . "\n"; | ||
| 5094 | } | ||
| 5095 | # if ($tmpSemf eq $semf) { # TO change to compare categories | ||
| 5096 | # $document->getAnnotations->delSemanticUnit($tmpTerm); | ||
| 5097 | # # $document->getAnnotations->delSemanticFeaturesFromTermId($tmpTerm); | ||
| 5098 | # } | ||
| 5099 | if ($tmpTerm->end_token->getTo <= $term->end_token->getTo) { | ||
| 5100 | # Merge Term if same type | ||
| 5101 | if ($tmpSemf eq $semf) { # TO change to compare categories | ||
| 5102 | $end_form = undef; | ||
| 5103 | @endTokens = (); | ||
| 5104 | # expansion | ||
| 5105 | if ($self->{"EXPAND_TERMS"}->{$lang} == 1) { | ||
| 5106 | if (index($term->getForm, "(") > -1) { | ||
| 5107 | $self->_getEndForm($term, $document, \$end_form, \@endTokens); | ||
| 5108 | } | ||
| 5109 | } | ||
| 5110 | push @termlist, $self->_semanticUnitMerging($document, $tmpTerm, $term, undef, undef, $end_form, \@endTokens); | ||
| 5111 | } | ||
| 5112 | } else { | ||
| 5113 | if ($tmpSemf eq $semf) { # TO change to compare categories | ||
| 5114 | if ((!defined $tmpTerm->canonical_form) && (defined ($term->canonical_form))) { | ||
| 5115 | $tmpTerm->canonical_form($term->canonical_form); | ||
| 5116 | } | ||
| 5117 | $document->getAnnotations->delSemanticUnit($term); | ||
| 5118 | # $document->getAnnotations->delSemanticFeaturesFromTermId($term); | ||
| 5119 | } | ||
| 5120 | } | ||
| 5121 | |||
| 5122 | } | ||
| 5123 | } | ||
| 5124 | } | ||
| 5125 | |||
| 5126 | if ($self->{"EXPAND_TERMS"}->{$lang} == 1) { | ||
| 5127 | |||
| 5128 | if ($document->getAnnotations->getSemanticUnitLevel->existsElement($term->getId)) { | ||
| 5129 | # expansion | ||
| 5130 | # following NEs with or without separators | ||
| 5131 | my @sepTokens; | ||
| 5132 | my $sep_form; | ||
| 5133 | $token = $term->end_token; | ||
| 5134 | while ((defined $token->next) && | ||
| 5135 | (($token->next->isSep) || ($token->next->isSymb)) && | ||
| 5136 | (!exists($medsep{$token->next->getContent})) && | ||
| 5137 | (!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) | ||
| 5138 | ){ | ||
| 5139 | $token = $token->next; | ||
| 5140 | push @sepTokens, $token; | ||
| 5141 | $sep_form .= $token->getContent; | ||
| 5142 | } | ||
| 5143 | # expansion | ||
| 5144 | if ((((defined $sep_form) && (index ($sep_form , "(") > -1 )) || (index ($term->getForm , "(") > -1 )) | ||
| 5145 | && (index ($term->getForm , ")") == -1 )) { | ||
| 5146 | $end_form = ""; | ||
| 5147 | @endTokens = (); | ||
| 5148 | |||
| 5149 | # expansion | ||
| 5150 | while ((defined $token->next) && | ||
| 5151 | (index($token->next->getContent, ")") == -1) && | ||
| 5152 | (!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) | ||
| 5153 | ) { | ||
| 5154 | $token = $token->next; | ||
| 5155 | push @endTokens, $token; | ||
| 5156 | $end_form .= $token->getContent; | ||
| 5157 | } | ||
| 5158 | # expansion | ||
| 5159 | if ((index($token->next->getContent, ")") > -1)) { | ||
| 5160 | $token = $token->next; | ||
| 5161 | push @endTokens, $token; | ||
| 5162 | $end_form .= $token->getContent; | ||
| 5163 | |||
| 5164 | } else { | ||
| 5165 | $end_form = ""; | ||
| 5166 | } | ||
| 5167 | if ($end_form eq "") { | ||
| 5168 | $end_form = undef; | ||
| 5169 | } | ||
| 5170 | |||
| 5171 | if (defined $end_form) { | ||
| 5172 | push @termlist, $self->_semanticUnitMerging($document, $term, undef, $sep_form, \@sepTokens, $end_form, \@endTokens); | ||
| 5173 | } | ||
| 5174 | } else { | ||
| 5175 | if (!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 5176 | foreach my $nextTerm (@{$document->getAnnotations->getSemanticUnitLevel->getElementByOffset($token->next->getFrom)}) { | ||
| 5177 | if (($nextTerm->type eq $type) && ($document->getAnnotations->getSemanticUnitLevel->existsElement($term->getId))) { | ||
| 5178 | if (defined $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $nextTerm->getId)->[0]) { | ||
| 5179 | $nextSemf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $nextTerm->getId)->[0]->first_node_first_semantic_category; | ||
| 5180 | } else { | ||
| 5181 | $nextSemf = ""; | ||
| 5182 | } | ||
| 5183 | # expansion | ||
| 5184 | if (($nextTerm->start_token->getTo >= $term->start_token->getFrom) && ( | ||
| 5185 | (!defined $semf) || (!defined $nextSemf) || ($nextSemf eq $semf))) { | ||
| 5186 | # TODO if the sepform contains (, going to ) | ||
| 5187 | $end_form = undef; | ||
| 5188 | @endTokens = (); | ||
| 5189 | if ((defined $sep_form) && (index ($sep_form , "(") > -1 ) || (index ($term->getForm , "(") > -1 ) || (index ($nextTerm->getForm , "(") > -1 )) { | ||
| 5190 | $self->_getEndForm($nextTerm, $document, \$end_form, \@endTokens); | ||
| 5191 | |||
| 5192 | } | ||
| 5193 | push @termlist, $self->_semanticUnitMerging($document, $term, $nextTerm, $sep_form, \@sepTokens, $end_form, \@endTokens); | ||
| 5194 | } | ||
| 5195 | } | ||
| 5196 | } | ||
| 5197 | } | ||
| 5198 | } | ||
| 5199 | } | ||
| 5200 | } | ||
| 5201 | } | ||
| 5202 | } | ||
| 5203 | } | ||
| 5204 | |||
| 5205 | sub _printVocabulary { | ||
| 5206 | my ($self) = @_; | ||
| 5207 | |||
| 5208 | my $word; | ||
| 5209 | my $lemma; | ||
| 5210 | my $document; | ||
| 5211 | |||
| 5212 | my %vocabulary; | ||
| 5213 | |||
| 5214 | foreach $document (@{$self->_documentSet}) { | ||
| 5215 | foreach $word (@{$document->getAnnotations->getWordLevel->getElements}) { | ||
| 5216 | $vocabulary{$word->getForm}++; | ||
| 5217 | $vocabulary{$word->getLemma($document)->canonical_form}++; | ||
| 5218 | } | ||
| 5219 | print lc(join("\n", keys(%vocabulary))) . "\n"; | ||
| 5220 | } | ||
| 5221 | } | ||
| 5222 | |||
| 5223 | sub _printVocabulary_FF { | ||
| 5224 | my ($self) = @_; | ||
| 5225 | |||
| 5226 | my $word; | ||
| 5227 | my $lemma; | ||
| 5228 | my $document; | ||
| 5229 | |||
| 5230 | my %vocabulary; | ||
| 5231 | |||
| 5232 | foreach $document (@{$self->_documentSet}) { | ||
| 5233 | foreach $word (@{$document->getAnnotations->getWordLevel->getElements}) { | ||
| 5234 | $vocabulary{$word->getForm}++; | ||
| 5235 | } | ||
| 5236 | print join("\n", keys(%vocabulary)) . "\n"; | ||
| 5237 | } | ||
| 5238 | } | ||
| 5239 | |||
| 5240 | sub _printVocabulary_LM { | ||
| 5241 | my ($self) = @_; | ||
| 5242 | |||
| 5243 | my $word; | ||
| 5244 | my $lemma; | ||
| 5245 | my $document; | ||
| 5246 | |||
| 5247 | my %vocabulary; | ||
| 5248 | |||
| 5249 | foreach $document (@{$self->_documentSet}) { | ||
| 5250 | foreach $word (@{$document->getAnnotations->getWordLevel->getElements}) { | ||
| 5251 | $vocabulary{$word->getLemma($document)->canonical_form}++; | ||
| 5252 | } | ||
| 5253 | print join("\n", keys(%vocabulary)) . "\n"; | ||
| 5254 | } | ||
| 5255 | } | ||
| 5256 | |||
| 5257 | sub _printTagSet { | ||
| 5258 | my ($self) = @_; | ||
| 5259 | |||
| 5260 | my $word; | ||
| 5261 | my $lemma; | ||
| 5262 | my $document; | ||
| 5263 | |||
| 5264 | my %Tagset; | ||
| 5265 | |||
| 5266 | foreach $document (@{$self->_documentSet}) { | ||
| 5267 | foreach $word (@{$document->getAnnotations->getWordLevel->getElements}) { | ||
| 5268 | $Tagset{$word->getMorphoSyntacticFeatures($document)->syntactic_category}++; | ||
| 5269 | } | ||
| 5270 | print join("\n", keys(%Tagset)) . "\n"; | ||
| 5271 | } | ||
| 5272 | } | ||
| 5273 | |||
| 5274 | sub _convert2ascii { | ||
| 5275 | my($self, $str, $sep) = @_; | ||
| 5276 | |||
| 5277 | if (!defined $sep) { | ||
| 5278 | $sep = "_"; | ||
| 5279 | } | ||
| 5280 | |||
| 5281 | $str =~ tr/è/e/; | ||
| 5282 | $str =~ tr/ÁÂÀÅÃÄÇÉÊÈËÍÎÌÏÑÓÔÓØÕÖÚÛÙÜÝáâàåãäçéêèëíîìïñóôòøõöúûùüý/AAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuy/; | ||
| 5283 | |||
| 5284 | $str =~ s/æ/ae/g; | ||
| 5285 | $str =~ s/Æ/AE/g; | ||
| 5286 | $str =~ s/œ/oe/g; | ||
| 5287 | $str =~ s/Œ/OE/g; | ||
| 5288 | |||
| 5289 | return(lc($str)); | ||
| 5290 | } | ||
| 5291 | |||
| 5292 | |||
| 5293 | sub _convertHTML { | ||
| 5294 | my($self, $str, $sep) = @_; | ||
| 5295 | |||
| 5296 | if (!defined $sep) { | ||
| 5297 | $sep = "_"; | ||
| 5298 | } | ||
| 5299 | |||
| 5300 | $str =~ s/&/\&/go; | ||
| 5301 | $str =~ s/"/\"/og; | ||
| 5302 | $str =~ s/'/\'/og; | ||
| 5303 | $str =~ s/</</og; | ||
| 5304 | $str =~ s/>/>/og; | ||
| 5305 | |||
| 5306 | return(lc($str)); | ||
| 5307 | } | ||
| 5308 | |||
| 5309 | sub _convertChar { | ||
| 5310 | my($self, $str, $sep) = @_; | ||
| 5311 | |||
| 5312 | if (!defined $sep) { | ||
| 5313 | $sep = "_"; | ||
| 5314 | } | ||
| 5315 | |||
| 5316 | $str =~ s/\//$sep/g; | ||
| 5317 | $str =~ s/ +/$sep/g; | ||
| 5318 | $str =~ s/:/$sep/g; | ||
| 5319 | $str =~ s/!/$sep/g; | ||
| 5320 | $str =~ s/\?/$sep/g; | ||
| 5321 | $str =~ s/\&/$sep/g; | ||
| 5322 | $str =~ s/\'/$sep/g; | ||
| 5323 | $str =~ s/$sep+/$sep/g; | ||
| 5324 | # $str =~ s/\.+$//g; | ||
| 5325 | return(lc($str)); | ||
| 5326 | } | ||
| 5327 | |||
| 5328 | sub findNamedEntityAfter { | ||
| 5329 | my ($self, $semantic_unit, $categories, $semanticFeatures, $document, $windowLimits, $wordLimit) = @_; | ||
| 5330 | |||
| 5331 | my $token; | ||
| 5332 | my $term; | ||
| 5333 | my @relatedNE; | ||
| 5334 | |||
| 5335 | my %words; | ||
| 5336 | my $reachedLimit = 0; | ||
| 5337 | |||
| 5338 | $token = $semantic_unit->end_token; | ||
| 5339 | $token = $token->next; | ||
| 5340 | |||
| 5341 | # warn "Limit: " . $windowLimits->{'after'} . "\n"; | ||
| 5342 | |||
| 5343 | while((defined $token) && ($token->getFrom <= $windowLimits->{'after'}) && ($reachedLimit == 0) && ((!defined $wordLimit) || (scalar(keys %words) < $wordLimit))) { | ||
| 5344 | # warn ">token: " . $token->getContent . " (" . $token->getFrom . ")\n"; | ||
| 5345 | my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 5346 | if (scalar(@words) > 0) { | ||
| 5347 | $words{$words[0]->getId}++; | ||
| 5348 | } | ||
| 5349 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 5350 | # warn "\t=> " . $term->getForm . "?\n"; | ||
| 5351 | if ($term->isNamedEntity) { | ||
| 5352 | # warn "named entity: " . $term->getForm . "\n"; | ||
| 5353 | if (exists $categories->{$term->NEtype}) { | ||
| 5354 | # warn "===\n"; | ||
| 5355 | push @relatedNE, $term; | ||
| 5356 | $token = $term->end_token; | ||
| 5357 | } | ||
| 5358 | } elsif ((!$term->equals($semantic_unit)) && ($term->isTerm)# && | ||
| 5359 | # ($document->getAnnotations->getSemanticUnitLevel->existsElement($term->getId)) | ||
| 5360 | ) { | ||
| 5361 | if ((defined $semanticFeatures) && (scalar(keys %$semanticFeatures) > 0)) { | ||
| 5362 | if ($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $term->getId)) { | ||
| 5363 | my $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]; | ||
| 5364 | if ((defined $semf) && ($semanticFeatures->{$semf->first_node_first_semantic_category})) { | ||
| 5365 | $reachedLimit = 1; | ||
| 5366 | } | ||
| 5367 | } | ||
| 5368 | } else { | ||
| 5369 | $reachedLimit = 1; | ||
| 5370 | } | ||
| 5371 | } | ||
| 5372 | } | ||
| 5373 | if ($token->isNum) { | ||
| 5374 | if (!$document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token",$token->getId)) { | ||
| 5375 | warn "add numeric as quantities\n"; | ||
| 5376 | my $namedEntity = Lingua::Ogmios::Annotations::SemanticUnit->newNamedEntity( | ||
| 5377 | {'form' => $token->getContent, | ||
| 5378 | 'named_entity_type' => 'numeric_internal', | ||
| 5379 | 'list_refid_token' => [$token], | ||
| 5380 | 'canonical_form' => $token->getContent, | ||
| 5381 | }); | ||
| 5382 | $document->getAnnotations->addSemanticUnit($namedEntity); | ||
| 5383 | push @relatedNE, $namedEntity; | ||
| 5384 | } | ||
| 5385 | } | ||
| 5386 | $token = $token->next; | ||
| 5387 | # warn ">next token: " . $token->getContent . " (" . $token->getFrom . ") ($reachedLimit)\n"; | ||
| 5388 | }; | ||
| 5389 | # warn "OUT\n"; | ||
| 5390 | return(\@relatedNE); | ||
| 5391 | } | ||
| 5392 | |||
| 5393 | |||
| 5394 | sub findNamedEntityBefore { | ||
| 5395 | my ($self, $semantic_unit, $categories, $semanticFeatures, $document, $windowLimits, $wordLimit) = @_; | ||
| 5396 | |||
| 5397 | my $token; | ||
| 5398 | my $term; | ||
| 5399 | my @relatedNE; | ||
| 5400 | |||
| 5401 | my $category = "modeadm"; | ||
| 5402 | my %words; | ||
| 5403 | my $reachedLimit = 0; | ||
| 5404 | |||
| 5405 | $token = $semantic_unit->start_token; | ||
| 5406 | $token = $token->previous; | ||
| 5407 | %words = (); | ||
| 5408 | |||
| 5409 | # warn "Limit: " . $windowLimits->{'before'} . "\n"; | ||
| 5410 | |||
| 5411 | # warn "Semantic_unit: " . $semantic_unit->getForm . "\n"; | ||
| 5412 | while((defined $token) && ($token->getFrom >= $windowLimits->{'before'}) && ($reachedLimit == 0) && ((!defined $wordLimit) || (scalar(keys %words) < $wordLimit))) { | ||
| 5413 | # warn ">token: " . $token->getContent . " (" . $token->getFrom . ")\n"; | ||
| 5414 | my @words = @{$document->getAnnotations->getWordLevel->getElementByToken($token)}; | ||
| 5415 | if (scalar(@words) > 0) { | ||
| 5416 | $words{$words[0]->getId}++; | ||
| 5417 | } | ||
| 5418 | # if (!$reachedLimit) { | ||
| 5419 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 5420 | # warn "\t=> " . $term->getForm . "?\n"; | ||
| 5421 | if ($term->isNamedEntity) { | ||
| 5422 | # warn "named entity: " . $term->getForm . "\n"; | ||
| 5423 | if ((!defined $categories) || (exists $categories->{$term->NEtype})) { | ||
| 5424 | # warn "===\n"; | ||
| 5425 | push @relatedNE, $term; | ||
| 5426 | $token = $term->start_token; | ||
| 5427 | } | ||
| 5428 | } elsif ((!$term->equals($semantic_unit)) && ($term->isTerm)) { | ||
| 5429 | if ((defined $semanticFeatures) && (scalar(keys %$semanticFeatures) > 0)) { | ||
| 5430 | if ($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $term->getId)) { | ||
| 5431 | my $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]; | ||
| 5432 | if ((defined $semf) && ($semanticFeatures->{$semf->first_node_first_semantic_category})) { | ||
| 5433 | $reachedLimit = 1; | ||
| 5434 | } | ||
| 5435 | } | ||
| 5436 | } else { | ||
| 5437 | $reachedLimit = 1; | ||
| 5438 | } | ||
| 5439 | } | ||
| 5440 | # } | ||
| 5441 | } | ||
| 5442 | if ($token->isNum) { | ||
| 5443 | if (!$document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token",$token->getId)) { | ||
| 5444 | warn "add numeric as quantities\n"; | ||
| 5445 | my $namedEntity = Lingua::Ogmios::Annotations::SemanticUnit->newNamedEntity( | ||
| 5446 | {'form' => $token->getContent, | ||
| 5447 | 'named_entity_type' => 'numeric_internal', | ||
| 5448 | 'list_refid_token' => [$token], | ||
| 5449 | 'canonical_form' => $token->getContent, | ||
| 5450 | }); | ||
| 5451 | $document->getAnnotations->addSemanticUnit($namedEntity); | ||
| 5452 | push @relatedNE, $namedEntity; | ||
| 5453 | } | ||
| 5454 | } | ||
| 5455 | $token = $token->previous; | ||
| 5456 | # warn ">prevtoken: " . $token->getContent . " (" . $token->getFrom . ") ($reachedLimit)\n"; | ||
| 5457 | }; | ||
| 5458 | # warn "OUT\n"; | ||
| 5459 | return(\@relatedNE); | ||
| 5460 | } | ||
| 5461 | |||
| 5462 | sub getWindowLimits { | ||
| 5463 | my ($self, $semantic_unit, $document, $semanticFeatures, $goThroughtList, $additionalLimits) = @_; | ||
| 5464 | |||
| 5465 | my $limitBeforeOffset; | ||
| 5466 | my $limitAfterOffset; | ||
| 5467 | |||
| 5468 | my $token; | ||
| 5469 | # my $limitLine; | ||
| 5470 | |||
| 5471 | # my $line = $termIdx->{$semantic_unit->type . $semantic_unit->getId}->{info_e}->[0]; | ||
| 5472 | # my $E_char = $termIdx->{$semantic_unit->type . $semantic_unit->getId}->{info_e}->[1]; | ||
| 5473 | |||
| 5474 | # # warn "\nSearch Window limits for " . $semantic_unit->getForm . " from $line / $E_char (" . $semantic_unit->start_token->getFrom . ")\n"; | ||
| 5475 | |||
| 5476 | # # comput before limit | ||
| 5477 | # my $shift = 0; | ||
| 5478 | # do { | ||
| 5479 | # $limitLine = $line - 2 - $shift; | ||
| 5480 | # if ($limitLine < 1) { | ||
| 5481 | # $limitLine = 1; | ||
| 5482 | # } | ||
| 5483 | # $shift++; | ||
| 5484 | # } while(!exists($documentIndex->{"I2O"}->{"$limitLine:0"})); | ||
| 5485 | # $limitBeforeOffset = $documentIndex->{"I2O"}->{"$limitLine:0"}->[2]; | ||
| 5486 | |||
| 5487 | |||
| 5488 | # comput after limit | ||
| 5489 | # $limitLine = $line + 2; | ||
| 5490 | # if ($limitLine > $documentIndex->{"I2OMaxLine"}) { | ||
| 5491 | # $limitLine = $documentIndex->{"I2OMaxLine"}; | ||
| 5492 | # } | ||
| 5493 | # $limitLine++; | ||
| 5494 | # $limitAfterOffset = $documentIndex->{"I2O"}->{"$limitLine:0"}->[2]; | ||
| 5495 | |||
| 5496 | my $regex; | ||
| 5497 | my $continue = 1; | ||
| 5498 | my $nexttoken; | ||
| 5499 | my $semf; | ||
| 5500 | my $term; | ||
| 5501 | my $word; | ||
| 5502 | |||
| 5503 | my $lang = $document->getAnnotations->getLanguage; | ||
| 5504 | $token = $semantic_unit->end_token; | ||
| 5505 | # warn $semantic_unit->getId . "\n"; | ||
| 5506 | # warn $semantic_unit->getForm . "\n"; | ||
| 5507 | do { | ||
| 5508 | if ((!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) && | ||
| 5509 | (!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $token->getId)) && | ||
| 5510 | ((!defined $additionalLimits) || (!defined $additionalLimits->{'afterLimit'}) || ($token->getFrom < $additionalLimits->{'afterLimit'}))) { | ||
| 5511 | |||
| 5512 | $token = $token->next; | ||
| 5513 | # warn $token->getContent . "\n"; | ||
| 5514 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 5515 | if ($term->isTerm) { | ||
| 5516 | $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]; | ||
| 5517 | if ((defined $semf) && (exists $semanticFeatures->{$semf->first_node_first_semantic_category})) { | ||
| 5518 | $continue = 0; | ||
| 5519 | $token = $token->previous; | ||
| 5520 | last; | ||
| 5521 | } | ||
| 5522 | } | ||
| 5523 | } | ||
| 5524 | if ($continue) { | ||
| 5525 | foreach $word (@{$document->getAnnotations->getWordLevel->getElementByToken($token)}) { | ||
| 5526 | if (exists($self->{'lexicon_limit_after'}->{$lang}->{$word->getForm})) { | ||
| 5527 | # warn "find and\n"; | ||
| 5528 | $continue = 0; | ||
| 5529 | $token = $word->start_token->previous; | ||
| 5530 | last; | ||
| 5531 | } | ||
| 5532 | } | ||
| 5533 | } | ||
| 5534 | # punctuation ? | ||
| 5535 | # Taking into account list ? | ||
| 5536 | $regex = $additionalLimits->{'afterRegex'}; | ||
| 5537 | # warn "regex: $regex\n"; | ||
| 5538 | if ((defined $regex) && ($token->getContent =~ /$regex/)) { | ||
| 5539 | $continue = 0; #by default | ||
| 5540 | |||
| 5541 | if ($goThroughtList > 0) { | ||
| 5542 | # warn "this part of the code has not been checked\n"; | ||
| 5543 | $nexttoken = $token; | ||
| 5544 | $token = $token->previous; | ||
| 5545 | do { | ||
| 5546 | $nexttoken = $nexttoken->next; | ||
| 5547 | } while(($nexttoken->isSep) && | ||
| 5548 | (!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $nexttoken->getId)) && | ||
| 5549 | (!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $nexttoken->getId)) && | ||
| 5550 | ((!defined $additionalLimits) || ($nexttoken->getFrom < $additionalLimits->{'afterLimit'})) | ||
| 5551 | ); | ||
| 5552 | |||
| 5553 | foreach my $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($nexttoken)}) { | ||
| 5554 | if ($term->isNamedEntity) { | ||
| 5555 | # warn "find a named Entity: " . $document->getAnnotations->getSemanticUnitLevel->getElementByToken($nexttoken)->[0]->getForm | ||
| 5556 | # . "\n"; | ||
| 5557 | $continue = 1; | ||
| 5558 | $token = $term->end_token; | ||
| 5559 | last; | ||
| 5560 | } | ||
| 5561 | } | ||
| 5562 | if (!$continue) { | ||
| 5563 | foreach $word (@{$document->getAnnotations->getWordLevel->getElementByToken($token)}) { | ||
| 5564 | if ($word->getForm eq "and") { | ||
| 5565 | # warn "find and\n"; | ||
| 5566 | $continue = 1; | ||
| 5567 | $token = $word->end_token; | ||
| 5568 | last | ||
| 5569 | } | ||
| 5570 | } | ||
| 5571 | } | ||
| 5572 | } | ||
| 5573 | } | ||
| 5574 | # warn "continue : $continue\n"; | ||
| 5575 | } | ||
| 5576 | } while ((!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) && | ||
| 5577 | (!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $token->getId)) && | ||
| 5578 | ((!defined $additionalLimits) || (!defined $additionalLimits->{'afterLimit'}) || ($token->getFrom < $additionalLimits->{'afterLimit'})) && | ||
| 5579 | ($continue) | ||
| 5580 | ); | ||
| 5581 | |||
| 5582 | $limitAfterOffset = $token->getFrom; | ||
| 5583 | |||
| 5584 | # warn "Limit\n"; | ||
| 5585 | # warn "\tAfter: " . $token->getContent . " (" . $token->getId . ")\n"; | ||
| 5586 | |||
| 5587 | |||
| 5588 | # warn "search restricted window before\n"; | ||
| 5589 | $token = $semantic_unit->start_token; | ||
| 5590 | $continue = 1; | ||
| 5591 | while ((!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_start_token", $token->getId)) && | ||
| 5592 | (!$document->getAnnotations->getSectionLevel->existsElementFromIndex("from", $token->getId)) && | ||
| 5593 | ((!defined $additionalLimits) || (!defined $additionalLimits->{'beforeLimit'}) || ($token->getFrom < $additionalLimits->{'beforeLimit'})) && | ||
| 5594 | ($continue) | ||
| 5595 | ) { | ||
| 5596 | $token = $token->previous; | ||
| 5597 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 5598 | if ($term->isTerm) { | ||
| 5599 | $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)->[0]; | ||
| 5600 | if ((defined $semf) && (exists $semanticFeatures->{$semf->first_node_first_semantic_category})) { | ||
| 5601 | $continue = 0; | ||
| 5602 | $token = $token->next; | ||
| 5603 | last; | ||
| 5604 | } | ||
| 5605 | } | ||
| 5606 | } | ||
| 5607 | if ($continue) { | ||
| 5608 | foreach $word (@{$document->getAnnotations->getWordLevel->getElementByToken($token)}) { | ||
| 5609 | if (exists($self->{'lexicon_limit_after'}->{$lang}->{$word->getForm})) { | ||
| 5610 | # warn "find and\n"; | ||
| 5611 | $continue = 0; | ||
| 5612 | $token = $word->start_token->previous; | ||
| 5613 | last; | ||
| 5614 | } | ||
| 5615 | } | ||
| 5616 | } | ||
| 5617 | $regex = $additionalLimits->{'beforeRegex'}; | ||
| 5618 | # warn "regex: $regex\n"; | ||
| 5619 | if ((defined $regex) && ($token->getContent =~ /$regex/)) { | ||
| 5620 | $continue = 0; #by default | ||
| 5621 | $token = $token->next; | ||
| 5622 | } else { | ||
| 5623 | # warn "OK\n"; | ||
| 5624 | } | ||
| 5625 | } ; | ||
| 5626 | $limitBeforeOffset = $token->getFrom; | ||
| 5627 | # warn "\tBefore: " . $token->getContent . "\n"; | ||
| 5628 | |||
| 5629 | return($limitBeforeOffset, $limitAfterOffset); | ||
| 5630 | } | ||
| 5631 | |||
| 5632 | sub _addRelation_NE_Term { | ||
| 5633 | my ($self, $document, $semantic_feature_list, $additionalLimits, $correspondanceList) = @_; | ||
| 5634 | |||
| 5635 | my $lang = $document->getAnnotations->getLanguage; | ||
| 5636 | my $term; | ||
| 5637 | my $semcat; | ||
| 5638 | my $semf; | ||
| 5639 | my $termlist; | ||
| 5640 | my @named_entities; | ||
| 5641 | my %limits; | ||
| 5642 | my $named_entity; | ||
| 5643 | |||
| 5644 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElements}) { | ||
| 5645 | if (($term->isTerm) && (defined $term->getSemanticFeatureFC($document))) { | ||
| 5646 | foreach $semf (@{$document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $term->getId)}) { | ||
| 5647 | $termlist = 0; | ||
| 5648 | foreach $semcat (@{$semf->semantic_category}) { | ||
| 5649 | if (!defined $semantic_feature_list) { | ||
| 5650 | $termlist++; | ||
| 5651 | } elsif (exists($semantic_feature_list->{$semcat->[0]})) { | ||
| 5652 | $termlist++; | ||
| 5653 | } | ||
| 5654 | } | ||
| 5655 | } | ||
| 5656 | if ($termlist > 0) { | ||
| 5657 | # warn "find named entities for " . $term->getForm . "(" . $term->getFrom . ")\n"; | ||
| 5658 | @named_entities = (); | ||
| 5659 | ($limits{'before'}, $limits{'after'}) = $self->getWindowLimits($term, $document, undef, 0, $additionalLimits); | ||
| 5660 | push @named_entities, @{$self->findNamedEntityBefore($term, $self->{'named_entity_categories'}->{$lang}, undef, $document, \%limits, undef)}; | ||
| 5661 | # warn "\tfound " . scalar(@named_entities) . " named entities (before)\n"; | ||
| 5662 | push @named_entities, @{$self->findNamedEntityAfter($term, $self->{'named_entity_categories'}->{$lang}, undef, $document, \%limits, undef)}; | ||
| 5663 | # warn "\tfound " . scalar(@named_entities) . " named entities (after)\n"; | ||
| 5664 | foreach $named_entity (@named_entities) { | ||
| 5665 | if ($named_entity->NEtype eq 'numeric_internal') { | ||
| 5666 | if ((defined $correspondanceList) && (exists $correspondanceList->{$term->getSemanticFeatureFC($document)})) { | ||
| 5667 | # warn "change NE type in " . $corresondanceList->{$term->getSemanticFeatureFC($document)} . "\n"; | ||
| 5668 | $named_entity->NEtype($correspondanceList->{$term->getSemanticFeatureFC($document)}); | ||
| 5669 | $term->incr_weight('quantified', 1); | ||
| 5670 | } | ||
| 5671 | } else { | ||
| 5672 | $term->incr_weight('quantified', 1); | ||
| 5673 | } | ||
| 5674 | # warn "add relation between " . $term->getForm . " (" . $term->getId . ") and " . $named_entity->getForm . "\n"; | ||
| 5675 | $document->getAnnotations->addDomainSpecificRelation( | ||
| 5676 | Lingua::Ogmios::Annotations::DomainSpecificRelation->new( | ||
| 5677 | {'domain_specific_relation_type' => "has_quantity", | ||
| 5678 | 'list_refid_semantic_unit' => [$term, $named_entity] | ||
| 5679 | }) | ||
| 5680 | ); | ||
| 5681 | } | ||
| 5682 | if (scalar(@named_entities) == 0 ) { | ||
| 5683 | if (!$term->existsWeight('quantified')) { | ||
| 5684 | $term->weight('quantified', 0); | ||
| 5685 | } | ||
| 5686 | } | ||
| 5687 | } else { | ||
| 5688 | if (!$term->existsWeight('quantified')) { | ||
| 5689 | $term->weight('quantified', 0); | ||
| 5690 | } | ||
| 5691 | |||
| 5692 | } | ||
| 5693 | } | ||
| 5694 | } | ||
| 5695 | } | ||
| 5696 | |||
| 5697 | sub _arff2deft { | ||
| 5698 | my ($self, $value) = @_; | ||
| 5699 | #[A-Z] | ||
| 5700 | # [a-z] | ||
| 5701 | $value =~ tr/ÁÂÀÅÃÄÇÉÊÈËÍÎÌÏÑÓÔÓØÕÖÚÛÙÜÝáâàåãäçéêèëíîìïñóôòøõöúûùüý[A-Z]/AAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuy[a-z]/; | ||
| 5702 | # $value =~ tr/ÁÂÀÅÃÄÇÉÊÈËÍÎÌÏÑÓÔÓØÕÖÚÛÙÜÝáâàåãäçéêèëíîìïñóôòøõöúûùüý/AAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuy/; | ||
| 5703 | # $value =~ tr/é[A-Z]/e[a-z]/; | ||
| 5704 | # $value = lc($value); | ||
| 5705 | # $str =~ $value =~ tr/ÁÂÀÅÃÄ/AAAAAA/; | ||
| 5706 | # $str =~ $value =~ tr/ÇÉÊÈËÍÎÌÏÑÓÔÓØÕÖÚÛÙÜÝ/CEEEEIIIINOOOOOOUUUUY/; | ||
| 5707 | # $str =~ $value =~ tr/áâàåãäçéêèëíîìïñóôòøõöúûùüý/aaaaaaceeeeiiiinoooooouuuuy/; | ||
| 5708 | # $str =~ $value =~ tr/[A-Z]/[a-z]/; | ||
| 5709 | # $str =~ s/é/e/g; | ||
| 5710 | # $str =~ | ||
| 5711 | # $value =~ tr/ÁÂÀÅÃÄÇÉÊÈËÍÎÌÏÑÓÔÓØÕÖÚÛÙÜÝáâàåãäçéêèëíîìïñóôòøõöúûùüý[A-Z]/AAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuy[a-z]/; | ||
| 5712 | # $str =~ | ||
| 5713 | # $value =~ tr/ÁÂÀÅÃÄÇÉÊÈËÍÎÌÏÑÓÔÓØÕÖÚÛÙÜÝáâàåãäçéêèëíîìïñóôòøõöúûùüý[A-Z]/AAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuy[a-z]/; | ||
| 5714 | # $str =~ | ||
| 5715 | # $value =~ tr/ÁÂÀÅÃÄÇÉÊÈËÍÎÌÏÑÓÔÓØÕÖÚÛÙÜÝáâàåãäçéêèëíîìïñóôòøõöúûùüý[A-Z]/AAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuy[a-z]/; | ||
| 5716 | $value =~ s/æ/ae/g; | ||
| 5717 | $value =~ s/Æ/AE/g; | ||
| 5718 | $value =~ s/œ/oe/g; | ||
| 5719 | $value =~ s/Œ/OE/g; | ||
| 5720 | $value =~ s/&/\&/go; | ||
| 5721 | $value =~ s/"/\"/og; | ||
| 5722 | $value =~ s/'/\'/og; | ||
| 5723 | $value =~ s/</</og; | ||
| 5724 | $value =~ s/>/>/og; | ||
| 5725 | $value =~ s//oe/og; | ||
| 5726 | $value =~ s/\//_/g; | ||
| 5727 | $value =~ s/ +/_/g; | ||
| 5728 | $value =~ s/:/_/g; | ||
| 5729 | $value =~ s/!/_/g; | ||
| 5730 | $value =~ s/\?/_/g; | ||
| 5731 | $value =~ s/\&/_/g; | ||
| 5732 | $value =~ s/\'/_/g; | ||
| 5733 | $value =~ s/_+/_/g; | ||
| 5734 | $value =~ s/_/ /go; | ||
| 5735 | $value =~ s/\n/ /go; | ||
| 5736 | $value =~ s/ +$//go; | ||
| 5737 | $value =~ s/^ +//go; | ||
| 5738 | $value =~ s/ /-/go; | ||
| 5739 | # warn "=====> $value\n"; | ||
| 5740 | |||
| 5741 | return($value); | ||
| 5742 | } | ||
| 5743 | |||
| 5744 | sub char_norm { | ||
| 5745 | my ($self, $str) = @_; | ||
| 5746 | |||
| 5747 | $str =~ s/œ/oe/go; | ||
| 5748 | $str =~ s/ / /go; | ||
| 5749 | $str =~ s/’/'/go; | ||
| 5750 | $str =~ s/´/'/go; | ||
| 5751 | $str =~ s/•/-/go; | ||
| 5752 | $str =~ s/`/'/go; | ||
| 5753 | $str =~ s/“/ /go; | ||
| 5754 | $str =~ s/”/ /go; | ||
| 5755 | $str =~ s/‘/'/go; | ||
| 5756 | $str =~ s//oe/og; | ||
| 5757 | $str =~ s/……/....../go; | ||
| 5758 | $str =~ s/þ//go; | ||
| 5759 | $str =~ s/([\.:;,?!])([A-Za-z])/$1 $2/go; | ||
| 5760 | |||
| 5761 | return($str); | ||
| 5762 | } | ||
| 5763 | |||
| 5764 | sub _deft2arff { | ||
| 5765 | my ($self, $value) = @_; | ||
| 5766 | |||
| 5767 | $value =~ s/ /_/go; | ||
| 5768 | |||
| 5769 | return($value); | ||
| 5770 | } | ||
| 5771 | |||
| 5772 | sub _makeHeader { | ||
| 5773 | my ($self, $lang, $relation, $property, $classes) = @_; | ||
| 5774 | |||
| 5775 | my $attr; | ||
| 5776 | my $LDS; | ||
| 5777 | my $type; | ||
| 5778 | |||
| 5779 | $LDS = Lingua::Ogmios::LearningDataSet->new(); | ||
| 5780 | $LDS->relation($relation); | ||
| 5781 | $LDS->classes($classes); | ||
| 5782 | if (defined $self->{"attribute_file"}->{$lang}) { | ||
| 5783 | warn "read from filelist\n"; | ||
| 5784 | $self->_readAttributeList($lang, $relation, $property, $classes, $LDS); | ||
| 5785 | } else { | ||
| 5786 | warn "read from lists\n"; | ||
| 5787 | foreach $attr (sort {$a->{ORDER} <=> $b->{ORDER}} values %{$self->{ATTRIBUTES}->{$lang}}) { | ||
| 5788 | if (defined $attr->{FILE}) { | ||
| 5789 | # warn "==>" . $attr->{FILE} . "\n"; | ||
| 5790 | $self->_load_attribute_file($attr->{FILE}, $attr->{TYPE}, $attr->{PREFIX}, $LDS); | ||
| 5791 | } else { | ||
| 5792 | if (defined $attr->{VALUE}) { | ||
| 5793 | # warn $attr->{VALUE} ."\n"; | ||
| 5794 | my @typeValue = split /,/,$self->_deft2arff($attr->{TYPE}); | ||
| 5795 | |||
| 5796 | if (scalar(@typeValue) > 1) { | ||
| 5797 | $type = \@typeValue; | ||
| 5798 | } else { | ||
| 5799 | $type = $self->_deft2arff($attr->{TYPE}); | ||
| 5800 | } | ||
| 5801 | my $attribute = Lingua::Ogmios::LearningDataSet::Attribute->new( | ||
| 5802 | {"name" => $attr->{PREFIX} . "_" . $attr->{VALUE}, | ||
| 5803 | "type" => $type, | ||
| 5804 | "prefix" => $attr->{PREFIX}, | ||
| 5805 | "value" => $attr->{VALUE}, | ||
| 5806 | } | ||
| 5807 | ); | ||
| 5808 | # my $idx = | ||
| 5809 | $LDS->addAttribute($attribute); | ||
| 5810 | # warn "\t$idx\n"; | ||
| 5811 | # $attribute = Lingua::Ogmios::LearningDataSet::Attribute->new( | ||
| 5812 | # {"name" => $attr->{PREFIX} . "_" . $attr->{VALUE}, | ||
| 5813 | # "type" => [split /,/, $self->_deft2arff($attr->{TYPE})], | ||
| 5814 | # } | ||
| 5815 | # ); | ||
| 5816 | # $LDS->addAttribute($attribute); | ||
| 5817 | } | ||
| 5818 | } | ||
| 5819 | } | ||
| 5820 | } | ||
| 5821 | if (defined $self->{ATTRIBUTES}->{$lang}->{DOCUMENTFREQUENCY}->{FILE}) { | ||
| 5822 | $self->{"attr2df"}->{$lang} = {}; | ||
| 5823 | $self->_load_df_values($self->{ATTRIBUTES}->{$lang}->{DOCUMENTFREQUENCY}->{FILE}, $self->{"attr2df"}->{$lang}); | ||
| 5824 | } | ||
| 5825 | |||
| 5826 | return($LDS); | ||
| 5827 | } | ||
| 5828 | |||
| 5829 | sub _load_attribute_file { | ||
| 5830 | my ($self,$file, $type, $prefix, $LDS) = @_; | ||
| 5831 | my $line; | ||
| 5832 | my $attr; | ||
| 5833 | my $attr_str; | ||
| 5834 | my $freq; | ||
| 5835 | |||
| 5836 | warn "Loading $file ($type : $prefix)\n"; | ||
| 5837 | open FILE, $file or die "No such file $file\n"; | ||
| 5838 | binmode(FILE, ':utf8'); | ||
| 5839 | while($line = <FILE>) { | ||
| 5840 | chomp $line; | ||
| 5841 | |||
| 5842 | if ($line !~ /^\s*#/) { | ||
| 5843 | ($attr_str, $freq) = split /\t/, $line; | ||
| 5844 | my $attr = Lingua::Ogmios::LearningDataSet::Attribute->new( | ||
| 5845 | {"name" => $prefix . "_" . $self->_deft2arff($self->_arff2deft($attr_str)), | ||
| 5846 | "type" => uc($type), | ||
| 5847 | "prefix" => $prefix, | ||
| 5848 | "value" => $attr_str, | ||
| 5849 | |||
| 5850 | } | ||
| 5851 | ); | ||
| 5852 | $LDS->addAttribute($attr); | ||
| 5853 | |||
| 5854 | } | ||
| 5855 | } | ||
| 5856 | close FILE; | ||
| 5857 | } | ||
| 5858 | |||
| 5859 | |||
| 5860 | sub _load_df_values { | ||
| 5861 | my ($self,$file, $attr2freq) = @_; | ||
| 5862 | |||
| 5863 | my $line; | ||
| 5864 | my $attr_str; | ||
| 5865 | my $freq; | ||
| 5866 | |||
| 5867 | warn "Loading $file (document frequency)\n"; | ||
| 5868 | open FILE, $file or die "No such file $file\n"; | ||
| 5869 | binmode(FILE, ':utf8'); | ||
| 5870 | while($line = <FILE>) { | ||
| 5871 | chomp $line; | ||
| 5872 | |||
| 5873 | if ($line !~ /^\s*#/) { | ||
| 5874 | ($attr_str, $freq) = split /\t/, $line; | ||
| 5875 | if (defined $attr2freq) { | ||
| 5876 | $attr2freq->{$self->_deft2arff($self->_arff2deft($attr_str))} = $freq; | ||
| 5877 | } | ||
| 5878 | } | ||
| 5879 | } | ||
| 5880 | close FILE; | ||
| 5881 | } | ||
| 5882 | |||
| 5883 | |||
| 5884 | sub _readAttributeList { | ||
| 5885 | my ($self, $lang, $relation, $property, $classes, $LDS) = @_; | ||
| 5886 | |||
| 5887 | my $section; | ||
| 5888 | my $attr; | ||
| 5889 | my $string; | ||
| 5890 | my $attr_str; | ||
| 5891 | my $attr_str_wprefix; | ||
| 5892 | my $type; | ||
| 5893 | my $line; | ||
| 5894 | my $order; | ||
| 5895 | my $prefix; | ||
| 5896 | |||
| 5897 | warn "readAttributeList ($relation - $property)\n"; | ||
| 5898 | |||
| 5899 | warn "Open " . $self->{"attribute_file"}->{$lang} . "\n"; | ||
| 5900 | open FILE, $self->{"attribute_file"}->{$lang} or die "No such file ". $self->{"attribute_file"}->{$lang}; | ||
| 5901 | binmode(FILE, ":utf8"); | ||
| 5902 | while($line = <FILE>) { | ||
| 5903 | chomp $line; | ||
| 5904 | |||
| 5905 | if ($line !~ /^\s*#/) { | ||
| 5906 | $prefix = undef; | ||
| 5907 | ($attr_str, $attr_str_wprefix, $type, $prefix) = split /\t/, $line; | ||
| 5908 | if (!exists $self->{ATTRIBUTES}->{$lang}->{uc($attr_str)}) { | ||
| 5909 | if (!defined $prefix) { | ||
| 5910 | ($prefix, ) = split /_/, $attr_str_wprefix; | ||
| 5911 | } | ||
| 5912 | $self->{ATTRIBUTES}->{$lang}->{uc($prefix)}->{PREFIX} = $prefix; | ||
| 5913 | $self->{ATTRIBUTES}->{$lang}->{uc($prefix)}->{TYPE} = $type; | ||
| 5914 | } | ||
| 5915 | my $attr = Lingua::Ogmios::LearningDataSet::Attribute->new( | ||
| 5916 | {"name" => $self->_deft2arff($attr_str_wprefix), | ||
| 5917 | "type" => $self->_deft2arff($type), | ||
| 5918 | "prefix" => $prefix, | ||
| 5919 | "value" => $attr_str, | ||
| 5920 | } | ||
| 5921 | ); | ||
| 5922 | $LDS->addAttribute($attr); | ||
| 5923 | } | ||
| 5924 | } | ||
| 5925 | close FILE; | ||
| 5926 | |||
| 5927 | ### | ||
| 5928 | |||
| 5929 | # foreach $document (@{$self->_documentSet}) { | ||
| 5930 | # foreach $word (@{$document->getAnnotations->getWordLevel->getElements}) { | ||
| 5931 | # $self->_postag_attribute($document, $LDS, $data, $word); | ||
| 5932 | # $self->_word_attribute($document, $LDS, $data, $word); | ||
| 5933 | # $self->_lemma_attribute($document, $LDS, $data, $word); | ||
| 5934 | # } | ||
| 5935 | # $self->_semtag_attribute($document, $LDS, $data); | ||
| 5936 | # } | ||
| 5937 | # $self->LDS($lang, $LDS); | ||
| 5938 | warn "done\n"; | ||
| 5939 | } | ||
| 5940 | |||
| 5941 | sub _printLearningSet { | ||
| 5942 | my ($self, $lang, $output, $fh, $printHeader) = @_; | ||
| 5943 | |||
| 5944 | my $LDS = $self->LDS($lang); | ||
| 5945 | |||
| 5946 | if ($output eq "ARFF+SVM") { | ||
| 5947 | print $fh "ARFF|"; | ||
| 5948 | print $fh $LDS->getARFFData; | ||
| 5949 | print $fh "SVM|"; | ||
| 5950 | print $fh $LDS->getSVM; | ||
| 5951 | } | ||
| 5952 | if ($output eq "ARFF") { | ||
| 5953 | if ($printHeader == 1) { | ||
| 5954 | print $fh $LDS->getARFFHeader; | ||
| 5955 | print $fh '@DATA' . "\n"; | ||
| 5956 | } | ||
| 5957 | print $fh $LDS->getARFFData; | ||
| 5958 | } | ||
| 5959 | if ($output eq "SVM") { | ||
| 5960 | print $fh $LDS->getSVM; | ||
| 5961 | } | ||
| 5962 | } | ||
| 5963 | |||
| 5964 | sub LDS { | ||
| 5965 | my $self = shift; | ||
| 5966 | my $lang = shift; | ||
| 5967 | |||
| 5968 | if (@_) { | ||
| 5969 | $self->{'LDS'} = shift; | ||
| 5970 | } | ||
| 5971 | return($self->{'LDS'}); | ||
| 5972 | } | ||
| 5973 | |||
| 5974 | |||
| 5975 | sub _termWeight_attribute { | ||
| 5976 | my ($self, $document, $LDS, $data, $term) = @_; | ||
| 5977 | |||
| 5978 | my $lang = $document->getAnnotations->getLanguage; | ||
| 5979 | my $string; | ||
| 5980 | my $weight; | ||
| 5981 | my @weights; | ||
| 5982 | |||
| 5983 | if (defined $self->{ATTRIBUTES}->{$lang}->{WEIGHT}->{PREFIX}) { | ||
| 5984 | my $prefix = $self->{ATTRIBUTES}->{$lang}->{WEIGHT}->{PREFIX}; | ||
| 5985 | # warn "=> $prefix\n"; | ||
| 5986 | foreach $weight (keys %{$term->weights}) { | ||
| 5987 | # warn "====> $weight\n"; | ||
| 5988 | # $string = $prefix . "_" . $self->_deft2arff($self->_arff2deft($weight)); | ||
| 5989 | $string = $prefix . "_" . $self->_deft2arff($weight); | ||
| 5990 | # warn "$string\n"; | ||
| 5991 | if ($LDS->existsAttribute($string)) { | ||
| 5992 | $data->value($LDS->getAttributeIndex($string), $term->weight($weight)); | ||
| 5993 | # $data->incr_value($LDS->getAttributeIndex($string)); | ||
| 5994 | } | ||
| 5995 | } | ||
| 5996 | if ($LDS->existsAttribute($prefix . "_position")) { | ||
| 5997 | # warn "====> position\n"; | ||
| 5998 | $data->value($LDS->getAttributeIndex($prefix . "_position"), $term->start_token->getFrom); | ||
| 5999 | } | ||
| 6000 | if ($LDS->existsAttribute($prefix . "_wordLength")) { | ||
| 6001 | # warn "====> position\n"; | ||
| 6002 | $data->value($LDS->getAttributeIndex($prefix . "_wordLength"), $term->getReferenceWordSize); | ||
| 6003 | } | ||
| 6004 | |||
| 6005 | } | ||
| 6006 | } | ||
| 6007 | |||
| 6008 | sub _termWeight_attribute_v2 { | ||
| 6009 | my ($self, $document, $LDS, $data, $term) = @_; | ||
| 6010 | |||
| 6011 | my $lang = $document->getAnnotations->getLanguage; | ||
| 6012 | my $string; | ||
| 6013 | my $attr; | ||
| 6014 | my $weight; | ||
| 6015 | |||
| 6016 | if (defined $self->{ATTRIBUTES}->{$lang}->{WEIGHT}->{PREFIX}) { | ||
| 6017 | my $prefix = $self->{ATTRIBUTES}->{$lang}->{WEIGHT}->{PREFIX}; | ||
| 6018 | foreach $attr (keys %{$LDS->attributes}) { | ||
| 6019 | if ($attr->prefix eq "weight") { | ||
| 6020 | $weight = $attr->value; | ||
| 6021 | # $string = $prefix . "_" . $self->_deft2arff($self->_arff2deft($term->weight($weight))); | ||
| 6022 | $string = $prefix . "_" . $self->_deft2arff($term->weight($weight)); | ||
| 6023 | # warn "$string\n"; | ||
| 6024 | if ($term->existsWeight($weight)) { | ||
| 6025 | $data->value($LDS->getAttributeIndex($string), $term->weight($weight)); | ||
| 6026 | } elsif ($weight eq "position") { | ||
| 6027 | $data->value($LDS->getAttributeIndex($string), $term->start_token->getFrom); | ||
| 6028 | } | ||
| 6029 | |||
| 6030 | } | ||
| 6031 | } | ||
| 6032 | } | ||
| 6033 | } | ||
| 6034 | |||
| 6035 | sub _parseWekaEvalOutput { | ||
| 6036 | my ($self, $property) = @_; | ||
| 6037 | |||
| 6038 | my $line; | ||
| 6039 | my $instnb; | ||
| 6040 | my $actual_nb_class; | ||
| 6041 | my $predicted_nb_class; | ||
| 6042 | my $actual_Class; | ||
| 6043 | my $predicted_Class; | ||
| 6044 | my $actual_classNb; | ||
| 6045 | my $predicted_classNb; | ||
| 6046 | my $error; | ||
| 6047 | my @distribution; | ||
| 6048 | my $document; | ||
| 6049 | my %results; | ||
| 6050 | |||
| 6051 | open FILE, $self->_output_filename or die "No such file " . $self->_output_filename; | ||
| 6052 | binmode(FILE, ":utf8"); | ||
| 6053 | while($line = <FILE>){ | ||
| 6054 | if ($line =~ /^inst\#/o) { | ||
| 6055 | last; | ||
| 6056 | } | ||
| 6057 | }; | ||
| 6058 | |||
| 6059 | while($line = <FILE>) { | ||
| 6060 | chomp $line; | ||
| 6061 | |||
| 6062 | if ($line ne "") { | ||
| 6063 | warn "$line\n"; | ||
| 6064 | ($instnb, $actual_nb_class, $predicted_nb_class, $error, @distribution) = split /\t/, $line; | ||
| 6065 | ($actual_classNb, $actual_Class) = split /:/, $actual_nb_class; | ||
| 6066 | ($predicted_classNb, $predicted_Class) = split /:/, $predicted_nb_class; | ||
| 6067 | # $document = $self->_documentSet->[$instnb - 1]; | ||
| 6068 | # warn "$property,$predicted_Class\n"; | ||
| 6069 | # $document->getAnnotations->replaceProperty($property,$predicted_Class); | ||
| 6070 | # warn $document->getAnnotations->getProperty($property) . "\n";; | ||
| 6071 | } | ||
| 6072 | } | ||
| 6073 | |||
| 6074 | close FILE; | ||
| 6075 | |||
| 6076 | } | ||
| 6077 | |||
| 6078 | sub _createSemanticFeaturesFromString { | ||
| 6079 | my ($self, $semtags, $termUnitId) = @_; | ||
| 6080 | |||
| 6081 | my $semtag; | ||
| 6082 | my $semFeatures; | ||
| 6083 | my @list_semtags; | ||
| 6084 | |||
| 6085 | if (defined $semtags) { | ||
| 6086 | foreach $semtag (split /;/, $semtags) { | ||
| 6087 | # warn "\t$semtag\n"; | ||
| 6088 | my @semtags = split /\//, $semtag; | ||
| 6089 | push @list_semtags, \@semtags; | ||
| 6090 | # TODO Check if the semtag already exists in order to avoid to create one another? | ||
| 6091 | $semFeatures = Lingua::Ogmios::Annotations::SemanticFeatures->new( | ||
| 6092 | { 'semantic_category' => \@list_semtags, | ||
| 6093 | 'refid_semantic_unit' => $termUnitId, | ||
| 6094 | }); | ||
| 6095 | } | ||
| 6096 | } | ||
| 6097 | return($semFeatures); | ||
| 6098 | } | ||
| 6099 | |||
| 6100 | sub getTermsFromSpan { | ||
| 6101 | my ($self, $document, $ref_start_token, $ref_end_token) = @_; | ||
| 6102 | |||
| 6103 | my $word; | ||
| 6104 | # my $lemma; | ||
| 6105 | |||
| 6106 | # my $sentLemma = "";; | ||
| 6107 | my $token = $ref_start_token; | ||
| 6108 | |||
| 6109 | # my $token_prec = 0; | ||
| 6110 | my @terms = (); | ||
| 6111 | my @semfs; | ||
| 6112 | my $semf; | ||
| 6113 | |||
| 6114 | if (defined $token) { | ||
| 6115 | do { | ||
| 6116 | warn $token->getId . " : " . $token->getContent . "\n"; | ||
| 6117 | if ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 6118 | warn $token->getContent . "\n"; | ||
| 6119 | @semfs = @{$document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("list_refid_token", $token->getId)}; | ||
| 6120 | # $lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 6121 | foreach $semf (@semfs) { | ||
| 6122 | if ($semf->isTerm) { | ||
| 6123 | push @terms, $semf; | ||
| 6124 | } | ||
| 6125 | } | ||
| 6126 | } | ||
| 6127 | $token = $token->next; | ||
| 6128 | } while((defined $token) && (defined $token->previous) && (!($token->previous->equals($ref_end_token)))); | ||
| 6129 | } | ||
| 6130 | return(\@terms); | ||
| 6131 | } | ||
| 6132 | |||
| 6133 | |||
| 6134 | 1; | ||
| 6135 | |||
| 6136 | __END__ |