« blib/lib/Lingua/Ogmios/NLPWrappers/LexicalSyntacticPatterns.pm
↑ Summary
blib/lib/Lingua/Ogmios/NLPWrappers/Distribution.pm »
| 1 | package Lingua::Ogmios::NLPWrappers::TreeTagger; | ||
| 2 | |||
| 3 | |||
| 4 | our $VERSION='0.1'; | ||
| 5 | |||
| 6 | use utf8; | ||
| 7 | |||
| 8 | use Lingua::Ogmios::NLPWrappers::Wrapper; | ||
| 9 | |||
| 10 | use Lingua::Ogmios::Annotations::MorphosyntacticFeatures; | ||
| 11 | use Lingua::Ogmios::Annotations::Lemma; | ||
| 12 | |||
| 13 | use Encode qw(:fallbacks);; | ||
| 14 | |||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper); | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output) = @_; | ||
| 23 | |||
| 24 | warn "[LOG] Creating a wrapper of the TreeTagger\n"; | ||
| 25 | |||
| 26 | |||
| 27 | my $TreeTagger = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output); | ||
| 28 | |||
| 29 | $TreeTagger->_input_filename($tmpfile_prefix . ".TreeTagger.in"); | ||
| 30 | $TreeTagger->_output_filename($tmpfile_prefix . ".TreeTagger.out"); | ||
| 31 | |||
| 32 | unlink($TreeTagger->_input_filename); | ||
| 33 | unlink($TreeTagger->_output_filename); | ||
| 34 | |||
| 35 | return($TreeTagger); | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | sub _processTreeTagger { | ||
| 40 | my ($self, $lang) = @_; | ||
| 41 | |||
| 42 | warn "[LOG] POS tagger\n"; | ||
| 43 | |||
| 44 | $lang = $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 45 | |||
| 46 | return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{TreeTagger_CMD} . " < " . $self->_input_filename . ">" . $self->_output_filename))); | ||
| 47 | |||
| 48 | warn "[LOG]\n"; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _inputTreeTagger { | ||
| 52 | my ($self) = @_; | ||
| 53 | |||
| 54 | my $token; | ||
| 55 | my $next_token; | ||
| 56 | # my $corpus_in = ""; | ||
| 57 | my $document; | ||
| 58 | my $doc_idx; | ||
| 59 | my $previously_end_sentence = 0; | ||
| 60 | my $docId; | ||
| 61 | my @corpus_in_t; | ||
| 62 | my @elementRef; | ||
| 63 | |||
| 64 | my $word; | ||
| 65 | warn "[LOG] making Decision TreeTagger input\n"; | ||
| 66 | |||
| 67 | # foreach $document (@{$self->_documentSet}) { | ||
| 68 | for($docId = 0; $docId < scalar(@{$self->_documentSet});$docId++) { | ||
| 69 | $document = $self->_documentSet->[$docId]; | ||
| 70 | $previously_end_sentence = 0; | ||
| 71 | for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) { | ||
| 72 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 73 | # warn "$doc_idx: " . $token->getContent . "\n"; | ||
| 74 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 75 | # WORD | ||
| 76 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 77 | my $wordform = $word->getForm; | ||
| 78 | $wordform =~ s/[\t\n]/ /gos; | ||
| 79 | $wordform =~ s/ +/ /gos; | ||
| 80 | push @corpus_in_t, "$wordform"; | ||
| 81 | push @elementRef, ["word", $docId, [$word->getId]]; | ||
| 82 | $previously_end_sentence = 0; | ||
| 83 | |||
| 84 | $doc_idx += $word->getReferenceSize - 1; | ||
| 85 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 86 | # warn "$doc_idx: " . $token->getContent . "\n"; | ||
| 87 | } else { | ||
| 88 | if (!($token->isSep)) { | ||
| 89 | if (($token->getContent =~ /[\x{2019}\x{2032}']/go) && ($previously_end_sentence == 0) && ($#corpus_in_t > -1) ) { | ||
| 90 | # (!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->previous->getId))) { | ||
| 91 | $corpus_in_t[$#corpus_in_t] .= $token->getContent; | ||
| 92 | push @{$elementRef[$#elementRef]->[2]}, $token->getId; | ||
| 93 | } else { | ||
| 94 | push @corpus_in_t, $token->getContent; | ||
| 95 | push @elementRef, ["token", $docId, [$token->getId]]; | ||
| 96 | $previously_end_sentence = 0; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 101 | if ($token->isSymb) { | ||
| 102 | if (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) { | ||
| 103 | $corpus_in_t[$#corpus_in_t] .= "\tSENT"; | ||
| 104 | } else { | ||
| 105 | push @corpus_in_t, $token->getContent . "\tSENT"; | ||
| 106 | push @elementRef, ["token", $docId, [$token->getId]]; | ||
| 107 | } | ||
| 108 | } else { | ||
| 109 | push @corpus_in_t, ".\tSENT"; | ||
| 110 | push @elementRef, ["empty",$docId,[]]; | ||
| 111 | } | ||
| 112 | $previously_end_sentence = 1; | ||
| 113 | } | ||
| 114 | |||
| 115 | } | ||
| 116 | } | ||
| 117 | $self->_input_array(\@elementRef); | ||
| 118 | warn "elementRef: " . scalar(@elementRef) . "\n"; | ||
| 119 | warn "corpus_in_t: " . scalar(@corpus_in_t) . "\n"; | ||
| 120 | open FILE_IN, ">" . $self->_input_filename; | ||
| 121 | |||
| 122 | # print FILE_IN Encode::encode("iso-8859-1", $corpus_in, Encode::FB_DEFAULT); #$corpus_in; | ||
| 123 | map {$_ =~ s/[\x{2019}\x{2032}]/\'/go} @corpus_in_t; | ||
| 124 | |||
| 125 | print FILE_IN Encode::encode("iso-8859-1", join("\n",@corpus_in_t), Encode::FB_DEFAULT); #$corpus_in; | ||
| 126 | print FILE_IN "\n"; | ||
| 127 | close FILE_IN; | ||
| 128 | |||
| 129 | warn "[LOG] done\n"; | ||
| 130 | |||
| 131 | |||
| 132 | } | ||
| 133 | |||
| 134 | sub _outputParsing { | ||
| 135 | my ($self) = @_; | ||
| 136 | my $line; | ||
| 137 | my @TreeTaggerOutput; | ||
| 138 | |||
| 139 | warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 140 | |||
| 141 | open FILE_OUT, $self->_output_filename or warn "Can't open the file " . $self->_output_filename;; | ||
| 142 | # binmode(FILE_OUT, ":utf8"); | ||
| 143 | |||
| 144 | while($line = <FILE_OUT>) { | ||
| 145 | chomp $line; | ||
| 146 | if ($line ne "") { | ||
| 147 | # $line =~ s/œ/oe/go; | ||
| 148 | # $line =~ s//oe/go; | ||
| 149 | my @tmp = split /\t/, $line; | ||
| 150 | # work around some strange tagging | ||
| 151 | if (!defined $tmp[1]) { | ||
| 152 | $tmp[1] = "SYM"; | ||
| 153 | } | ||
| 154 | if ((!defined $tmp[2]) || ($tmp[2] eq '@card@') || ($tmp[2] eq '@ord@')) { | ||
| 155 | $tmp[2] = $tmp[0]; | ||
| 156 | } | ||
| 157 | # warn "$line\n"; | ||
| 158 | push @TreeTaggerOutput, \@tmp; | ||
| 159 | } | ||
| 160 | |||
| 161 | } | ||
| 162 | close FILE_OUT; | ||
| 163 | |||
| 164 | warn "TT output size: " . scalar(@TreeTaggerOutput) . "\n"; | ||
| 165 | |||
| 166 | my $TreeTaggerOutput_idx = 0; | ||
| 167 | # my $TreeTaggerInput_idx = 0; | ||
| 168 | my $docId; | ||
| 169 | my $document; | ||
| 170 | my $word; | ||
| 171 | my $posInLemma; | ||
| 172 | my $posInWord; | ||
| 173 | my $substringBefore; | ||
| 174 | my $substringAfter; | ||
| 175 | |||
| 176 | |||
| 177 | for($TreeTaggerOutput_idx = 0; $TreeTaggerOutput_idx < scalar(@{$self->_input_array}) ; $TreeTaggerOutput_idx++) { | ||
| 178 | $document = $self->_documentSet->[$self->_input_array->[$TreeTaggerOutput_idx]->[1]]; | ||
| 179 | # warn join("\t", @{$TreeTaggerOutput[$TreeTaggerOutput_idx]}) . "\n"; | ||
| 180 | if ($self->_input_array->[$TreeTaggerOutput_idx]->[0] eq "word") { | ||
| 181 | # warn "=>word\n"; | ||
| 182 | $word = $document->getAnnotations->getWordLevel->getElementById($self->_input_array->[$TreeTaggerOutput_idx]->[2]->[0]); | ||
| 183 | |||
| 184 | # Correct POSTag if it's named entity | ||
| 185 | if ($word->isNE) { | ||
| 186 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] = "NP"; | ||
| 187 | # warn "Remove _ if it's a complex word i.e. a named entity\n"; | ||
| 188 | |||
| 189 | # Remove _ if it's a complex word i.e. a named entity | ||
| 190 | |||
| 191 | ######################################################################## | ||
| 192 | # $posInLemma = 0; | ||
| 193 | # do { | ||
| 194 | # if ((($posInLemma = index($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], "_"), $posInLemma) != -1) && | ||
| 195 | # ((($posInWord = index($TreeTaggerOutput[$TreeTaggerOutput_idx]->[0], "_", $posInLemma)) == -1) || | ||
| 196 | # ($posInLemma != $posInWord))) { | ||
| 197 | # # warn "lemma to correct : " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] . "\n"; | ||
| 198 | # $substringBefore = substr($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], 0, $posInLemma); | ||
| 199 | # $substringAfter = substr($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], $posInLemma + 1); | ||
| 200 | |||
| 201 | # # warn "New lemma : $substringBefore $substringAfter ($posInLemma)\n"; | ||
| 202 | # $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] = "$substringBefore $substringAfter"; | ||
| 203 | # $posInLemma++; | ||
| 204 | # } | ||
| 205 | # } while (($posInLemma != -1) && ($posInLemma != $posInWord) && ($posInLemma < length($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2]))); | ||
| 206 | ######################################################################## | ||
| 207 | } | ||
| 208 | |||
| 209 | # work around some strange tagging | ||
| 210 | # warn "TreeTaggerOutput_idx: $TreeTaggerOutput_idx\n"; | ||
| 211 | if (!defined $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1]) { | ||
| 212 | warn "** POSTAG not defined for $TreeTaggerOutput_idx (" . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] . ")\n"; | ||
| 213 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] = "SYM"; | ||
| 214 | } else { | ||
| 215 | if ($TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] eq "SENT") { | ||
| 216 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] = "NP"; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | # warn join("\t", @{$TreeTaggerOutput[$TreeTaggerOutput_idx]}) . "\n"; | ||
| 220 | |||
| 221 | my $MSFeatures = Lingua::Ogmios::Annotations::MorphosyntacticFeatures->new( | ||
| 222 | {'refid_word' => $word, | ||
| 223 | 'syntactic_category' => $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1], | ||
| 224 | }); | ||
| 225 | $document->getAnnotations->addMorphosyntacticFeatures($MSFeatures); | ||
| 226 | |||
| 227 | |||
| 228 | my $Lemma = Lingua::Ogmios::Annotations::Lemma->new( | ||
| 229 | {'refid_word' => $word, | ||
| 230 | 'canonical_form' => $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], | ||
| 231 | }); | ||
| 232 | $document->getAnnotations->addLemma($Lemma); | ||
| 233 | |||
| 234 | } else { | ||
| 235 | # warn "=>other\n"; | ||
| 236 | # $TreeTaggerOutput_idx++; | ||
| 237 | |||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | # foreach $document (@{$self->_documentSet}) { | ||
| 242 | |||
| 243 | |||
| 244 | warn "[LOG] done\n"; | ||
| 245 | } | ||
| 246 | |||
| 247 | |||
| 248 | sub _outputParsing2 { | ||
| 249 | my ($self) = @_; | ||
| 250 | warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 251 | |||
| 252 | $self->_parseTreeTaggerFormatOutput($self->_output_filename, 0); | ||
| 253 | |||
| 254 | warn "[LOG] done\n"; | ||
| 255 | } | ||
| 256 | |||
| 257 | |||
| 258 | sub _outputParsing1 { | ||
| 259 | my ($self) = @_; | ||
| 260 | |||
| 261 | |||
| 262 | my $line; | ||
| 263 | my @TreeTaggerOutput; | ||
| 264 | |||
| 265 | my $doc_idx; | ||
| 266 | my $word_idx; | ||
| 267 | my $document; | ||
| 268 | |||
| 269 | my $word; | ||
| 270 | my $token; | ||
| 271 | |||
| 272 | my $posInWord; | ||
| 273 | my $posInLemma; | ||
| 274 | my $tmpStr = ""; | ||
| 275 | my $substringBefore; | ||
| 276 | my $substringAfter; | ||
| 277 | my $previously_end_sentence = 0; | ||
| 278 | |||
| 279 | warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 280 | |||
| 281 | open FILE_OUT, $self->_output_filename or warn "Can't open the file " . $self->_output_filename;; | ||
| 282 | # binmode(FILE_OUT, ":utf8"); | ||
| 283 | |||
| 284 | while($line = <FILE_OUT>) { | ||
| 285 | chomp $line; | ||
| 286 | if ($line ne "") { | ||
| 287 | # $line =~ s/œ/oe/go; | ||
| 288 | # $line =~ s//oe/go; | ||
| 289 | my @tmp = split /\t/, $line; | ||
| 290 | # work around some strange tagging | ||
| 291 | if (!defined $tmp[1]) { | ||
| 292 | $tmp[1] = "SYM"; | ||
| 293 | } | ||
| 294 | if ((!defined $tmp[2]) || ($tmp[2] eq '@card@') || ($tmp[2] eq '@ord@')) { | ||
| 295 | $tmp[2] = $tmp[0]; | ||
| 296 | } | ||
| 297 | # warn "$line\n"; | ||
| 298 | push @TreeTaggerOutput, \@tmp; | ||
| 299 | } | ||
| 300 | |||
| 301 | } | ||
| 302 | close FILE_OUT; | ||
| 303 | |||
| 304 | # warn "TT output size: " . scalar(@TreeTaggerOutput) . "\n"; | ||
| 305 | |||
| 306 | my $TreeTaggerOutput_idx = 0; | ||
| 307 | |||
| 308 | foreach $document (@{$self->_documentSet}) { | ||
| 309 | $tmpStr = ""; | ||
| 310 | for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) { | ||
| 311 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 312 | # warn "TOK: " . $token->getContent . "\n"; | ||
| 313 | # warn "TreeTaggerOutput_idx: $TreeTaggerOutput_idx\n"; | ||
| 314 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 315 | $previously_end_sentence = 0; | ||
| 316 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 317 | |||
| 318 | # warn "WORD: " . $word->getForm . " (" . $word->getId . ") " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] . "\n"; | ||
| 319 | |||
| 320 | # Correct POSTag if it's named entity | ||
| 321 | if ($word->isNE) { | ||
| 322 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] = "NP"; | ||
| 323 | # warn "Remove _ if it's a complex word i.e. a named entity\n"; | ||
| 324 | |||
| 325 | # Remove _ if it's a complex word i.e. a named entity | ||
| 326 | |||
| 327 | $posInLemma = 0; | ||
| 328 | do { | ||
| 329 | if ((($posInLemma = index($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], "_"), $posInLemma) != -1) && | ||
| 330 | ((($posInWord = index($TreeTaggerOutput[$TreeTaggerOutput_idx]->[0], "_", $posInLemma)) == -1) || | ||
| 331 | ($posInLemma != $posInWord))) { | ||
| 332 | # warn "lemma to correct : " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] . "\n"; | ||
| 333 | $substringBefore = substr($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], 0, $posInLemma); | ||
| 334 | $substringAfter = substr($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], $posInLemma + 1); | ||
| 335 | |||
| 336 | # warn "New lemma : $substringBefore $substringAfter ($posInLemma)\n"; | ||
| 337 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2] = "$substringBefore $substringAfter"; | ||
| 338 | $posInLemma++; | ||
| 339 | } | ||
| 340 | } while (($posInLemma != -1) && ($posInLemma != $posInWord) && ($posInLemma < length($TreeTaggerOutput[$TreeTaggerOutput_idx]->[2]))); | ||
| 341 | } | ||
| 342 | |||
| 343 | # work around some strange tagging | ||
| 344 | # warn "TreeTaggerOutput_idx: $TreeTaggerOutput_idx\n"; | ||
| 345 | if (!defined $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1]) { | ||
| 346 | warn "** POSTAG not defined for $TreeTaggerOutput_idx (" . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] . ")\n"; | ||
| 347 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] = "SYM"; | ||
| 348 | } else { | ||
| 349 | if ($TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] eq "SENT") { | ||
| 350 | $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1] = "NP"; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | my $MSFeatures = Lingua::Ogmios::Annotations::MorphosyntacticFeatures->new( | ||
| 355 | {'refid_word' => $word, | ||
| 356 | 'syntactic_category' => $TreeTaggerOutput[$TreeTaggerOutput_idx]->[1], | ||
| 357 | }); | ||
| 358 | $document->getAnnotations->addMorphosyntacticFeatures($MSFeatures); | ||
| 359 | |||
| 360 | |||
| 361 | my $Lemma = Lingua::Ogmios::Annotations::Lemma->new( | ||
| 362 | {'refid_word' => $word, | ||
| 363 | 'canonical_form' => $TreeTaggerOutput[$TreeTaggerOutput_idx]->[2], | ||
| 364 | }); | ||
| 365 | $document->getAnnotations->addLemma($Lemma); | ||
| 366 | |||
| 367 | # warn "===> " . $MSFeatures->syntactic_category . " : " . $Lemma->canonical_form . "\n"; | ||
| 368 | |||
| 369 | $TreeTaggerOutput_idx++; | ||
| 370 | |||
| 371 | $doc_idx += $word->getReferenceSize - 1; | ||
| 372 | $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 373 | |||
| 374 | } else { | ||
| 375 | # warn "here (1)\n"; | ||
| 376 | # if ((!($token->isSep)) && (($token->getContent !~ /[\x{2019}\x{2032}']/go) || | ||
| 377 | |||
| 378 | # ((defined $token->previous) && | ||
| 379 | |||
| 380 | # ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->previous->getId))))) { | ||
| 381 | # $TreeTaggerOutput_idx++; | ||
| 382 | # } | ||
| 383 | if (!($token->isSep)) { | ||
| 384 | if (($token->getContent !~ /[\x{2019}\x{2032}']/go) || (length($TreeTaggerOutput[$TreeTaggerOutput_idx]->[0]) == 1)) { | ||
| 385 | # warn "here (2) : $previously_end_sentence\n"; | ||
| 386 | $tmpStr .= $token->getContent; | ||
| 387 | # warn $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] . " / " . $tmpStr . ";(1)\n"; | ||
| 388 | if (length($TreeTaggerOutput[$TreeTaggerOutput_idx]->[0]) == length($tmpStr)) { | ||
| 389 | # warn "=> new treetagger line\n"; | ||
| 390 | $TreeTaggerOutput_idx++; | ||
| 391 | $tmpStr = ""; | ||
| 392 | } | ||
| 393 | } else { | ||
| 394 | # warn ">> " . $token->previous->getContent . ":: $previously_end_sentence\n"; | ||
| 395 | # if (($previously_end_sentence) || ((defined $token->previous) && | ||
| 396 | # ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->previous->getId)))) { | ||
| 397 | if ($previously_end_sentence) { | ||
| 398 | # warn ">>>> ===\n"; | ||
| 399 | $TreeTaggerOutput_idx++; | ||
| 400 | $previously_end_sentence = 0; | ||
| 401 | } | ||
| 402 | if ($tmpStr ne "") { | ||
| 403 | $tmpStr .= $token->getContent; | ||
| 404 | # warn $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] . " / " . $tmpStr . ";(2)\n"; | ||
| 405 | if (length($TreeTaggerOutput[$TreeTaggerOutput_idx]->[0]) == length($tmpStr)) { | ||
| 406 | # warn "=> new treetagger line\n"; | ||
| 407 | $TreeTaggerOutput_idx++; | ||
| 408 | $tmpStr = ""; | ||
| 409 | } | ||
| 410 | } | ||
| 411 | } | ||
| 412 | } | ||
| 413 | # warn $token->getContent . " (" . $token->getId . ") " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] . "\n"; | ||
| 414 | } | ||
| 415 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 416 | $previously_end_sentence = 1; | ||
| 417 | if ($token->isSymb) { | ||
| 418 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 419 | |||
| 420 | # } else { | ||
| 421 | $TreeTaggerOutput_idx++; | ||
| 422 | # warn $token->getContent . " (" . $token->getId . ") " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] . " 0 \n"; | ||
| 423 | } | ||
| 424 | } else { | ||
| 425 | $TreeTaggerOutput_idx++; | ||
| 426 | # warn $token->getContent . "(.) ( 000 ) " . $TreeTaggerOutput[$TreeTaggerOutput_idx]->[0] . "\n"; | ||
| 427 | } | ||
| 428 | } else { | ||
| 429 | $previously_end_sentence = 0; | ||
| 430 | # warn "here (3)\n"; | ||
| 431 | } | ||
| 432 | } | ||
| 433 | } | ||
| 434 | warn "[LOG] done\n"; | ||
| 435 | } | ||
| 436 | |||
| 437 | sub run { | ||
| 438 | my ($self, $documentSet) = @_; | ||
| 439 | |||
| 440 | my $line; | ||
| 441 | |||
| 442 | # Set variables according the the configuration | ||
| 443 | $self->_documentSet($documentSet); | ||
| 444 | |||
| 445 | if ($documentSet->[0]->getAnnotations->existsLemmaLevel) { | ||
| 446 | warn "lemmas exist in the first document\n"; | ||
| 447 | warn " Assuming that no lemma idenfication is required for the current document set\n"; | ||
| 448 | # return(0); | ||
| 449 | } else { | ||
| 450 | if ($documentSet->[0]->getAnnotations->existsMorphosyntacticFeaturesLevel) { | ||
| 451 | warn "MorphosyntacticFeaturres exist in the first document\n"; | ||
| 452 | warn " Assuming that no Part-of-Speech tagging is required for the current document set\n"; | ||
| 453 | # return(0); | ||
| 454 | } else { | ||
| 455 | warn "[LOG] POS tagger ... \n"; | ||
| 456 | $self->_inputTreeTagger; | ||
| 457 | |||
| 458 | my $command_line = $self->_processTreeTagger; | ||
| 459 | |||
| 460 | $self->_outputParsing; | ||
| 461 | # Put log information | ||
| 462 | my $information = { 'software_name' => 'Decision TreeTagger', | ||
| 463 | 'comments' => 'POS tagging\n', | ||
| 464 | 'command_line' => $command_line, | ||
| 465 | 'list_modified_level' => ['morphosyntactic_features_level', 'lemma_level'], | ||
| 466 | }; | ||
| 467 | |||
| 468 | $self->_log($information); | ||
| 469 | my $document; | ||
| 470 | foreach $document (@{$documentSet}) { | ||
| 471 | $document->getAnnotations->addLogProcessing( | ||
| 472 | Lingua::Ogmios::Annotations::LogProcessing->new( | ||
| 473 | { 'comments' => 'Found ' . $document->getAnnotations->getLemmaLevel->getSize . ' lemma\n', | ||
| 474 | } | ||
| 475 | ) | ||
| 476 | ); | ||
| 477 | $document->getAnnotations->addLogProcessing( | ||
| 478 | Lingua::Ogmios::Annotations::LogProcessing->new( | ||
| 479 | { 'comments' => 'Found ' . $document->getAnnotations->getMorphosyntacticFeaturesLevel->getSize . ' morphosyntactic features\n', | ||
| 480 | } | ||
| 481 | ) | ||
| 482 | ); | ||
| 483 | # basic check | ||
| 484 | if ($document->getAnnotations->getLemmaLevel->getSize != $document->getAnnotations->getWordLevel->getSize) { | ||
| 485 | warn "Document " . $document->getId . ": there is difference between word and lemma size\n"; | ||
| 486 | } | ||
| 487 | if ($document->getAnnotations->getMorphosyntacticFeaturesLevel->getSize != $document->getAnnotations->getWordLevel->getSize) { | ||
| 488 | warn "Document " . $document->getId . ": there is difference between word and morphosyntactic feature size\n"; | ||
| 489 | } | ||
| 490 | } | ||
| 491 | |||
| 492 | } | ||
| 493 | } | ||
| 494 | |||
| 495 | if (($self->_position eq "last") && ($self->_no_standard_output)) { | ||
| 496 | if (($self->_no_standard_output eq "IFPOSLM") || ($self->_no_standard_output == 1) || ($self->_no_standard_output == 2) || ($self->_no_standard_output == 3)) { | ||
| 497 | |||
| 498 | $self->_printTreeTaggerFormatOutput('stdout', 'LATIN1',0); | ||
| 499 | } | ||
| 500 | if ($self->_no_standard_output eq "IFPOSLMLINE") { | ||
| 501 | my $document; | ||
| 502 | my $word; | ||
| 503 | my $MS_features; | ||
| 504 | my $lemma; | ||
| 505 | my $sentence; | ||
| 506 | my $token_start; | ||
| 507 | my $token_end; | ||
| 508 | my $token; | ||
| 509 | |||
| 510 | my @word_set; | ||
| 511 | my @MS_features_set; | ||
| 512 | my @lemma_set; | ||
| 513 | |||
| 514 | foreach $document (@{$documentSet}) { | ||
| 515 | foreach $sentence (@{$document->getAnnotations->getSentenceLevel->getElements}) { | ||
| 516 | @word_set = (); | ||
| 517 | @MS_features_set = (); | ||
| 518 | @lemma_set = (); | ||
| 519 | $token_start = $sentence->refid_start_token; | ||
| 520 | $token = $token_start; | ||
| 521 | $token_end = $sentence->refid_end_token; | ||
| 522 | # do { | ||
| 523 | # if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 524 | # $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 525 | # $lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 526 | # $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 527 | # push @word_set, $word->getForm; | ||
| 528 | # push @MS_features_set, $MS_features->syntactic_category; | ||
| 529 | # push @lemma_set, $lemma->canonical_form; | ||
| 530 | # $token = $word->end_token; | ||
| 531 | # } | ||
| 532 | # if (!$token->equals($token_end)) { | ||
| 533 | # $token = $token->next; | ||
| 534 | # } | ||
| 535 | # } while (!$token->equals($token_end)); | ||
| 536 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 537 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 538 | $lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 539 | $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 540 | push @word_set, $word->getForm; | ||
| 541 | push @MS_features_set, $MS_features->syntactic_category; | ||
| 542 | push @lemma_set, $lemma->canonical_form; | ||
| 543 | $token = $word->end_token; | ||
| 544 | } | ||
| 545 | while (!$token->equals($token_end)) { | ||
| 546 | $token = $token->next; | ||
| 547 | # warn $token->getContent . "\n"; | ||
| 548 | if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 549 | # warn "add\n"; | ||
| 550 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 551 | $lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 552 | $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 553 | push @word_set, $word->getForm; | ||
| 554 | push @MS_features_set, $MS_features->syntactic_category; | ||
| 555 | push @lemma_set, $lemma->canonical_form; | ||
| 556 | $token = $word->end_token; | ||
| 557 | } else { | ||
| 558 | if (!$token->isSep) { | ||
| 559 | push @word_set, $token->getContent; | ||
| 560 | push @MS_features_set, "SYM"; | ||
| 561 | push @lemma_set, $token->getContent; | ||
| 562 | } | ||
| 563 | } | ||
| 564 | } | ||
| 565 | print join(" ", @word_set); | ||
| 566 | print "\t"; | ||
| 567 | print join(" ", @MS_features_set); | ||
| 568 | print "\t"; | ||
| 569 | print join(" ", @lemma_set); | ||
| 570 | print "\n"; | ||
| 571 | } | ||
| 572 | } | ||
| 573 | } | ||
| 574 | if ($self->_no_standard_output eq "VOCABULARY") { | ||
| 575 | $self->_printVocabulary; | ||
| 576 | } | ||
| 577 | if ($self->_no_standard_output eq "VOCABULARY_ALL") { | ||
| 578 | binmode(STDOUT, ":utf8"); | ||
| 579 | print "#####################\n"; | ||
| 580 | print "# FF\n"; | ||
| 581 | $self->_printVocabulary_FF; | ||
| 582 | print "#####################\n"; | ||
| 583 | print "# LM\n"; | ||
| 584 | $self->_printVocabulary_LM; | ||
| 585 | print "#####################\n"; | ||
| 586 | print "# TAGSET\n"; | ||
| 587 | $self->_printTagSet; | ||
| 588 | } | ||
| 589 | if ($self->_no_standard_output eq "TAGSET") { | ||
| 590 | $self->_printTagSet; | ||
| 591 | } | ||
| 592 | } | ||
| 593 | |||
| 594 | # } else { | ||
| 595 | # } | ||
| 596 | |||
| 597 | # die "You call the 'rum' method of the wrapper class base\n | ||
| 598 | # You should define a 'run' method for your wrapper\n"; | ||
| 599 | $self->getTimer->_printTimesInLine($self->_config->name); | ||
| 600 | warn "[LOG] done\n"; | ||
| 601 | } | ||
| 602 | |||
| 603 | 1; | ||
| 604 | |||
| 605 | __END__ |