| 1 | package Alvis::NLPPlatform::UserNLPWrappers; | ||
| 2 | |||
| 3 | |||
| 4 | use Alvis::NLPPlatform::NLPWrappers; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Data::Dumper; | ||
| 10 | |||
| 11 | use UNIVERSAL qw(isa); | ||
| 12 | |||
| 13 | our @ISA = ("Alvis::NLPPlatform::NLPWrappers"); | ||
| 14 | |||
| 15 | |||
| 16 | our $VERSION=$Alvis::NLPPlatform::VERSION; | ||
| 17 | |||
| 18 | |||
| 19 | sub tokenize { | ||
| 20 | my @arg = @_; | ||
| 21 | |||
| 22 | my $class = shift @arg; | ||
| 23 | |||
| 24 | return($class->SUPER::tokenize(@arg)); | ||
| 25 | |||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | sub scan_ne | ||
| 31 | { | ||
| 32 | my @arg = @_; | ||
| 33 | |||
| 34 | my $class = shift @arg; | ||
| 35 | |||
| 36 | $class->SUPER::scan_ne(@arg); | ||
| 37 | |||
| 38 | } | ||
| 39 | |||
| 40 | sub word_segmentation | ||
| 41 | { | ||
| 42 | my @arg = @_; | ||
| 43 | |||
| 44 | my $class = shift @arg; | ||
| 45 | |||
| 46 | $class->SUPER::word_segmentation(@arg); | ||
| 47 | |||
| 48 | } | ||
| 49 | |||
| 50 | sub sentence_segmentation | ||
| 51 | { | ||
| 52 | my @arg = @_; | ||
| 53 | |||
| 54 | my $class = shift @arg; | ||
| 55 | |||
| 56 | $class->SUPER::sentence_segmentation(@arg); | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub pos_tag | ||
| 62 | { | ||
| 63 | my @arg = @_; | ||
| 64 | |||
| 65 | my $class = shift @arg; | ||
| 66 | |||
| 67 | $class->SUPER::pos_tag(@arg); | ||
| 68 | |||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | sub lemmatization | ||
| 73 | { | ||
| 74 | my @arg = @_; | ||
| 75 | |||
| 76 | my $class = shift @arg; | ||
| 77 | |||
| 78 | $class->SUPER::lemmatization(@arg); | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | sub term_tag | ||
| 84 | { | ||
| 85 | my @arg = @_; | ||
| 86 | |||
| 87 | my $class = shift @arg; | ||
| 88 | |||
| 89 | $class->SUPER::term_tag(@arg); | ||
| 90 | # &PrintOutputTreeTagger(@arg, \*STDOUT); | ||
| 91 | # exit; | ||
| 92 | # &execYaTeA(@arg); | ||
| 93 | # exit; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub PrintOutputTreeTagger { | ||
| 97 | my ($h_config, $doc_hash, $output_stream) = @_; | ||
| 98 | |||
| 99 | my $line; | ||
| 100 | my $insentence; | ||
| 101 | my $sentence; | ||
| 102 | |||
| 103 | my $tokens; | ||
| 104 | my $analyses; | ||
| 105 | my $analysis; | ||
| 106 | my $nsentence; | ||
| 107 | my $token_start; | ||
| 108 | my $token_end; | ||
| 109 | my $relation; | ||
| 110 | my $left_wall; | ||
| 111 | my $right_wall; | ||
| 112 | |||
| 113 | my $relation_id; | ||
| 114 | |||
| 115 | my @arr_tokens; | ||
| 116 | my $last_token; | ||
| 117 | my $wordidshift=0; | ||
| 118 | |||
| 119 | my $phrase_idx=$Alvis::NLPPlatform::Annotation::phrase_idx; | ||
| 120 | |||
| 121 | print STDERR " Performing TreeTagger like Output\n"; | ||
| 122 | |||
| 123 | my $word; | ||
| 124 | my $worddecal; | ||
| 125 | my $word_cont; | ||
| 126 | my $word_id; | ||
| 127 | my $i; | ||
| 128 | my $sentences_cont=""; | ||
| 129 | |||
| 130 | my @tab_word_punct; | ||
| 131 | my @tab_word; | ||
| 132 | my $idx_tab_word_punct=1; | ||
| 133 | my $idx_tab_word=1; | ||
| 134 | my @tab_mapping; | ||
| 135 | |||
| 136 | # print out words+punct and fill in a tab | ||
| 137 | push @tab_word_punct," "; | ||
| 138 | push @tab_word," "; | ||
| 139 | |||
| 140 | my $decal=1; | ||
| 141 | |||
| 142 | my $searchterm; | ||
| 143 | my $sti; | ||
| 144 | my $word_np; | ||
| 145 | |||
| 146 | my @tab_tmp; | ||
| 147 | my $tmp_sp; | ||
| 148 | my $spi=0; | ||
| 149 | |||
| 150 | my $termsfound=0; | ||
| 151 | my $stubs=0; | ||
| 152 | |||
| 153 | my $skip=0; | ||
| 154 | |||
| 155 | my @tab_start_term=(); | ||
| 156 | my @tab_end_term=(); | ||
| 157 | |||
| 158 | my $constituents; | ||
| 159 | my $nb_constituents; | ||
| 160 | |||
| 161 | my $min; | ||
| 162 | my $max; | ||
| 163 | |||
| 164 | my $btw_start; | ||
| 165 | my $btw_end; | ||
| 166 | my $token; | ||
| 167 | |||
| 168 | my $start_token=$Alvis::NLPPlatform::word_start[1]; | ||
| 169 | |||
| 170 | my $current_sentence_id = 1; | ||
| 171 | my $current_section_id = 0; | ||
| 172 | |||
| 173 | my $end_of_sentence = 0; | ||
| 174 | my $last_mark_is_end_of_sentence = 0; | ||
| 175 | |||
| 176 | |||
| 177 | foreach $word (Alvis::NLPPlatform::Annotation::sort(\%Alvis::NLPPlatform::hash_words_punct)){ | ||
| 178 | if($skip>0){ | ||
| 179 | $skip--; | ||
| 180 | } | ||
| 181 | $min=$Alvis::NLPPlatform::word_start[Alvis::NLPPlatform::Annotation::read_key_id("word$decal")]; | ||
| 182 | $max=$Alvis::NLPPlatform::word_end[Alvis::NLPPlatform::Annotation::read_key_id("word$decal")]; | ||
| 183 | $word_cont=$Alvis::NLPPlatform::hash_words_punct{$word}; | ||
| 184 | |||
| 185 | push @tab_word_punct,$word_cont; | ||
| 186 | |||
| 187 | $worddecal = "word$decal"; | ||
| 188 | |||
| 189 | my $postag=$Alvis::NLPPlatform::hash_postags{"word$decal"}; | ||
| 190 | my $lemma=$Alvis::NLPPlatform::hash_lemmas{"word$decal"}; | ||
| 191 | # print STDERR "$word_cont\n"; | ||
| 192 | # print STDERR "$lemma\n"; | ||
| 193 | |||
| 194 | if((exists $Alvis::NLPPlatform::hash_words{"word$decal"})&&($word_cont ne $Alvis::NLPPlatform::hash_words{"word$decal"})){ | ||
| 195 | # punctuation, delay incrementation of index "decal" | ||
| 196 | $decal--; | ||
| 197 | $postag="PUNCT"; | ||
| 198 | $lemma=$word_cont; | ||
| 199 | } | ||
| 200 | |||
| 201 | if ((defined $doc_hash->{"$worddecal"}) && (Alvis::NLPPlatform::token_id_just_before_last_of_list_refid_token($doc_hash->{"$worddecal"}->{'list_refid_token'}->{"refid_token"}, $doc_hash->{"sentence$current_sentence_id"}->{'refid_end_token'}))) { | ||
| 202 | if (index(";:.!?", $doc_hash->{$doc_hash->{"sentence$current_sentence_id"}->{'refid_end_token'}}->{"content"}) >-1) { | ||
| 203 | $sentences_cont.= $doc_hash->{$doc_hash->{"sentence$current_sentence_id"}->{'refid_end_token'}}->{"content"} . "\tSENT\t" . $doc_hash->{$doc_hash->{"sentence$current_sentence_id"}->{'refid_end_token'}}->{"content"} . "\n"; | ||
| 204 | $end_of_sentence = 1; | ||
| 205 | $last_mark_is_end_of_sentence = 1; | ||
| 206 | } else { | ||
| 207 | $sentences_cont.= " \tSENT\t \n"; | ||
| 208 | $last_mark_is_end_of_sentence = 1; | ||
| 209 | if (!defined $postag) { | ||
| 210 | $postag = "FW"; | ||
| 211 | } | ||
| 212 | if (!defined $lemma) { | ||
| 213 | $lemma = $word_cont | ||
| 214 | } | ||
| 215 | $sentences_cont.="$word_cont\t$postag\t$lemma\n"; | ||
| 216 | $last_mark_is_end_of_sentence = 0; | ||
| 217 | $end_of_sentence = 1; | ||
| 218 | } | ||
| 219 | $current_sentence_id++; | ||
| 220 | } | ||
| 221 | if (!$end_of_sentence) { | ||
| 222 | if (!defined $postag) { | ||
| 223 | $postag = "SYM"; | ||
| 224 | } | ||
| 225 | if (!defined $lemma) { | ||
| 226 | $lemma = $word_cont; | ||
| 227 | } | ||
| 228 | $sentences_cont.="$word_cont\t$postag\t$lemma\n"; | ||
| 229 | $last_mark_is_end_of_sentence = 0; | ||
| 230 | } else { | ||
| 231 | $end_of_sentence = 0; | ||
| 232 | } | ||
| 233 | # insert tokens between the current word and the next (spaces, punctuation, ...) | ||
| 234 | $btw_start=$max+1; | ||
| 235 | |||
| 236 | # print STDERR (Alvis::NLPPlatform::Annotation::read_key_id("word$decal")+1) . " >? " . $Alvis::NLPPlatform::number_of_words . "\n"; | ||
| 237 | |||
| 238 | if(Alvis::NLPPlatform::Annotation::read_key_id("word$decal")+1 > $Alvis::NLPPlatform::number_of_words){ | ||
| 239 | # We've reached the end of the document | ||
| 240 | $end_of_sentence = 1; | ||
| 241 | $btw_end=$Alvis::NLPPlatform::Annotation::nb_max_tokens; | ||
| 242 | # print STDERR "===> $btw_start : $btw_end\n"; | ||
| 243 | for($i=$btw_start;$i<=$btw_end;$i++){ | ||
| 244 | $token = $Alvis::NLPPlatform::hash_tokens{"token".$i}; | ||
| 245 | if (($token !~ /^ +/o) && ($token ne ".") && ($token ne "\\n")) { | ||
| 246 | $sentences_cont .= "$token\t$token\t$token\n"; | ||
| 247 | $last_mark_is_end_of_sentence = 0; | ||
| 248 | } | ||
| 249 | if ($token eq ".") { | ||
| 250 | $sentences_cont .= "$token\tSENT\t$token\n"; | ||
| 251 | $last_mark_is_end_of_sentence = 1; | ||
| 252 | |||
| 253 | } | ||
| 254 | } | ||
| 255 | }else{ | ||
| 256 | $btw_end=$Alvis::NLPPlatform::word_start[Alvis::NLPPlatform::Annotation::read_key_id("word$decal")+1]-1; | ||
| 257 | for($i=$btw_start;$i<=$btw_end;$i++){ | ||
| 258 | $token = $Alvis::NLPPlatform::hash_tokens{"token".$i}; | ||
| 259 | if (($token !~ /^ +/o) && ($token ne ".") && ($token ne "\\n")) { | ||
| 260 | $sentences_cont .= "$token\t$token\t$token\n"; | ||
| 261 | $last_mark_is_end_of_sentence = 0; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | $decal++; | ||
| 267 | } | ||
| 268 | |||
| 269 | # fill words tab | ||
| 270 | foreach $word(Alvis::NLPPlatform::Annotation::sort(\%Alvis::NLPPlatform::hash_words)){ | ||
| 271 | push @tab_word,$Alvis::NLPPlatform::hash_words{$word}; | ||
| 272 | } | ||
| 273 | |||
| 274 | # pre-compute mapping between words+punct and words | ||
| 275 | my $idx_nopunct=1; | ||
| 276 | for($i=0;$i<scalar @tab_word_punct;$i++){ | ||
| 277 | if(($idx_nopunct<scalar @tab_word)&&($tab_word_punct[$i] eq $tab_word[$idx_nopunct])){ | ||
| 278 | $tab_mapping[$i]=$idx_nopunct; | ||
| 279 | $idx_nopunct++; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | # remove whitespaces in NE | ||
| 284 | my $ne; | ||
| 285 | my $ne_cont; | ||
| 286 | my $ne_mod; | ||
| 287 | foreach $ne(keys %Alvis::NLPPlatform::hash_named_entities){ | ||
| 288 | $ne_cont=$Alvis::NLPPlatform::hash_named_entities{$ne}; | ||
| 289 | $ne_mod=$ne_cont; | ||
| 290 | if(($ne_cont =~ / /) && ($ne_cont !~ /^ *$/)) { | ||
| 291 | if($sentences_cont =~ /\Q$ne_cont\E/){ | ||
| 292 | $ne_mod =~ s/ /\_/g; | ||
| 293 | $sentences_cont =~ s/\Q$ne_cont\E/$ne_mod/g; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | # if ($ne_cont=~/ /){ | ||
| 297 | # if($sentences_cont=~/\Q$ne_cont\E/){ | ||
| 298 | # $ne_mod=~s/ /\_/g; | ||
| 299 | # $sentences_cont=~s/$ne_cont/$ne_mod/g; | ||
| 300 | # } | ||
| 301 | # } | ||
| 302 | } | ||
| 303 | if ($last_mark_is_end_of_sentence == 0) { | ||
| 304 | $sentences_cont.= " \tSENT\t \n"; | ||
| 305 | } | ||
| 306 | |||
| 307 | print $output_stream $sentences_cont; | ||
| 308 | } | ||
| 309 | |||
| 310 | |||
| 311 | sub execYaTeA { | ||
| 312 | my ($h_config, $doc_hash) = @_; | ||
| 313 | |||
| 314 | my $line; | ||
| 315 | my $insentence; | ||
| 316 | my $sentence; | ||
| 317 | |||
| 318 | my $tokens; | ||
| 319 | my $analyses; | ||
| 320 | my $analysis; | ||
| 321 | my $nsentence; | ||
| 322 | my $token_start; | ||
| 323 | my $token_end; | ||
| 324 | my $relation; | ||
| 325 | my $left_wall; | ||
| 326 | my $right_wall; | ||
| 327 | |||
| 328 | my $relation_id; | ||
| 329 | |||
| 330 | my @arr_tokens; | ||
| 331 | my $last_token; | ||
| 332 | my $wordidshift=0; | ||
| 333 | |||
| 334 | my $phrase_idx=$Alvis::NLPPlatform::Annotation::phrase_idx; | ||
| 335 | |||
| 336 | my $word; | ||
| 337 | my $word_cont; | ||
| 338 | my $word_id; | ||
| 339 | my $i; | ||
| 340 | my $sentences_cont=""; | ||
| 341 | |||
| 342 | my @tab_word_punct; | ||
| 343 | my @tab_word; | ||
| 344 | my $idx_tab_word_punct=1; | ||
| 345 | my $idx_tab_word=1; | ||
| 346 | my @tab_mapping; | ||
| 347 | |||
| 348 | # print out words+punct and fill in a tab | ||
| 349 | push @tab_word_punct," "; | ||
| 350 | push @tab_word," "; | ||
| 351 | my $decal=1; | ||
| 352 | |||
| 353 | my $searchterm; | ||
| 354 | my $sti; | ||
| 355 | my $word_np; | ||
| 356 | |||
| 357 | my @tab_tmp; | ||
| 358 | my $tmp_sp; | ||
| 359 | my $spi=0; | ||
| 360 | |||
| 361 | my $termsfound=0; | ||
| 362 | my $stubs=0; | ||
| 363 | |||
| 364 | my $skip=0; | ||
| 365 | |||
| 366 | my @tab_start_term=(); | ||
| 367 | my @tab_end_term=(); | ||
| 368 | |||
| 369 | my $constituents; | ||
| 370 | my $nb_constituents; | ||
| 371 | |||
| 372 | my $min; | ||
| 373 | my $max; | ||
| 374 | |||
| 375 | my $btw_start; | ||
| 376 | my $btw_end; | ||
| 377 | my $token; | ||
| 378 | my $sentence_cont; | ||
| 379 | |||
| 380 | print STDERR " Performing term extraction... \n"; | ||
| 381 | open CORPUS, ">>" . $h_config->{"TMPFILE"} . ".corpus.yatea.tmp"; | ||
| 382 | binmode(CORPUS, ":utf8"); | ||
| 383 | |||
| 384 | print CORPUS $Alvis::NLPPlatform::Annotation::document_record_id . "\tDOCUMENT\t" . $Alvis::NLPPlatform::Annotation::document_record_id . "\n" ; | ||
| 385 | |||
| 386 | &PrintOutputTreeTagger($h_config, $doc_hash, \*CORPUS); | ||
| 387 | |||
| 388 | close CORPUS; | ||
| 389 | |||
| 390 | # if ((exists $h_config->{"XML_OUTPUT"}->{"YATEA"}) && ($h_config->{"XML_OUTPUT"}->{"YATEA"} == 1)) { | ||
| 391 | # %$doc_hash = (); | ||
| 392 | # %Alvis::NLPPlatform::hash_tokens = (); | ||
| 393 | # %Alvis::NLPPlatform::hash_words = (); | ||
| 394 | # %Alvis::NLPPlatform::hash_words_punct = (); | ||
| 395 | # %Alvis::NLPPlatform::hash_sentences = (); | ||
| 396 | # %Alvis::NLPPlatform::hash_postags = (); | ||
| 397 | # %Alvis::NLPPlatform::hash_named_entities = (); | ||
| 398 | # %Alvis::NLPPlatform::hash_lemmas = (); | ||
| 399 | |||
| 400 | # $Alvis::NLPPlatform::number_of_words = 0; | ||
| 401 | # $Alvis::NLPPlatform::number_of_sentences = 0; | ||
| 402 | # $Alvis::NLPPlatform::nb_relations = 0; | ||
| 403 | # $Alvis::NLPPlatform::dont_annotate = 0; | ||
| 404 | |||
| 405 | # @Alvis::NLPPlatform::word_start = (); | ||
| 406 | # @Alvis::NLPPlatform::word_end = (); | ||
| 407 | |||
| 408 | # @Alvis::NLPPlatform::en_start = (); | ||
| 409 | # @Alvis::NLPPlatform::en_end = (); | ||
| 410 | # @Alvis::NLPPlatform::en_type = (); | ||
| 411 | |||
| 412 | # @Alvis::NLPPlatform::en_tokens_start = (); | ||
| 413 | # @Alvis::NLPPlatform::en_tokens_end = (); | ||
| 414 | # %Alvis::NLPPlatform::en_tokens_hash = (); | ||
| 415 | |||
| 416 | # } | ||
| 417 | |||
| 418 | if ( $Alvis::NLPPlatform::last_doc == 0) { | ||
| 419 | return(1); | ||
| 420 | } | ||
| 421 | |||
| 422 | require Lingua::YaTeA::Corpus; | ||
| 423 | require Lingua::YaTeA; | ||
| 424 | my %config_yatea = Lingua::YaTeA::load_config($h_config->{'NLP_tools'}->{'YATEARC'}); | ||
| 425 | |||
| 426 | |||
| 427 | my $yatea = Lingua::YaTeA->new($config_yatea{"OPTIONS"}, \%config_yatea); | ||
| 428 | |||
| 429 | if (defined $h_config->{'NLP_tools'}->{'YATEAOUTPUT'}) { | ||
| 430 | print STDERR "\nYaTeA output defined is " . $h_config->{'NLP_tools'}->{'YATEAOUTPUT'} . "\n\n"; | ||
| 431 | $yatea->getOptionSet->addOption("output-path", $h_config->{'NLP_tools'}->{'YATEAOUTPUT'}); | ||
| 432 | } else { | ||
| 433 | print STDERR "\nNo YaTeA output defined\n\n"; | ||
| 434 | $yatea->getOptionSet->addOption("output-path", $h_config->{"ALVISTMP"}); | ||
| 435 | } | ||
| 436 | |||
| 437 | my $corpus_path = $h_config->{"TMPFILE"} . ".corpus.yatea.tmp"; | ||
| 438 | my $corpus = Lingua::YaTeA::Corpus->new($corpus_path,$yatea->getOptionSet,$yatea->getMessageSet); | ||
| 439 | |||
| 440 | |||
| 441 | |||
| 442 | ######################################################################## | ||
| 443 | |||
| 444 | my $sentence_boundary = $yatea->getOptionSet->getSentenceBoundary; | ||
| 445 | my $document_boundary = $yatea->getOptionSet->getDocumentBoundary; | ||
| 446 | |||
| 447 | # $yatea->loadTestifiedTerms(\$process_counter,$corpus,$sentence_boundary,$document_boundary,$yatea->getOptionSet->MatchTypeValue,$yatea->getMessageSet,$yatea->getOptionSet->getDisplayLanguage); | ||
| 448 | |||
| 449 | print STDERR $Lingua::YaTeA::process_counter++ . ") " . ($yatea->getMessageSet->getMessage('LOAD_CORPUS')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\n"; | ||
| 450 | |||
| 451 | $corpus->read($sentence_boundary,$document_boundary,$yatea->getFSSet,$yatea->getTestifiedTermSet,$yatea->getMessageSet,$yatea->getOptionSet->getDisplayLanguage); | ||
| 452 | |||
| 453 | my $phrase_set = Lingua::YaTeA::PhraseSet->new; | ||
| 454 | |||
| 455 | print STDERR $Lingua::YaTeA::process_counter++ . ") " . ($yatea->getMessageSet->getMessage('CHUNKING')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\n"; | ||
| 456 | $corpus->chunk($phrase_set,$sentence_boundary,$document_boundary,$yatea->getChunkingDataSet,$yatea->getFSSet,$yatea->getTagSet,$yatea->getParsingPatternSet,$yatea->getTestifiedTermSet,$yatea->getOptionSet); | ||
| 457 | |||
| 458 | $phrase_set->sortUnparsed; | ||
| 459 | |||
| 460 | print STDERR $Lingua::YaTeA::process_counter++ . ") " . ($yatea->getMessageSet->getMessage('PARSING')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\n"; | ||
| 461 | |||
| 462 | $phrase_set->parseProgressively($yatea->getTagSet,$yatea->getOptionSet->getParsingDirection,$yatea->getParsingPatternSet,$yatea->getChunkingDataSet,$corpus->getLexicon,$corpus->getSentenceSet,$yatea->getMessageSet,$yatea->getOptionSet->getDisplayLanguage,\*STDERR); | ||
| 463 | |||
| 464 | $phrase_set->addTermCandidates($yatea->getOptionSet); | ||
| 465 | |||
| 466 | print STDERR $Lingua::YaTeA::process_counter++ . ") " . ($yatea->getMessageSet->getMessage('RESULTS')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\n"; | ||
| 467 | |||
| 468 | |||
| 469 | # coments to keep | ||
| 470 | print STDERR "\t-" . ($yatea->getMessageSet->getMessage('DISPLAY_RAW')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\'". $corpus->getOutputFileSet->getFile('debug')->getPath . "'\n"; | ||
| 471 | $phrase_set->printPhrases(FileHandle->new(">" . $corpus->getOutputFileSet->getFile('debug')->getPath)); | ||
| 472 | $phrase_set->printUnparsable($corpus->getOutputFileSet->getFile('unparsable')); | ||
| 473 | |||
| 474 | |||
| 475 | print STDERR "\t-" . ($yatea->getMessageSet->getMessage('DISPLAY_TC_XML')->getContent($yatea->getOptionSet->getDisplayLanguage)) . "\'". $corpus->getOutputFileSet->getFile('candidates')->getPath . "'\n"; | ||
| 476 | # | ||
| 477 | if ((exists $h_config->{"XML_OUTPUT"}->{"YATEA"}) && ($h_config->{"XML_OUTPUT"}->{"YATEA"} == 1)) { | ||
| 478 | $phrase_set->printTermCandidatesXML("stdout",$yatea->getTagSet); | ||
| 479 | exit; | ||
| 480 | } else { | ||
| 481 | # $phrase_set->printTermCandidatesXML($corpus->getOutputFileSet->getFile("candidates"),$yatea->getTagSet); | ||
| 482 | &storeTerms($phrase_set,$doc_hash,$yatea->getTagSet); | ||
| 483 | } | ||
| 484 | |||
| 485 | ######################################################################## | ||
| 486 | |||
| 487 | |||
| 488 | |||
| 489 | ######################################################################## | ||
| 490 | |||
| 491 | # } | ||
| 492 | |||
| 493 | |||
| 494 | ######################################################################## | ||
| 495 | |||
| 496 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $h_config->{'TMPFILE'}. ".corpus.tmp"; | ||
| 497 | |||
| 498 | # print STDERR "done - Found $Alvis::NLPPlatform::nb_relations relations, $termsfound full terms, $nb_constituents constituents.\n"; | ||
| 499 | } | ||
| 500 | |||
| 501 | |||
| 502 | sub storeTerms | ||
| 503 | { | ||
| 504 | my ($phrase_set,$doc_hash,$tagset) = @_; | ||
| 505 | |||
| 506 | my $fh = \*STDOUT; | ||
| 507 | |||
| 508 | my $term_candidate; | ||
| 509 | my $if; | ||
| 510 | my $pos; | ||
| 511 | my $lf; | ||
| 512 | my $occurrence; | ||
| 513 | my $island; | ||
| 514 | my $position; | ||
| 515 | |||
| 516 | my $sem_unit; | ||
| 517 | my $term; | ||
| 518 | my $term_id = 1; | ||
| 519 | my $syn_relation_id = 1; | ||
| 520 | |||
| 521 | my $phrase_idx = $Alvis::NLPPlatform::Annotation::phrase_idx; | ||
| 522 | my $relation_id = $Alvis::NLPPlatform::Annotation::syntactic_relation_idx; | ||
| 523 | |||
| 524 | my %YateaTerms2AlvisSemUnits; | ||
| 525 | my %YateaTermOcc2AlvisSemUnits; | ||
| 526 | |||
| 527 | my $syntactic_relation_id; | ||
| 528 | |||
| 529 | my $term_refid_head; | ||
| 530 | my $term_refid_modifier; | ||
| 531 | my $refid_head; | ||
| 532 | my $refid_modifier; | ||
| 533 | |||
| 534 | my $i_sem; | ||
| 535 | |||
| 536 | |||
| 537 | $sem_unit=$Alvis::NLPPlatform::last_semantic_unit + 1; | ||
| 538 | |||
| 539 | |||
| 540 | foreach $term_candidate (values(%{$phrase_set->getTermCandidates})) | ||
| 541 | { | ||
| 542 | ($if,$pos,$lf) = $term_candidate->buildLinguisticInfos($tagset); | ||
| 543 | |||
| 544 | $term = $if; | ||
| 545 | |||
| 546 | my $sent; | ||
| 547 | my $offset; | ||
| 548 | my $token_start; | ||
| 549 | my $token_end; | ||
| 550 | my $offset_start; | ||
| 551 | my $offset_end; | ||
| 552 | my $j; | ||
| 553 | my $token_term; | ||
| 554 | |||
| 555 | my @tabtmp; | ||
| 556 | $YateaTerms2AlvisSemUnits{$term_candidate->getID} = \@tabtmp; | ||
| 557 | |||
| 558 | foreach $occurrence (@{$term_candidate->getOccurrences}) | ||
| 559 | { | ||
| 560 | push @tabtmp, $sem_unit; | ||
| 561 | $YateaTermOcc2AlvisSemUnits{$occurrence->getID} = $sem_unit; | ||
| 562 | |||
| 563 | $doc_hash->{"semantic_unit$sem_unit"}={}; | ||
| 564 | $doc_hash->{"semantic_unit$sem_unit"}->{"datatype"}="semantic_unit"; | ||
| 565 | $doc_hash->{"semantic_unit$sem_unit"}->{"term"}={}; | ||
| 566 | $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"datatype"}="term"; | ||
| 567 | $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"id"}="term" . ($term_id++); # . ":" . $sem_unit | ||
| 568 | $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"form"}=$term; | ||
| 569 | push @Alvis::NLPPlatform::found_terms,$term; | ||
| 570 | push @Alvis::NLPPlatform::found_terms_smidx,($term_id-1); | ||
| 571 | |||
| 572 | |||
| 573 | $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"canonical_form"} = $lf; # "sentence" . $occurrence->getSentence->getInDocID; | ||
| 574 | $sent = $occurrence->getSentence->getInDocID; | ||
| 575 | $offset = $occurrence->getStartChar; | ||
| 576 | |||
| 577 | $doc_hash->{"sentence$sent"}->{"refid_start_token"}=~m/token([0-9]+)/i; | ||
| 578 | $token_start=$1; | ||
| 579 | $doc_hash->{"sentence$sent"}->{"refid_end_token"}=~m/token([0-9]+)/i; | ||
| 580 | $token_end=$1; | ||
| 581 | |||
| 582 | $offset_start=$doc_hash->{$doc_hash->{"sentence$sent"}->{"refid_start_token"}}->{"from"}; | ||
| 583 | $offset_end=$doc_hash->{$doc_hash->{"sentence$sent"}->{"refid_start_token"}}->{"to"}; | ||
| 584 | |||
| 585 | $offset+=$offset_start; | ||
| 586 | |||
| 587 | $j=$token_start; | ||
| 588 | while(($j<$token_end) && ($doc_hash->{"token$j"}->{"from"} < $offset)){ | ||
| 589 | $j++; | ||
| 590 | } | ||
| 591 | if (index($term, $doc_hash->{"token$j"}->{"content"}) == 0) { | ||
| 592 | $token_term=$j; | ||
| 593 | } else { | ||
| 594 | $token_term=$j-1; | ||
| 595 | } | ||
| 596 | my @tab_tokens; | ||
| 597 | |||
| 598 | for($j=$token_term; $doc_hash->{"token$j"}->{"to"} < ($doc_hash->{"token$token_term"}->{"from"} + ($occurrence->getEndChar - $occurrence->getStartChar) );$j++){ | ||
| 599 | push @tab_tokens, "token$j"; | ||
| 600 | } | ||
| 601 | |||
| 602 | my $k=1; | ||
| 603 | my $term_word_start=-1; | ||
| 604 | my $term_word_end=-1; | ||
| 605 | my @tab_words; | ||
| 606 | for($k=1;$k<=$Alvis::NLPPlatform::number_of_words;$k++){ | ||
| 607 | if($Alvis::NLPPlatform::word_start[$k]==Alvis::NLPPlatform::Annotation::read_key_id($tab_tokens[0])){ | ||
| 608 | $term_word_start=$k; | ||
| 609 | } | ||
| 610 | if($Alvis::NLPPlatform::word_end[$k]==Alvis::NLPPlatform::Annotation::read_key_id($tab_tokens[(scalar @tab_tokens)-1])){ | ||
| 611 | $term_word_end=$k; | ||
| 612 | last; | ||
| 613 | } | ||
| 614 | } | ||
| 615 | if (($term_word_start != -1) && ($term_word_end != -1)) { | ||
| 616 | for($k=$term_word_start;$k<=$term_word_end;$k++){ | ||
| 617 | push @tab_words,"word$k"; | ||
| 618 | } | ||
| 619 | } | ||
| 620 | if (scalar @tab_words == 0) { | ||
| 621 | # warn "No word found for the term $term\n"; | ||
| 622 | $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"list_refid_token"}={}; | ||
| 623 | $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"list_refid_token"}->{"datatype"} = "list_refid_token"; | ||
| 624 | $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"list_refid_token"}->{"refid_token"}=\@tab_tokens; | ||
| 625 | |||
| 626 | } | ||
| 627 | if(scalar @tab_words==1){ | ||
| 628 | $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"refid_word"}=$tab_words[0]; | ||
| 629 | } | ||
| 630 | if(scalar @tab_words>1){ | ||
| 631 | $doc_hash->{"phrase$phrase_idx"}={}; | ||
| 632 | $doc_hash->{"phrase$phrase_idx"}->{'id'}="phrase$phrase_idx"; | ||
| 633 | $doc_hash->{"phrase$phrase_idx"}->{'datatype'}="phrase"; | ||
| 634 | $doc_hash->{"phrase$phrase_idx"}->{'list_refid_components'}={}; | ||
| 635 | $doc_hash->{"phrase$phrase_idx"}->{'list_refid_components'}->{"datatype"}="list_refid_components"; | ||
| 636 | $doc_hash->{"phrase$phrase_idx"}->{'list_refid_components'}->{"refid_word"}=\@tab_words; | ||
| 637 | |||
| 638 | $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"refid_phrase"}="phrase$phrase_idx"; | ||
| 639 | # At this point, we have created a term and a phrase. We need to commit this to memory, | ||
| 640 | # as it will come in handy! | ||
| 641 | push @Alvis::NLPPlatform::found_terms_phr,$phrase_idx; | ||
| 642 | push @Alvis::NLPPlatform::found_terms_words,\@tab_words; | ||
| 643 | |||
| 644 | $phrase_idx++; | ||
| 645 | } | ||
| 646 | else{ | ||
| 647 | push @Alvis::NLPPlatform::found_terms_phr,-666; # there is no phrase | ||
| 648 | push @Alvis::NLPPlatform::found_terms_words,\@tab_words; | ||
| 649 | } | ||
| 650 | |||
| 651 | $sem_unit++; | ||
| 652 | } | ||
| 653 | |||
| 654 | } | ||
| 655 | $Alvis::NLPPlatform::Annotation::phrase_idx = $phrase_idx - 1; | ||
| 656 | $Alvis::NLPPlatform::last_semantic_unit = $sem_unit - 1; | ||
| 657 | |||
| 658 | |||
| 659 | print STDERR "done - Found ". ($term_id - 1) ." semantic units\n"; | ||
| 660 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Terms: " . ($term_id - 1); | ||
| 661 | print STDERR "done - Found ". $Alvis::NLPPlatform::Annotation::phrase_idx ." phrases\n"; | ||
| 662 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Phrase: " . $Alvis::NLPPlatform::Annotation::phrase_idx; | ||
| 663 | |||
| 664 | |||
| 665 | # Second foreach to add syntactic relations | ||
| 666 | |||
| 667 | foreach $term_candidate (values(%{$phrase_set->getTermCandidates})) | ||
| 668 | { | ||
| 669 | if(isa($term_candidate,'Lingua::YaTeA::MultiWordTermCandidate')) | ||
| 670 | { | ||
| 671 | foreach $occurrence (@{$term_candidate->getOccurrences}) { | ||
| 672 | $sem_unit = $YateaTermOcc2AlvisSemUnits{$occurrence->getID}; | ||
| 673 | if (($term_refid_head = &getSem_unitFromTermOcc($term_candidate->getRootHead->getKey, $phrase_set, $occurrence->getStartChar, $occurrence->getEndChar, \%YateaTermOcc2AlvisSemUnits)) == -1) { | ||
| 674 | warn "Occurrence not found (H)\n"; | ||
| 675 | } | ||
| 676 | |||
| 677 | if (($term_refid_modifier = &getSem_unitFromTermOcc($term_candidate->getRootModifier->getKey, $phrase_set, $occurrence->getStartChar, $occurrence->getEndChar, \%YateaTermOcc2AlvisSemUnits)) == -1) { | ||
| 678 | warn "Occurrence not found (M)\n"; | ||
| 679 | } | ||
| 680 | |||
| 681 | $syntactic_relation_id = "syntactic_relation$relation_id"; | ||
| 682 | $doc_hash->{$syntactic_relation_id}={}; | ||
| 683 | $doc_hash->{$syntactic_relation_id}->{'id'}=$syntactic_relation_id; | ||
| 684 | $doc_hash->{$syntactic_relation_id}->{'datatype'}="syntactic_relation"; | ||
| 685 | $doc_hash->{$syntactic_relation_id}->{'syntactic_relation_type'}="Head_of"; | ||
| 686 | $doc_hash->{$syntactic_relation_id}->{'refid_head'} = {}; | ||
| 687 | $doc_hash->{$syntactic_relation_id}->{'refid_head'}->{'datatype'}="refid_head"; | ||
| 688 | if (exists $doc_hash->{"semantic_unit$term_refid_head"}->{"term"}->{"refid_phrase"}) { | ||
| 689 | $doc_hash->{$syntactic_relation_id}->{'refid_head'}->{"refid_phrase"}= $doc_hash->{"semantic_unit$term_refid_head"}->{"term"}->{"refid_phrase"}; | ||
| 690 | } else { | ||
| 691 | if (exists $doc_hash->{"semantic_unit$term_refid_head"}->{"term"}->{"refid_word"}) { | ||
| 692 | $doc_hash->{$syntactic_relation_id}->{'refid_head'}->{"refid_word"}= $doc_hash->{"semantic_unit$term_refid_head"}->{"term"}->{"refid_word"}; | ||
| 693 | } else { | ||
| 694 | warn "Mismath when finding the term (tokens found rather than word or phrase)"; | ||
| 695 | } | ||
| 696 | } | ||
| 697 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'} = {}; | ||
| 698 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'}->{'datatype'}="refid_modifier"; | ||
| 699 | if (exists $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"refid_phrase"}) { | ||
| 700 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'}->{"refid_phrase"}= $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"refid_phrase"}; | ||
| 701 | } else { | ||
| 702 | if (exists $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"refid_word"}) { | ||
| 703 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'}->{"refid_word"} = $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"refid_word"}; | ||
| 704 | } else { | ||
| 705 | warn "Mismath when finding the term (tokens found rather than word or phrase)"; | ||
| 706 | } | ||
| 707 | } | ||
| 708 | $relation_id++; | ||
| 709 | |||
| 710 | $syntactic_relation_id = "syntactic_relation$relation_id"; | ||
| 711 | $doc_hash->{$syntactic_relation_id}={}; | ||
| 712 | $doc_hash->{$syntactic_relation_id}->{'id'}=$syntactic_relation_id; | ||
| 713 | $doc_hash->{$syntactic_relation_id}->{'datatype'}="syntactic_relation"; | ||
| 714 | $doc_hash->{$syntactic_relation_id}->{'syntactic_relation_type'}="Modifier_of"; | ||
| 715 | $doc_hash->{$syntactic_relation_id}->{'refid_head'} = {}; | ||
| 716 | $doc_hash->{$syntactic_relation_id}->{'refid_head'}->{'datatype'}="refid_head"; | ||
| 717 | if (exists $doc_hash->{"semantic_unit$term_refid_modifier"}->{"term"}->{"refid_phrase"}) { | ||
| 718 | $doc_hash->{$syntactic_relation_id}->{'refid_head'}->{"refid_phrase"}= $doc_hash->{"semantic_unit$term_refid_modifier"}->{"term"}->{"refid_phrase"}; | ||
| 719 | } else { | ||
| 720 | if (exists $doc_hash->{"semantic_unit$term_refid_modifier"}->{"term"}->{"refid_word"}) { | ||
| 721 | $doc_hash->{$syntactic_relation_id}->{'refid_head'}->{"refid_word"}= $doc_hash->{"semantic_unit$term_refid_modifier"}->{"term"}->{"refid_word"}; | ||
| 722 | } else { | ||
| 723 | warn "Mismath when finding the term (tokens found rather than word or phrase)"; | ||
| 724 | } | ||
| 725 | } | ||
| 726 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'} = {}; | ||
| 727 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'}->{'datatype'}="refid_modifier"; | ||
| 728 | if (exists $doc_hash->{"semantic_unit$term_refid_head"}->{"term"}->{"refid_phrase"}) { | ||
| 729 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'}->{"refid_phrase"}= $doc_hash->{"semantic_unit$term_refid_head"}->{"term"}->{"refid_phrase"}; | ||
| 730 | } else { | ||
| 731 | if (exists $doc_hash->{"semantic_unit$term_refid_head"}->{"term"}->{"refid_word"}) { | ||
| 732 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'}->{"refid_word"}= $doc_hash->{"semantic_unit$term_refid_head"}->{"term"}->{"refid_word"}; | ||
| 733 | } else { | ||
| 734 | warn "Mismath when finding the term (tokens found rather than word or phrase)"; | ||
| 735 | } | ||
| 736 | } | ||
| 737 | $relation_id++; | ||
| 738 | |||
| 739 | $syntactic_relation_id = "syntactic_relation$relation_id"; | ||
| 740 | $doc_hash->{$syntactic_relation_id}={}; | ||
| 741 | $doc_hash->{$syntactic_relation_id}->{'id'}=$syntactic_relation_id; | ||
| 742 | $doc_hash->{$syntactic_relation_id}->{'datatype'}="syntactic_relation"; | ||
| 743 | $doc_hash->{$syntactic_relation_id}->{'syntactic_relation_type'}="Component_in_modifier_position"; | ||
| 744 | $doc_hash->{$syntactic_relation_id}->{'refid_head'} = {}; | ||
| 745 | $doc_hash->{$syntactic_relation_id}->{'refid_head'}->{'datatype'}="refid_head"; | ||
| 746 | if (exists $doc_hash->{"semantic_unit$term_refid_modifier"}->{"term"}->{"refid_phrase"}) { | ||
| 747 | $doc_hash->{$syntactic_relation_id}->{'refid_head'}->{"refid_phrase"}= $doc_hash->{"semantic_unit$term_refid_modifier"}->{"term"}->{"refid_phrase"}; | ||
| 748 | } else { | ||
| 749 | if (exists $doc_hash->{"semantic_unit$term_refid_modifier"}->{"term"}->{"refid_word"}) { | ||
| 750 | $doc_hash->{$syntactic_relation_id}->{'refid_head'}->{"refid_word"}= $doc_hash->{"semantic_unit$term_refid_modifier"}->{"term"}->{"refid_word"}; | ||
| 751 | } else { | ||
| 752 | warn "Mismath when finding the term (tokens found rather than word or phrase)"; | ||
| 753 | } | ||
| 754 | } | ||
| 755 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'} = {}; | ||
| 756 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'}->{'datatype'}="refid_modifier"; | ||
| 757 | if (exists $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"refid_phrase"}) { | ||
| 758 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'}->{"refid_phrase"}= $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"refid_phrase"}; | ||
| 759 | } else { | ||
| 760 | if (exists $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"refid_word"}) { | ||
| 761 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'}->{"refid_word"}= $doc_hash->{"semantic_unit$sem_unit"}->{"term"}->{"refid_word"}; | ||
| 762 | } else { | ||
| 763 | warn "Mismath when finding the term (tokens found rather than word or phrase)"; | ||
| 764 | } | ||
| 765 | } | ||
| 766 | $relation_id++; | ||
| 767 | } | ||
| 768 | } | ||
| 769 | } | ||
| 770 | $Alvis::NLPPlatform::Annotation::syntactic_relation_idx = $relation_id; | ||
| 771 | |||
| 772 | print STDERR "done - Found ". ($relation_id - 1) ." syntactic relations\n"; | ||
| 773 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Syntactic Relations: " . ($relation_id - 1); | ||
| 774 | |||
| 775 | |||
| 776 | } | ||
| 777 | |||
| 778 | |||
| 779 | sub getSem_unitFromTermOcc | ||
| 780 | { | ||
| 781 | my ($termKey, $phrase_set, $start_char, $end_char, $ref_YateaTermOcc2AlvisSemUnits) = @_; | ||
| 782 | |||
| 783 | my @occurrences; | ||
| 784 | my $i; | ||
| 785 | |||
| 786 | |||
| 787 | my $term_candidate = $phrase_set->getTermCandidates->{$termKey}; | ||
| 788 | |||
| 789 | |||
| 790 | @occurrences = @{$term_candidate->getOccurrences}; | ||
| 791 | |||
| 792 | |||
| 793 | $i = 0; | ||
| 794 | |||
| 795 | while (($i<scalar(@occurrences)) && (($start_char > $occurrences[$i]->getStartChar) || ($occurrences[$i]->getStartChar > $end_char))) { | ||
| 796 | $i++; | ||
| 797 | } | ||
| 798 | if ($i < scalar @occurrences) { | ||
| 799 | return($ref_YateaTermOcc2AlvisSemUnits->{$occurrences[$i]->getID}); | ||
| 800 | } | ||
| 801 | |||
| 802 | return(-1); | ||
| 803 | |||
| 804 | |||
| 805 | } | ||
| 806 | |||
| 807 | sub mergeYaTeAResults | ||
| 808 | { | ||
| 809 | my ($doc_hash, $yatea) = @_; | ||
| 810 | |||
| 811 | # creation of the terms | ||
| 812 | |||
| 813 | # creation of the phrases | ||
| 814 | |||
| 815 | } | ||
| 816 | |||
| 817 | sub syntactic_parsing | ||
| 818 | { | ||
| 819 | my @arg = @_; | ||
| 820 | |||
| 821 | |||
| 822 | my $class = shift @arg; | ||
| 823 | |||
| 824 | $class->SUPER::syntactic_parsing(@arg); | ||
| 825 | # &bio_syntactic_parsing(@arg); | ||
| 826 | } | ||
| 827 | |||
| 828 | my $word_id_np=1; | ||
| 829 | |||
| 830 | sub parse_constituents { | ||
| 831 | my $constituents=$_[0]; | ||
| 832 | my $tmpptr=$_[1]; | ||
| 833 | my $decal_phrase_idx=$_[1]; | ||
| 834 | my $doc_hash=$_[2]; | ||
| 835 | my $lexer; | ||
| 836 | my @tab_type; | ||
| 837 | my @tab_string; | ||
| 838 | my $lconst = 0; | ||
| 839 | my $nconst = 0; | ||
| 840 | my $phrase_id = ""; | ||
| 841 | my $csti; | ||
| 842 | my $phrase_idx_start = $Alvis::NLPPlatform::Annotation::phrase_idx; | ||
| 843 | require Alvis::NLPPlatform::ParseConstituents; | ||
| 844 | |||
| 845 | |||
| 846 | my $parser = Alvis::NLPPlatform::ParseConstituents->new(); | ||
| 847 | |||
| 848 | # print STDERR $constituents; | ||
| 849 | |||
| 850 | $parser->YYData->{CONSTITUENT_STRING} = $constituents; | ||
| 851 | $parser->YYData->{DOC_HASH} = $doc_hash; | ||
| 852 | $parser->YYData->{DECAL_PHRASE_IDX} = $decal_phrase_idx; | ||
| 853 | $parser->YYData->{WORD_ID_NP_REF} = \$word_id_np; | ||
| 854 | $parser->YYData->{TAB_TYPE_REF} = \@tab_type; | ||
| 855 | $parser->YYData->{TAB_STRING_REF} = \@tab_string; | ||
| 856 | |||
| 857 | $parser->YYData->{LCONST_REF} = \$lconst; | ||
| 858 | $parser->YYData->{NCONST_REF} = \$nconst; | ||
| 859 | ; | ||
| 860 | $parser->YYParse(yylex => \&Alvis::NLPPlatform::ParseConstituents::_Lexer, yyerror => \&Alvis::NLPPlatform::ParseConstituents::_Error); | ||
| 861 | |||
| 862 | |||
| 863 | |||
| 864 | for($csti=1;$csti<scalar @tab_type;$csti++){ | ||
| 865 | $phrase_id = "phrase" . $Alvis::NLPPlatform::Annotation::phrase_idx; | ||
| 866 | $doc_hash->{$phrase_id}={}; | ||
| 867 | $doc_hash->{$phrase_id}->{"id"}=$phrase_id; | ||
| 868 | $doc_hash->{$phrase_id}->{"datatype"}="phrase"; | ||
| 869 | $doc_hash->{$phrase_id}->{"type"}=$tab_type[$csti]; | ||
| 870 | $doc_hash->{$phrase_id}->{'list_refid_components'}={}; | ||
| 871 | $doc_hash->{$phrase_id}->{'list_refid_components'}->{"datatype"}="list_refid_components"; | ||
| 872 | if (scalar(@{$tab_string[$csti]}) == 1) { | ||
| 873 | $doc_hash->{$phrase_id}->{'list_refid_components'}->{"refid_word"}=$tab_string[$csti]; | ||
| 874 | } else { | ||
| 875 | $doc_hash->{$phrase_id}->{'list_refid_components'}->{"refid_phrase"}=$tab_string[$csti]; | ||
| 876 | } | ||
| 877 | $Alvis::NLPPlatform::Annotation::phrase_idx++; | ||
| 878 | } | ||
| 879 | |||
| 880 | print STDERR "done - Found ". ($Alvis::NLPPlatform::Annotation::phrase_idx - $phrase_idx_start) ." semantic units\n"; | ||
| 881 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Terms: " . ($Alvis::NLPPlatform::Annotation::phrase_idx - $phrase_idx_start); | ||
| 882 | |||
| 883 | # $word_count=$word_id_np-$word_count; | ||
| 884 | # print STDERR "\nWord count for this sentence: $word_count\n"; | ||
| 885 | |||
| 886 | return $decal_phrase_idx+$csti-1; | ||
| 887 | } | ||
| 888 | |||
| 889 | |||
| 890 | sub parse_constituents_old { | ||
| 891 | my $constituents=$_[0]; | ||
| 892 | my $tmpptr=$_[1]; | ||
| 893 | my $decal_phrase_idx=$_[1]; | ||
| 894 | my $doc_hash=$_[2]; | ||
| 895 | my $lexer; | ||
| 896 | my $debug_mode=0; | ||
| 897 | my $lconst=0; | ||
| 898 | my $nconst=0; | ||
| 899 | |||
| 900 | my @tab_nconst; | ||
| 901 | my @tab_type; | ||
| 902 | my @tab_string; | ||
| 903 | |||
| 904 | my $word_count=$word_id_np; | ||
| 905 | my $lastword=""; | ||
| 906 | |||
| 907 | # print STDERR "PHRASE DECAL: $decal_phrase_idx\n"; | ||
| 908 | |||
| 909 | my @cst_token=( | ||
| 910 | # OPEN | ||
| 911 | 'type1','\[([A-Z]+)', sub { | ||
| 912 | # open constituent | ||
| 913 | if($lconst>0){ | ||
| 914 | $tab_string[$tab_nconst[$lconst]].="phrase".($decal_phrase_idx+$nconst+1)." "; | ||
| 915 | } | ||
| 916 | $lconst++; | ||
| 917 | $nconst++; | ||
| 918 | $tab_nconst[$lconst]=$nconst; | ||
| 919 | |||
| 920 | # get type | ||
| 921 | $tab_type[$tab_nconst[$lconst]]=$1; | ||
| 922 | $lexer->end('type1'); | ||
| 923 | $lexer->start('string'); | ||
| 924 | |||
| 925 | print STDERR "*** DEBUG *** Opened constituent $nconst with type ".$1."\n" unless ($debug_mode==0); | ||
| 926 | |||
| 927 | }, | ||
| 928 | # CLOSE | ||
| 929 | 'type2','([A-Z]+)\]', sub { | ||
| 930 | # check type | ||
| 931 | if($1 ne $tab_type[$tab_nconst[$lconst]]){ | ||
| 932 | print STDERR "Error found at level $lconst: types don't match!\n"; | ||
| 933 | $lexer->end('ALL'); | ||
| 934 | exit 0; | ||
| 935 | } | ||
| 936 | # remove ending space | ||
| 937 | $tab_string[$tab_nconst[$lconst]]=~s/\s+$//sgo; | ||
| 938 | # close constituent | ||
| 939 | print STDERR "*** DEBUG *** Closing constituent $tab_nconst[$lconst]\n" unless ($debug_mode==0); | ||
| 940 | $lconst--; | ||
| 941 | }, | ||
| 942 | # STRING | ||
| 943 | 'string','[^\s]+\s', sub { | ||
| 944 | print STDERR "*** DEBUG *** Found string '".$_[0]->text."'\n" unless ($debug_mode==0); | ||
| 945 | if((defined $tab_string[$tab_nconst[$lconst]])&&($tab_string[$tab_nconst[$lconst]] ne "")){ | ||
| 946 | print STDERR "*** DEBUG *** Appended to previously found string\n" unless ($debug_mode==0); | ||
| 947 | # $tab_string[$tab_nconst[$lconst]].=$_[0]->text; | ||
| 948 | if(($_[0]->text eq $lastword) || ($_[0]->text=~/^\./)){ | ||
| 949 | }else{ | ||
| 950 | $tab_string[$tab_nconst[$lconst]].="word$word_id_np "; | ||
| 951 | $word_id_np++; | ||
| 952 | $lastword=$_[0]->text; | ||
| 953 | } | ||
| 954 | }else{ | ||
| 955 | # $tab_string[$tab_nconst[$lconst]]=$_[0]->text; | ||
| 956 | if(!(($_[0]->text eq $lastword)||($_[0]->text=~/^\./))){ | ||
| 957 | $lastword=$_[0]->text; | ||
| 958 | $tab_string[$tab_nconst[$lconst]]="word$word_id_np "; | ||
| 959 | $word_id_np++; | ||
| 960 | }else{ | ||
| 961 | } | ||
| 962 | } | ||
| 963 | $lexer->end('string'); | ||
| 964 | $lexer->start('type2'); | ||
| 965 | }, | ||
| 966 | # MISC | ||
| 967 | 'espace','\s+', sub { | ||
| 968 | }, | ||
| 969 | 'newline','\n', sub { | ||
| 970 | # dump result and reset automaton memory | ||
| 971 | }, | ||
| 972 | |||
| 973 | # ERROR HANDLER | ||
| 974 | 'ALL:ERROR' => '' => sub { | ||
| 975 | print STDERR "Syntax error: malformed string\n"; | ||
| 976 | $lexer->end('ALL'); | ||
| 977 | #return $decal_phrase_idx; | ||
| 978 | } | ||
| 979 | |||
| 980 | ); | ||
| 981 | |||
| 982 | Parse::Lex->exclusive('parse'); | ||
| 983 | $lexer=Parse::Lex->new(@cst_token); | ||
| 984 | # print STDERR "\nPARSING CONSTITUENTS STRING\n$constituents\n"; | ||
| 985 | $lexer->from($constituents); | ||
| 986 | $lexer->every(sub{}); | ||
| 987 | |||
| 988 | # my $parametre; | ||
| 989 | # my $format=0; | ||
| 990 | # foreach $parametre (@ARGV){ | ||
| 991 | # if($parametre eq "--xml"){ | ||
| 992 | # $format=1; | ||
| 993 | # } | ||
| 994 | # } | ||
| 995 | |||
| 996 | # create phrases (constituents) | ||
| 997 | # $doc_hash->{"syntactic_relation$relation_id"}={}; | ||
| 998 | # $doc_hash->{"syntactic_relation$relation_id"}->{'id'}="syntactic_relation$relation_id"; | ||
| 999 | # $doc_hash->{"syntactic_relation$relation_id"}->{'datatype'}="syntactic_relation"; | ||
| 1000 | # $doc_hash->{"syntactic_relation$relation_id"}->{'syntactic_relation_type'}="$relation"; | ||
| 1001 | # $doc_hash->{"syntactic_relation$relation_id"}->{'refid_head'} = {}; | ||
| 1002 | # $doc_hash->{"syntactic_relation$relation_id"}->{'refid_head'}->{'datatype'}="refid_head"; | ||
| 1003 | my $csti=1; | ||
| 1004 | # for($csti=1;$csti<scalar @tab_type;$csti++){ | ||
| 1005 | # $doc_hash->{"phrase$csti"}={}; | ||
| 1006 | # $doc_hash->{"phrase$csti"}->{"id"}="phrase$csti"; | ||
| 1007 | # $doc_hash->{"phrase$csti"}->{"datatype"}="phrase"; | ||
| 1008 | # $doc_hash->{"phrase$csti"}->{"type"}=$tab_type[$csti]; | ||
| 1009 | # $doc_hash->{"phrase$csti"}->{"contents"}=$tab_string[$csti]; | ||
| 1010 | # print STDERR "<phrase>\n"; | ||
| 1011 | # print STDERR " <id>phrase".($decal_phrase_idx+$csti)."</id>\n"; | ||
| 1012 | # print STDERR " <type>$tab_type[$csti]</type>\n"; | ||
| 1013 | # print STDERR " <form>$tab_string[$csti]</form>\n"; | ||
| 1014 | # print STDERR "</phrase>\n"; | ||
| 1015 | # } | ||
| 1016 | |||
| 1017 | $lexer->end('ALL'); | ||
| 1018 | undef $lexer; | ||
| 1019 | |||
| 1020 | # $word_count=$word_id_np-$word_count; | ||
| 1021 | # print STDERR "\nWord count for this sentence: $word_count\n"; | ||
| 1022 | |||
| 1023 | return $decal_phrase_idx+$csti-1; | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | |||
| 1027 | |||
| 1028 | # TODO : Check that parsing is only performed on english texts | ||
| 1029 | |||
| 1030 | sub bio_syntactic_parsing { | ||
| 1031 | my ($h_config, $doc_hash) = @_; | ||
| 1032 | |||
| 1033 | my $line; | ||
| 1034 | my $insentence; | ||
| 1035 | my $sentence; | ||
| 1036 | |||
| 1037 | my $parsedconstituent = 0; | ||
| 1038 | |||
| 1039 | my $tokens; | ||
| 1040 | my $analyses; | ||
| 1041 | my $analysis; | ||
| 1042 | my $nsentence; | ||
| 1043 | my $token_start; | ||
| 1044 | my $token_end; | ||
| 1045 | my $relation; | ||
| 1046 | my $left_wall; | ||
| 1047 | my $right_wall; | ||
| 1048 | |||
| 1049 | my $relation_id; | ||
| 1050 | |||
| 1051 | my @arr_tokens; | ||
| 1052 | my $last_token; | ||
| 1053 | my $wordidshift=0; | ||
| 1054 | |||
| 1055 | my $phrase_idx=$Alvis::NLPPlatform::Annotation::phrase_idx; | ||
| 1056 | |||
| 1057 | |||
| 1058 | my $word; | ||
| 1059 | my $word_cont; | ||
| 1060 | my $word_id; | ||
| 1061 | my $i; | ||
| 1062 | my $sentences_cont=""; | ||
| 1063 | |||
| 1064 | my @tab_word_punct; | ||
| 1065 | my @tab_word; | ||
| 1066 | my $idx_tab_word_punct=1; | ||
| 1067 | my $idx_tab_word=1; | ||
| 1068 | my @tab_mapping; | ||
| 1069 | |||
| 1070 | # print out words+punct and fill in a tab | ||
| 1071 | push @tab_word_punct," "; | ||
| 1072 | push @tab_word," "; | ||
| 1073 | my $decal=1; | ||
| 1074 | |||
| 1075 | my $searchterm; | ||
| 1076 | my $sti; | ||
| 1077 | my $word_np; | ||
| 1078 | |||
| 1079 | my @tab_tmp; | ||
| 1080 | my $tmp_sp; | ||
| 1081 | my $spi=0; | ||
| 1082 | |||
| 1083 | my $termsfound=0; | ||
| 1084 | my $stubs=0; | ||
| 1085 | |||
| 1086 | my $skip=0; | ||
| 1087 | |||
| 1088 | my @tab_start_term=(); | ||
| 1089 | my @tab_end_term=(); | ||
| 1090 | |||
| 1091 | my $constituents; | ||
| 1092 | my $nb_constituents; | ||
| 1093 | |||
| 1094 | |||
| 1095 | print STDERR " Performing syntactic analysis..."; | ||
| 1096 | |||
| 1097 | $sentences_cont .= "<sentences>\n\n"; | ||
| 1098 | |||
| 1099 | $sentences_cont .= "\n<sentence>\n"; | ||
| 1100 | |||
| 1101 | |||
| 1102 | foreach $word(Alvis::NLPPlatform::Annotation::sort(\%Alvis::NLPPlatform::hash_words_punct)){ | ||
| 1103 | if($skip>0){ | ||
| 1104 | $skip--; | ||
| 1105 | # next; | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | # print STDERR "word\n"; | ||
| 1109 | $word_cont=$Alvis::NLPPlatform::hash_words_punct{$word}; | ||
| 1110 | push @tab_word_punct,$word_cont; | ||
| 1111 | |||
| 1112 | my $postag=$Alvis::NLPPlatform::hash_postags{"word$decal"}; | ||
| 1113 | if((exists $Alvis::NLPPlatform::hash_words{"word$decal"})&&($word_cont ne $Alvis::NLPPlatform::hash_words{"word$decal"})){ | ||
| 1114 | # punctuation, delay incrementation of index "decal" | ||
| 1115 | $decal--; | ||
| 1116 | $postag="PUNCT"; | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | if($word_cont eq "."){ | ||
| 1120 | $sentences_cont.="<w c=\"SENT\">.</w>\n"; | ||
| 1121 | $sentences_cont.="</sentence>\n"; | ||
| 1122 | $sentences_cont.="<sentence>\n"; | ||
| 1123 | }else{ | ||
| 1124 | # determine if current word is part of a term | ||
| 1125 | $searchterm=""; | ||
| 1126 | @tab_tmp=(); | ||
| 1127 | $tmp_sp=0; | ||
| 1128 | |||
| 1129 | $word_np=$Alvis::NLPPlatform::hash_words{"word$decal"}; | ||
| 1130 | for($sti=0;$sti<scalar @Alvis::NLPPlatform::found_terms;$sti++){ | ||
| 1131 | my $ref_tab_tmp_words; | ||
| 1132 | $searchterm=$Alvis::NLPPlatform::found_terms[$sti]; | ||
| 1133 | # print STDERR "Searching for term $searchterm / comparing with word ".$Alvis::NLPPlatform::hash_words{"word$decal"}."\n"; | ||
| 1134 | $ref_tab_tmp_words=$Alvis::NLPPlatform::found_terms_words[$sti]; | ||
| 1135 | # print STDERR "word$decal- Searching for term ($sti) $searchterm / words "; | ||
| 1136 | # print STDERR join ",",@$ref_tab_tmp_words; | ||
| 1137 | # print STDERR "\n"; | ||
| 1138 | |||
| 1139 | if(lc($Alvis::NLPPlatform::hash_words{"word$decal"}) eq lc($searchterm)){ | ||
| 1140 | # look for one-word terms | ||
| 1141 | # print STDERR "\n--== Found term $searchterm ==--\n"; | ||
| 1142 | # print STDERR "word$decal makes up '$searchterm' on its own (single word/NE).\n"; | ||
| 1143 | $termsfound++; | ||
| 1144 | push @Alvis::NLPPlatform::found_terms_tidx,$sti; | ||
| 1145 | $sti=scalar @Alvis::NLPPlatform::found_terms; | ||
| 1146 | $tmp_sp=1; | ||
| 1147 | last; | ||
| 1148 | }else{ | ||
| 1149 | # look for multiple word terms (they have to be phrases) | ||
| 1150 | if($Alvis::NLPPlatform::found_terms_phr[$sti] != -666){ | ||
| 1151 | # determine if the current word ($decal) is part of it | ||
| 1152 | # then set $tmp_sp to the nb of words involved | ||
| 1153 | if(@$ref_tab_tmp_words[0] eq "word$decal"){ | ||
| 1154 | # the current word is the first word of the term | ||
| 1155 | # print STDERR "word$decal is the first word of the term '$searchterm'.\n"; | ||
| 1156 | $termsfound++; | ||
| 1157 | push @Alvis::NLPPlatform::found_terms_tidx,$sti; | ||
| 1158 | $sti=scalar @Alvis::NLPPlatform::found_terms; | ||
| 1159 | $tmp_sp=scalar @$ref_tab_tmp_words; | ||
| 1160 | last; | ||
| 1161 | } | ||
| 1162 | } | ||
| 1163 | |||
| 1164 | ############################################### | ||
| 1165 | # if($searchterm=~/^$word_np/i){ | ||
| 1166 | # print STDERR "\n--== $word_np is the beginning of $searchterm ==--\n"; | ||
| 1167 | # $stubs++; | ||
| 1168 | |||
| 1169 | # @tab_tmp=split / /,$searchterm; | ||
| 1170 | # $tmp_sp=scalar @tab_tmp; | ||
| 1171 | # print STDERR "--== $tmp_sp words, right? ==--\n"; | ||
| 1172 | # $skip=$tmp_sp; | ||
| 1173 | |||
| 1174 | # for($spi=$decal+1;$spi<$decal+$tmp_sp;$spi++){ | ||
| 1175 | # $word_np.=" ".$Alvis::NLPPlatform::hash_words{"word$spi"}; | ||
| 1176 | # } | ||
| 1177 | # print STDERR "--== Rebuilt term to $word_np ==--\n"; | ||
| 1178 | # $termsfound++; | ||
| 1179 | # push @Alvis::NLPPlatform::found_terms_tidx,$sti; | ||
| 1180 | # $sti=scalar @Alvis::NLPPlatform::found_terms; | ||
| 1181 | # last; | ||
| 1182 | # } | ||
| 1183 | ############################################### | ||
| 1184 | } | ||
| 1185 | $searchterm=""; | ||
| 1186 | } | ||
| 1187 | # | ||
| 1188 | if($searchterm eq ""){ | ||
| 1189 | # there was no term | ||
| 1190 | if (!defined($postag)) { warn "no POStag defined for $word_cont - setting to symbol\n"; $postag = "SYM";} | ||
| 1191 | if (!defined($word_cont)) { warn "no word_cont $word_cont\n";} | ||
| 1192 | $sentences_cont.="<w c=\"$postag\">$word_cont</w>\n"; | ||
| 1193 | }else{ | ||
| 1194 | # there was a term | ||
| 1195 | # print STDERR "Searchterm : $searchterm\n"; | ||
| 1196 | # print STDERR "word_np : $word_np\n"; | ||
| 1197 | $sentences_cont.="<term c=\"$postag\" parse_as=\"$word_cont.n\" internal=\"\" head=\"0\">\n"; | ||
| 1198 | # insert all the words that make up this term | ||
| 1199 | my $nbsteps=$decal+$tmp_sp; | ||
| 1200 | if($nbsteps==$decal){ | ||
| 1201 | $nbsteps++; | ||
| 1202 | } | ||
| 1203 | push @tab_start_term,$decal; | ||
| 1204 | push @tab_end_term,($nbsteps-1); | ||
| 1205 | for($spi=$decal;$spi<$nbsteps;$spi++){ | ||
| 1206 | $sentences_cont.="<w c=\"NN\">".$Alvis::NLPPlatform::hash_words{"word$spi"}."</w>\n"; | ||
| 1207 | # print STDERR "Adding ".$Alvis::NLPPlatform::hash_words{"word$spi"}."\n"; | ||
| 1208 | } | ||
| 1209 | ## | ||
| 1210 | $sentences_cont.="</term>\n"; | ||
| 1211 | } | ||
| 1212 | } | ||
| 1213 | |||
| 1214 | $decal++; | ||
| 1215 | |||
| 1216 | } | ||
| 1217 | |||
| 1218 | # fill words tab | ||
| 1219 | foreach $word(Alvis::NLPPlatform::Annotation::sort(\%Alvis::NLPPlatform::hash_words)){ | ||
| 1220 | push @tab_word,$Alvis::NLPPlatform::hash_words{$word}; | ||
| 1221 | } | ||
| 1222 | |||
| 1223 | # pre-compute mapping between words+punct and words | ||
| 1224 | my $idx_nopunct=1; | ||
| 1225 | for($i=0;$i<scalar @tab_word_punct;$i++){ | ||
| 1226 | if(($idx_nopunct<scalar @tab_word)&&($tab_word_punct[$i] eq $tab_word[$idx_nopunct])){ | ||
| 1227 | $tab_mapping[$i]=$idx_nopunct; | ||
| 1228 | $idx_nopunct++; | ||
| 1229 | } | ||
| 1230 | } | ||
| 1231 | # for($i=0;$i<scalar @tab_mapping;$i++){ | ||
| 1232 | # print STDERR "$i : " . $tab_mapping[$i] . "\n"; | ||
| 1233 | # } | ||
| 1234 | |||
| 1235 | # remove whitespaces in NE | ||
| 1236 | my $ne; | ||
| 1237 | my $ne_cont; | ||
| 1238 | my $ne_mod; | ||
| 1239 | foreach $ne(keys %Alvis::NLPPlatform::hash_named_entities){ | ||
| 1240 | $ne_cont=$Alvis::NLPPlatform::hash_named_entities{$ne}; | ||
| 1241 | $ne_mod=$ne_cont; | ||
| 1242 | if($ne_cont=~/ /){ | ||
| 1243 | if($sentences_cont=~/\Q$ne_cont\E/){ | ||
| 1244 | $ne_mod=~s/ /\_/g; | ||
| 1245 | $sentences_cont=~s/\Q$ne_cont\E/$ne_mod/g; | ||
| 1246 | } | ||
| 1247 | } | ||
| 1248 | } | ||
| 1249 | |||
| 1250 | $sentences_cont=~s/<sentence>\n$//sgo; | ||
| 1251 | |||
| 1252 | if ($sentences_cont !~ /<\sentence>\n/) { | ||
| 1253 | # to remove after checking the wrapper (above) | ||
| 1254 | $sentences_cont.="</sentence>\n"; | ||
| 1255 | } | ||
| 1256 | |||
| 1257 | $sentences_cont .= "\n\n</sentences>\n"; | ||
| 1258 | |||
| 1259 | # Setting options | ||
| 1260 | |||
| 1261 | $sentences_cont = "<command string=\"constituents=2\">\n" . $sentences_cont; | ||
| 1262 | $sentences_cont .= "</command>"; | ||
| 1263 | $sentences_cont = "<command string=\"graphics\">\n" . $sentences_cont; | ||
| 1264 | $sentences_cont .= "</command>"; | ||
| 1265 | # $sentences_cont = "<command string=\"timeout=60\">\n" . $sentences_cont; | ||
| 1266 | # $sentences_cont .= "</command>"; | ||
| 1267 | $sentences_cont = "<command string=\"postscript\">\n" . $sentences_cont; | ||
| 1268 | $sentences_cont .= "</command>"; | ||
| 1269 | # $sentences_cont = "<command string=\"null\">\n" . $sentences_cont; | ||
| 1270 | # $sentences_cont .= "</command>"; | ||
| 1271 | # $sentences_cont = "<command string=\"ask\">\n" . $sentences_cont; | ||
| 1272 | # $sentences_cont .= "</command>"; | ||
| 1273 | # $sentences_cont = "<command string=\"walls\">\n" . $sentences_cont; | ||
| 1274 | # $sentences_cont .= "</command>"; | ||
| 1275 | # $sentences_cont = "<command string=\"union\">\n" . $sentences_cont; | ||
| 1276 | # $sentences_cont .= "</command>"; | ||
| 1277 | |||
| 1278 | open CORPUS, ">" . $h_config->{"TMPFILE"} . ".corpus.tmp"; | ||
| 1279 | |||
| 1280 | print CORPUS Encode::encode_utf8($sentences_cont); | ||
| 1281 | # print CORPUS $sentences_cont; | ||
| 1282 | close CORPUS; | ||
| 1283 | |||
| 1284 | my $command_line; | ||
| 1285 | my $command_line2; | ||
| 1286 | my $command_line3; | ||
| 1287 | if($Alvis::NLPPlatform::Annotation::ALVISLANGUAGE eq "FR"){ | ||
| 1288 | # French parser command line | ||
| 1289 | }else{ | ||
| 1290 | $command_line = $h_config->{'NLP_tools'}->{'SYNTACTIC_ANALYSIS_EN'} . " < " . $h_config->{'TMPFILE'} . ".corpus.tmp > " . $h_config->{'TMPFILE'} . ".result.tmp.1 2>> " . $Alvis::NLPPlatform::ALVISLOGFILE; | ||
| 1291 | $command_line2 = $h_config->{'NLP_tools'}->{'SYNTACTIC_ANALYSIS_EN_LP2LP_CLEAN'} . " < " . $h_config->{'TMPFILE'} . ".result.tmp.1 > " . $h_config->{'TMPFILE'} . ".result.tmp.2 2>> " . $Alvis::NLPPlatform::ALVISLOGFILE; | ||
| 1292 | |||
| 1293 | $command_line3 = $h_config->{'NLP_tools'}->{'SYNTACTIC_ANALYSIS_EN_LP2LP'} . " " . $h_config->{'TMPFILE'} . ".result.tmp.2 > " . $h_config->{'TMPFILE'} . ".result.tmp 2>> " . $Alvis::NLPPlatform::ALVISLOGFILE; | ||
| 1294 | } | ||
| 1295 | # print STDERR "$command_line\n"; | ||
| 1296 | `$command_line`; | ||
| 1297 | |||
| 1298 | &clean_bioLG($h_config->{'TMPFILE'} . ".result.tmp.1", $h_config->{'TMPFILE'} . ".result.tmp.2"); | ||
| 1299 | # print STDERR "$command_line2\n"; | ||
| 1300 | # `$command_line2`; | ||
| 1301 | |||
| 1302 | # print STDERR "$command_line3\n"; | ||
| 1303 | `$command_line3`; | ||
| 1304 | |||
| 1305 | # print STDERR "\n$command_line\n$command_line2\n"; | ||
| 1306 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $h_config->{'TMPFILE'} . ".result.tmp.1"; | ||
| 1307 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $h_config->{'TMPFILE'} . ".result.tmp.2"; | ||
| 1308 | |||
| 1309 | # process syntactic analysis | ||
| 1310 | |||
| 1311 | $insentence=0; | ||
| 1312 | $nsentence=0; | ||
| 1313 | $relation_id=1; | ||
| 1314 | |||
| 1315 | $constituents=""; | ||
| 1316 | $nb_constituents=0; | ||
| 1317 | |||
| 1318 | open SYN_RES, "<" . $h_config->{'TMPFILE'}. ".result.tmp"; | ||
| 1319 | |||
| 1320 | open CONSTITUENT_OUTPUT,">" . $Alvis::NLPPlatform::Annotation::document_record_id . ".constituents"; | ||
| 1321 | |||
| 1322 | while($line=<SYN_RES>) | ||
| 1323 | { | ||
| 1324 | if (index($line, "[Sentence") == 0) { | ||
| 1325 | $parsedconstituent = 0; | ||
| 1326 | } | ||
| 1327 | if(index($line,"[(")==0){ | ||
| 1328 | $insentence=1; | ||
| 1329 | # XXX | ||
| 1330 | $nsentence++; | ||
| 1331 | $sentence=""; | ||
| 1332 | $tokens=""; | ||
| 1333 | $analyses=""; | ||
| 1334 | $left_wall=0; | ||
| 1335 | } | ||
| 1336 | if(index($line,"[S ")==0){ | ||
| 1337 | $constituents=$line; | ||
| 1338 | # print STDERR "**** FOUND CONSTITUENTS SENDING DECAL $phrase_idx ****\n"; | ||
| 1339 | if ($parsedconstituent == 0) { | ||
| 1340 | $nb_constituents++; | ||
| 1341 | $phrase_idx=parse_constituents($constituents,$phrase_idx,$doc_hash); | ||
| 1342 | } | ||
| 1343 | $parsedconstituent = 1; | ||
| 1344 | $constituents=~s/\[([A-Z]+) /($1 /sgo; | ||
| 1345 | # $constituents=~s/\[([A-Z]+) /<constituent>$1 /sgo; | ||
| 1346 | $constituents=~s/[A-Z]+\]/)/sgo; | ||
| 1347 | # $constituents=~s/[A-Z]+\]/<\/constituent>/sgo; | ||
| 1348 | # print CONSTITUENT_OUTPUT $Alvis::NLPPlatform::Annotation::document_record_id . "\t"; | ||
| 1349 | print CONSTITUENT_OUTPUT "$constituents\n"; | ||
| 1350 | |||
| 1351 | # print STDERR "**** RECUP $phrase_idx ****\n"; | ||
| 1352 | } | ||
| 1353 | if($insentence==1){ | ||
| 1354 | $sentence.=$line; | ||
| 1355 | } | ||
| 1356 | # if(index($line,"diagram")==0){ | ||
| 1357 | if(index($line,"[]")==0){ | ||
| 1358 | # process the line | ||
| 1359 | $sentence=~s/\[Sentence\s+[0-9]+\]//sgo; | ||
| 1360 | $sentence=~s/\[Linkage\s+[0-9]+\]//sgo; | ||
| 1361 | $sentence=~s/\[\]//sgo; | ||
| 1362 | $sentence=~s/\n//sgo; | ||
| 1363 | # $sentence=~s/\[[0-9\s]*\]diagram$//g; | ||
| 1364 | if ($sentence=~m/^(.+)\[\[/) { | ||
| 1365 | $tokens=$1; | ||
| 1366 | # print STDERR "\n\n--> $sentence\n\n"; | ||
| 1367 | $analyses = $'; | ||
| 1368 | # ' | ||
| 1369 | # output | ||
| 1370 | # search left-wall to shift identifiers | ||
| 1371 | if($tokens =~ /LEFT\-WALL/so){ | ||
| 1372 | $left_wall=1; | ||
| 1373 | }else{ | ||
| 1374 | $left_wall=0; | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | # search right-wall, simply to ignore it | ||
| 1378 | if($tokens =~ /RIGHT\-WALL/so){ | ||
| 1379 | $right_wall=1; | ||
| 1380 | }else{ | ||
| 1381 | $right_wall=0; | ||
| 1382 | } | ||
| 1383 | |||
| 1384 | # parse tokens | ||
| 1385 | @arr_tokens=split /\)\(/,$tokens; | ||
| 1386 | $last_token=(scalar @arr_tokens)-1; | ||
| 1387 | $arr_tokens[0]=~s/^\[\(//sgo; | ||
| 1388 | $arr_tokens[$last_token]=~s/\)\]$//sgo; | ||
| 1389 | |||
| 1390 | # my $tmpfdsf; | ||
| 1391 | # for($tmpfdsf=0;$tmpfdsf<=$last_token;$tmpfdsf++){ | ||
| 1392 | # #print STDERR "******\$\$\$\$\$\$****** ($tmpfdsf) $arr_tokens[$tmpfdsf]\n"; | ||
| 1393 | # } | ||
| 1394 | |||
| 1395 | # Parsing | ||
| 1396 | my $valid_analysis; | ||
| 1397 | while($analyses=~/(\[[0-9]+\s[0-9]+\s[0-9]+\s[^\]]+\])/sgoc){ | ||
| 1398 | my $kref=0; | ||
| 1399 | $analysis=$1; | ||
| 1400 | if($analysis=~m/\[([0-9]+)\s([0-9]+)\s([0-9]+)\s\(([^\]]+)\)\]/sgo){ # m?? | ||
| 1401 | $valid_analysis=1; | ||
| 1402 | }else{ | ||
| 1403 | $valid_analysis=0; | ||
| 1404 | } | ||
| 1405 | $token_start=$1; | ||
| 1406 | $token_end=$2; | ||
| 1407 | $relation=$4; | ||
| 1408 | if( | ||
| 1409 | (($left_wall==1)&&(($token_start==0) || ($token_end==0))) | ||
| 1410 | ||(($right_wall==1)&&(($token_start==$last_token) || ($token_end==$last_token))) | ||
| 1411 | || ($valid_analysis==0) | ||
| 1412 | ){ | ||
| 1413 | # ignore any relation with the left or right wall | ||
| 1414 | # print STDERR "$relation [$token_start $token_end] ==> ignored\n"; | ||
| 1415 | }else{ | ||
| 1416 | if($left_wall==0){ | ||
| 1417 | $token_start++; | ||
| 1418 | $token_end++; | ||
| 1419 | } | ||
| 1420 | # make sure we're not dealing with punctuation, otherwise just ignore | ||
| 1421 | my $tmp1=$token_start+$wordidshift; | ||
| 1422 | my $tmp2=$token_end+$wordidshift; | ||
| 1423 | my $tmp1_within=0; | ||
| 1424 | my $tmp2_within=0; | ||
| 1425 | if(($tmp1 < scalar @tab_mapping) && ($tmp2 < scalar @tab_mapping)){ | ||
| 1426 | # print STDERR "$tmp1 $tmp2\n"; | ||
| 1427 | # if (defined($tab_mapping[$tmp1])) { print STDERR $tab_mapping[$tmp1] . "\n";} | ||
| 1428 | # if (defined($tab_mapping[$tmp2])) { print STDERR $tab_mapping[$tmp2] . "\n";} | ||
| 1429 | if ((defined($tab_mapping[$tmp1])) && (defined($tab_mapping[$tmp2])) && ($tab_mapping[$tmp1] ne "") && ($tab_mapping[$tmp2] ne "")){ | ||
| 1430 | # determine if there is a relation between a word inside a term and another word not inside a term | ||
| 1431 | my $lft; | ||
| 1432 | for($lft=0;$lft<scalar @tab_start_term;$lft++){ | ||
| 1433 | # is head within term? | ||
| 1434 | if( | ||
| 1435 | ($tab_mapping[$tmp1]>=$tab_start_term[$lft] && | ||
| 1436 | $tab_mapping[$tmp1]<=$tab_end_term[$lft]) | ||
| 1437 | ){ | ||
| 1438 | $tmp1_within=1; | ||
| 1439 | } | ||
| 1440 | |||
| 1441 | # is modifier within term? | ||
| 1442 | if( | ||
| 1443 | ($tab_mapping[$tmp2]>=$tab_start_term[$lft] && | ||
| 1444 | $tab_mapping[$tmp2]<=$tab_end_term[$lft]) | ||
| 1445 | ){ | ||
| 1446 | $tmp2_within=1; | ||
| 1447 | } | ||
| 1448 | |||
| 1449 | # rules set here: | ||
| 1450 | # relation between two words in a term: W-W relation | ||
| 1451 | # relation between two words outside of a term: W-W relation | ||
| 1452 | # relation between a word in a term and another word outside this term: W-P relation | ||
| 1453 | if(($tmp1_within+$tmp2_within)==1){ | ||
| 1454 | # one of them is in, the other is out | ||
| 1455 | # print STDERR "\n"; | ||
| 1456 | # find term id | ||
| 1457 | $kref=$Alvis::NLPPlatform::found_terms_tidx[$lft]; | ||
| 1458 | $kref++; # it's always >0 | ||
| 1459 | last; | ||
| 1460 | } | ||
| 1461 | } | ||
| 1462 | $doc_hash->{"syntactic_relation$relation_id"}={}; | ||
| 1463 | $doc_hash->{"syntactic_relation$relation_id"}->{'id'}="syntactic_relation$relation_id"; | ||
| 1464 | $doc_hash->{"syntactic_relation$relation_id"}->{'datatype'}="syntactic_relation"; | ||
| 1465 | $doc_hash->{"syntactic_relation$relation_id"}->{'syntactic_relation_type'}="$relation"; | ||
| 1466 | $doc_hash->{"syntactic_relation$relation_id"}->{'refid_head'} = {}; | ||
| 1467 | $doc_hash->{"syntactic_relation$relation_id"}->{'refid_head'}->{'datatype'}="refid_head"; | ||
| 1468 | if(($kref>0)&&($tmp1_within==1)&&($Alvis::NLPPlatform::found_terms_phr[($kref-1)]!=-666)){ | ||
| 1469 | $doc_hash->{"syntactic_relation$relation_id"}->{'refid_head'}->{"refid_phrase"}="phrase".$Alvis::NLPPlatform::found_terms_phr[($kref-1)]; | ||
| 1470 | # print STDERR "\n\nSize: ".scalar @Alvis::NLPPlatform::found_terms_phr."\n"; | ||
| 1471 | # print STDERR "Index: $kref\n\n"; | ||
| 1472 | }else{ | ||
| 1473 | $doc_hash->{"syntactic_relation$relation_id"}->{'refid_head'}->{"refid_word"}="word".$tab_mapping[($token_start+$wordidshift)]; | ||
| 1474 | } | ||
| 1475 | # $doc_hash->{"syntactic_relation$relation_id"}->{'refid_head'}="word".$tab_mapping[($token_start+$wordidshift)]; | ||
| 1476 | $doc_hash->{"syntactic_relation$relation_id"}->{'refid_modifier'} = {}; | ||
| 1477 | $doc_hash->{"syntactic_relation$relation_id"}->{'refid_modifier'}->{'datatype'}="refid_modifier"; | ||
| 1478 | if(($kref>0)&&($tmp2_within==1)&&($Alvis::NLPPlatform::found_terms_phr[($kref-1)]!=-666)){ | ||
| 1479 | $doc_hash->{"syntactic_relation$relation_id"}->{'refid_modifier'}->{"refid_phrase"}="phrase".$Alvis::NLPPlatform::found_terms_phr[($kref-1)]; | ||
| 1480 | # print STDERR "\n\nIndex: $kref\n\n"; | ||
| 1481 | }else{ | ||
| 1482 | $doc_hash->{"syntactic_relation$relation_id"}->{'refid_modifier'}->{"refid_word"}="word".$tab_mapping[($token_end+$wordidshift)]; | ||
| 1483 | } | ||
| 1484 | # $doc_hash->{"syntactic_relation$relation_id"}->{'refid_modifier'}="word".$tab_mapping[($token_end+$wordidshift)]; | ||
| 1485 | |||
| 1486 | $relation_id++; | ||
| 1487 | } | ||
| 1488 | } | ||
| 1489 | } | ||
| 1490 | } | ||
| 1491 | |||
| 1492 | # trash everything and continue the loop | ||
| 1493 | |||
| 1494 | $insentence=0; | ||
| 1495 | $wordidshift+=$last_token-1; | ||
| 1496 | } | ||
| 1497 | } | ||
| 1498 | } | ||
| 1499 | close CONSTITUENT_OUTPUT; | ||
| 1500 | close SYN_RES; | ||
| 1501 | |||
| 1502 | # print STDERR $h_config->{'TMPFILE'}. ".corpus.tmp" . "\n"; | ||
| 1503 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $h_config->{'TMPFILE'}. ".corpus.tmp"; | ||
| 1504 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $h_config->{'TMPFILE'} . ".result.tmp"; | ||
| 1505 | |||
| 1506 | $Alvis::NLPPlatform::nb_relations=$relation_id-1; | ||
| 1507 | $Alvis::NLPPlatform::Annotation::phrase_idx=$phrase_idx; | ||
| 1508 | |||
| 1509 | print STDERR "done - Found $Alvis::NLPPlatform::nb_relations relations, $termsfound full terms, $nb_constituents constituents.\n"; | ||
| 1510 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Syntactic Relations : " . $Alvis::NLPPlatform::nb_relations; | ||
| 1511 | } | ||
| 1512 | |||
| 1513 | |||
| 1514 | sub clean_bioLG { | ||
| 1515 | |||
| 1516 | my ($infile, $outfile) = @_; | ||
| 1517 | |||
| 1518 | |||
| 1519 | my $line = ""; | ||
| 1520 | my $sentence_counter = 0; | ||
| 1521 | my $linkage_counter = 0; | ||
| 1522 | |||
| 1523 | my @linkage_output; | ||
| 1524 | |||
| 1525 | # my $line_prec = ""; | ||
| 1526 | |||
| 1527 | open INFILE, $infile or die "No such file $infile\n"; | ||
| 1528 | binmode INFILE; | ||
| 1529 | open OUTFILE, ">$outfile" or die "No such file $outfile\n"; | ||
| 1530 | |||
| 1531 | # puts the text on only one line | ||
| 1532 | do { | ||
| 1533 | # We first remove the outputting input | ||
| 1534 | while((defined ($line = <INFILE>)) && ($line !~ /^\+\+\+\+Time/o)) { | ||
| 1535 | # print $line; | ||
| 1536 | # $line_prec = $line; | ||
| 1537 | }; | ||
| 1538 | |||
| 1539 | if ((defined $line) && ($line =~ /^\+\+\+\+Time/o)) { | ||
| 1540 | $linkage_counter = 0; | ||
| 1541 | @linkage_output = (); | ||
| 1542 | do { | ||
| 1543 | #We remove the postscript output until we found constituent part | ||
| 1544 | while((defined ($line = <INFILE>)) && ($line !~ /^\[/o)) { | ||
| 1545 | # nothing | ||
| 1546 | } | ||
| 1547 | # we print the output until we find the next postscript part | ||
| 1548 | $linkage_output[$linkage_counter] = $line; | ||
| 1549 | # print $line; | ||
| 1550 | while((defined ($line = <INFILE>)) && ($line ne "diagram\n")) { | ||
| 1551 | # print STDERR "=> $line\n"; | ||
| 1552 | $linkage_output[$linkage_counter] .= $line; | ||
| 1553 | # print $line; | ||
| 1554 | } | ||
| 1555 | # we remove the next postscript part | ||
| 1556 | while ((defined ($line =<INFILE>)) && ($line ne "%%EndDocument\n")) { | ||
| 1557 | # nothing | ||
| 1558 | } | ||
| 1559 | $line = <INFILE>; | ||
| 1560 | $linkage_output[$linkage_counter] .= "\n"; | ||
| 1561 | $linkage_counter++; | ||
| 1562 | # print "\n"; | ||
| 1563 | # Next Linkage ? | ||
| 1564 | } while((defined ($line = <INFILE>)) && ($line =~ /^%!PS-Adobe/o)); | ||
| 1565 | # we print the constituent | ||
| 1566 | print OUTFILE "[Sentence " . $sentence_counter . "]\n"; | ||
| 1567 | $sentence_counter++; | ||
| 1568 | for($linkage_counter = 0; $linkage_counter < scalar(@linkage_output); $linkage_counter++) { | ||
| 1569 | print OUTFILE "[Linkage " . $linkage_counter ."]\n"; | ||
| 1570 | print OUTFILE $linkage_output[$linkage_counter]; | ||
| 1571 | print OUTFILE "$line\n"; | ||
| 1572 | |||
| 1573 | } | ||
| 1574 | # we remove all it remains | ||
| 1575 | while((defined ($line = <INFILE>)) && ($line ne "Press RETURN for the next linkage.\n")) { | ||
| 1576 | #nothing | ||
| 1577 | } | ||
| 1578 | } | ||
| 1579 | |||
| 1580 | } while ($line = <INFILE>); | ||
| 1581 | |||
| 1582 | close INFILE; | ||
| 1583 | close OUTFILE; | ||
| 1584 | |||
| 1585 | return 0; | ||
| 1586 | |||
| 1587 | } | ||
| 1588 | |||
| 1589 | sub semantic_feature_tagging | ||
| 1590 | { | ||
| 1591 | my @arg = @_; | ||
| 1592 | |||
| 1593 | my $class = shift @arg; | ||
| 1594 | |||
| 1595 | $class->SUPER::semantic_feature_tagging(@arg); | ||
| 1596 | |||
| 1597 | } | ||
| 1598 | |||
| 1599 | sub semantic_relation_tagging | ||
| 1600 | { | ||
| 1601 | my @arg = @_; | ||
| 1602 | |||
| 1603 | my $class = shift @arg; | ||
| 1604 | |||
| 1605 | $class->SUPER::semantic_relation_tagging(@arg); | ||
| 1606 | |||
| 1607 | } | ||
| 1608 | |||
| 1609 | |||
| 1610 | sub anaphora_resolution | ||
| 1611 | { | ||
| 1612 | my @arg = @_; | ||
| 1613 | |||
| 1614 | my $class = shift @arg; | ||
| 1615 | |||
| 1616 | $class->SUPER::anaphora_resolution(@arg); | ||
| 1617 | |||
| 1618 | } | ||
| 1619 | |||
| 1620 | |||
| 1621 | |||
| 1622 | 1; | ||
| 1623 | |||
| 1624 | __END__ |