« blib/lib/Lingua/Ogmios/DocumentRecord.pm
↑ Summary
blib/lib/Lingua/Ogmios/NLPWrappers/StanfordParser.pm »
| 1 | package Lingua::Ogmios::NLPWrappers::BasicSentenceSegmentizer; | ||
| 2 | |||
| 3 | |||
| 4 | our $VERSION='0.1'; | ||
| 5 | |||
| 6 | |||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | use Lingua::Ogmios::NLPWrappers::Wrapper; | ||
| 10 | |||
| 11 | use Lingua::Ogmios::Annotations::Sentence; | ||
| 12 | |||
| 13 | # use Lingua::Identify qw(:language_identification); | ||
| 14 | |||
| 15 | use Lingua::Identify qw/langof set_active_languages/; | ||
| 16 | |||
| 17 | # qw/langof set_active_languages/ | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | |||
| 22 | our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper); | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output) = @_; | ||
| 26 | my $lang; | ||
| 27 | my $lang2; | ||
| 28 | |||
| 29 | warn "[LOG] Creating a wrapper of the basic sentence segmentizer\n"; | ||
| 30 | |||
| 31 | my $SentenceSeg = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output); | ||
| 32 | |||
| 33 | # if (scalar(keys %{$SentenceSeg->_config->configuration}) > 0) { | ||
| 34 | # $SentenceSeg->{'sentenceSeparatorCharList'}->{"FR"} = "?.!"; | ||
| 35 | # $SentenceSeg->{'sentenceSeparatorCharList'}->{"EN"} = "?.!"; | ||
| 36 | # warn "*** SENTENCESEPARATORCHARLIST is not set ***\n"; | ||
| 37 | # } | ||
| 38 | if (defined $SentenceSeg->_config->configuration->{'CONFIG'}) { | ||
| 39 | foreach $lang (keys %{$SentenceSeg->_config->configuration->{'CONFIG'}}) { | ||
| 40 | if ($lang =~ /language=([\w]+)/io) { | ||
| 41 | $lang2 = $1; | ||
| 42 | |||
| 43 | $SentenceSeg->_setOption($lang2, "LANG_LIST", "lang_list", "en,fr,de,es,it"); | ||
| 44 | # warn "$lang2: " . $SentenceSeg->{"lang_list"}->{$lang2} . "\n"; | ||
| 45 | $SentenceSeg->{"lang_list"}->{$lang2} = [split /[, ]/, $SentenceSeg->{"lang_list"}->{$lang2}]; | ||
| 46 | # warn "$lang2: " . join('/', @{$SentenceSeg->{"lang_list"}->{$lang2}}) . "\n"; | ||
| 47 | |||
| 48 | if (exists $SentenceSeg->_config->configuration->{'CONFIG'}->{$lang}->{LANGID_METHODS}) { | ||
| 49 | $SentenceSeg->{'lang_methods'}->{$lang2} = {'method' => $SentenceSeg->_config->configuration->{'CONFIG'}->{$lang}->{LANGID_METHODS}}; | ||
| 50 | # warn Dumper($SentenceSeg->{'lang_methods'}->{$lang2}); | ||
| 51 | } else { | ||
| 52 | $SentenceSeg->{'lang_methods'}->{$lang2} = {'method' => { | ||
| 53 | smallwords => 1, | ||
| 54 | prefixes1 => 0, | ||
| 55 | prefixes2 => 0, | ||
| 56 | prefixes3 => 0, | ||
| 57 | prefixes4 => 0, | ||
| 58 | suffixes1 => 1, | ||
| 59 | suffixes2 => 1, | ||
| 60 | suffixes3 => 1, | ||
| 61 | suffixes4 => 0, | ||
| 62 | ngrams1 => 1, | ||
| 63 | ngrams2 => 1.5, | ||
| 64 | ngrams3 => 0, | ||
| 65 | ngrams4 => 0, | ||
| 66 | }, | ||
| 67 | }; | ||
| 68 | # warn Dumper($SentenceSeg->{'lang_methods'}->{$lang2}); | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | if (defined $SentenceSeg->_config->configuration->{'CONFIG'}->{$lang}->{"SENTENCESEPARATORCHARLIST"}) { | ||
| 73 | $SentenceSeg->{'sentenceSeparatorCharList'}->{$lang2} = $SentenceSeg->_config->configuration->{'CONFIG'}->{$lang}->{"SENTENCESEPARATORCHARLIST"}; | ||
| 74 | } else { | ||
| 75 | $SentenceSeg->{'sentenceSeparatorCharList'}->{"FR"} = "?.!"; | ||
| 76 | $SentenceSeg->{'sentenceSeparatorCharList'}->{"EN"} = "?.!"; | ||
| 77 | warn "*** SENTENCESEPARATORCHARLIST is not set ***\n"; | ||
| 78 | } | ||
| 79 | } else { | ||
| 80 | $SentenceSeg->{'sentenceSeparatorCharList'}->{"FR"} = "?.!"; | ||
| 81 | $SentenceSeg->{'sentenceSeparatorCharList'}->{"EN"} = "?.!"; | ||
| 82 | warn "*** SENTENCESEPARATORCHARLIST is not set ***\n"; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } else { | ||
| 86 | $SentenceSeg->{'sentenceSeparatorCharList'}->{"FR"} = "?.!"; | ||
| 87 | $SentenceSeg->{'sentenceSeparatorCharList'}->{"EN"} = "?.!"; | ||
| 88 | } | ||
| 89 | # $SentenceSeg->_input_filename($tmpfile_prefix . ".SentSeg.in"); | ||
| 90 | # $SentenceSeg->_output_filename($tmpfile_prefix . ".SentSeg.out"); | ||
| 91 | |||
| 92 | return($SentenceSeg); | ||
| 93 | |||
| 94 | } | ||
| 95 | |||
| 96 | sub run { | ||
| 97 | my ($self, $documentSet) = @_; | ||
| 98 | |||
| 99 | my $sentence; | ||
| 100 | my $document; | ||
| 101 | my $sentForm; | ||
| 102 | |||
| 103 | # Set variables according the the configuration | ||
| 104 | $self->_documentSet($documentSet); | ||
| 105 | |||
| 106 | if ($documentSet->[0]->getAnnotations->existsSentenceLevel) { | ||
| 107 | warn "sentences exist in the first document\n"; | ||
| 108 | warn " Assuming that no sentence segmentation is required for the current document set\n"; | ||
| 109 | # return(0); | ||
| 110 | } else { | ||
| 111 | warn "[LOG] Sentence segmentizer... \n"; | ||
| 112 | |||
| 113 | $self->_segmentation; | ||
| 114 | |||
| 115 | # Put log information | ||
| 116 | my $information = { 'software_name' => 'basic internal sentence segmentizer', | ||
| 117 | 'comments' => 'Sentence Segmentation\n', | ||
| 118 | 'command_line' => "", | ||
| 119 | 'list_modified_level' => ['sentence_level'], | ||
| 120 | }; | ||
| 121 | $self->_log($information); | ||
| 122 | foreach $document (@{$documentSet}) { | ||
| 123 | $document->getAnnotations->addLogProcessing( | ||
| 124 | Lingua::Ogmios::Annotations::LogProcessing->new( | ||
| 125 | { 'comments' => 'Found ' . $document->getAnnotations->getSentenceLevel->getSize . ' sentences\n', | ||
| 126 | } | ||
| 127 | ) | ||
| 128 | ); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | if ($self->_position eq "last") { | ||
| 133 | if ($self->_no_standard_output eq "SENT") { | ||
| 134 | warn "print no standard output\n"; | ||
| 135 | |||
| 136 | foreach $document (@{$documentSet}) { | ||
| 137 | foreach $sentence (@{$document->getAnnotations->getSentenceLevel->getElements}) { | ||
| 138 | $sentForm = $sentence->getForm; | ||
| 139 | print $sentForm; | ||
| 140 | if ($sentForm !~ /\n$/) { | ||
| 141 | print "\n"; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | if ($self->_no_standard_output eq "SENTLANG") { | ||
| 147 | warn "print no standard output\n"; | ||
| 148 | |||
| 149 | foreach $document (@{$documentSet}) { | ||
| 150 | foreach $sentence (@{$document->getAnnotations->getSentenceLevel->getElements}) { | ||
| 151 | $sentForm = $sentence->getForm; | ||
| 152 | $sentForm =~ s/\n/ /g; | ||
| 153 | # print $sentence->lang . "\t:\t" . $sentForm; | ||
| 154 | if (!defined $sentence->lang) { | ||
| 155 | print "INT"; | ||
| 156 | } else { | ||
| 157 | print $sentence->lang; | ||
| 158 | } | ||
| 159 | print "\t:\t" . $sentForm; | ||
| 160 | if ($sentForm !~ /\n$/) { | ||
| 161 | print "\n"; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | if ($self->_no_standard_output eq "SENT+INFO") { | ||
| 167 | warn "print no standard output\n"; | ||
| 168 | print "# detected language\t:\tsentence form\t:\tnb of chars (v1 -- length(form))\t:\tnb of chars (v2 -- offset diff)\t:\tnb of words\n"; | ||
| 169 | foreach $document (@{$documentSet}) { | ||
| 170 | foreach $sentence (@{$document->getAnnotations->getSentenceLevel->getElements}) { | ||
| 171 | $sentForm = $sentence->getForm; | ||
| 172 | $sentForm =~ s/\n/ /g; | ||
| 173 | # print $sentence->lang . "\t:\t" . $sentForm; | ||
| 174 | if (!defined $sentence->lang) { | ||
| 175 | print "INT"; | ||
| 176 | } else { | ||
| 177 | print $sentence->lang; | ||
| 178 | } | ||
| 179 | my $sentNbChar1 = length($sentForm); | ||
| 180 | my $sentNbChar2 = $sentence->end_token->getTo - $sentence->start_token->getFrom + 1; | ||
| 181 | my $sentNbWord = scalar(@{$sentence->getWordsFromSentence($document)}); | ||
| 182 | chomp $sentForm; | ||
| 183 | print "\t:\t" . $sentForm; | ||
| 184 | print "\t:\t" . $sentNbChar1; | ||
| 185 | print "\t:\t" . $sentNbChar2; | ||
| 186 | print "\t:\t" . $sentNbWord; | ||
| 187 | |||
| 188 | if ($sentForm !~ /\n$/) { | ||
| 189 | print "\n"; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | $self->getTimer->_printTimesInLine($self->_config->name); | ||
| 197 | warn "[LOG] done\n"; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub isSentenceEnd { | ||
| 201 | my ($self, $token, $lang, $doc_idx, $sent_form, $tokensTail, $document) = @_; | ||
| 202 | |||
| 203 | my $endSent; | ||
| 204 | my $currenttoken; | ||
| 205 | my $i; | ||
| 206 | |||
| 207 | if (!defined $lang) { | ||
| 208 | warn "language not defined, default is EN\n"; | ||
| 209 | $lang = 'EN'; | ||
| 210 | } | ||
| 211 | # warn "$lang\n"; | ||
| 212 | # warn $self->{'sentenceSeparatorCharList'}->{$lang} . "\n"; | ||
| 213 | # warn $$token->getContent . "\n"; | ||
| 214 | |||
| 215 | if (index($self->{'sentenceSeparatorCharList'}->{$lang}, $$token->getContent) != -1) { | ||
| 216 | $endSent = 1; | ||
| 217 | } else { | ||
| 218 | $endSent = 0; | ||
| 219 | } | ||
| 220 | # warn "$endSent\n"; | ||
| 221 | |||
| 222 | if ($endSent == 1) { | ||
| 223 | # warn "ckeck dots\n"; | ||
| 224 | $endSent = $self->_dots($token, $lang, $doc_idx, $sent_form, $tokensTail, $document); | ||
| 225 | |||
| 226 | # warn "ckeck acronyms\n"; | ||
| 227 | $endSent &&= $self->_acronyms($$token, $lang, $doc_idx, $sent_form, $tokensTail, $document); | ||
| 228 | |||
| 229 | # inside word | ||
| 230 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex('list_refid_token', $$token->getId)) { | ||
| 231 | # warn " " . $self->currentDocument->getAnnotations->getWordLevel->getElementByToken($$token)->[0]->end_token->getContent . "\n"; | ||
| 232 | $endSent &&= $self->currentDocument->getAnnotations->getWordLevel->getElementByToken($$token)->[0]->end_token->equals($$token); | ||
| 233 | # warn "==> $endSent\n"; | ||
| 234 | } | ||
| 235 | # warn "$endSent\n"; | ||
| 236 | # # case ... | ||
| 237 | # $i=0; | ||
| 238 | # $currenttoken = $$token; | ||
| 239 | # while(($i < 3) && ($currenttoken->getContent eq ".") && (defined $currenttoken->next)) { | ||
| 240 | # warn $currenttoken->getContent . "\n"; | ||
| 241 | # $i++; | ||
| 242 | # $currenttoken = $currenttoken->next; | ||
| 243 | # } | ||
| 244 | # if (($i >= 3) && (defined $currenttoken)) { | ||
| 245 | # warn "No\n"; | ||
| 246 | # $endSent = 0; | ||
| 247 | |||
| 248 | # while(($currenttoken->isSep) && (defined $currenttoken->next)) { | ||
| 249 | # $currenttoken = $currenttoken->next; | ||
| 250 | # } | ||
| 251 | # if (defined $currenttoken) { | ||
| 252 | # warn $currenttoken->isAlpha . " : " . substr($currenttoken->getContent,0,1) . "\n"; | ||
| 253 | # if ($currenttoken->getContent eq ')') { | ||
| 254 | # $endSent = 0; | ||
| 255 | # # $$doc_idx += 2; | ||
| 256 | # # if (defined $sent_form) { | ||
| 257 | # # $$sent_form .= $$token->getContent; | ||
| 258 | # # $$sent_form .= $$token->next->getContent; | ||
| 259 | # # $$token = $$token->next->next; | ||
| 260 | # # } | ||
| 261 | # # if (defined $tokensTail) { | ||
| 262 | # # push @$tokensTail, $$token; | ||
| 263 | # # push @$tokensTail, $$token->next; | ||
| 264 | # # } | ||
| 265 | # } elsif (($currenttoken->isAlpha) && (substr($currenttoken->getContent,0,1) eq uc(substr($currenttoken->getContent,0,1)) )) { | ||
| 266 | # $endSent = 1; | ||
| 267 | # } else { | ||
| 268 | # $endSent = 0; | ||
| 269 | # } | ||
| 270 | # $$doc_idx += 2; | ||
| 271 | # if (defined $sent_form) { | ||
| 272 | # $$sent_form .= $$token->getContent; | ||
| 273 | # $$sent_form .= $$token->next->getContent; | ||
| 274 | # $$token = $$token->next->next; | ||
| 275 | # } | ||
| 276 | # if (defined $tokensTail) { | ||
| 277 | # push @$tokensTail, $$token; | ||
| 278 | # push @$tokensTail, $$token->next; | ||
| 279 | # } | ||
| 280 | # } | ||
| 281 | # } else { | ||
| 282 | # $endSent = 1; | ||
| 283 | # } | ||
| 284 | } | ||
| 285 | # warn "-> $endSent\n"; | ||
| 286 | return($endSent); | ||
| 287 | |||
| 288 | } | ||
| 289 | |||
| 290 | sub _acronyms { | ||
| 291 | my ($self, $token, $lang, $doc_idx, $sent_form, $tokensTail, $document) = @_; | ||
| 292 | |||
| 293 | my %acronyms = ("etc" => 1, "etc." => 1, | ||
| 294 | "e.g." => 1, "e.g" => 1, | ||
| 295 | "i.e." => 1, "i.e" => 1, | ||
| 296 | ); | ||
| 297 | my @words; | ||
| 298 | my $word; | ||
| 299 | my $i; | ||
| 300 | my $endSent = 1; | ||
| 301 | my $nextToken; | ||
| 302 | |||
| 303 | $nextToken = $token->next; | ||
| 304 | while((defined $nextToken) && ($nextToken->isSep)) { | ||
| 305 | $nextToken = $nextToken->next; | ||
| 306 | } | ||
| 307 | |||
| 308 | # warn "===" . $token->getContent . "\n"; | ||
| 309 | if ((defined $nextToken) && (defined $token->previous)) { | ||
| 310 | # warn " " . $nextToken->getContent . "\n"; | ||
| 311 | # warn " " . $token->previous->getContent . "\n"; | ||
| 312 | if ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $nextToken->getId)) { | ||
| 313 | $endSent = 0; | ||
| 314 | } else { | ||
| 315 | @words = @{$self->currentDocument->getAnnotations->getWordLevel->getElementByToken($token->previous)}; | ||
| 316 | # if (scalar(@words) > 0) { | ||
| 317 | $i = 0; | ||
| 318 | # foreach $word (@words) { | ||
| 319 | # warn $word->getForm . "\n"; | ||
| 320 | # } | ||
| 321 | while(($i < scalar(@words)) && | ||
| 322 | (!(exists($acronyms{$words[$i]->getForm}) || (exists($acronyms{$words[$i]->getForm}))))) { | ||
| 323 | # warn "=> " . $words[$i]->getForm . "\n"; | ||
| 324 | $i++; | ||
| 325 | } | ||
| 326 | if ($i < scalar(@words)) { | ||
| 327 | # warn "OK: " . $nextToken->getContent . " " . substr($nextToken->getContent,0,1) . "\n"; | ||
| 328 | if (($nextToken->isAlpha) && (substr($nextToken->getContent,0,1) eq uc(substr($nextToken->getContent,0,1)) )) { | ||
| 329 | # warn "OK2\n"; | ||
| 330 | $endSent = 1; | ||
| 331 | } else { | ||
| 332 | $endSent = 0; | ||
| 333 | } | ||
| 334 | } | ||
| 335 | } | ||
| 336 | } | ||
| 337 | # warn "-> $endSent\n"; | ||
| 338 | return($endSent); | ||
| 339 | } | ||
| 340 | |||
| 341 | sub _dots { | ||
| 342 | my ($self, $token, $lang, $doc_idx, $sent_form, $tokensTail, $document) = @_; | ||
| 343 | |||
| 344 | my $endSent; | ||
| 345 | my $currenttoken; | ||
| 346 | my $i; | ||
| 347 | |||
| 348 | # case ... | ||
| 349 | $i=0; | ||
| 350 | $currenttoken = $$token; | ||
| 351 | while(($i < 3) && ($currenttoken->getContent eq ".") && (defined $currenttoken->next)) { | ||
| 352 | # warn $currenttoken->getContent . "\n"; | ||
| 353 | $i++; | ||
| 354 | $currenttoken = $currenttoken->next; | ||
| 355 | } | ||
| 356 | if (($i >= 3) && (defined $currenttoken) && | ||
| 357 | (!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $currenttoken->getId))) { | ||
| 358 | # warn "No\n"; | ||
| 359 | $endSent = 0; | ||
| 360 | |||
| 361 | while(($currenttoken->isSep) && (defined $currenttoken->next) && | ||
| 362 | (!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $currenttoken->getId))) { | ||
| 363 | $currenttoken = $currenttoken->next; | ||
| 364 | } | ||
| 365 | if (defined $currenttoken) { | ||
| 366 | # warn $currenttoken->isAlpha . " : " . substr($currenttoken->getContent,0,1) . "\n"; | ||
| 367 | if (($currenttoken->getContent eq ')') || | ||
| 368 | ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $currenttoken->getId))) { | ||
| 369 | $endSent = 0; | ||
| 370 | # $$doc_idx += 2; | ||
| 371 | # if (defined $sent_form) { | ||
| 372 | # $$sent_form .= $$token->getContent; | ||
| 373 | # $$sent_form .= $$token->next->getContent; | ||
| 374 | # $$token = $$token->next->next; | ||
| 375 | # } | ||
| 376 | # if (defined $tokensTail) { | ||
| 377 | # push @$tokensTail, $$token; | ||
| 378 | # push @$tokensTail, $$token->next; | ||
| 379 | # } | ||
| 380 | } elsif (($currenttoken->isAlpha) && (substr($currenttoken->getContent,0,1) eq uc(substr($currenttoken->getContent,0,1)) )) { | ||
| 381 | $endSent = 1; | ||
| 382 | } elsif (!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $currenttoken->getId)) { | ||
| 383 | $endSent = 1; | ||
| 384 | } else { | ||
| 385 | $endSent = 0; | ||
| 386 | } | ||
| 387 | $$doc_idx += 2; | ||
| 388 | if (defined $sent_form) { | ||
| 389 | $$sent_form .= $$token->getContent; | ||
| 390 | $$sent_form .= $$token->next->getContent; | ||
| 391 | $$token = $$token->next->next; | ||
| 392 | } | ||
| 393 | if (defined $tokensTail) { | ||
| 394 | push @$tokensTail, $$token; | ||
| 395 | push @$tokensTail, $$token->next; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | } else { | ||
| 399 | $endSent = 1; | ||
| 400 | } | ||
| 401 | return($endSent); | ||
| 402 | } | ||
| 403 | |||
| 404 | sub _segmentation { | ||
| 405 | my ($self) = @_; | ||
| 406 | |||
| 407 | my $start_token; | ||
| 408 | my $end_token; | ||
| 409 | |||
| 410 | my $document; | ||
| 411 | my $doc_idx; | ||
| 412 | my $lang; | ||
| 413 | my $token; | ||
| 414 | my $isNE; | ||
| 415 | my $isWord; | ||
| 416 | |||
| 417 | my $beginSection; | ||
| 418 | my $endSection; | ||
| 419 | |||
| 420 | my $sentence; | ||
| 421 | my $sentence_form; | ||
| 422 | |||
| 423 | foreach $document (@{$self->_documentSet}) { | ||
| 424 | $lang = $document->getAnnotations->getLanguage; | ||
| 425 | $self->currentDocument($document); | ||
| 426 | |||
| 427 | $doc_idx = 0; | ||
| 428 | $start_token = undef; # $document->getAnnotations->getSectionLevel->getElements->[0]; | ||
| 429 | $end_token = undef; | ||
| 430 | |||
| 431 | # warn "Processing document " . $document ->getId . "\n"; | ||
| 432 | |||
| 433 | for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) { | ||
| 434 | do { | ||
| 435 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 436 | $doc_idx++; | ||
| 437 | } while(($doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements})) && ($token->isSep)); | ||
| 438 | $doc_idx--; | ||
| 439 | $start_token = $token; | ||
| 440 | $sentence_form = ""; | ||
| 441 | # warn $self->isSentenceEnd(\$token, $lang, \$doc_idx, \$sentence_form, undef, $document) . "\n"; | ||
| 442 | |||
| 443 | # TODO take into account a NE follown by a capitalized word (which is not a NE) | ||
| 444 | # while(($doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements})) && | ||
| 445 | # ((!($self->isSentenceEnd(\$token, $lang, \$doc_idx, \$sentence_form, undef))) || | ||
| 446 | # ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId)) || | ||
| 447 | # ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) && | ||
| 448 | # (!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $token->getId))) { | ||
| 449 | while(($doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements})) && | ||
| 450 | (!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $token->getId)) && | ||
| 451 | (!$self->isSentenceEnd(\$token, $lang, \$doc_idx, \$sentence_form, undef, $document)) #&& | ||
| 452 | # ( | ||
| 453 | # # ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) | ||
| 454 | # || ($document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId))) | ||
| 455 | # ) | ||
| 456 | ) { | ||
| 457 | $sentence_form .= $token->getContent; | ||
| 458 | # warn $token->getContent. "\n"; | ||
| 459 | # warn $document->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("list_refid_token", $token->getId) . "\n"; | ||
| 460 | # warn $document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId) . "\n"; | ||
| 461 | # warn "$sentence_form (1)\n"; | ||
| 462 | $end_token = $token; | ||
| 463 | $doc_idx++; | ||
| 464 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 465 | # warn $self->isSentenceEnd(\$token, $lang, \$doc_idx, \$sentence_form, undef, $document) . "\n"; | ||
| 466 | } | ||
| 467 | # warn "$sentence_form (1b)\n"; | ||
| 468 | if ($doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements})) { | ||
| 469 | $sentence_form .= $token->getContent; | ||
| 470 | # warn "$sentence_form (2)\n"; | ||
| 471 | $end_token = $token; | ||
| 472 | if (!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $token->getId)) { | ||
| 473 | my $end_token_tail = $token; | ||
| 474 | my $sentence_form_tail = ""; | ||
| 475 | my @tokensTail; | ||
| 476 | my $tail = 0; | ||
| 477 | do { | ||
| 478 | |||
| 479 | $end_token_tail = $end_token_tail->next; | ||
| 480 | if (defined $end_token_tail) { | ||
| 481 | $tail ||= $self->isSentenceEnd(\$end_token_tail, $lang, \$doc_idx, \$sentence_form_tail, \@tokensTail, $document); | ||
| 482 | push @tokensTail, $end_token_tail; | ||
| 483 | $sentence_form_tail .= $end_token_tail->getContent; | ||
| 484 | # warn "$sentence_form_tail (3)\n"; | ||
| 485 | } | ||
| 486 | $doc_idx++; | ||
| 487 | } while (($doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements})) | ||
| 488 | && (!$document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $end_token_tail->getId)) | ||
| 489 | && (($end_token_tail->isSep) || ($self->isSentenceEnd(\$end_token_tail, $lang, \$doc_idx, undef, undef, $document)))); | ||
| 490 | $doc_idx--; | ||
| 491 | if ($tail) { | ||
| 492 | pop @tokensTail; | ||
| 493 | while((scalar(@tokensTail) > 0) && ($tokensTail[$#tokensTail]->isSep)) { | ||
| 494 | pop @tokensTail; | ||
| 495 | } | ||
| 496 | if (scalar(@tokensTail) > 0) { | ||
| 497 | foreach my $tk (@tokensTail) { | ||
| 498 | $sentence_form .= $tk->getContent; | ||
| 499 | # warn "$sentence_form (4)\n"; | ||
| 500 | } | ||
| 501 | $end_token = $tokensTail[$#tokensTail]; | ||
| 502 | } | ||
| 503 | } | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | if ((defined $start_token) && | ||
| 508 | ((($document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $start_token->getId) == 1) | ||
| 509 | && ((! $start_token->equals($end_token)) || (!$start_token->isSep))) | ||
| 510 | || ((defined $end_token) && (! $start_token->equals($end_token))))) { | ||
| 511 | |||
| 512 | if (!defined ($end_token)) {$end_token = $start_token;}; | ||
| 513 | |||
| 514 | # my $method = {'method' => { | ||
| 515 | # smallwords => 1, | ||
| 516 | # prefixes1 => 0, | ||
| 517 | # prefixes2 => 0, | ||
| 518 | # prefixes3 => 0, | ||
| 519 | # prefixes4 => 0, | ||
| 520 | # suffixes1 => 1, | ||
| 521 | # suffixes2 => 1, | ||
| 522 | # suffixes3 => 1, | ||
| 523 | # suffixes4 => 0, | ||
| 524 | # ngrams1 => 1, | ||
| 525 | # ngrams2 => 1.5, | ||
| 526 | # ngrams3 => 0, | ||
| 527 | # ngrams4 => 0, | ||
| 528 | # }, | ||
| 529 | # }; | ||
| 530 | |||
| 531 | # warn Dumper($method); | ||
| 532 | |||
| 533 | # 'active-languages' => [ 'en', 'de', 'fr'], | ||
| 534 | # 'max-size' => 50, | ||
| 535 | # warn "$lang: " . join('/', @{$self->{"lang_list"}->{$lang}}) . "\n"; | ||
| 536 | set_active_languages(@{$self->{"lang_list"}->{$lang}}); # qw/en fr de es it/); # | ||
| 537 | # warn "lang of the sentence $sentence_form : " . langof($method, $sentence_form) . "\n"; | ||
| 538 | # warn "start sentence at " . $start_token->getId . "\n"; | ||
| 539 | # warn "end sentence at " . $end_token->getId . "\n"; | ||
| 540 | # warn "Sentence: $sentence_form\n"; | ||
| 541 | my $sentence = Lingua::Ogmios::Annotations::Sentence->new( | ||
| 542 | {'form' => $sentence_form, | ||
| 543 | 'refid_start_token' => $start_token, | ||
| 544 | 'refid_end_token' => $end_token, | ||
| 545 | }); | ||
| 546 | $sentence->lang(langof($self->{'lang_methods'}->{$lang}, $sentence_form)); # | ||
| 547 | |||
| 548 | $document->getAnnotations->addSentence($sentence); | ||
| 549 | } | ||
| 550 | |||
| 551 | |||
| 552 | # $isNE = $document->getAnnotations->getSemantic_UnitLevel->existsElementFromIndex("list_refid_token", $token->getId); | ||
| 553 | # $isWord = $document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId); | ||
| 554 | |||
| 555 | # $beginSection = $document->getAnnotations->getSectionLevel->existsElementFromIndex("from", $token->getId); | ||
| 556 | |||
| 557 | # $endSection = $document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $token->getId); | ||
| 558 | |||
| 559 | |||
| 560 | |||
| 561 | # warn "Token(" . $token->getType . "): " . $token->getId . " , " . ($isNE * 1) . " , " . ($isWord * 1) . " , " . ($beginSection * 1) . " , " . ($endSection * 1) . "\n"; | ||
| 562 | |||
| 563 | |||
| 564 | |||
| 565 | # if ($token->isSep) { | ||
| 566 | # warn "Token(" . $token->getType . "): " . $token->getId . " , " . ($isNE * 1) . " , " . ($isWord * 1) . " , " . ($beginSection * 1) . " , " . ($endSection * 1) . "\n"; | ||
| 567 | |||
| 568 | # } | ||
| 569 | |||
| 570 | } | ||
| 571 | } | ||
| 572 | |||
| 573 | } | ||
| 574 | |||
| 575 | 1; | ||
| 576 | |||
| 577 | |||
| 578 | __END__ |