| 1 | package Alvis::NLPPlatform::NLPWrappers; | ||
| 2 | |||
| 3 | #use diagnostics; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Alvis::NLPPlatform::Annotation; | ||
| 8 | use Alvis::TermTagger; | ||
| 9 | |||
| 10 | #use Encode; | ||
| 11 | use Encode qw(:fallbacks);; | ||
| 12 | |||
| 13 | our $VERSION=$Alvis::NLPPlatform::VERSION; | ||
| 14 | |||
| 15 | my @term_list_EN; | ||
| 16 | my @regex_term_list_EN; | ||
| 17 | |||
| 18 | my @term_list_FR; | ||
| 19 | my @regex_term_list_FR; | ||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | sub tokenize | ||
| 24 | { | ||
| 25 | ################################################### | ||
| 26 | my ($class, $h_config, $doc_hash) = @_; | ||
| 27 | my $line; | ||
| 28 | my @characters; | ||
| 29 | my $char; | ||
| 30 | |||
| 31 | my $offset; | ||
| 32 | my $current_char; | ||
| 33 | my $last_char; | ||
| 34 | my $length; | ||
| 35 | my $string; | ||
| 36 | my $token_id; | ||
| 37 | |||
| 38 | my $section_id = 0; | ||
| 39 | |||
| 40 | my $alpha="[A-Za-z\x{C0}-\x{D6}\x{D8}-\x{F6}\x{F8}-\x{FF}]"; | ||
| 41 | |||
| 42 | my $num="[0-9]"; | ||
| 43 | my $sep="[ \\s\\t\\n\\r]"; | ||
| 44 | |||
| 45 | my $canonical; | ||
| 46 | my @lines; | ||
| 47 | |||
| 48 | # my $shift_offset = 0; | ||
| 49 | # my $shift_offset_prec = 0; | ||
| 50 | |||
| 51 | my $dtype; | ||
| 52 | |||
| 53 | my $token_id_str; | ||
| 54 | |||
| 55 | ################################################### | ||
| 56 | |||
| 57 | |||
| 58 | $offset=0; | ||
| 59 | $last_char=0; | ||
| 60 | $length=0; | ||
| 61 | $string=""; | ||
| 62 | $token_id=1; | ||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | print STDERR " Tokenizing... "; | ||
| 67 | |||
| 68 | $canonical = $Alvis::NLPPlatform::Annotation::canonicalDocument; | ||
| 69 | $canonical = Alvis::NLPPlatform::Canonical::CleanUp($canonical, $h_config->{"XML_INPUT"}->{"PRESERVEWHITESPACE"}); | ||
| 70 | |||
| 71 | @lines=split /\n/,$canonical; | ||
| 72 | # map {$_ .= "\n"} @lines; | ||
| 73 | |||
| 74 | foreach $line(@lines) | ||
| 75 | { | ||
| 76 | $line .= "\n"; | ||
| 77 | # convert SGML into characters | ||
| 78 | |||
| 79 | # character spliting | ||
| 80 | @characters=split //, $line; | ||
| 81 | |||
| 82 | foreach $char(@characters){ | ||
| 83 | |||
| 84 | # determine the type of the current character | ||
| 85 | $current_char=4; # default type | ||
| 86 | if($char=~/$alpha/o){$current_char=1;# print STDERR "$char : OK\n"; | ||
| 87 | } | ||
| 88 | if($char=~/$num/o){$current_char=2;} | ||
| 89 | if($char=~/$sep/o){$current_char=3;} | ||
| 90 | # comparison with last seen character | ||
| 91 | |||
| 92 | # if it is the same ... | ||
| 93 | if(($current_char==$last_char) && ($current_char!=4)){ | ||
| 94 | $string=$string . $char; | ||
| 95 | $length++; | ||
| 96 | }else{ | ||
| 97 | if($length>0){ | ||
| 98 | ####################################################### | ||
| 99 | if($last_char==1){$dtype="alpha";} | ||
| 100 | if($last_char==2){$dtype="num";} | ||
| 101 | if($last_char==3){$dtype="sep";} | ||
| 102 | if($last_char==4){$dtype="symb";} | ||
| 103 | $token_id_str = "token$token_id"; | ||
| 104 | $doc_hash->{$token_id_str}={}; | ||
| 105 | $doc_hash->{$token_id_str}->{'datatype'}="token"; | ||
| 106 | $doc_hash->{$token_id_str}->{'type'}=$dtype; | ||
| 107 | $doc_hash->{$token_id_str}->{'id'}=$token_id_str; | ||
| 108 | $doc_hash->{$token_id_str}->{'from'}=$offset; | ||
| 109 | $doc_hash->{$token_id_str}->{'to'}=$offset+$length-1; | ||
| 110 | |||
| 111 | while(($section_id < scalar(@Alvis::NLPPlatform::tab_end_sections_byaddr)) && ($Alvis::NLPPlatform::tab_end_sections_byaddr[$section_id] <= $offset + $length - 1)) { | ||
| 112 | push @Alvis::NLPPlatform::tab_end_sections_bytoken, $token_id_str; #"token$token_id"; | ||
| 113 | $section_id++; | ||
| 114 | } | ||
| 115 | |||
| 116 | if($last_char==3){ | ||
| 117 | $string=~s/\n/\\n/go; | ||
| 118 | $string=~s/\r/\\r/go; | ||
| 119 | $string=~s/\t/\\t/go; | ||
| 120 | } | ||
| 121 | $doc_hash->{$token_id_str}->{"content"}=$string; | ||
| 122 | $Alvis::NLPPlatform::hash_tokens{$token_id_str}=$string; | ||
| 123 | $token_id++; | ||
| 124 | $offset+=$length; | ||
| 125 | ####################################################### | ||
| 126 | } | ||
| 127 | $length=1; | ||
| 128 | $string=$char; | ||
| 129 | $last_char=$current_char; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | ################################################### | ||
| 135 | if ($#lines > -1) { | ||
| 136 | if($last_char==1){$dtype="alpha";} | ||
| 137 | if($last_char==2){$dtype="num";} | ||
| 138 | if($last_char==3){$dtype="sep";} | ||
| 139 | if($last_char==4){$dtype="symb";} | ||
| 140 | $token_id_str = "token$token_id"; | ||
| 141 | while(($section_id < scalar(@Alvis::NLPPlatform::tab_end_sections_byaddr))) {# && ($Alvis::NLPPlatform::tab_end_sections_byaddr[$section_id] <= $offset + $length - 1)) { | ||
| 142 | push @Alvis::NLPPlatform::tab_end_sections_bytoken, $token_id_str; | ||
| 143 | $section_id++; | ||
| 144 | } | ||
| 145 | $doc_hash->{$token_id_str}={}; | ||
| 146 | $doc_hash->{$token_id_str}->{"datatype"}="token"; | ||
| 147 | $doc_hash->{$token_id_str}->{"type"}=$dtype; | ||
| 148 | $doc_hash->{$token_id_str}->{"id"}=$token_id_str; | ||
| 149 | $doc_hash->{$token_id_str}->{"from"}=$offset; | ||
| 150 | $doc_hash->{$token_id_str}->{"to"}=$offset+$length-1; | ||
| 151 | |||
| 152 | if($last_char==3){ | ||
| 153 | $string=~s/\n/\\n/go; | ||
| 154 | $string=~s/\r/\\r/go; | ||
| 155 | $string=~s/\t/\\t/go; | ||
| 156 | } | ||
| 157 | $doc_hash->{$token_id_str}->{"content"}=$string; | ||
| 158 | $Alvis::NLPPlatform::hash_tokens{$token_id_str}=$string; | ||
| 159 | $token_id++; | ||
| 160 | $offset+=$length; | ||
| 161 | ################################################### | ||
| 162 | |||
| 163 | $Alvis::NLPPlatform::Annotation::nb_max_tokens=$token_id-1; | ||
| 164 | } else { | ||
| 165 | $Alvis::NLPPlatform::Annotation::nb_max_tokens=0; | ||
| 166 | } | ||
| 167 | print STDERR "done - Found " . $Alvis::NLPPlatform::Annotation::nb_max_tokens ." tokens\n"; | ||
| 168 | |||
| 169 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Tokens : " . $Alvis::NLPPlatform::Annotation::nb_max_tokens; | ||
| 170 | |||
| 171 | # my $ii = 0; | ||
| 172 | # foreach my $sec_token (@Alvis::NLPPlatform::tab_end_sections_bytoken) { | ||
| 173 | # print STDERR "section $ii -> $sec_token\n"; | ||
| 174 | # $ii++; | ||
| 175 | # } | ||
| 176 | |||
| 177 | return($Alvis::NLPPlatform::Annotation::nb_max_tokens); | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | sub scan_ne | ||
| 182 | { | ||
| 183 | my ($class, $h_config, $doc_hash) = @_; | ||
| 184 | |||
| 185 | my $corpus; | ||
| 186 | my $token; | ||
| 187 | my $line; | ||
| 188 | my $id; | ||
| 189 | my $tok_ct; | ||
| 190 | |||
| 191 | my @tab_tokens; # experimental | ||
| 192 | my $t; # experimental | ||
| 193 | |||
| 194 | |||
| 195 | my $NE_type; | ||
| 196 | my $NE_start; | ||
| 197 | my $NE_end; | ||
| 198 | |||
| 199 | my $offset=0; | ||
| 200 | my $i; | ||
| 201 | my $en=0; | ||
| 202 | my $j; | ||
| 203 | my $start; | ||
| 204 | my $end; | ||
| 205 | my $ref_tab; | ||
| 206 | my $refid_n; | ||
| 207 | |||
| 208 | my $en_cont; | ||
| 209 | my $number_of_tokens; | ||
| 210 | my $last_en; | ||
| 211 | |||
| 212 | my $corpus_filename; | ||
| 213 | my $result_filename; | ||
| 214 | |||
| 215 | print STDERR " Named entites tagging... "; | ||
| 216 | |||
| 217 | $corpus=""; | ||
| 218 | |||
| 219 | foreach $token(Alvis::NLPPlatform::Annotation::sort(\%Alvis::NLPPlatform::hash_tokens)){ | ||
| 220 | $tok_ct=$Alvis::NLPPlatform::hash_tokens{$token}; # why not $token ? (TH) | ||
| 221 | Alvis::NLPPlatform::XMLEntities::decode($tok_ct); | ||
| 222 | |||
| 223 | # (TH) those replacements are required to workaround a bug in | ||
| 224 | # tagen (Named entity following a \n is not analyse - because | ||
| 225 | # n is concatenate with the next word) | ||
| 226 | |||
| 227 | $tok_ct=~s/\\n/\\n /go; | ||
| 228 | $tok_ct=~s/\\r/\\r /go; | ||
| 229 | $tok_ct=~s/\\t/\\t /go; | ||
| 230 | $corpus.=$tok_ct; | ||
| 231 | push @tab_tokens,$tok_ct; | ||
| 232 | } | ||
| 233 | |||
| 234 | $corpus_filename = $h_config->{'TMPFILE'} . ".corpus_en.txt"; | ||
| 235 | |||
| 236 | open CORPUS,">$corpus_filename"; | ||
| 237 | # binmode(CORPUS,":utf8"); | ||
| 238 | |||
| 239 | |||
| 240 | print CORPUS Encode::encode_utf8($corpus); | ||
| 241 | close CORPUS; | ||
| 242 | |||
| 243 | print STDERR "done\n"; | ||
| 244 | |||
| 245 | my $command_line; | ||
| 246 | if($Alvis::NLPPlatform::Annotation::ALVISLANGUAGE eq "FR"){ | ||
| 247 | $command_line = $h_config->{'NLP_tools'}->{'NETAG_FR'} . " $corpus_filename 2>> " . $Alvis::NLPPlatform::ALVISLOGFILE; | ||
| 248 | } else { | ||
| 249 | $command_line = $h_config->{'NLP_tools'}->{'NETAG_EN'} . " $corpus_filename 2>> " . $Alvis::NLPPlatform::ALVISLOGFILE; | ||
| 250 | } | ||
| 251 | # nice idea, though TagEN seems to return 0 anyhow... | ||
| 252 | #`$command_line` && print STDERR "FAILED TO EXECUTE \"$command_line\": &!\n"; | ||
| 253 | `$command_line`; | ||
| 254 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $corpus_filename; | ||
| 255 | @Alvis::NLPPlatform::en_start=(); | ||
| 256 | @Alvis::NLPPlatform::en_end=(); | ||
| 257 | @Alvis::NLPPlatform::en_type=(); | ||
| 258 | |||
| 259 | $result_filename = $h_config->{'TMPFILE'} . ".corpus_en.tag.txt"; | ||
| 260 | |||
| 261 | open REN,"<$result_filename" or warn "Can't open the file $result_filename"; | ||
| 262 | binmode REN; | ||
| 263 | while($line=<REN>){ | ||
| 264 | ($NE_type, $NE_start, $NE_end) = split /\t/, $line; | ||
| 265 | # $line=~m/(.+)\s+([0-9]+)\s+([0-9]+)/; | ||
| 266 | # $NE_type = $1; | ||
| 267 | # $NE_start = $2; | ||
| 268 | # $NE_end = $3; | ||
| 269 | push @Alvis::NLPPlatform::en_type,$NE_type; | ||
| 270 | if ((exists($h_config->{'XML_INPUT'}->{"PRESERVEWHITESPACE"})) && ($h_config->{'XML_INPUT'}->{"PRESERVEWHITESPACE"})) { | ||
| 271 | push @Alvis::NLPPlatform::en_start,($NE_start-1); | ||
| 272 | push @Alvis::NLPPlatform::en_end,($NE_end-1); | ||
| 273 | } else { | ||
| 274 | push @Alvis::NLPPlatform::en_start,$NE_start; | ||
| 275 | push @Alvis::NLPPlatform::en_end,$NE_end; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | close REN; | ||
| 279 | |||
| 280 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $result_filename; | ||
| 281 | |||
| 282 | # print STDERR scalar(@Alvis::NLPPlatform::en_type) . " to find\n"; | ||
| 283 | |||
| 284 | print STDERR " Matching EN with tokens... "; | ||
| 285 | |||
| 286 | # scan tokens and match with NE | ||
| 287 | |||
| 288 | @Alvis::NLPPlatform::en_tokens_start=(); | ||
| 289 | @Alvis::NLPPlatform::en_tokens_end=(); | ||
| 290 | %Alvis::NLPPlatform::en_tokens_hash=(); | ||
| 291 | $number_of_tokens=scalar @tab_tokens; | ||
| 292 | |||
| 293 | $en=$Alvis::NLPPlatform::last_semantic_unit+1; | ||
| 294 | $last_en=0; | ||
| 295 | |||
| 296 | my $en_str = ""; | ||
| 297 | for($t=0;$t<$number_of_tokens;$t++){ | ||
| 298 | print STDERR "\r Matching EN with tokens... ".($t+1)."/".$number_of_tokens." "; | ||
| 299 | for($i=$last_en;$i<scalar @Alvis::NLPPlatform::en_start;$i++){ | ||
| 300 | # print STDERR "\ti = $i :: last_en = $last_en\n"; | ||
| 301 | if($Alvis::NLPPlatform::en_start[$i]==$offset){ | ||
| 302 | # print STDERR "Found\n"; | ||
| 303 | $last_en=$i; | ||
| 304 | $Alvis::NLPPlatform::en_tokens_start[$en]="token".($t+1); | ||
| 305 | $Alvis::NLPPlatform::en_tokens_hash{($t+1)}=$en; | ||
| 306 | $start=$t+1; | ||
| 307 | while($Alvis::NLPPlatform::en_end[$i]>$offset-1){ | ||
| 308 | $Alvis::NLPPlatform::en_tokens_end[$en]="token".($t+1); | ||
| 309 | $end=$t+1; | ||
| 310 | $offset+=length($tab_tokens[$t]); | ||
| 311 | $t++; | ||
| 312 | } | ||
| 313 | $en_str = "semantic_unit$en"; | ||
| 314 | |||
| 315 | $doc_hash->{$en_str}={}; | ||
| 316 | $doc_hash->{$en_str}->{"datatype"}="semantic_unit"; | ||
| 317 | $doc_hash->{$en_str}->{"named_entity"}={}; | ||
| 318 | $doc_hash->{$en_str}->{"named_entity"}->{"datatype"}="named_entity"; | ||
| 319 | $doc_hash->{$en_str}->{"named_entity"}->{"named_entity_type"}=$Alvis::NLPPlatform::en_type[$i]; | ||
| 320 | $doc_hash->{$en_str}->{"named_entity"}->{"id"}="named_entity$en"; | ||
| 321 | |||
| 322 | $ref_tab=$doc_hash->{$en_str}->{"named_entity"}->{"list_refid_token"}={}; | ||
| 323 | $ref_tab->{'datatype'}="list_refid_token"; | ||
| 324 | $en_cont=""; | ||
| 325 | $refid_n=1; | ||
| 326 | my @tab_tokens_en; | ||
| 327 | $ref_tab->{"refid_token"}=\@tab_tokens_en; | ||
| 328 | for($j=$start;$j<=$end;$j++){ | ||
| 329 | push @tab_tokens_en, "token$j"; | ||
| 330 | $refid_n++; | ||
| 331 | $en_cont.=$Alvis::NLPPlatform::hash_tokens{"token$j"}; | ||
| 332 | } | ||
| 333 | $doc_hash->{$en_str}->{"named_entity"}->{"form"}=$en_cont; | ||
| 334 | |||
| 335 | $Alvis::NLPPlatform::hash_named_entities{$en_str}=$en_cont; | ||
| 336 | |||
| 337 | $en++; | ||
| 338 | last; # go out the Named Entity hash table scan | ||
| 339 | } | ||
| 340 | } | ||
| 341 | $offset+=length($tab_tokens[$t]); | ||
| 342 | } | ||
| 343 | $Alvis::NLPPlatform::last_semantic_unit=$en ; | ||
| 344 | print STDERR "done - Found ". ($Alvis::NLPPlatform::last_semantic_unit - 1) ." named entities\n"; | ||
| 345 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Named Entities : " . ($Alvis::NLPPlatform::last_semantic_unit - 1); | ||
| 346 | } | ||
| 347 | |||
| 348 | |||
| 349 | sub word_segmentation | ||
| 350 | { | ||
| 351 | my ($class, $h_config, $doc_hash) = @_; | ||
| 352 | my $token; | ||
| 353 | my $id; | ||
| 354 | my $nb_doc; | ||
| 355 | my $command_line; | ||
| 356 | |||
| 357 | my $proposedword; | ||
| 358 | my $current_word = ""; | ||
| 359 | my $token_id; | ||
| 360 | my $word_id; | ||
| 361 | my $ref_tab; | ||
| 362 | my $elision; | ||
| 363 | my $i; | ||
| 364 | |||
| 365 | my $is_en; | ||
| 366 | my $en_id; | ||
| 367 | my $token_end; | ||
| 368 | my $token_start; | ||
| 369 | my $append; | ||
| 370 | my $refid_n; | ||
| 371 | |||
| 372 | my $token_tmp; | ||
| 373 | |||
| 374 | my $corpus_filename; | ||
| 375 | my $result_filename; | ||
| 376 | |||
| 377 | my $token_id_str; | ||
| 378 | my $word_id_str; | ||
| 379 | |||
| 380 | #### | ||
| 381 | print STDERR " Word segmentation... "; | ||
| 382 | my $content; | ||
| 383 | # open CORPUS,">:utf8",$h_config->{'TMPFILE'} . ".corpus.tmp"; | ||
| 384 | |||
| 385 | $corpus_filename = $h_config->{'TMPFILE'} . ".corpus_word.tmp"; | ||
| 386 | $result_filename = $h_config->{'TMPFILE'} . ".words.tmp"; | ||
| 387 | |||
| 388 | open CORPUS,">$corpus_filename"; | ||
| 389 | # binmode(CORPUS); | ||
| 390 | # binmode(CORPUS, ":utf8"); | ||
| 391 | foreach $token(Alvis::NLPPlatform::Annotation::sort(\%Alvis::NLPPlatform::hash_tokens)){ | ||
| 392 | $content=$Alvis::NLPPlatform::hash_tokens{$token}; | ||
| 393 | $content=~s/\\n/\n/og; | ||
| 394 | $content=~s/\\t/\t/og; | ||
| 395 | $content=~s/\\r/\r/og; | ||
| 396 | #Encode::decode_utf8("Å“") | ||
| 397 | # $content =~ s/\x{65}/oe/g; | ||
| 398 | |||
| 399 | Alvis::NLPPlatform::XMLEntities::decode($content); | ||
| 400 | # Encode::from_to($content, "utf8", "iso-8859-1"); | ||
| 401 | print CORPUS Encode::encode("iso-8859-1", $content, Encode::FB_DEFAULT); | ||
| 402 | # print CORPUS $content; | ||
| 403 | } | ||
| 404 | close CORPUS; | ||
| 405 | |||
| 406 | if($Alvis::NLPPlatform::Annotation::ALVISLANGUAGE eq "FR"){ | ||
| 407 | $command_line = $h_config->{"NLP_tools"}->{'WORDSEG_FR'} . " < $corpus_filename > $result_filename 2>> " . $Alvis::NLPPlatform::ALVISLOGFILE; | ||
| 408 | }else{ | ||
| 409 | $command_line = $h_config->{"NLP_tools"}->{'WORDSEG_EN'} . " < $corpus_filename > $result_filename 2>> ". $Alvis::NLPPlatform::ALVISLOGFILE; | ||
| 410 | } | ||
| 411 | |||
| 412 | `$command_line`; | ||
| 413 | |||
| 414 | open(MOTS, $result_filename) or warn "Can't open the file $result_filename";; | ||
| 415 | # binmode(MOTS,":utf8"); | ||
| 416 | binmode(MOTS); | ||
| 417 | |||
| 418 | $token_id=1; | ||
| 419 | $word_id=1; | ||
| 420 | |||
| 421 | $token_id_str = "token$token_id"; | ||
| 422 | while($proposedword=<MOTS>) | ||
| 423 | { | ||
| 424 | # $proposedword = Encode::encode_utf8($proposedword); | ||
| 425 | $word_id_str = "word$word_id"; | ||
| 426 | # if ($proposedword !~ /^[\s ]*\n$/o) { | ||
| 427 | if ($proposedword !~ /^[\s\x{A0}]*\n$/o) { | ||
| 428 | chomp $proposedword; | ||
| 429 | |||
| 430 | # print STDERR $proposedword; | ||
| 431 | $current_word=""; | ||
| 432 | $doc_hash->{$word_id_str}={}; | ||
| 433 | $doc_hash->{$word_id_str}->{'id'}=$word_id_str; | ||
| 434 | $doc_hash->{$word_id_str}->{'datatype'}='word'; | ||
| 435 | $ref_tab=$doc_hash->{$word_id_str}->{'list_refid_token'}={}; | ||
| 436 | $ref_tab->{'datatype'}="list_refid_token"; | ||
| 437 | my @tab_tokens; | ||
| 438 | $refid_n=1; | ||
| 439 | $ref_tab->{"refid_token"}=\@tab_tokens; | ||
| 440 | |||
| 441 | $is_en=0; | ||
| 442 | while(length($current_word)<length($proposedword)){ | ||
| 443 | if($token_id>$Alvis::NLPPlatform::Annotation::nb_max_tokens){ | ||
| 444 | $Alvis::NLPPlatform::dont_annotate=1; | ||
| 445 | return; | ||
| 446 | } | ||
| 447 | if($doc_hash->{$token_id_str}->{'type'} ne "sep"){ | ||
| 448 | if(exists $Alvis::NLPPlatform::en_tokens_hash{$token_id}){ | ||
| 449 | $en_id=$Alvis::NLPPlatform::en_tokens_hash{$token_id}; | ||
| 450 | $is_en=1; | ||
| 451 | } | ||
| 452 | |||
| 453 | $token_tmp=$Alvis::NLPPlatform::hash_tokens{$token_id_str}; | ||
| 454 | ################################ | ||
| 455 | $token_tmp=~s/\\n/\n/og; | ||
| 456 | $token_tmp=~s/\\t/\t/og; | ||
| 457 | $token_tmp=~s/\\r/\r/og; | ||
| 458 | Alvis::NLPPlatform::XMLEntities::decode($token_tmp); | ||
| 459 | ################################ | ||
| 460 | |||
| 461 | $token_tmp=~s/\s+/ /og; | ||
| 462 | $current_word=$current_word.$token_tmp; | ||
| 463 | push @tab_tokens, $token_id_str; | ||
| 464 | if($refid_n==1){ | ||
| 465 | $Alvis::NLPPlatform::word_start[$word_id]=$token_id; | ||
| 466 | } | ||
| 467 | $Alvis::NLPPlatform::word_end[$word_id]=$token_id; | ||
| 468 | $refid_n++; | ||
| 469 | } | ||
| 470 | $token_id++; | ||
| 471 | $token_id_str = "token$token_id"; | ||
| 472 | } | ||
| 473 | #### is the rebuilt word a named entity ? is it fully built | ||
| 474 | my $append; | ||
| 475 | if($is_en){ | ||
| 476 | $Alvis::NLPPlatform::en_tokens_start[$en_id] =~ m/^token([0-9]+)/io; | ||
| 477 | $token_start=$1; | ||
| 478 | |||
| 479 | $Alvis::NLPPlatform::en_tokens_end[$en_id] =~ m/^token([0-9]+)/io; | ||
| 480 | $token_end=$1; | ||
| 481 | |||
| 482 | while($token_end>($token_id-1)){ | ||
| 483 | $token_tmp=$Alvis::NLPPlatform::hash_tokens{$token_id_str}; | ||
| 484 | ################################ | ||
| 485 | $token_tmp=~s/\\n/\n/og; | ||
| 486 | $token_tmp=~s/\\t/\t/og; | ||
| 487 | $token_tmp=~s/\\r/\r/og; | ||
| 488 | Alvis::NLPPlatform::XMLEntities::decode($token_tmp); | ||
| 489 | ################################ | ||
| 490 | $token_tmp=~s/\s+/ /og; | ||
| 491 | $current_word=$current_word.$token_tmp; | ||
| 492 | |||
| 493 | ######################################################## | ||
| 494 | # TO BE CHECK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! (still necessary ?) | ||
| 495 | if(length($current_word)>length($proposedword)){ | ||
| 496 | $append=<MOTS>; | ||
| 497 | chomp $append; | ||
| 498 | if($doc_hash->{$token_id_str}->{'type'} eq "sep"){ | ||
| 499 | $append=" ".$append; | ||
| 500 | } | ||
| 501 | $proposedword.=$append; | ||
| 502 | } | ||
| 503 | ######################################################## | ||
| 504 | |||
| 505 | push @tab_tokens, $token_id_str; | ||
| 506 | $Alvis::NLPPlatform::word_end[$word_id]=$token_id; # Added by thierry (10/02/2006) | ||
| 507 | $refid_n++; | ||
| 508 | $token_id++; | ||
| 509 | $token_id_str = "token$token_id"; | ||
| 510 | } | ||
| 511 | ######################################################################## | ||
| 512 | # Chech if the rebuilt word is not too short | ||
| 513 | while(length($current_word)<length($proposedword)){ | ||
| 514 | if($doc_hash->{$token_id_str}->{'type'} ne "sep"){ | ||
| 515 | $token_tmp=$Alvis::NLPPlatform::hash_tokens{$token_id_str}; | ||
| 516 | ################################ | ||
| 517 | $token_tmp=~s/\\n/\n/go; | ||
| 518 | $token_tmp=~s/\\t/\t/go; | ||
| 519 | $token_tmp=~s/\\r/\r/go; | ||
| 520 | Alvis::NLPPlatform::XMLEntities::decode($token_tmp); | ||
| 521 | ################################ | ||
| 522 | $token_tmp=~s/\s+/ /og; | ||
| 523 | $current_word=$current_word.$token_tmp; | ||
| 524 | push @tab_tokens, $token_id_str; | ||
| 525 | $Alvis::NLPPlatform::word_end[$word_id]=$token_id; | ||
| 526 | $refid_n++; | ||
| 527 | } | ||
| 528 | $token_id++; | ||
| 529 | $token_id_str = "token$token_id"; | ||
| 530 | } | ||
| 531 | ########################################################################"" | ||
| 532 | } | ||
| 533 | |||
| 534 | #### the rebuilt word is too long... elision? | ||
| 535 | my $mempos; | ||
| 536 | if(length($current_word)>length($proposedword)){ | ||
| 537 | # read the next word | ||
| 538 | $mempos=tell(MOTS); | ||
| 539 | $elision=<MOTS>; | ||
| 540 | chomp $elision; | ||
| 541 | if($elision=~/(\'s|\'d|\'m|\'ll|\'re|\'ve|\'t)/io){ | ||
| 542 | # Elision (english) | ||
| 543 | # Solution: going on the loop | ||
| 544 | $proposedword=$proposedword.$elision; | ||
| 545 | }else{ | ||
| 546 | # not a elision: going back (backtracking) | ||
| 547 | seek(MOTS,$mempos,0); | ||
| 548 | } | ||
| 549 | } | ||
| 550 | #### | ||
| 551 | |||
| 552 | #### the rebuilt word is too short... it is not an elision | ||
| 553 | if (length($current_word)<length($proposedword)){ | ||
| 554 | if(index($proposedword,$current_word)==0){ | ||
| 555 | # Tokens are missing | ||
| 556 | while(length($current_word)<length($proposedword)){ | ||
| 557 | if($doc_hash->{$token_id_str}->{'type'} ne "sep"){ | ||
| 558 | $token_tmp=$Alvis::NLPPlatform::hash_tokens{$token_id_str}; | ||
| 559 | ################################ | ||
| 560 | $token_tmp=~s/\\n/\n/og; | ||
| 561 | $token_tmp=~s/\\t/\t/og; | ||
| 562 | $token_tmp=~s/\\r/\r/og; | ||
| 563 | Alvis::NLPPlatform::XMLEntities::decode($token_tmp); | ||
| 564 | ################################ | ||
| 565 | $token_tmp=~s/\s+/ /og; | ||
| 566 | $current_word=$current_word.$token_tmp; | ||
| 567 | push @tab_tokens, $token_id_str; | ||
| 568 | $Alvis::NLPPlatform::word_end[$word_id]=$token_id; | ||
| 569 | $refid_n++; | ||
| 570 | } | ||
| 571 | $token_id++; | ||
| 572 | $token_id_str = "token$token_id"; | ||
| 573 | } | ||
| 574 | } | ||
| 575 | } | ||
| 576 | |||
| 577 | $doc_hash->{$word_id_str}->{'form'}="$current_word"; | ||
| 578 | |||
| 579 | if(length($current_word)!=length($proposedword)){ | ||
| 580 | print STDERR "**** Alignment error between '$current_word'(re-built word) and '$proposedword'(proposed by segmenter) ****\n"; | ||
| 581 | print STDERR "Length($current_word)=".length($current_word)."\n"; | ||
| 582 | print STDERR "Length($proposedword)=".length($proposedword)."\n"; | ||
| 583 | print STDERR "**** PROCESSING ABORTED ****\n"; | ||
| 584 | push @Alvis::NLPPlatform::tab_errors,"Word segmentation: alignment error\n"; | ||
| 585 | push @Alvis::NLPPlatform::tab_errors,"Re-built '$current_word' not aligned with '$proposedword' proposed by used segmenter\n"; | ||
| 586 | push @Alvis::NLPPlatform::tab_errors,"Respective lengths:".length($current_word)." and ".length($proposedword)."\n"; | ||
| 587 | push @Alvis::NLPPlatform::tab_errors,"* Processing aborted *\n"; | ||
| 588 | push @Alvis::NLPPlatform::tab_errors,"\n"; | ||
| 589 | last; | ||
| 590 | } | ||
| 591 | |||
| 592 | ##### | ||
| 593 | ## Remove punctuation | ||
| 594 | if(($doc_hash->{"token".($token_id-1)}->{'type'} eq "symb") && (length($current_word)==1)){ | ||
| 595 | # print STDERR $Alvis::NLPPlatform::hash_tokens{"token".($token_id-1)} . "-\n"; | ||
| 596 | # print STDERR index(".;:!?",$Alvis::NLPPlatform::hash_tokens{"token".($token_id-1)}) . "\n"; | ||
| 597 | ## add ... ? | ||
| 598 | if(index(".;:!?",$Alvis::NLPPlatform::hash_tokens{"token".($token_id-1)})>-1){ | ||
| 599 | # This obviously marks the end of a sentence; | ||
| 600 | # store the "word" ID for sentence segmentation | ||
| 601 | # print STDERR "We go there\n"; | ||
| 602 | $Alvis::NLPPlatform::last_words{"word".($word_id-1)}=($token_id-1); # to be checked carefully - added the 11/05/2007 | ||
| 603 | } | ||
| 604 | delete $doc_hash->{$word_id_str}; | ||
| 605 | $word_id--; | ||
| 606 | } | ||
| 607 | $word_id++; | ||
| 608 | ##### | ||
| 609 | } | ||
| 610 | } | ||
| 611 | close MOTS; | ||
| 612 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $corpus_filename; | ||
| 613 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $result_filename; | ||
| 614 | |||
| 615 | my $word; | ||
| 616 | my $word_punct=1; | ||
| 617 | # keys NEED to be sorted here, so we can insert punctuation in the 2ndary word hash table | ||
| 618 | foreach $word (Alvis::NLPPlatform::Annotation::sort($doc_hash)){ | ||
| 619 | if($word=~/^(word[0-9]+)/o){ | ||
| 620 | $id=$1; | ||
| 621 | $Alvis::NLPPlatform::hash_words{$id}=$doc_hash->{$id}->{'form'}; | ||
| 622 | $Alvis::NLPPlatform::hash_words_punct{"word".$word_punct}=$Alvis::NLPPlatform::hash_words{$id}; | ||
| 623 | # $Alvis::NLPPlatform::hash_words_punct2words{"word".$word_punct}=word; | ||
| 624 | $word_punct++; | ||
| 625 | if(exists($Alvis::NLPPlatform::last_words{$id})){ | ||
| 626 | |||
| 627 | $Alvis::NLPPlatform::hash_words_punct{"word".$word_punct}=$Alvis::NLPPlatform::hash_tokens{"token". $Alvis::NLPPlatform::last_words{$id}}; | ||
| 628 | $word_punct++; | ||
| 629 | } | ||
| 630 | } | ||
| 631 | } | ||
| 632 | $Alvis::NLPPlatform::last_words{"word" . ($word_id - 1)} = $token_id; # $Alvis::NLPPlatform::Annotation::nb_max_tokens | ||
| 633 | $Alvis::NLPPlatform::number_of_words=$word_id-1; | ||
| 634 | print STDERR "done - Found ".$Alvis::NLPPlatform::number_of_words ." words\n"; | ||
| 635 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Words: $Alvis::NLPPlatform::number_of_words"; | ||
| 636 | } | ||
| 637 | |||
| 638 | |||
| 639 | sub sentence_segmentation | ||
| 640 | { | ||
| 641 | my ($class, $h_config, $doc_hash) = @_; | ||
| 642 | my $word; | ||
| 643 | my $sentence_id=1; | ||
| 644 | my $min; | ||
| 645 | my $max; | ||
| 646 | |||
| 647 | my $btw_start; | ||
| 648 | my $btw_end; | ||
| 649 | my $i; | ||
| 650 | my $token; | ||
| 651 | my $sentence; | ||
| 652 | my $sentence_cont=""; | ||
| 653 | |||
| 654 | my $current_section_id = 0; | ||
| 655 | |||
| 656 | my $start_token=$Alvis::NLPPlatform::word_start[1]; | ||
| 657 | |||
| 658 | my $word_index; | ||
| 659 | |||
| 660 | my $next_wordtoken; | ||
| 661 | my $current_wordtoken; | ||
| 662 | |||
| 663 | my $sentence_id_str; | ||
| 664 | |||
| 665 | print STDERR " Sentence segmentation..."; | ||
| 666 | |||
| 667 | foreach $word (Alvis::NLPPlatform::Annotation::sort(\%Alvis::NLPPlatform::hash_words)){ | ||
| 668 | |||
| 669 | # get starting and ending tokens for the word | ||
| 670 | $min=$Alvis::NLPPlatform::word_start[Alvis::NLPPlatform::Annotation::read_key_id($word)]; | ||
| 671 | $max=$Alvis::NLPPlatform::word_end[Alvis::NLPPlatform::Annotation::read_key_id($word)]; | ||
| 672 | for($i=$min;$i<=$max;$i++){ | ||
| 673 | $token = $Alvis::NLPPlatform::hash_tokens{"token$i"}; | ||
| 674 | $sentence_cont .= $token; | ||
| 675 | } | ||
| 676 | |||
| 677 | # insert tokens between the current word and the next (spaces, punctuation, ...) | ||
| 678 | $btw_start=$max+1; | ||
| 679 | if(Alvis::NLPPlatform::Annotation::read_key_id($word)+1 > $Alvis::NLPPlatform::number_of_words){ | ||
| 680 | # We've reached the end of the document | ||
| 681 | $btw_end=$Alvis::NLPPlatform::Annotation::nb_max_tokens; | ||
| 682 | }else{ | ||
| 683 | $btw_end=$Alvis::NLPPlatform::word_start[Alvis::NLPPlatform::Annotation::read_key_id($word)+1]-1; | ||
| 684 | |||
| 685 | # we check where is the punctuation mark between the kast | ||
| 686 | # word of the current sentence and the first word of the | ||
| 687 | # next sentence. | ||
| 688 | |||
| 689 | $next_wordtoken = $Alvis::NLPPlatform::word_start[Alvis::NLPPlatform::Annotation::read_key_id($word)+1]; | ||
| 690 | $current_wordtoken = $Alvis::NLPPlatform::word_end[Alvis::NLPPlatform::Annotation::read_key_id($word)]; | ||
| 691 | |||
| 692 | |||
| 693 | $btw_end=$current_wordtoken + 1; | ||
| 694 | ## add "..." ? | ||
| 695 | |||
| 696 | while (($btw_end < $next_wordtoken) && (index(".;:!?",$Alvis::NLPPlatform::hash_tokens{"token$btw_end"})==-1) && | ||
| 697 | (($Alvis::NLPPlatform::tab_end_sections_bytoken[$current_section_id] ne "token$btw_end"))){ | ||
| 698 | $btw_end++; | ||
| 699 | } | ||
| 700 | } | ||
| 701 | |||
| 702 | # print STDERR "==\n"; | ||
| 703 | # print STDERR $Alvis::NLPPlatform::hash_tokens{"token".$btw_start} . "\n"; | ||
| 704 | # print STDERR $Alvis::NLPPlatform::hash_tokens{"token".$btw_end} . "\n"; | ||
| 705 | |||
| 706 | # we add tokens between last word and boundary (next word or end of section) | ||
| 707 | for($i=$btw_start;$i<$btw_end;$i++){ | ||
| 708 | $token = $Alvis::NLPPlatform::hash_tokens{"token".$i}; | ||
| 709 | $sentence_cont .= $token; | ||
| 710 | } | ||
| 711 | # if the current word is the last word of the sentence, | ||
| 712 | # then create an entry in the hash | ||
| 713 | |||
| 714 | # print STDERR "$sentence_cont\n"; | ||
| 715 | |||
| 716 | if ( ($Alvis::NLPPlatform::tab_end_sections_bytoken[$current_section_id] eq "token$btw_end") | ||
| 717 | || (exists($Alvis::NLPPlatform::last_words{$word})) | ||
| 718 | || (Alvis::NLPPlatform::Annotation::read_key_id($Alvis::NLPPlatform::tab_end_sections_bytoken[$current_section_id]) <= $Alvis::NLPPlatform::word_start[Alvis::NLPPlatform::Annotation::read_key_id($word)+1])) { | ||
| 719 | $sentence_id_str = "sentence$sentence_id"; | ||
| 720 | $doc_hash->{$sentence_id_str}={}; | ||
| 721 | $doc_hash->{$sentence_id_str}->{'id'}=$sentence_id_str; | ||
| 722 | $doc_hash->{$sentence_id_str}->{'datatype'}='sentence'; | ||
| 723 | $doc_hash->{$sentence_id_str}->{'refid_start_token'}="token$start_token"; | ||
| 724 | $doc_hash->{$sentence_id_str}->{'refid_end_token'}="token$btw_end"; | ||
| 725 | $sentence_cont .= $Alvis::NLPPlatform::hash_tokens{"token$btw_end"}; | ||
| 726 | $sentence_cont =~s /\\n/\n/go; | ||
| 727 | $sentence_cont =~s /\\r/\r/go; | ||
| 728 | $sentence_cont =~s /\\t/\t/go; | ||
| 729 | $doc_hash->{"sentence$sentence_id"}->{'form'}="$sentence_cont" ;# . $Alvis::NLPPlatform::hash_tokens{"token".$btw_end}; # ($current_section_id)"; | ||
| 730 | |||
| 731 | $sentence_id++; | ||
| 732 | $sentence_cont=""; | ||
| 733 | |||
| 734 | # determining the beginning the next sentence | ||
| 735 | if($btw_end != $Alvis::NLPPlatform::Annotation::nb_max_tokens){ | ||
| 736 | if (Alvis::NLPPlatform::Annotation::read_key_id($Alvis::NLPPlatform::tab_end_sections_bytoken[$current_section_id]) >= | ||
| 737 | $Alvis::NLPPlatform::word_start[Alvis::NLPPlatform::Annotation::read_key_id($word)+1]) { | ||
| 738 | ### To check some token can start the sentence ! | ||
| 739 | $start_token=$Alvis::NLPPlatform::word_start[Alvis::NLPPlatform::Annotation::read_key_id($word)+1]; | ||
| 740 | } else { | ||
| 741 | # "while" because we can have sections without sentences ! | ||
| 742 | while (Alvis::NLPPlatform::Annotation::read_key_id($Alvis::NLPPlatform::tab_end_sections_bytoken[$current_section_id]) < | ||
| 743 | $Alvis::NLPPlatform::word_start[Alvis::NLPPlatform::Annotation::read_key_id($word)+1]) { | ||
| 744 | $start_token=Alvis::NLPPlatform::Annotation::read_key_id($Alvis::NLPPlatform::tab_end_sections_bytoken[$current_section_id]) + 1; | ||
| 745 | $current_section_id++; | ||
| 746 | # print STDERR "==> $current_section_id ($word / $start_token / $btw_end)\n"; | ||
| 747 | } | ||
| 748 | } | ||
| 749 | # making the beginning of the new sentence | ||
| 750 | $i=$btw_end + 1; | ||
| 751 | while ($doc_hash->{"token".$i}->{'type'} eq "sep") {$i++;} | ||
| 752 | |||
| 753 | for(;$i < $next_wordtoken;$i++){ | ||
| 754 | $token = $Alvis::NLPPlatform::hash_tokens{"token".$i}; | ||
| 755 | $sentence_cont .= $token; | ||
| 756 | } | ||
| 757 | |||
| 758 | } | ||
| 759 | |||
| 760 | } | ||
| 761 | } | ||
| 762 | |||
| 763 | # create an entry for the last sentence, only if it isn't empty | ||
| 764 | # (when the last sentence does not contain any ending punctuation) | ||
| 765 | |||
| 766 | if((defined $btw_end) && ($btw_end!=$Alvis::NLPPlatform::Annotation::nb_max_tokens)) { | ||
| 767 | $sentence_id_str = "sentence$sentence_id"; | ||
| 768 | $doc_hash->{$sentence_id_str}={}; | ||
| 769 | $doc_hash->{$sentence_id_str}->{'id'}=$sentence_id_str; | ||
| 770 | $doc_hash->{$sentence_id_str}->{'datatype'}='sentence'; | ||
| 771 | $doc_hash->{$sentence_id_str}->{'refid_start_token'}="token$start_token"; | ||
| 772 | $doc_hash->{$sentence_id_str}->{'refid_end_token'}="token$btw_end"; | ||
| 773 | $sentence_cont =~s /\\n/\n/go; | ||
| 774 | $sentence_cont =~s /\\r/\r/go; | ||
| 775 | $sentence_cont =~s /\\t/\t/go; | ||
| 776 | $doc_hash->{$sentence_id_str}->{'form'}=$sentence_cont; | ||
| 777 | $sentence_cont=""; | ||
| 778 | } | ||
| 779 | |||
| 780 | foreach $sentence(keys %$doc_hash){ | ||
| 781 | if($sentence=~/^(sentence[0-9]+)/o){ | ||
| 782 | $Alvis::NLPPlatform::hash_sentences{$1}=$doc_hash->{$1}->{'form'}; | ||
| 783 | } | ||
| 784 | } | ||
| 785 | $Alvis::NLPPlatform::number_of_sentences=$sentence_id-1; | ||
| 786 | print STDERR "done - Found ".$Alvis::NLPPlatform::number_of_sentences." sentences\n"; | ||
| 787 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Sentences: $Alvis::NLPPlatform::number_of_sentences"; | ||
| 788 | } | ||
| 789 | |||
| 790 | |||
| 791 | |||
| 792 | sub pos_tag | ||
| 793 | { | ||
| 794 | my ($class, $h_config, $doc_hash) = @_; | ||
| 795 | my $word; | ||
| 796 | my $cont; | ||
| 797 | my $i; | ||
| 798 | my $line; | ||
| 799 | my $word_id = 0; | ||
| 800 | my $tag; | ||
| 801 | my $lemma; | ||
| 802 | my %hash_validtags_en; | ||
| 803 | my %hash_validtags_fr; | ||
| 804 | my $inflected; | ||
| 805 | |||
| 806 | my $corpus_filename; | ||
| 807 | my $result_filename; | ||
| 808 | |||
| 809 | my $morphosyntactic_features_id; | ||
| 810 | my $lemma_id; | ||
| 811 | my $word_id_str; | ||
| 812 | my $word_punct_id_str; | ||
| 813 | |||
| 814 | my @words; | ||
| 815 | |||
| 816 | $corpus_filename = $h_config->{'TMPFILE'} . ".corpus_pos.tmp"; | ||
| 817 | $result_filename = $h_config->{'TMPFILE'} . ".tags.tmp"; | ||
| 818 | |||
| 819 | print STDERR " Part-Of-Speech tagging.."; | ||
| 820 | open CORPUS,">$corpus_filename"; | ||
| 821 | # binmode(CORPUS,":encoding(latin1)"); | ||
| 822 | # TH - 16/07/2007 - replacement of hash_words by hash_words_punct | ||
| 823 | |||
| 824 | my $fullcontent = ""; | ||
| 825 | foreach $word (Alvis::NLPPlatform::Annotation::sort(\%Alvis::NLPPlatform::hash_words_punct)){ | ||
| 826 | $cont=$Alvis::NLPPlatform::hash_words_punct{$word}; | ||
| 827 | $fullcontent .= Encode::encode("iso-8859-1", $cont, Encode::FB_DEFAULT); | ||
| 828 | $fullcontent .= "\n"; | ||
| 829 | # Encode::from_to($cont, "utf8", "iso-8859-1"); | ||
| 830 | # $fullcontent .= "$cont\n"; | ||
| 831 | } | ||
| 832 | print CORPUS $fullcontent; | ||
| 833 | close CORPUS; | ||
| 834 | |||
| 835 | my $command_line; | ||
| 836 | if($Alvis::NLPPlatform::Annotation::ALVISLANGUAGE eq "FR"){ | ||
| 837 | $command_line = $h_config->{'NLP_tools'}->{'POSTAG_FR'} . " < $corpus_filename > $result_filename 2>> " . $Alvis::NLPPlatform::ALVISLOGFILE; | ||
| 838 | }else{ | ||
| 839 | $command_line = $h_config->{'NLP_tools'}->{'POSTAG_EN'} . " < $corpus_filename > $result_filename 2>> " . $Alvis::NLPPlatform::ALVISLOGFILE; | ||
| 840 | } | ||
| 841 | `$command_line`; | ||
| 842 | |||
| 843 | open TAGS,"<$result_filename"; | ||
| 844 | binmode(TAGS); #, ":encoding(latin9)"); | ||
| 845 | $word_id=0; | ||
| 846 | |||
| 847 | my $decal = 0; | ||
| 848 | my $wordecal; | ||
| 849 | my $word_punct_id = 1; | ||
| 850 | $word_punct_id_str = "word$word_punct_id"; | ||
| 851 | |||
| 852 | while ($line = <TAGS>) { | ||
| 853 | # Read $Alvis::NLPPlatform::hash_words_punct{"word$word_punct"} | ||
| 854 | # Encode::from_to($line, "iso-8859-9", "utf8"); | ||
| 855 | $line = Encode::decode("latin9",$line); | ||
| 856 | chomp $line; | ||
| 857 | ($inflected, $tag, $lemma) = split /\t/, $line; | ||
| 858 | |||
| 859 | $word_id = $word_punct_id + $decal; | ||
| 860 | $word_id_str = "word$word_id"; | ||
| 861 | |||
| 862 | if ((!exists $Alvis::NLPPlatform::hash_words{$word_id_str}) || ($Alvis::NLPPlatform::hash_words_punct{$word_punct_id_str} ne $Alvis::NLPPlatform::hash_words{$word_id_str})) { | ||
| 863 | # it is not a word | ||
| 864 | # punctuation, delay incrementation of index "decal" | ||
| 865 | $decal--; | ||
| 866 | } else { | ||
| 867 | ####################################################### | ||
| 868 | # Correct outputs from treetagger | ||
| 869 | if (!defined $tag) { $tag = "NP"; $lemma = $inflected;} | ||
| 870 | |||
| 871 | if ((($Alvis::NLPPlatform::Annotation::ALVISLANGUAGE eq "FR") && (!exists($h_config->{"NLP_misc"}->{"POSTAG_LIST"}->{"FR"}->{$tag})))|| | ||
| 872 | (($Alvis::NLPPlatform::Annotation::ALVISLANGUAGE ne "FR") && (!exists($h_config->{"NLP_misc"}->{"POSTAG_LIST"}->{"EN"}->{$tag})))){ | ||
| 873 | if ($inflected ne $tag){ # ??? | ||
| 874 | $tag="NP"; | ||
| 875 | } | ||
| 876 | } | ||
| 877 | |||
| 878 | # in case of named entities, we remove '_' in lemma and inflected form | ||
| 879 | # and we force the POS tag to be NP | ||
| 880 | if ((index($lemma,"_")>-1)&&(index($inflected,"_")==-1)){ | ||
| 881 | $lemma=~s/\_/ /og; | ||
| 882 | $tag="NP"; | ||
| 883 | } | ||
| 884 | |||
| 885 | # in case of number, lemma is the same as the inflected form | ||
| 886 | if ($lemma eq '@card@'){ | ||
| 887 | $lemma=$inflected; | ||
| 888 | } | ||
| 889 | ####################################################### | ||
| 890 | |||
| 891 | # POS tag | ||
| 892 | $morphosyntactic_features_id = "morphosyntactic_features$word_id"; | ||
| 893 | $doc_hash->{$morphosyntactic_features_id}={}; | ||
| 894 | $doc_hash->{$morphosyntactic_features_id}->{'id'}=$morphosyntactic_features_id; | ||
| 895 | $doc_hash->{$morphosyntactic_features_id}->{'datatype'}="morphosyntactic_features"; | ||
| 896 | $doc_hash->{$morphosyntactic_features_id}->{'refid_word'}=$word_id_str; | ||
| 897 | $doc_hash->{$morphosyntactic_features_id}->{'syntactic_category'}="$tag"; | ||
| 898 | |||
| 899 | $Alvis::NLPPlatform::hash_postags{$word_id_str}=$tag; | ||
| 900 | |||
| 901 | # lemma | ||
| 902 | $lemma_id = "lemma$word_id"; | ||
| 903 | $doc_hash->{$lemma_id}={}; | ||
| 904 | $doc_hash->{$lemma_id}->{'id'}=$lemma_id; | ||
| 905 | $doc_hash->{$lemma_id}->{'datatype'}="lemma"; | ||
| 906 | $doc_hash->{$lemma_id}->{'refid_word'}=$word_id_str; | ||
| 907 | $doc_hash->{$lemma_id}->{'canonical_form'}="$lemma"; | ||
| 908 | |||
| 909 | |||
| 910 | $Alvis::NLPPlatform::hash_lemmas{$word_id_str}=$lemma; | ||
| 911 | } | ||
| 912 | $word_punct_id++; | ||
| 913 | $word_punct_id_str = "word$word_punct_id"; | ||
| 914 | } | ||
| 915 | close TAGS; | ||
| 916 | |||
| 917 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $corpus_filename; | ||
| 918 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $result_filename; | ||
| 919 | |||
| 920 | print STDERR "done - Found " . $word_id ." tags.\n"; | ||
| 921 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found POS Tags: " . $word_id ; | ||
| 922 | } | ||
| 923 | |||
| 924 | # sub pos_tag # WRAPPER FOR BRILL | ||
| 925 | # { | ||
| 926 | # my $word; | ||
| 927 | # my $cont; | ||
| 928 | |||
| 929 | # print STDERR " Part-Of-Speech tagging..."; | ||
| 930 | # open CORPUS,">$TMPFILE.corpus.tmp"; | ||
| 931 | # binmode(CORPUS,":utf8"); | ||
| 932 | # foreach $word(sort Alvis::NLPPlatform::Annotation::sort_keys keys %Alvis::NLPPlatform::hash_words){ | ||
| 933 | # $cont=$Alvis::NLPPlatform::hash_words{$word}; | ||
| 934 | # print CORPUS "$cont "; | ||
| 935 | # if($cont eq "."){ | ||
| 936 | # print CORPUS "\n"; | ||
| 937 | # } | ||
| 938 | # } | ||
| 939 | # close CORPUS; | ||
| 940 | # } | ||
| 941 | |||
| 942 | |||
| 943 | sub lemmatization | ||
| 944 | { | ||
| 945 | my ($class, $h_config, $doc_hash) = @_; | ||
| 946 | |||
| 947 | # done with the postagging | ||
| 948 | } | ||
| 949 | |||
| 950 | |||
| 951 | # TODO : Check that term tagging is only performed on english texts | ||
| 952 | |||
| 953 | sub term_tag | ||
| 954 | { | ||
| 955 | my ($class, $h_config, $doc_hash) = @_; | ||
| 956 | |||
| 957 | my $cont; | ||
| 958 | my $word; | ||
| 959 | my $sentence; | ||
| 960 | my $i; | ||
| 961 | my $s; | ||
| 962 | my $line; | ||
| 963 | my $tmp; | ||
| 964 | my %tabh_sent_terms; | ||
| 965 | my $key; | ||
| 966 | my $sent; | ||
| 967 | my $term_regex; | ||
| 968 | my $term; | ||
| 969 | my $phrase_idx=1; | ||
| 970 | my $canonical_form; | ||
| 971 | my %corpus; | ||
| 972 | my %lc_corpus; | ||
| 973 | my $sent_id; | ||
| 974 | my $command_line; | ||
| 975 | my %corpus_index; | ||
| 976 | my %idtrm_select; | ||
| 977 | my @tab_results; | ||
| 978 | my $semtag; | ||
| 979 | |||
| 980 | my $token_start; | ||
| 981 | my $token_end; | ||
| 982 | my $offset_start; | ||
| 983 | my $offset_end; | ||
| 984 | my $offset; | ||
| 985 | |||
| 986 | my $semantic_unit_id_str; | ||
| 987 | my $semantic_feature_id_str; | ||
| 988 | my $sf = 1; | ||
| 989 | |||
| 990 | my $token_term; | ||
| 991 | my $token_term_end; | ||
| 992 | my $j; | ||
| 993 | |||
| 994 | |||
| 995 | print STDERR " Term tagging... "; | ||
| 996 | |||
| 997 | $sent_id = 1; | ||
| 998 | foreach $sentence(Alvis::NLPPlatform::Annotation::sort(\%Alvis::NLPPlatform::hash_sentences)){ | ||
| 999 | $tmp = "$Alvis::NLPPlatform::hash_sentences{$sentence}\n"; | ||
| 1000 | $tmp=~s/\n/ /go; | ||
| 1001 | $tmp=~s/\r/ /go; | ||
| 1002 | $tmp=~s/\t/ /go; | ||
| 1003 | # $tmp=~s/\n/\\n/go; | ||
| 1004 | # $tmp=~s/\r/\\r/go; | ||
| 1005 | # $tmp=~s/\t/\\t/go; | ||
| 1006 | # print STDERR "$tmp\n"; | ||
| 1007 | $corpus{$sent_id} = $tmp; | ||
| 1008 | $lc_corpus{$sent_id} = lc($tmp); | ||
| 1009 | $sent_id++; | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | |||
| 1013 | |||
| 1014 | # Term list loading | ||
| 1015 | |||
| 1016 | if($Alvis::NLPPlatform::Annotation::ALVISLANGUAGE eq "FR"){ | ||
| 1017 | if (scalar(@term_list_FR) == 0) { | ||
| 1018 | Alvis::TermTagger::load_TermList($h_config->{'NLP_misc'}->{'TERM_LIST_FR'},\@term_list_FR); | ||
| 1019 | Alvis::TermTagger::get_Regex_TermList(\@term_list_FR, \@regex_term_list_FR); | ||
| 1020 | } | ||
| 1021 | Alvis::TermTagger::corpus_Indexing(\%lc_corpus, \%corpus_index); | ||
| 1022 | Alvis::TermTagger::term_Selection(\%corpus_index, \@term_list_FR, \%idtrm_select); | ||
| 1023 | Alvis::TermTagger::term_tagging_offset_tab(\@term_list_FR, \@regex_term_list_FR, \%idtrm_select, \%corpus, \%tabh_sent_terms); | ||
| 1024 | } else { | ||
| 1025 | if (scalar(@term_list_EN) == 0) { | ||
| 1026 | Alvis::TermTagger::load_TermList($h_config->{'NLP_misc'}->{'TERM_LIST_EN'},\@term_list_EN); | ||
| 1027 | Alvis::TermTagger::get_Regex_TermList(\@term_list_EN, \@regex_term_list_EN); | ||
| 1028 | } | ||
| 1029 | Alvis::TermTagger::corpus_Indexing(\%lc_corpus, \%corpus_index); | ||
| 1030 | Alvis::TermTagger::term_Selection(\%corpus_index, \@term_list_EN, \%idtrm_select); | ||
| 1031 | Alvis::TermTagger::term_tagging_offset_tab(\@term_list_EN, \@regex_term_list_EN, \%idtrm_select, \%corpus, \%tabh_sent_terms); | ||
| 1032 | } | ||
| 1033 | %lc_corpus = (); | ||
| 1034 | %corpus_index = (); | ||
| 1035 | %idtrm_select = (); | ||
| 1036 | %corpus = (); | ||
| 1037 | |||
| 1038 | # TODO : taking into account the case where terms appear at least twice in a sentence | ||
| 1039 | |||
| 1040 | $i=0; | ||
| 1041 | for $key (keys %tabh_sent_terms) { | ||
| 1042 | $sent = $tabh_sent_terms{$key}->[0]; | ||
| 1043 | $term = $tabh_sent_terms{$key}->[1]; | ||
| 1044 | $term_regex = $term; | ||
| 1045 | $term_regex =~ s/ /\[ \n\]+/go; | ||
| 1046 | # print STDERR "try to find $term in sentence$sent\n"; | ||
| 1047 | |||
| 1048 | |||
| 1049 | $canonical_form = $tabh_sent_terms{$key}->[2]; | ||
| 1050 | $semtag = $tabh_sent_terms{$key}->[3]; | ||
| 1051 | |||
| 1052 | # look for the term in the sentence, compute the reference to the words | ||
| 1053 | $token_term = -1; | ||
| 1054 | $offset = 0; | ||
| 1055 | while (($offset != -1)&&($token_term == -1)) { | ||
| 1056 | if ($Alvis::NLPPlatform::hash_sentences{"sentence$sent"} =~ /$term_regex/igc) { # replace regex by index/subtring ? | ||
| 1057 | $offset = length($`); | ||
| 1058 | } else { | ||
| 1059 | $offset = -1; | ||
| 1060 | } | ||
| 1061 | # print STDERR "Found (offset = $offset)\n"; | ||
| 1062 | if ($offset != -1) { | ||
| 1063 | $doc_hash->{"sentence$sent"}->{"refid_start_token"}=~m/token([0-9]+)/i; | ||
| 1064 | $token_start=$1; | ||
| 1065 | $doc_hash->{"sentence$sent"}->{"refid_end_token"}=~m/token([0-9]+)/i; | ||
| 1066 | $token_end=$1; | ||
| 1067 | $offset_start=$doc_hash->{"token$token_start"}->{"from"}; | ||
| 1068 | $offset_end=$doc_hash->{"token$token_end"}->{"to"}; | ||
| 1069 | |||
| 1070 | $offset+=$offset_start; | ||
| 1071 | |||
| 1072 | # print STDERR "Search token starting at $offset\n"; | ||
| 1073 | for($j=$token_start;$j<$token_end;$j++){ | ||
| 1074 | # print STDERR "Current offset : " . $doc_hash->{"token$j"}->{"from"} . "\n"; | ||
| 1075 | if($doc_hash->{"token$j"}->{"from"}==$offset){ | ||
| 1076 | $token_term=$j; | ||
| 1077 | last; | ||
| 1078 | } | ||
| 1079 | } | ||
| 1080 | # print STDERR "Token Term start at $token_term\n"; | ||
| 1081 | if ($token_term != -1) { | ||
| 1082 | $cont=""; | ||
| 1083 | my @tab_tokens; | ||
| 1084 | for($j=$token_term;length($cont)<length($term);$j++){ | ||
| 1085 | $cont.=$Alvis::NLPPlatform::hash_tokens{"token$j"}; | ||
| 1086 | push @tab_tokens, "token$j"; | ||
| 1087 | $cont =~ s/\\[nrt]/ /go; | ||
| 1088 | } | ||
| 1089 | # print STDERR "$cont\n"; | ||
| 1090 | if (length($cont) == length($term)) { | ||
| 1091 | $token_term_end=$j-1; | ||
| 1092 | $Alvis::NLPPlatform::hash_sentences{"sentence$sent"} =~ /^/g; | ||
| 1093 | |||
| 1094 | # Creation of a semantic unit | ||
| 1095 | $s=$Alvis::NLPPlatform::last_semantic_unit; | ||
| 1096 | $semantic_unit_id_str = "semantic_unit$s"; | ||
| 1097 | $doc_hash->{$semantic_unit_id_str}={}; | ||
| 1098 | $doc_hash->{$semantic_unit_id_str}->{"datatype"}="semantic_unit"; | ||
| 1099 | $doc_hash->{$semantic_unit_id_str}->{"term"}={}; | ||
| 1100 | $doc_hash->{$semantic_unit_id_str}->{"term"}->{"datatype"}="term"; | ||
| 1101 | $doc_hash->{$semantic_unit_id_str}->{"term"}->{"id"}="term" . (++$i); | ||
| 1102 | $doc_hash->{$semantic_unit_id_str}->{"term"}->{"form"}=$term; | ||
| 1103 | push @Alvis::NLPPlatform::found_terms,$term; | ||
| 1104 | push @Alvis::NLPPlatform::found_terms_smidx,$i; | ||
| 1105 | |||
| 1106 | if (defined($canonical_form)) { | ||
| 1107 | $doc_hash->{$semantic_unit_id_str}->{"term"}->{"canonical_form"}=$canonical_form; | ||
| 1108 | } | ||
| 1109 | if (defined($semtag)) { | ||
| 1110 | # print STDERR "Add $semtag for $term\n"; | ||
| 1111 | $sf=$Alvis::NLPPlatform::last_semantic_feature + 1; | ||
| 1112 | $semantic_feature_id_str = "semantic_features$sf"; | ||
| 1113 | $doc_hash->{$semantic_feature_id_str}={}; | ||
| 1114 | $doc_hash->{$semantic_feature_id_str}->{"datatype"}="semantic_features"; | ||
| 1115 | $doc_hash->{$semantic_feature_id_str}->{"id"}=$semantic_feature_id_str; #"term" . ($i-1); | ||
| 1116 | $doc_hash->{$semantic_feature_id_str}->{"refid_semantic_unit"}="term$i"; | ||
| 1117 | $doc_hash->{$semantic_feature_id_str}->{"semantic_category"}={}; | ||
| 1118 | $doc_hash->{$semantic_feature_id_str}->{"semantic_category"}->{"datatype"}="semantic_category"; | ||
| 1119 | $doc_hash->{$semantic_feature_id_str}->{"semantic_category"}->{"list_refid_ontology_node"}={}; | ||
| 1120 | $doc_hash->{$semantic_feature_id_str}->{"semantic_category"}->{"list_refid_ontology_node"}->{"datatype"}="list_refid_ontology_node"; | ||
| 1121 | my @semtag = split /[\.\/]/, $semtag; | ||
| 1122 | $doc_hash->{$semantic_feature_id_str}->{"semantic_category"}->{"list_refid_ontology_node"}->{"refid_ontology_node"} = \@semtag; | ||
| 1123 | $Alvis::NLPPlatform::last_semantic_feature++; | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | |||
| 1127 | # XXX TO BE OPTIMIZED !!! | ||
| 1128 | |||
| 1129 | my $k=1; | ||
| 1130 | my $term_word_start=-1; | ||
| 1131 | my $term_word_end=-1; | ||
| 1132 | my @tab_words; | ||
| 1133 | for($k=1;$k<$Alvis::NLPPlatform::number_of_words;$k++){ | ||
| 1134 | if($Alvis::NLPPlatform::word_start[$k]==Alvis::NLPPlatform::Annotation::read_key_id($tab_tokens[0])){ | ||
| 1135 | $term_word_start=$k; | ||
| 1136 | } | ||
| 1137 | if($Alvis::NLPPlatform::word_end[$k]==Alvis::NLPPlatform::Annotation::read_key_id($tab_tokens[(scalar @tab_tokens)-1])){ | ||
| 1138 | $term_word_end=$k; | ||
| 1139 | last; | ||
| 1140 | } | ||
| 1141 | } | ||
| 1142 | if (($term_word_start != -1) && ($term_word_end != -1)) { | ||
| 1143 | for($k=$term_word_start;$k<=$term_word_end;$k++){ | ||
| 1144 | push @tab_words,"word$k"; | ||
| 1145 | } | ||
| 1146 | } | ||
| 1147 | # XXX | ||
| 1148 | if (scalar @tab_words == 0) { | ||
| 1149 | $doc_hash->{$semantic_unit_id_str}->{"term"}->{"list_refid_token"}={}; | ||
| 1150 | $doc_hash->{$semantic_unit_id_str}->{"term"}->{"list_refid_token"}->{"datatype"} = "list_refid_token"; | ||
| 1151 | $doc_hash->{$semantic_unit_id_str}->{"term"}->{"list_refid_token"}->{"refid_token"}=\@tab_tokens; | ||
| 1152 | |||
| 1153 | } | ||
| 1154 | if(scalar @tab_words==1){ | ||
| 1155 | $doc_hash->{$semantic_unit_id_str}->{"term"}->{"refid_word"}=\@tab_words; | ||
| 1156 | } | ||
| 1157 | if(scalar @tab_words>1){ | ||
| 1158 | $doc_hash->{"phrase$phrase_idx"}={}; | ||
| 1159 | $doc_hash->{"phrase$phrase_idx"}->{'id'}="phrase$phrase_idx"; | ||
| 1160 | $doc_hash->{"phrase$phrase_idx"}->{'datatype'}="phrase"; | ||
| 1161 | $doc_hash->{"phrase$phrase_idx"}->{'list_refid_components'}={}; | ||
| 1162 | $doc_hash->{"phrase$phrase_idx"}->{'list_refid_components'}->{"datatype"}="list_refid_components"; | ||
| 1163 | $doc_hash->{"phrase$phrase_idx"}->{'list_refid_components'}->{"refid_word"}=\@tab_words; | ||
| 1164 | |||
| 1165 | $doc_hash->{$semantic_unit_id_str}->{"term"}->{"refid_phrase"}="phrase$phrase_idx"; | ||
| 1166 | # At this point, we have created a term and a phrase. We need to commit this to memory, | ||
| 1167 | # as it will come in handy! | ||
| 1168 | push @Alvis::NLPPlatform::found_terms_phr,$phrase_idx; | ||
| 1169 | push @Alvis::NLPPlatform::found_terms_words,\@tab_words; | ||
| 1170 | |||
| 1171 | $phrase_idx++; | ||
| 1172 | } | ||
| 1173 | else{ | ||
| 1174 | push @Alvis::NLPPlatform::found_terms_phr,-666; # there is no phrase | ||
| 1175 | push @Alvis::NLPPlatform::found_terms_words,\@tab_words; | ||
| 1176 | } | ||
| 1177 | |||
| 1178 | $Alvis::NLPPlatform::last_semantic_unit++; | ||
| 1179 | } else { | ||
| 1180 | if ($j <$token_end) { | ||
| 1181 | $token_term = -1; | ||
| 1182 | } else { | ||
| 1183 | warn "+++ Term content not found ($term -- $cont)\n"; | ||
| 1184 | |||
| 1185 | } | ||
| 1186 | } | ||
| 1187 | } | ||
| 1188 | } | ||
| 1189 | } | ||
| 1190 | } | ||
| 1191 | $Alvis::NLPPlatform::Annotation::phrase_idx=$phrase_idx; | ||
| 1192 | print STDERR "done - Found " . ($phrase_idx - 1) . " phrases\n"; | ||
| 1193 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Phrases: " . ($phrase_idx - 1); | ||
| 1194 | |||
| 1195 | $Alvis::NLPPlatform::last_semantic_feature=$sf; | ||
| 1196 | print STDERR "done - Found " . ($sf - 1) . " semantic features\n"; | ||
| 1197 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Semantic Features: " . ($sf - 1); | ||
| 1198 | |||
| 1199 | print STDERR "done - Found ". ($i) ." terms\n"; | ||
| 1200 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Terms: " . $i; | ||
| 1201 | } | ||
| 1202 | |||
| 1203 | |||
| 1204 | # TODO : Check that parsing is only performed on english texts | ||
| 1205 | |||
| 1206 | # TODO TODO : Check this method | ||
| 1207 | |||
| 1208 | sub syntactic_parsing{ | ||
| 1209 | my ($class, $h_config, $doc_hash) = @_; | ||
| 1210 | my $line; | ||
| 1211 | my $insentence; | ||
| 1212 | my $sentence; | ||
| 1213 | |||
| 1214 | my $tokens; | ||
| 1215 | my $analyses; | ||
| 1216 | my $analysis; | ||
| 1217 | my $nsentence; | ||
| 1218 | my $token_start; | ||
| 1219 | my $token_end; | ||
| 1220 | my $relation; | ||
| 1221 | my $left_wall; | ||
| 1222 | my $right_wall; | ||
| 1223 | |||
| 1224 | my $relation_id; | ||
| 1225 | |||
| 1226 | my @arr_tokens; | ||
| 1227 | my $last_token = 1; | ||
| 1228 | my $wordidshift=0; | ||
| 1229 | |||
| 1230 | my $corpus_filename; | ||
| 1231 | my $result_filename; | ||
| 1232 | |||
| 1233 | my $command_line; | ||
| 1234 | |||
| 1235 | my $word; | ||
| 1236 | my $word_cont; | ||
| 1237 | my $word_id; | ||
| 1238 | my $i; | ||
| 1239 | my $sentences_cont=""; | ||
| 1240 | |||
| 1241 | my @tab_word_punct; | ||
| 1242 | my @tab_word; | ||
| 1243 | my $idx_tab_word_punct=1; | ||
| 1244 | my $idx_tab_word=1; | ||
| 1245 | my @tab_mapping; | ||
| 1246 | my $inpostscript_output = 0; | ||
| 1247 | my $ingraphics_output = 0; | ||
| 1248 | |||
| 1249 | my $syntactic_relation_id; | ||
| 1250 | |||
| 1251 | $corpus_filename = $h_config->{'TMPFILE'}. ".corpus_syn.tmp"; | ||
| 1252 | |||
| 1253 | print STDERR " Performing syntactic analysis..."; | ||
| 1254 | open CORPUS,">$corpus_filename"; | ||
| 1255 | print CORPUS "!whitespace\n"; | ||
| 1256 | print CORPUS "!postscript\n"; | ||
| 1257 | print CORPUS "!graphics\n"; | ||
| 1258 | print CORPUS "!union\n"; | ||
| 1259 | print CORPUS "!walls\n"; | ||
| 1260 | # print CORPUS "!width=10000\n"; | ||
| 1261 | |||
| 1262 | # print out words+punct and fill in a tab | ||
| 1263 | push @tab_word_punct," "; | ||
| 1264 | push @tab_word," "; | ||
| 1265 | |||
| 1266 | foreach $word (Alvis::NLPPlatform::Annotation::sort(\%Alvis::NLPPlatform::hash_words_punct)){ | ||
| 1267 | |||
| 1268 | # print word | ||
| 1269 | $word_cont=$Alvis::NLPPlatform::hash_words_punct{$word}; | ||
| 1270 | push @tab_word_punct,$word_cont; | ||
| 1271 | if($word_cont eq "."){ | ||
| 1272 | $sentences_cont.="\n"; | ||
| 1273 | }else{ | ||
| 1274 | my $word_tmp=$word_cont; | ||
| 1275 | $word_tmp=~s/ /\_/g; | ||
| 1276 | $sentences_cont.="$word_tmp "; | ||
| 1277 | } | ||
| 1278 | } | ||
| 1279 | |||
| 1280 | # fill words tab | ||
| 1281 | foreach $word (Alvis::NLPPlatform::Annotation::sort(\%Alvis::NLPPlatform::hash_words)){ | ||
| 1282 | push @tab_word,$Alvis::NLPPlatform::hash_words{$word}; | ||
| 1283 | } | ||
| 1284 | |||
| 1285 | # pre-compute mapping between words+punct and words | ||
| 1286 | my $idx_nopunct=1; | ||
| 1287 | for($i=0;$i<scalar @tab_word_punct;$i++){ | ||
| 1288 | if(($idx_nopunct<scalar @tab_word)&&($tab_word_punct[$i] eq $tab_word[$idx_nopunct])){ | ||
| 1289 | # print STDERR "$tab_word_punct[$i] => $tab_word[$idx_nopunct] ($i => $idx_nopunct) $Alvis::NLPPlatform::hash_words{'word'.$idx_nopunct}\n"; | ||
| 1290 | $tab_mapping[$idx_nopunct]=$idx_nopunct; | ||
| 1291 | $idx_nopunct++; | ||
| 1292 | } | ||
| 1293 | } | ||
| 1294 | |||
| 1295 | # remove whitespaces in NE | ||
| 1296 | # my $ne; | ||
| 1297 | # my $ne_cont; | ||
| 1298 | # my $ne_mod; | ||
| 1299 | # foreach $ne(keys %Alvis::NLPPlatform::hash_named_entities){ | ||
| 1300 | # $ne_cont=$Alvis::NLPPlatform::hash_named_entities{$ne}; | ||
| 1301 | # $ne_mod=$ne_cont; | ||
| 1302 | # if($ne_cont=~/ /){ | ||
| 1303 | # if($sentences_cont=~/$ne_cont/){ | ||
| 1304 | # print STDERR "Found NE $ne_cont in sentence\n"; | ||
| 1305 | # $ne_mod=~s/ /\_/g; | ||
| 1306 | # $sentences_cont=~s/$ne_cont/$ne_mod/g; | ||
| 1307 | # } | ||
| 1308 | # } | ||
| 1309 | # } | ||
| 1310 | |||
| 1311 | print CORPUS $sentences_cont; | ||
| 1312 | close CORPUS; | ||
| 1313 | |||
| 1314 | # generate input for syntactic analyser | ||
| 1315 | |||
| 1316 | |||
| 1317 | $result_filename = $h_config->{'TMPFILE'} . ".result.tmp"; | ||
| 1318 | |||
| 1319 | if($Alvis::NLPPlatform::Annotation::ALVISLANGUAGE eq "FR"){ | ||
| 1320 | # French parser command line | ||
| 1321 | }else{ | ||
| 1322 | $command_line = $h_config->{'NLP_tools'}->{'SYNTACTIC_ANALYSIS_EN'} . " < $corpus_filename > $result_filename 2>> " . $Alvis::NLPPlatform::ALVISLOGFILE; | ||
| 1323 | } | ||
| 1324 | if (defined $command_line) { | ||
| 1325 | |||
| 1326 | `$command_line`; | ||
| 1327 | |||
| 1328 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $corpus_filename; | ||
| 1329 | |||
| 1330 | # process syntactic analysis | ||
| 1331 | |||
| 1332 | $insentence=0; | ||
| 1333 | $nsentence=0; | ||
| 1334 | $relation_id=1; | ||
| 1335 | |||
| 1336 | open SYN_RES,"<$result_filename"; | ||
| 1337 | |||
| 1338 | while($line=<SYN_RES>) | ||
| 1339 | { | ||
| 1340 | # print STDERR $line; | ||
| 1341 | if(index($line,"[(")==0){ | ||
| 1342 | $insentence=1; | ||
| 1343 | # XXX | ||
| 1344 | $nsentence++; | ||
| 1345 | $sentence=""; | ||
| 1346 | $tokens=""; | ||
| 1347 | $analyses=""; | ||
| 1348 | $left_wall=0; | ||
| 1349 | } | ||
| 1350 | if($insentence==1){ | ||
| 1351 | $sentence.=$line; | ||
| 1352 | } | ||
| 1353 | if(index($line,"[]")==0){ | ||
| 1354 | # process the line | ||
| 1355 | $sentence=~s/\[Sentence\s+[0-9]+\]//sgo; | ||
| 1356 | $sentence=~s/\[Linkage\s+[0-9]+\]//sgo; | ||
| 1357 | $sentence=~s/\[\]//sgo; | ||
| 1358 | $sentence=~s/\n//sgo; | ||
| 1359 | if ($sentence=~m/^(.+)\[\[/so) { | ||
| 1360 | $tokens=$1; | ||
| 1361 | $analyses = $'; | ||
| 1362 | # output' | ||
| 1363 | # search left-wall to shift identifiers | ||
| 1364 | if($tokens=~/LEFT\-WALL/so){ | ||
| 1365 | $left_wall=1; | ||
| 1366 | }else{ | ||
| 1367 | $left_wall=0; | ||
| 1368 | } | ||
| 1369 | |||
| 1370 | # search right-wall, simply to ignore it | ||
| 1371 | if($tokens=~/RIGHT\-WALL/so){ | ||
| 1372 | $right_wall=1; | ||
| 1373 | }else{ | ||
| 1374 | $right_wall=0; | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | # parse tokens | ||
| 1378 | @arr_tokens=split /\)\(/,$tokens; | ||
| 1379 | $last_token=(scalar @arr_tokens)-1; | ||
| 1380 | $arr_tokens[0]=~s/^\[\(//sgo; | ||
| 1381 | $arr_tokens[$last_token]=~s/\)\]$//sgo; | ||
| 1382 | |||
| 1383 | # Parsing | ||
| 1384 | while($analyses=~/(\[[0-9]+\s[0-9]+\s[0-9]+\s[^\]]+\])/sgoc){ | ||
| 1385 | $analysis=$1; | ||
| 1386 | $analysis=~m/\[([0-9]+)\s([0-9]+)\s([0-9]+)\s\(([^\]]+)\)\]/sgo; | ||
| 1387 | $token_start=$1; | ||
| 1388 | $token_end=$2; | ||
| 1389 | $relation=$4; | ||
| 1390 | if( | ||
| 1391 | (($left_wall==1)&&(($token_start==0) || ($token_end==0))) | ||
| 1392 | ||(($right_wall==1)&&(($token_start==$last_token) || ($token_end==$last_token))) | ||
| 1393 | ){ | ||
| 1394 | # ignore any relation with the left or right wall | ||
| 1395 | }else{ | ||
| 1396 | if($left_wall==0){ | ||
| 1397 | $token_start++; | ||
| 1398 | $token_end++; | ||
| 1399 | } | ||
| 1400 | # make sure we're not dealing with punctuation, otherwise just ignore' | ||
| 1401 | if((defined($tab_mapping[$token_start+$wordidshift])) && (defined($tab_mapping[$token_end+$wordidshift]) ne "")){ | ||
| 1402 | $syntactic_relation_id = "syntactic_relation$relation_id"; | ||
| 1403 | $doc_hash->{$syntactic_relation_id}={}; | ||
| 1404 | $doc_hash->{$syntactic_relation_id}->{'id'}=$syntactic_relation_id; | ||
| 1405 | $doc_hash->{$syntactic_relation_id}->{'datatype'}="syntactic_relation"; | ||
| 1406 | $doc_hash->{$syntactic_relation_id}->{'syntactic_relation_type'}="$relation"; | ||
| 1407 | $doc_hash->{$syntactic_relation_id}->{'refid_head'} = {}; | ||
| 1408 | $doc_hash->{$syntactic_relation_id}->{'refid_head'}->{'datatype'}="refid_head"; | ||
| 1409 | $doc_hash->{$syntactic_relation_id}->{'refid_head'}->{"refid_word"}="word".$tab_mapping[($token_start+$wordidshift)]; | ||
| 1410 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'} = {}; | ||
| 1411 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'}->{'datatype'}="refid_modifier"; | ||
| 1412 | $doc_hash->{$syntactic_relation_id}->{'refid_modifier'}->{"refid_word"}="word".$tab_mapping[($token_end+$wordidshift)]; | ||
| 1413 | $relation_id++; | ||
| 1414 | } | ||
| 1415 | } | ||
| 1416 | } | ||
| 1417 | } | ||
| 1418 | # trash everything and continue the loop | ||
| 1419 | |||
| 1420 | $insentence=0; | ||
| 1421 | $wordidshift+=$last_token-1; | ||
| 1422 | } | ||
| 1423 | } | ||
| 1424 | close SYN_RES; | ||
| 1425 | |||
| 1426 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $h_config->{'TMPFILE'} . ".result.tmp"; | ||
| 1427 | |||
| 1428 | $Alvis::NLPPlatform::nb_relations=$relation_id-1; | ||
| 1429 | |||
| 1430 | print STDERR "done - Found $Alvis::NLPPlatform::nb_relations relations.\n"; | ||
| 1431 | } else { | ||
| 1432 | print STDERR "No parser for language $Alvis::NLPPlatform::Annotation::ALVISLANGUAGE - continue to the next step\n"; | ||
| 1433 | } | ||
| 1434 | |||
| 1435 | push @{$doc_hash->{"log_processing1"}->{"comments"}}, "Found Syntactic Relations : " . $Alvis::NLPPlatform::nb_relations; | ||
| 1436 | } | ||
| 1437 | |||
| 1438 | |||
| 1439 | |||
| 1440 | sub semantic_feature_tagging | ||
| 1441 | { | ||
| 1442 | |||
| 1443 | my ($class, $h_config, $doc_hash) = @_; | ||
| 1444 | |||
| 1445 | # &temp_semantic_feature_tagging(@arg); | ||
| 1446 | |||
| 1447 | } | ||
| 1448 | |||
| 1449 | sub temp_semantic_feature_tagging | ||
| 1450 | { | ||
| 1451 | my ($class, $h_config, $doc_hash) = @_; | ||
| 1452 | |||
| 1453 | print STDERR " Semantic tagging... "; | ||
| 1454 | |||
| 1455 | my $in_fn = $h_config->{'TMPFILE'} . ".ast.in"; | ||
| 1456 | |||
| 1457 | if($Alvis::NLPPlatform::Annotation::ALVISLANGUAGE eq "FR"){ | ||
| 1458 | # French parser command line | ||
| 1459 | }else{ | ||
| 1460 | open DOC,">$in_fn"; | ||
| 1461 | binmode(DOC,":utf8"); | ||
| 1462 | Alvis::NLPPlatform::Annotation::render_xml($doc_hash, \*DOC, 1); | ||
| 1463 | close DOC; | ||
| 1464 | |||
| 1465 | my $cmdline = $h_config->{'NLP_tools'}->{'SEMTAG_EN'} . " $in_fn > " . $h_config->{'TMPFILE'} . ".ast.out 2>> " . $Alvis::NLPPlatform::ALVISLOGFILE; | ||
| 1466 | # print STDERR "$cmdline\n"; | ||
| 1467 | |||
| 1468 | `$cmdline`; | ||
| 1469 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $h_config->{'TMPFILE'} . ".ast.in"; | ||
| 1470 | $Alvis::NLPPlatform::ALVISDEBUG || unlink $h_config->{'TMPFILE'} . ".ast.out"; | ||
| 1471 | # $semtagout == doc XML enriched-document | ||
| 1472 | # return $semtagout; | ||
| 1473 | |||
| 1474 | } | ||
| 1475 | print STDERR "done\n"; | ||
| 1476 | } | ||
| 1477 | |||
| 1478 | |||
| 1479 | |||
| 1480 | sub semantic_relation_tagging | ||
| 1481 | { | ||
| 1482 | |||
| 1483 | my ($class, $h_config, $doc_hash) = @_; | ||
| 1484 | |||
| 1485 | } | ||
| 1486 | |||
| 1487 | |||
| 1488 | sub anaphora_resolution | ||
| 1489 | { | ||
| 1490 | my ($class, $h_config, $doc_hash) = @_; | ||
| 1491 | |||
| 1492 | |||
| 1493 | } | ||
| 1494 | |||
| 1495 | |||
| 1496 | 1; | ||
| 1497 | |||
| 1498 | __END__ |