« blib/lib/Lingua/Ogmios/Annotations/Word.pm
↑ Summary
blib/lib/Lingua/Ogmios/Annotations/DomainSpecificRelation.pm »
| 1 | package Lingua::Ogmios::NLPWrappers::TermExpansion; | ||
| 2 | |||
| 3 | |||
| 4 | our $VERSION='0.1'; | ||
| 5 | |||
| 6 | |||
| 7 | use Lingua::Ogmios::NLPWrappers::Wrapper; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | our @ISA = qw(Lingua::Ogmios::NLPWrappers::Wrapper); | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my ($class, $config, $tmpfile_prefix, $logfile, $position, $no_standard_output) = @_; | ||
| 16 | |||
| 17 | my $lang; | ||
| 18 | my $lang2; | ||
| 19 | my $resourcename; | ||
| 20 | |||
| 21 | warn "[LOG] Creating a wrapper of the TermExpansion\n"; | ||
| 22 | |||
| 23 | my $TermExpansion = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output); | ||
| 24 | |||
| 25 | foreach $lang (keys %{$TermExpansion->_config->configuration->{'RESOURCE'}}) { | ||
| 26 | if ($lang =~ /language=([\w]+)/io) { | ||
| 27 | $lang2 = $1; | ||
| 28 | foreach $resourcename (keys %{$TermExpansion->_config->configuration->{'RESOURCE'}->{$lang}}) { | ||
| 29 | warn "*** $resourcename\n"; | ||
| 30 | if ($resourcename eq "VOCABULARY") { | ||
| 31 | $TermExpansion->loadVocabulary($lang, $lang2, $resourcename); | ||
| 32 | } else { | ||
| 33 | $TermExpansion->addResourceSemRel($lang, $lang2, $resourcename); | ||
| 34 | } | ||
| 35 | # , $TermExpansion->_config->configuration->{'RESOURCE'}->{$lang}->{$resourcename}); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | if (defined $TermExpansion->_config->configuration->{'CONFIG'}) { | ||
| 42 | foreach $lang (keys %{$TermExpansion->_config->configuration->{'CONFIG'}}) { | ||
| 43 | if ($lang =~ /language=([\w]+)/io) { | ||
| 44 | $lang2 = $1; | ||
| 45 | |||
| 46 | $TermExpansion->_setOption($lang2, "TERMS", "TERMS", 0); | ||
| 47 | $TermExpansion->_setOption($lang2, "EXPAND_SYNONYMY", "expand_synonymy", 0); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | $TermExpansion->_input_filename($tmpfile_prefix . ".TermExpansion.in"); | ||
| 53 | $TermExpansion->_output_filename($tmpfile_prefix . ".TermExpansion.out"); | ||
| 54 | |||
| 55 | return($TermExpansion); | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | sub _processTermExpansion { | ||
| 60 | my ($self, $lang) = @_; | ||
| 61 | |||
| 62 | warn "[LOG] TermExpansion\n"; | ||
| 63 | |||
| 64 | my $expandedSents; | ||
| 65 | my @sents; | ||
| 66 | my $i; | ||
| 67 | my $semanticFeature; | ||
| 68 | my $token; | ||
| 69 | my $word; | ||
| 70 | my $term; | ||
| 71 | my $lemma; | ||
| 72 | my $postag; | ||
| 73 | my $document; | ||
| 74 | my $terminology; | ||
| 75 | my $neighbour; | ||
| 76 | my $j = 0; | ||
| 77 | my @selectedSynonyms; | ||
| 78 | |||
| 79 | $lang = $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 80 | |||
| 81 | # return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{TermExpansion_CMD} . " < " . $self->_input_filename . ">" . $self->_output_filename))); | ||
| 82 | |||
| 83 | foreach $document (@{$self->_documentSet}) { | ||
| 84 | $lang = $document->getAnnotations->getLanguage; | ||
| 85 | |||
| 86 | @sents = @{$document->getAnnotations->getSentenceLevel->getElements}; | ||
| 87 | for($i=0; $i < scalar(@sents); $i+=2) { | ||
| 88 | # warn "i (sent) = $i\n"; | ||
| 89 | # warn ">> " . $sents[$i]->getForm . "\n"; | ||
| 90 | $semanticFeature = $sents[$i]->next->getForm; | ||
| 91 | $token = $sents[$i]->refid_start_token; | ||
| 92 | |||
| 93 | # add initialisation of the expanded sentence | ||
| 94 | $expandedSents = { "units" => [], | ||
| 95 | "expanded_forms" => [], | ||
| 96 | "sent_forms" => [""], | ||
| 97 | 'semanticFeature' => $semanticFeature, | ||
| 98 | 'initial_form' => $sents[$i]->getForm, | ||
| 99 | }; | ||
| 100 | |||
| 101 | |||
| 102 | $j = 0; | ||
| 103 | do { | ||
| 104 | if (($self->{"TERMS"}->{$lang}) && | ||
| 105 | ($document->getAnnotations->getSemanticUnitLevel->existsElementByToken($token))) { | ||
| 106 | $term = $self->_getLargerTerm($document->getAnnotations->getSemanticUnitLevel->getElementByToken($token)); | ||
| 107 | warn $term->getForm . "\n"; | ||
| 108 | $lemma = $term->canonical_form; | ||
| 109 | push @{$expandedSents->{'units'}}, $term; | ||
| 110 | push @{$expandedSents->{'expanded_forms'}}, [$term->getForm]; | ||
| 111 | ######################################################################## | ||
| 112 | |||
| 113 | if ($self->{'expand_synonymy'}->{$lang}) { | ||
| 114 | # warn "j = $j\n"; | ||
| 115 | foreach $terminology (@{$self->{"Resources"}->{$lang}->{'SYNONYMY'}}) { | ||
| 116 | $self->_selectSynonyms($term->getForm, $terminology->{'synonymy'}, \@selectedSynonyms); | ||
| 117 | foreach $neighbour (@selectedSynonyms) { | ||
| 118 | # warn "---> $neighbour ($i)\n"; | ||
| 119 | push @{$expandedSents->{'expanded_forms'}->[$j]}, $neighbour; | ||
| 120 | } | ||
| 121 | $self->_selectSynonyms($lemma, $terminology->{'synonymy'}, \@selectedSynonyms); | ||
| 122 | foreach $neighbour (@selectedSynonyms) { | ||
| 123 | # warn "---> $neighbour ($i)\n"; | ||
| 124 | push @{$expandedSents->{'expanded_forms'}->[$j]}, $neighbour; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | $token = $term->end_token; | ||
| 129 | ######################################################################## | ||
| 130 | } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) { | ||
| 131 | $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0]; | ||
| 132 | $lemma = $word->getLemma($document)->canonical_form; | ||
| 133 | $postag = $word->getMorphoSyntacticFeatures($document)->syntactic_category; | ||
| 134 | push @{$expandedSents->{'units'}}, $word; | ||
| 135 | push @{$expandedSents->{'expanded_forms'}}, [$word->getForm]; | ||
| 136 | |||
| 137 | ######################################################################## | ||
| 138 | |||
| 139 | if ($self->{'expand_synonymy'}->{$lang}) { | ||
| 140 | # warn "j = $j\n"; | ||
| 141 | foreach $terminology (@{$self->{"Resources"}->{$lang}->{'SYNONYMY'}}) { | ||
| 142 | $self->_selectSynonyms($word->getForm, $terminology->{'synonymy'}, \@selectedSynonyms); | ||
| 143 | foreach $neighbour (@selectedSynonyms) { | ||
| 144 | # warn "---> $neighbour ($i)\n"; | ||
| 145 | if (exists $self->{"vocabulary"}->{$neighbour}) { | ||
| 146 | push @{$expandedSents->{'expanded_forms'}->[$j]}, $neighbour; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | $self->_selectSynonyms($lemma, $terminology->{'synonymy'}, \@selectedSynonyms); | ||
| 150 | foreach $neighbour (@selectedSynonyms) { | ||
| 151 | # warn "---> $neighbour ($i)\n"; | ||
| 152 | if (exists $self->{"vocabulary"}->{$neighbour}) { | ||
| 153 | push @{$expandedSents->{'expanded_forms'}->[$j]}, $neighbour; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | $token = $word->end_token; | ||
| 159 | ######################################################################## | ||
| 160 | } else { | ||
| 161 | # put in the sets | ||
| 162 | push @{$expandedSents->{'units'}}, $token; | ||
| 163 | push @{$expandedSents->{'expanded_forms'}}, [$token->getContent]; | ||
| 164 | } | ||
| 165 | $j++; | ||
| 166 | $token = $token->next; | ||
| 167 | # } | ||
| 168 | } while((defined $token) && (!$token->previous->equals($sents[$i]->refid_end_token))); | ||
| 169 | # Generate Sentence Form | ||
| 170 | $self->_sentenceFormGeneration($expandedSents); | ||
| 171 | $self->_printExpandedSentences($expandedSents); | ||
| 172 | } | ||
| 173 | |||
| 174 | } | ||
| 175 | warn "[LOG]\n"; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub _sentenceFormGeneration { | ||
| 179 | my ($self, $expandedSents) = @_; | ||
| 180 | |||
| 181 | warn "Generation of the Sentence Forms\n"; | ||
| 182 | my $j = 0; | ||
| 183 | my $i = 0; | ||
| 184 | |||
| 185 | # for($j=0;$j < scalar(@{$expandedSents});$j++) { | ||
| 186 | # warn "sentence $j\n"; | ||
| 187 | # warn ">> " . $expandedSents->{'initial_form'} . "\n"; | ||
| 188 | $i = 0; | ||
| 189 | # ->[$j] | ||
| 190 | $self->_recursiveGen($expandedSents, $i); | ||
| 191 | # } | ||
| 192 | } | ||
| 193 | |||
| 194 | sub _recursiveGen { | ||
| 195 | my ($self, $expandedSent, $i) = @_; | ||
| 196 | |||
| 197 | my @new_forms; | ||
| 198 | my $expandedForm; | ||
| 199 | my $sentForm; | ||
| 200 | |||
| 201 | # warn "i: $i\n"; | ||
| 202 | # warn "max: " . scalar(@{$expandedSent->{'expanded_forms'}}) . "\n"; | ||
| 203 | |||
| 204 | if ($i < scalar(@{$expandedSent->{'expanded_forms'}})) { | ||
| 205 | |||
| 206 | # warn "\tmax$i: " . scalar(@{$expandedSent->{'expanded_forms'}->[$i]}) . "\n"; | ||
| 207 | foreach $expandedForm (@{$expandedSent->{'expanded_forms'}->[$i]}) { | ||
| 208 | # warn "$expandedForm\n"; | ||
| 209 | foreach $sentForm (@{$expandedSent->{'sent_forms'}}) { | ||
| 210 | # warn "$sentForm\n"; | ||
| 211 | push @new_forms, $sentForm . $expandedForm; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | @{$expandedSent->{'sent_forms'}} = @new_forms; | ||
| 215 | # warn "==\n"; | ||
| 216 | # warn join("\n", @{$expandedSent->{'sent_forms'}}) . "\n"; | ||
| 217 | $self->_recursiveGen($expandedSent, $i+1); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | sub _printExpandedSentences { | ||
| 222 | my ($self, $expandedSents) = @_; | ||
| 223 | |||
| 224 | my $i; | ||
| 225 | my $j; | ||
| 226 | |||
| 227 | # for($i=0;$i < scalar(@{$expandedSents});$i++) { | ||
| 228 | # warn "sentence $j\n"; | ||
| 229 | # warn ">> " . $expandedSents->[$j]->{'initial_form'} . "\n"; | ||
| 230 | # $i = 0; | ||
| 231 | foreach($j=0;$j < scalar(@{$expandedSents->{'sent_forms'}}); $j++) { | ||
| 232 | print $expandedSents->{'sent_forms'}->[$j] . " : : " . $expandedSents->{'semanticFeature'} . " : \n"; | ||
| 233 | } | ||
| 234 | # } | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | sub _inputTermExpansion { | ||
| 239 | my ($self) = @_; | ||
| 240 | |||
| 241 | warn "[LOG] making TermExpansion input\n"; | ||
| 242 | |||
| 243 | warn "[LOG] done\n"; | ||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | sub _outputTermExpansion { | ||
| 248 | my ($self) = @_; | ||
| 249 | |||
| 250 | warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 251 | |||
| 252 | warn "[LOG] done\n"; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub run { | ||
| 256 | my ($self, $documentSet) = @_; | ||
| 257 | |||
| 258 | # Set variables according the the configuration | ||
| 259 | |||
| 260 | $self->_documentSet($documentSet); | ||
| 261 | |||
| 262 | warn "[LOG] " . $self->_config->comments . " ... \n"; | ||
| 263 | |||
| 264 | $self->_inputTermExpansion; | ||
| 265 | |||
| 266 | my $command_line = $self->_processTermExpansion; | ||
| 267 | |||
| 268 | # if ($self->_position eq "last") { | ||
| 269 | # # TODO | ||
| 270 | if (($self->_position eq "last") && ($self->_no_standard_output)) { | ||
| 271 | warn "print no standard output\n"; | ||
| 272 | } else { | ||
| 273 | $self->_outputTermExpansion; | ||
| 274 | } | ||
| 275 | # $self->_outputParsing; | ||
| 276 | |||
| 277 | |||
| 278 | # Put log information | ||
| 279 | |||
| 280 | my $information = { 'software_name' => $self->_config->name, | ||
| 281 | 'comments' => $self->_config->comments, | ||
| 282 | 'command_line' => $command_line, | ||
| 283 | 'list_modified_level' => [''], | ||
| 284 | }; | ||
| 285 | $self->_log($information); | ||
| 286 | |||
| 287 | |||
| 288 | # die "You call the 'rum' method of the wrapper class base\n | ||
| 289 | # You should define a 'run' method for your wrapper\n"; | ||
| 290 | warn "[LOG] done\n"; | ||
| 291 | } | ||
| 292 | |||
| 293 | sub loadVocabulary { | ||
| 294 | my ($self, $lang, $lang2, $type) = @_; | ||
| 295 | |||
| 296 | my $exceptionFilename; | ||
| 297 | if (ref($self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}) eq "ARRAY") { | ||
| 298 | foreach $exceptionFilename (@{$self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}}) { | ||
| 299 | $self->_loadException($exceptionFilename, $lang2,"vocabulary"); | ||
| 300 | } | ||
| 301 | } else { | ||
| 302 | $self->_loadException($self->_config->configuration->{'RESOURCE'}->{$lang}->{$type}, $lang2, "vocabulary"); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | 1; | ||
| 307 | |||
| 308 | __END__ |