« blib/lib/Lingua/Ogmios/NLPWrappers/TreeTagger.pm
↑ Summary
blib/lib/Lingua/Ogmios/NLPWrappers/UKWordSegmentizer.pm »
| 1 | package Lingua::Ogmios::NLPWrappers::LexicalSyntacticPatterns; | ||
| 2 | |||
| 3 | |||
| 4 | our $VERSION='0.1'; | ||
| 5 | |||
| 6 | use Data::Dumper; | ||
| 7 | |||
| 8 | use Lingua::Ogmios::NLPWrappers::Wrapper; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper); | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output) = @_; | ||
| 17 | |||
| 18 | my $resourcename; | ||
| 19 | my $lang; | ||
| 20 | my $lang2; | ||
| 21 | |||
| 22 | warn "[LOG] Creating a wrapper of the LexicalSyntacticPatterns\n"; | ||
| 23 | |||
| 24 | my $LexicalSyntacticPatterns = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output); | ||
| 25 | |||
| 26 | $LexicalSyntacticPatterns->_input_filename($tmpfile_prefix . ".LexicalSyntacticPatterns.in"); | ||
| 27 | $LexicalSyntacticPatterns->_output_filename($tmpfile_prefix . ".LexicalSyntacticPatterns.out"); | ||
| 28 | $LexicalSyntacticPatterns->_input_hash({'sentences' => undef}); | ||
| 29 | |||
| 30 | $LexicalSyntacticPatterns->_addedElements_array([]); | ||
| 31 | |||
| 32 | foreach $lang (keys %{$LexicalSyntacticPatterns->_config->configuration->{'RESOURCE'}}) { | ||
| 33 | if ($lang =~ /language=([\w]+)/io) { | ||
| 34 | $lang2 = $1; | ||
| 35 | # if (defined $LexicalSyntacticPatterns->_config->configuration->{'RESOURCE'}->{$lang}->{"PATTERNS"}) { | ||
| 36 | foreach $resourcename (keys %{$LexicalSyntacticPatterns->_config->configuration->{'RESOURCE'}->{$lang}}) { | ||
| 37 | warn "*** $resourcename\n"; | ||
| 38 | $LexicalSyntacticPatterns->addResource($lang2, $resourcename, $LexicalSyntacticPatterns->_config->configuration->{'RESOURCE'}->{$lang}->{$resourcename}); | ||
| 39 | } | ||
| 40 | |||
| 41 | # $LexicalSyntacticPatterns->{'PATTERNS'}->{$lang2} = $LexicalSyntacticPatterns->_config->configuration->{'RESOURCE'}->{$lang}->{"PATTERNS"}; | ||
| 42 | # } else { | ||
| 43 | # warn "*** SENTENCESEPARATORCHARLIST is not set ***\n"; | ||
| 44 | # } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | return($LexicalSyntacticPatterns); | ||
| 50 | |||
| 51 | } | ||
| 52 | |||
| 53 | sub _processLexicalSyntacticPatterns { | ||
| 54 | my ($self) = @_; | ||
| 55 | my $lang; | ||
| 56 | my $pattern; | ||
| 57 | my $sentence; | ||
| 58 | my $term1; | ||
| 59 | my $term2; | ||
| 60 | my @terms1; | ||
| 61 | my @terms2; | ||
| 62 | my $document; | ||
| 63 | my $relation; | ||
| 64 | |||
| 65 | warn "[LOG] LexicalSyntacticPatterns\n"; | ||
| 66 | |||
| 67 | $lang = $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 68 | warn "open " . $self->_input_filename . " (2)\n"; | ||
| 69 | |||
| 70 | open FILEINPUT, "<:utf8", $self->_input_filename or die "No such file " . $self->_input_filename; | ||
| 71 | |||
| 72 | # open FILEINPUT, "<:utf8", "/home/thierry/Recherche/Projets/2011AIR-REACH/Data/GuidesV2/ogmios.metis.30920.LexicalSyntacticPatterns.in" or die "No such file " . "/home/thierry/Recherche/Projets/2011AIR-REACH/Data/GuidesV2/ogmios.metis.30920.LexicalSyntacticPatterns.in"; | ||
| 73 | # open FILEINPUT, "<:utf8", "/export/home/limbio/hamon/Research/Projets/2011AIR-REACH/Data/GuidesV2/ogmios.metis.30920.LexicalSyntacticPatterns.in" or die "No such file " . "/export/home/limbio/hamon/Research/Projets/2011AIR-REACH/Data/GuidesV2/ogmios.metis.30920.LexicalSyntacticPatterns.in"; | ||
| 74 | |||
| 75 | # $lang = $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 76 | |||
| 77 | while ($sentence = <FILEINPUT>) { | ||
| 78 | chomp $sentence; | ||
| 79 | # warn "--> $sentence\n"; | ||
| 80 | foreach $pattern (@{$self->{"Resources"}->{$lang}->{"PATTERNS"}->[1]}) { | ||
| 81 | # warn $pattern->{'pattern'} . "\n"; | ||
| 82 | # foreach $sentence (@{$self->_input_hash->{'sentences'}}) { | ||
| 83 | if ($sentence =~ m!$pattern->{'pattern'}!) { | ||
| 84 | # warn "OK\n"; | ||
| 85 | # warn $pattern->{'element1'}->{'role'} . ": " . $+{$pattern->{'element1'}->{'ID'}} . "\n"; | ||
| 86 | # warn $pattern->{'element2'}->{'role'} . ": " . $+{$pattern->{'element2'}->{'ID'}} . "\n"; | ||
| 87 | if ($pattern->{'element1'}->{'type'} eq "list") { | ||
| 88 | ($document, @terms1) = $self->_extractTermsFromList($lang, $+{$pattern->{'element1'}->{'ID'}}); | ||
| 89 | } else { | ||
| 90 | ($term1, $document) = $self->_extractInfos($+{$pattern->{'element1'}->{'ID'}}); | ||
| 91 | @terms1 = ($term1); | ||
| 92 | } | ||
| 93 | if ($pattern->{'element2'}->{'type'} eq "list") { | ||
| 94 | ($document, @terms2) = $self->_extractTermsFromList($lang, $+{$pattern->{'element2'}->{'ID'}}); | ||
| 95 | } else { | ||
| 96 | ($term2, $document) = $self->_extractInfos($+{$pattern->{'element2'}->{'ID'}}); | ||
| 97 | @terms2 = ($term2); | ||
| 98 | } | ||
| 99 | foreach $term1 (@terms1) { | ||
| 100 | foreach $term2 (@terms2) { | ||
| 101 | $relation = $self->_addRelation($document, $pattern->{'relation'} . " (" . $pattern->{'element1'}->{'role'} . "-" . $pattern->{'element2'}->{'role'} . ")", [$term1, $term2]); | ||
| 102 | if (defined $relation) { | ||
| 103 | # warn "==>" . $relation->getId . " " . $pattern->{'relation'} . "\n"; | ||
| 104 | push @{$self->_addedElements_array}, [$relation, $document->getId]; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | close FILEINPUT; | ||
| 112 | |||
| 113 | warn "[LOG]\n"; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub _extractInfos { | ||
| 117 | my ($self, $string) = @_; | ||
| 118 | |||
| 119 | my ($IF, $POSTAG, $LM, $ID, $DOCID, $SEMTAG) = split m!/!, $string; | ||
| 120 | my $document; | ||
| 121 | my $word; | ||
| 122 | my $termUnit; | ||
| 123 | |||
| 124 | $document = $self->_documentSet->[$DOCID]; | ||
| 125 | if ($POSTAG eq "term") { | ||
| 126 | $termUnit = $document->getAnnotations->getSemanticUnitLevel->getElementById($ID); | ||
| 127 | } else { | ||
| 128 | # creation of term | ||
| 129 | $word = $document->getAnnotations->getWordLevel->getElementById($ID); | ||
| 130 | $termUnit = Lingua::Ogmios::Annotations::SemanticUnit->newTerm( | ||
| 131 | {'form' => $word->getForm, | ||
| 132 | 'refid_word' => $word, | ||
| 133 | }); | ||
| 134 | $document->getAnnotations->addSemanticUnit($termUnit); | ||
| 135 | |||
| 136 | } | ||
| 137 | return($termUnit, $document); | ||
| 138 | |||
| 139 | |||
| 140 | } | ||
| 141 | |||
| 142 | sub _extractTermsFromList { | ||
| 143 | my ($self, $lang, $list) = @_; | ||
| 144 | my $term_pattern = $self->{"Resources"}->{$lang}->{"PATTERNS"}->[2]; | ||
| 145 | my $term_string; | ||
| 146 | my $term; | ||
| 147 | my @terms; | ||
| 148 | my $document; | ||
| 149 | my $docid; | ||
| 150 | |||
| 151 | while($list =~ / ?($term_pattern)/gc) { | ||
| 152 | # warn "\t$1\n"; | ||
| 153 | $term_string = $1; | ||
| 154 | ($term, $document) = $self->_extractInfos($term_string); | ||
| 155 | push @terms, $term; | ||
| 156 | # $document = $self->_documentSet->[$docid] | ||
| 157 | # print "\t" . &cleanBrillOutput($1) . "\n"; | ||
| 158 | } | ||
| 159 | # warn "(0)$document\n"; | ||
| 160 | return($document, @terms); | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | sub _inputLexicalSyntacticPatterns { | ||
| 165 | my ($self) = @_; | ||
| 166 | |||
| 167 | $self->getTimer->startsLap("making input"); | ||
| 168 | warn "[LOG] making LexicalSyntacticPatterns input\n"; | ||
| 169 | |||
| 170 | $self->_input_hash->{'sentences'} = {}; | ||
| 171 | |||
| 172 | warn "open " . $self->_input_filename . "(1)\n"; | ||
| 173 | |||
| 174 | open FILEINPUT, ">:utf8", $self->_input_filename or die "No such file " . $self->_input_filename; | ||
| 175 | |||
| 176 | $self->_makeTaggedSentences($self->_input_hash->{'sentences'}, \*FILEINPUT, "/", " "); | ||
| 177 | |||
| 178 | close FILEINPUT; | ||
| 179 | # $self->getTimer->_printTimesBySteps; | ||
| 180 | |||
| 181 | warn "[LOG] done\n"; | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | sub _outputLexicalSyntacticPatterns { | ||
| 186 | my ($self) = @_; | ||
| 187 | |||
| 188 | warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 189 | |||
| 190 | warn "[LOG] done\n"; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub printRelations { | ||
| 194 | my ($self) = @_; | ||
| 195 | my $rel_doc; | ||
| 196 | my $docId; | ||
| 197 | my $relationId; | ||
| 198 | my $relation; | ||
| 199 | my $document; | ||
| 200 | my $term; | ||
| 201 | |||
| 202 | foreach $rel_doc (@{$self->_addedElements_array}) { | ||
| 203 | $relation = $rel_doc->[0]; | ||
| 204 | $docId = $rel_doc->[1]; | ||
| 205 | # warn "$docId -- $relation\n"; | ||
| 206 | $document = $self->getDocumentFromDocId($docId); | ||
| 207 | # $relation = $document->getAnnotations->getDomainSpecificRelationLevel->getElementById($relationId); | ||
| 208 | print "Doc $docId : " . $relation->getId ; | ||
| 209 | foreach $term (@{$relation->list_refid_semantic_unit}) { | ||
| 210 | print " : " . $term->getForm . " : "; | ||
| 211 | print $self->_getCanonicalForm($term, $document); | ||
| 212 | print " : "; | ||
| 213 | print $self->_getSyntacticCategory($term, $document); | ||
| 214 | # if (!defined $term->canonical_form) { | ||
| 215 | # } else { | ||
| 216 | # print $term->canonical_form ; | ||
| 217 | # } | ||
| 218 | print " : " . $term->getId ; | ||
| 219 | } | ||
| 220 | print " : " . $relation->domain_specific_relation_type . "\n"; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | sub run { | ||
| 225 | my ($self, $documentSet) = @_; | ||
| 226 | |||
| 227 | # Set variables according the the configuration | ||
| 228 | |||
| 229 | $self->_documentSet($documentSet); | ||
| 230 | |||
| 231 | if (0 && ($documentSet->[0]->getAnnotations->existsSemanticUnitLevel) && | ||
| 232 | ($documentSet->[0]->getAnnotations->getSemanticUnitLevel->existsElementFromIndex("type", "named_entity"))) { | ||
| 233 | # warn "Named entities exist in the first document\n"; | ||
| 234 | # warn " Assuming that no Named Entity idenfication is required for the current document set\n"; | ||
| 235 | # return(0); | ||
| 236 | } else { | ||
| 237 | warn "[LOG] " . $self->_config->comments . " ... \n"; | ||
| 238 | |||
| 239 | $self->_inputLexicalSyntacticPatterns; | ||
| 240 | my $command_line = $self->_processLexicalSyntacticPatterns; | ||
| 241 | # $self->I2B2NER; | ||
| 242 | |||
| 243 | # Put log information | ||
| 244 | my $information = { 'software_name' => $self->_config->name, | ||
| 245 | 'comments' => $self->_config->comments, | ||
| 246 | 'command_line' => "", | ||
| 247 | 'list_modified_level' => [""], | ||
| 248 | }; | ||
| 249 | $self->_log($information); | ||
| 250 | } | ||
| 251 | |||
| 252 | # if ($self->_position eq "last") { | ||
| 253 | # # TODO | ||
| 254 | if (($self->_position eq "last") && ($self->_no_standard_output)) { | ||
| 255 | warn "print no standard output\n"; | ||
| 256 | if ($self->_no_standard_output eq "TXT") { | ||
| 257 | $self->printRelations; | ||
| 258 | } | ||
| 259 | } else { | ||
| 260 | $self->_outputLexicalSyntacticPatterns; | ||
| 261 | } | ||
| 262 | # $self->_outputParsing; | ||
| 263 | |||
| 264 | # Put log information | ||
| 265 | |||
| 266 | # my $information = { 'software_name' => $self->_config->name, | ||
| 267 | # 'comments' => $self->_config->comments, | ||
| 268 | # 'command_line' => $command_line, | ||
| 269 | # 'list_modified_level' => [''], | ||
| 270 | # }; | ||
| 271 | # $self->_log($information); | ||
| 272 | |||
| 273 | # die "You call the 'rum' method of the wrapper class base\n | ||
| 274 | # You should define a 'run' method for your wrapper\n"; | ||
| 275 | $self->getTimer->_printTimes; | ||
| 276 | warn "[LOG] done\n"; | ||
| 277 | } | ||
| 278 | |||
| 279 | sub _printTerms1 { | ||
| 280 | my ($self) = @_; | ||
| 281 | |||
| 282 | my $document; | ||
| 283 | my $sentence; | ||
| 284 | my $token; | ||
| 285 | my $SemUnit; | ||
| 286 | my %TermsNSemF; | ||
| 287 | |||
| 288 | warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 289 | |||
| 290 | my $sentenceForm; | ||
| 291 | foreach $document (@{$self->_documentSet}) { | ||
| 292 | foreach $sentence (@{$document->getAnnotations->getSentenceLevel->getElements}) { | ||
| 293 | $sentenceForm = $sentence->getForm; | ||
| 294 | # warn "$sentenceForm\n"; | ||
| 295 | $sentenceForm =~ s/\n//; | ||
| 296 | %TermsNSemF = (); | ||
| 297 | $token = $sentence->refid_start_token; | ||
| 298 | while((defined $token) && ((!defined $token->previous) || (!$token->previous->equals($sentence->refid_end_token)))){ | ||
| 299 | if (scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) > 0) { | ||
| 300 | foreach $SemUnit (@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) { | ||
| 301 | if ($SemUnit->isNamedEntity) { | ||
| 302 | $TermsNSemF{$SemUnit->getForm . "/" . $SemUnit->NEtype}++; | ||
| 303 | } elsif (($SemUnit->isTerm) && ($SemUnit->weight("relevance"))) { | ||
| 304 | my $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $SemUnit->getId)->[0]; | ||
| 305 | if (defined $semf) { | ||
| 306 | $TermsNSemF{$SemUnit->getForm . "/" . $semf->first_node_first_semantic_category}++; | ||
| 307 | } else { | ||
| 308 | $TermsNSemF{$SemUnit->getForm . "/Unknown"}++; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | } | ||
| 312 | } | ||
| 313 | $token = $token->next; | ||
| 314 | } | ||
| 315 | print "$sentenceForm :"; | ||
| 316 | if (scalar(keys %TermsNSemF) > 0) { | ||
| 317 | print " " . join(", ", keys %TermsNSemF); | ||
| 318 | } | ||
| 319 | print "\n"; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | warn "[LOG] done\n"; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub _printTerms2 { | ||
| 326 | my $self = shift; | ||
| 327 | my $encoding = "UTF-8"; | ||
| 328 | # my $printDocId = shift; | ||
| 329 | |||
| 330 | my $document; | ||
| 331 | my $doc_idx; | ||
| 332 | my $token; | ||
| 333 | my $word; | ||
| 334 | my $lemma; | ||
| 335 | my $MS_features; | ||
| 336 | my $SemUnit; | ||
| 337 | my @corpus_in_t; | ||
| 338 | my $canonical_form; | ||
| 339 | |||
| 340 | warn "[LOG] printing TreeTagger Like Output\n"; | ||
| 341 | |||
| 342 | foreach $document (@{$self->_documentSet}) { | ||
| 343 | |||
| 344 | # my @tmp; | ||
| 345 | # if ((!defined $printDocId) || ($printDocId)) { | ||
| 346 | # @tmp = ($document->getId, "DOCUMENT", $document->getId); | ||
| 347 | # push @corpus_in_t, \@tmp; | ||
| 348 | # } | ||
| 349 | $token = $document->getAnnotations->getTokenLevel->getElements->[0]; | ||
| 350 | while(defined ($token)) { | ||
| 351 | # for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) { | ||
| 352 | # warn "doc_idx: $doc_idx\n"; | ||
| 353 | # warn $token->getId . "\n"; | ||
| 354 | # $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 355 | if (scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) > 0) { | ||
| 356 | $SemUnit = $self->_getLargerTerm($document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)); | ||
| 357 | if ($SemUnit->isNamedEntity) { | ||
| 358 | # my @tmp = ($SemUnit->getForm, $SemUnit->getId, "term", ""); | ||
| 359 | # push @corpus_in_t, \@tmp; | ||
| 360 | |||
| 361 | if (!defined $SemUnit->canonical_form) { | ||
| 362 | $canonical_form = ""; | ||
| 363 | } else { | ||
| 364 | $canonical_form = $SemUnit->canonical_form; | ||
| 365 | } | ||
| 366 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 367 | "ID" => $SemUnit->getId, | ||
| 368 | "POSTAG" => "named_entity", | ||
| 369 | "LM" => $canonical_form, | ||
| 370 | "SEMTAG" => $SemUnit->NEtype}); | ||
| 371 | |||
| 372 | # } elsif (($SemUnit->isTerm) && ($SemUnit->weight("relevance"))) { | ||
| 373 | } elsif ($SemUnit->isTerm) { | ||
| 374 | my $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $SemUnit->getId)->[0]; | ||
| 375 | if (defined $semf) { | ||
| 376 | # my @tmp = ($SemUnit->getForm, $SemUnit->getId, "term", $semf->first_node_first_semantic_category); | ||
| 377 | # push @corpus_in_t, \@tmp; | ||
| 378 | if (!defined $SemUnit->canonical_form) { | ||
| 379 | $canonical_form = ""; | ||
| 380 | } else { | ||
| 381 | $canonical_form = $SemUnit->canonical_form; | ||
| 382 | } | ||
| 383 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 384 | "ID" => $SemUnit->getId, | ||
| 385 | "POSTAG" => "term", | ||
| 386 | "LM" => $canonical_form, | ||
| 387 | "SEMTAG" => $semf->first_node_first_semantic_category}); | ||
| 388 | } else { | ||
| 389 | # my @tmp = ($SemUnit->getForm, $SemUnit->getId, "term", ""); | ||
| 390 | # push @corpus_in_t, \@tmp; | ||
| 391 | if (!defined $SemUnit->canonical_form) { | ||
| 392 | $canonical_form = ""; | ||
| 393 | } else { | ||
| 394 | $canonical_form = $SemUnit->canonical_form; | ||
| 395 | } | ||
| 396 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 397 | "ID" => $SemUnit->getId, | ||
| 398 | "POSTAG" => "term", | ||
| 399 | "LM" => $canonical_form, | ||
| 400 | "SEMTAG" => ""}); | ||
| 401 | } | ||
| 402 | } | ||
| 403 | $token = $SemUnit->end_token; | ||
| 404 | } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 405 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 406 | $lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 407 | $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 408 | my $wordform = $word->getForm; | ||
| 409 | $wordform =~ s/[\t\n]/ /gos; | ||
| 410 | $wordform =~ s/ +/ /gos; | ||
| 411 | |||
| 412 | # my @tmp = ($wordform, $word->getId, $MS_features->syntactic_category, $lemma->canonical_form); | ||
| 413 | # push @corpus_in_t, \@tmp; | ||
| 414 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $wordform, | ||
| 415 | "ID" => $word->getId, | ||
| 416 | "POSTAG" => $MS_features->syntactic_category, | ||
| 417 | "LM" => $lemma->canonical_form, | ||
| 418 | "SEMTAG" => ""}); | ||
| 419 | |||
| 420 | # $doc_idx += $word->getReferenceSize - 1; | ||
| 421 | # $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx]; | ||
| 422 | $token = $word->end_token; | ||
| 423 | |||
| 424 | } else { | ||
| 425 | if (!($token->isSep)) { | ||
| 426 | # my @tmp = ($token->getContent, $token->getId, $token->getContent, $token->getContent); | ||
| 427 | # push @corpus_in_t, \@tmp; | ||
| 428 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $token->getContent, | ||
| 429 | "ID" => $token->getId, | ||
| 430 | "POSTAG" => $token->getContent, | ||
| 431 | "LM" => $token->getContent, | ||
| 432 | "SEMTAG" => ""}); | ||
| 433 | } | ||
| 434 | } | ||
| 435 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 436 | if ($token->isSymb) { | ||
| 437 | if (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) { | ||
| 438 | # my @tmp = ($corpus_in_t[$#corpus_in_t]->[0] , $corpus_in_t[$#corpus_in_t]->[1], "SENT", $corpus_in_t[$#corpus_in_t]->[0]); | ||
| 439 | # $corpus_in_t[$#corpus_in_t] = \@tmp; | ||
| 440 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $corpus_in_t[$#corpus_in_t]->[0], | ||
| 441 | "ID" => $corpus_in_t[$#corpus_in_t]->[1], | ||
| 442 | "POSTAG" => "SENT", | ||
| 443 | "LM" => $corpus_in_t[$#corpus_in_t]->[0], | ||
| 444 | "SEMTAG" => ""}); | ||
| 445 | } else { | ||
| 446 | # my @tmp = ($token->getContent, $token->getId, "SENT", $token->getContent); | ||
| 447 | # push @corpus_in_t, \@tmp; | ||
| 448 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $token->getContent, | ||
| 449 | "ID" => $token->getId, | ||
| 450 | "POSTAG" => "SENT", | ||
| 451 | "LM" => $token->getContent, | ||
| 452 | "SEMTAG" => ""}); | ||
| 453 | } | ||
| 454 | } else { | ||
| 455 | my @tmp = (".", $token->getId, "SENT", "."); | ||
| 456 | push @corpus_in_t, \@tmp; | ||
| 457 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => ".", | ||
| 458 | "ID" => $token->getId, | ||
| 459 | "POSTAG" => "SENT", | ||
| 460 | "LM" => ".", | ||
| 461 | "SEMTAG" => ""}); | ||
| 462 | } | ||
| 463 | } | ||
| 464 | $token = $token->next; | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | # warn "\tOpenning " . $filename . "\n"; | ||
| 469 | # if ($filename ne "stdout") { | ||
| 470 | # open $fh_out, ">". $filename or die "can't open " . $filename . "\n"; | ||
| 471 | # } else { | ||
| 472 | # $fh_out = \*STDOUT; | ||
| 473 | # } | ||
| 474 | |||
| 475 | my $word_ref; | ||
| 476 | foreach $word_ref (@corpus_in_t) { | ||
| 477 | |||
| 478 | # if ($word_ref->[0] =~ s/ /_/go) { | ||
| 479 | # $word_ref->[3] =~ s/ /_/go; | ||
| 480 | # } | ||
| 481 | # Encode::encode("iso-8859-1", join("\n",@corpus_in_t), Encode::FB_DEFAULT); | ||
| 482 | if ((!defined $encoding) || (uc($encoding) eq "UTF-8")) { | ||
| 483 | print Encode::encode("UTF-8", join("/",@$word_ref)); # . "\n"; | ||
| 484 | } else { | ||
| 485 | if ((defined $encoding) && (uc($encoding) eq "LATIN1")) { | ||
| 486 | # print FILE_IN Encode::encode("iso-8859-1", join("\t",@$word_ref), Encode::FB_DEFAULT) . "\n"; | ||
| 487 | print Encode::encode("iso-8859-1", join("/",@$word_ref)); # . "\n"; | ||
| 488 | } else { | ||
| 489 | warn "[WRAPPER LOG] Unknown enconding charset\n"; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | print "/"; | ||
| 493 | if ($word_ref->[1] eq "SENT") { | ||
| 494 | print "\n"; | ||
| 495 | } else { | ||
| 496 | print " "; | ||
| 497 | } | ||
| 498 | } | ||
| 499 | |||
| 500 | # close $fh_out; | ||
| 501 | warn "[LOG] done\n"; | ||
| 502 | } | ||
| 503 | |||
| 504 | sub _printTerms { | ||
| 505 | my $self = shift; | ||
| 506 | my $encoding = "UTF-8"; | ||
| 507 | |||
| 508 | my @sentences; | ||
| 509 | |||
| 510 | warn "[LOG] printing TreeTagger Like Output\n"; | ||
| 511 | |||
| 512 | # $self->_makeTaggedSentences(\@sentences); | ||
| 513 | |||
| 514 | print join("\n", @{$self->_input_hash->{'sentences'}}) . "/"; | ||
| 515 | print "\n"; | ||
| 516 | warn "[LOG] done\n"; | ||
| 517 | } | ||
| 518 | |||
| 519 | sub _makeTaggedSentences1 { | ||
| 520 | my $self = shift; | ||
| 521 | my $encoding = "UTF-8"; | ||
| 522 | my $sentences = shift; | ||
| 523 | # my $printDocId = shift; | ||
| 524 | |||
| 525 | my $document; | ||
| 526 | my $doc_idx; | ||
| 527 | my $token; | ||
| 528 | my $word; | ||
| 529 | my $lemma; | ||
| 530 | my $MS_features; | ||
| 531 | my $SemUnit; | ||
| 532 | my @corpus_in_t; | ||
| 533 | my $canonical_form; | ||
| 534 | my $taggedSentence; | ||
| 535 | my $i; | ||
| 536 | |||
| 537 | warn "[LOG] printing TreeTagger Like Output\n"; | ||
| 538 | |||
| 539 | for ($i = 0; $i < scalar(@{$self->_documentSet}); $i++) { | ||
| 540 | $document = $self->_documentSet->[$i]; | ||
| 541 | $token = $document->getAnnotations->getTokenLevel->getElements->[0]; | ||
| 542 | while(defined ($token)) { | ||
| 543 | if (scalar(@{$document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)}) > 0) { | ||
| 544 | $SemUnit = $self->_getLargerTerm($document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)); | ||
| 545 | if ($SemUnit->isNamedEntity) { | ||
| 546 | if (!defined $SemUnit->canonical_form) { | ||
| 547 | $canonical_form = ""; | ||
| 548 | } else { | ||
| 549 | $canonical_form = $SemUnit->canonical_form; | ||
| 550 | } | ||
| 551 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 552 | "ID" => $SemUnit->getId, | ||
| 553 | "POSTAG" => "named_entity", | ||
| 554 | "LM" => $canonical_form, | ||
| 555 | "SEMTAG" => $SemUnit->NEtype, | ||
| 556 | "DOCID" => $i, | ||
| 557 | }); | ||
| 558 | |||
| 559 | } elsif ($SemUnit->isTerm) { | ||
| 560 | my $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $SemUnit->getId)->[0]; | ||
| 561 | if (defined $semf) { | ||
| 562 | if (!defined $SemUnit->canonical_form) { | ||
| 563 | $canonical_form = ""; | ||
| 564 | } else { | ||
| 565 | $canonical_form = $SemUnit->canonical_form; | ||
| 566 | } | ||
| 567 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 568 | "ID" => $SemUnit->getId, | ||
| 569 | "POSTAG" => "term", | ||
| 570 | "LM" => $canonical_form, | ||
| 571 | "SEMTAG" => $semf->first_node_first_semantic_category, | ||
| 572 | "DOCID" => $i, | ||
| 573 | }); | ||
| 574 | } else { | ||
| 575 | if (!defined $SemUnit->canonical_form) { | ||
| 576 | $canonical_form = ""; | ||
| 577 | } else { | ||
| 578 | $canonical_form = $SemUnit->canonical_form; | ||
| 579 | } | ||
| 580 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $SemUnit->getForm, | ||
| 581 | "ID" => $SemUnit->getId, | ||
| 582 | "POSTAG" => "term", | ||
| 583 | "LM" => $canonical_form, | ||
| 584 | "SEMTAG" => "", | ||
| 585 | "DOCID" => $i, | ||
| 586 | }); | ||
| 587 | } | ||
| 588 | } | ||
| 589 | $token = $SemUnit->end_token; | ||
| 590 | } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 591 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 592 | $lemma = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 593 | $MS_features = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $word->getId)->[0]; | ||
| 594 | my $wordform = $word->getForm; | ||
| 595 | $wordform =~ s/[\t\n]/ /gos; | ||
| 596 | $wordform =~ s/ +/ /gos; | ||
| 597 | |||
| 598 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $wordform, | ||
| 599 | "ID" => $word->getId, | ||
| 600 | "POSTAG" => $MS_features->syntactic_category, | ||
| 601 | "LM" => $lemma->canonical_form, | ||
| 602 | "SEMTAG" => "", | ||
| 603 | "DOCID" => $i, | ||
| 604 | }); | ||
| 605 | |||
| 606 | $token = $word->end_token; | ||
| 607 | |||
| 608 | } else { | ||
| 609 | if (!($token->isSep)) { | ||
| 610 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $token->getContent, | ||
| 611 | "ID" => $token->getId, | ||
| 612 | "POSTAG" => $token->getContent, | ||
| 613 | "LM" => $token->getContent, | ||
| 614 | "SEMTAG" => "", | ||
| 615 | "DOCID" => $i, | ||
| 616 | }); | ||
| 617 | } | ||
| 618 | } | ||
| 619 | if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) { | ||
| 620 | if ($token->isSymb) { | ||
| 621 | if (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) { | ||
| 622 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $corpus_in_t[$#corpus_in_t]->[0], | ||
| 623 | "ID" => $corpus_in_t[$#corpus_in_t]->[1], | ||
| 624 | "POSTAG" => "SENT", | ||
| 625 | "LM" => $corpus_in_t[$#corpus_in_t]->[0], | ||
| 626 | "SEMTAG" => "", | ||
| 627 | "DOCID" => $i, | ||
| 628 | }); | ||
| 629 | } else { | ||
| 630 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => $token->getContent, | ||
| 631 | "ID" => $token->getId, | ||
| 632 | "POSTAG" => "SENT", | ||
| 633 | "LM" => $token->getContent, | ||
| 634 | "SEMTAG" => "", | ||
| 635 | "DOCID" => $i, | ||
| 636 | }); | ||
| 637 | } | ||
| 638 | } else { | ||
| 639 | my @tmp = (".", $token->getId, "SENT", "."); | ||
| 640 | push @corpus_in_t, \@tmp; | ||
| 641 | $self->_addInfosUnit(\@corpus_in_t, {"IF" => ".", | ||
| 642 | "ID" => $token->getId, | ||
| 643 | "POSTAG" => "SENT", | ||
| 644 | "LM" => ".", | ||
| 645 | "SEMTAG" => "", | ||
| 646 | "DOCID" => $i, | ||
| 647 | }); | ||
| 648 | } | ||
| 649 | } | ||
| 650 | $token = $token->next; | ||
| 651 | } | ||
| 652 | } | ||
| 653 | |||
| 654 | warn "open " . $self->_input_filename . "\n"; | ||
| 655 | open FILEINPUT, ">:utf8", $self->_input_filename or die "No such file " . $self->_input_filename; | ||
| 656 | |||
| 657 | |||
| 658 | $taggedSentence = ""; | ||
| 659 | my $word_ref; | ||
| 660 | foreach $word_ref (@corpus_in_t) { | ||
| 661 | |||
| 662 | if ((!defined $encoding) || (uc($encoding) eq "UTF-8")) { | ||
| 663 | |||
| 664 | $taggedSentence .= Encode::encode("UTF-8", join("/",@$word_ref)); # . "\n"; | ||
| 665 | } else { | ||
| 666 | if ((defined $encoding) && (uc($encoding) eq "LATIN1")) { | ||
| 667 | $taggedSentence .= Encode::encode("iso-8859-1", join("/",@$word_ref)); # . "\n"; | ||
| 668 | } else { | ||
| 669 | warn "[WRAPPER LOG] Unknown enconding charset\n"; | ||
| 670 | } | ||
| 671 | } | ||
| 672 | $taggedSentence .= "/"; | ||
| 673 | if ($word_ref->[1] eq "SENT") { | ||
| 674 | # push @$sentences, $taggedSentence; | ||
| 675 | print FILEINPUT "$taggedSentence\n";; | ||
| 676 | $taggedSentence = ""; | ||
| 677 | } else { | ||
| 678 | $taggedSentence .= " "; | ||
| 679 | } | ||
| 680 | } | ||
| 681 | close FILEINPUT; | ||
| 682 | warn "[LOG] done\n"; | ||
| 683 | } | ||
| 684 | |||
| 685 | |||
| 686 | |||
| 687 | sub addResource { | ||
| 688 | my ($self, $lang, $type, $name) = @_; | ||
| 689 | |||
| 690 | warn "Adding " . $name . "\n"; | ||
| 691 | |||
| 692 | # $self->{"Resources"}->{$type} = $name; | ||
| 693 | |||
| 694 | # ($frequenciesRE, $FreqType, $I2B2Type) | ||
| 695 | # my @tmp = ($name); | ||
| 696 | $self->{"Resources"}->{$lang}->{$type} = [$name]; | ||
| 697 | $self->loadResource($lang, $name, $type); | ||
| 698 | |||
| 699 | # $self->{"NEtypes"}->{$tmp[2]} = $type; | ||
| 700 | # warn $self->{"Resources"}->{$type}->[3] . "\n"; | ||
| 701 | warn "Done\n"; | ||
| 702 | } | ||
| 703 | |||
| 704 | |||
| 705 | |||
| 706 | sub loadResource { | ||
| 707 | |||
| 708 | my ($self, $lang, $name, $type) = @_; | ||
| 709 | |||
| 710 | my %resource; | ||
| 711 | my $line; | ||
| 712 | |||
| 713 | warn "Loadding " . $name . "\n"; | ||
| 714 | |||
| 715 | my $cg = new Config::General('-ConfigFile' => $name, | ||
| 716 | '-InterPolateVars' => 1, | ||
| 717 | '-InterPolateEnv' => 1, | ||
| 718 | '-CComments' => 0 | ||
| 719 | ); | ||
| 720 | |||
| 721 | %resource = $cg->getall; | ||
| 722 | |||
| 723 | # warn ref($resource{'pattern'}) . "\n"; | ||
| 724 | |||
| 725 | # warn Dumper($resource{"pattern"}) . "\n"; | ||
| 726 | |||
| 727 | my @patterns; | ||
| 728 | |||
| 729 | if (ref($resource{'pattern'}) eq "ARRAY") { | ||
| 730 | push @patterns, @{$resource{'pattern'}}; | ||
| 731 | } | ||
| 732 | if (ref($resource{'pattern'}) eq "HASH") { | ||
| 733 | push @patterns, $resource{'pattern'}; | ||
| 734 | } | ||
| 735 | |||
| 736 | warn "Done\n"; | ||
| 737 | |||
| 738 | push @{$self->{"Resources"}->{$lang}->{$type}}, \@patterns, $resource{'term'}, $resource{'termList'}, ; #, lc($resource{'TYPE'}), $resource{'I2B2Type'}); | ||
| 739 | |||
| 740 | # return(\@RE, $resource{'TYPE'}, $resource{'I2B2Type'}); | ||
| 741 | } | ||
| 742 | |||
| 743 | |||
| 744 | |||
| 745 | 1; | ||
| 746 | |||
| 747 | |||
| 748 | __END__ |