« blib/lib/Lingua/Ogmios/Annotations/AnaphoraRelation.pm
↑ Summary
blib/lib/Lingua/Ogmios/Annotations/Lemma.pm »
| 1 | package Lingua::Ogmios::NLPWrappers::TermMeasures; | ||
| 2 | |||
| 3 | |||
| 4 | our $VERSION='0.1'; | ||
| 5 | |||
| 6 | |||
| 7 | use Lingua::Ogmios::NLPWrappers::Wrapper; | ||
| 8 | |||
| 9 | use Math::Trig; | ||
| 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 $lang; | ||
| 19 | my $lang2; | ||
| 20 | |||
| 21 | warn "[LOG] Creating a wrapper of the TermMeasures\n"; | ||
| 22 | |||
| 23 | my $TermMeasures = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output); | ||
| 24 | |||
| 25 | # if (defined $TermMeasures->_config->configuration->{'CONFIG'}) { | ||
| 26 | # foreach $lang (keys %{$TermMeasures->_config->configuration->{'CONFIG'}}) { | ||
| 27 | # if ($lang =~ /language=([\w]+)/io) { | ||
| 28 | # $lang2 = $1; | ||
| 29 | |||
| 30 | # if (defined $TermMeasures->_config->configuration->{'CONFIG'}->{$lang}->{"OPTION"}) { | ||
| 31 | # $TermMeasures->{'parameter'}->{$lang2} = $TermMeasures->_config->configuration->{'CONFIG'}->{$lang}->{"OPTION"}; | ||
| 32 | # } else { | ||
| 33 | # $TermMeasures->{'parameter'}->{"FR"} = "?.!"; | ||
| 34 | # $TermMeasures->{'parameter'}->{"EN"} = "?.!"; | ||
| 35 | # warn "*** OPTION is not set ***\n"; | ||
| 36 | # } | ||
| 37 | # } else { | ||
| 38 | # $TermMeasures->{'parameter'}->{"FR"} = "?.!"; | ||
| 39 | # $TermMeasures->{'parameter'}->{"EN"} = "?.!"; | ||
| 40 | # warn "*** OPTION is not set ***\n"; | ||
| 41 | # } | ||
| 42 | # } | ||
| 43 | # } else { | ||
| 44 | # $TermMeasures->{'parameter'}->{"FR"} = "?.!"; | ||
| 45 | # $TermMeasures->{'parameter'}->{"EN"} = "?.!"; | ||
| 46 | # } | ||
| 47 | |||
| 48 | |||
| 49 | $TermMeasures->_input_filename($tmpfile_prefix . ".TermMeasures.in"); | ||
| 50 | $TermMeasures->_output_filename($tmpfile_prefix . ".TermMeasures.out"); | ||
| 51 | |||
| 52 | return($TermMeasures); | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | sub _processTermMeasures { | ||
| 57 | my ($self, $lang) = @_; | ||
| 58 | |||
| 59 | warn "[LOG] TermMeasures\n"; | ||
| 60 | |||
| 61 | my $document; | ||
| 62 | my $term; | ||
| 63 | my $termCur; | ||
| 64 | my %term2CanFreq; | ||
| 65 | my %term2Freq; | ||
| 66 | my %term2Doc; | ||
| 67 | my %termDoc; | ||
| 68 | my %maxFreq; | ||
| 69 | # my %termDocCan; | ||
| 70 | my $termForm; | ||
| 71 | my $D; | ||
| 72 | |||
| 73 | $lang = $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 74 | |||
| 75 | # return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{TermMeasures_CMD} . " < " . $self->_input_filename . ">" . $self->_output_filename))); | ||
| 76 | |||
| 77 | foreach $document (@{$self->_documentSet}) { | ||
| 78 | # $termCur = undef; | ||
| 79 | $maxFreq{$document->getId} = 0; | ||
| 80 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElements}) { | ||
| 81 | # warn $term->getForm . "\n"; | ||
| 82 | if ($term->isTerm) { | ||
| 83 | # warn "\tOK\n"; | ||
| 84 | $self->_computeTermFreq($term, $document); | ||
| 85 | $self->_computeTermPositionCos($term, $document); | ||
| 86 | $self->_computeTermPositionDistMid($term, $document); | ||
| 87 | $termDoc{$term->getForm} = $term->weight('tf'); | ||
| 88 | if ($maxFreq{$document->getId} < $term->weight('tf')) { | ||
| 89 | $maxFreq{$document->getId} = $term->weight('tf'); | ||
| 90 | } | ||
| 91 | # $termDocCan{$self->getCanonicalForm($term)} = $term->weight('cantf', $document); | ||
| 92 | # $termCur = $term; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | foreach $termForm (keys %termDoc) { | ||
| 96 | $term2Doc{$termForm}++; | ||
| 97 | $term2Freq{$termForm} += $termDoc{$termForm}; | ||
| 98 | # $term2CanFreq{$self->getCanonicalForm($term)} += $termDocCan{$self->getCanonicalForm($term, $document)}; | ||
| 99 | } | ||
| 100 | |||
| 101 | # if (defined $termCur) { | ||
| 102 | # warn "$termCur\n"; | ||
| 103 | # } | ||
| 104 | } | ||
| 105 | $D = scalar(@{$self->_documentSet}); | ||
| 106 | foreach $document (@{$self->_documentSet}) { | ||
| 107 | $self->_computeTermCanFreq($document, \%term2CanFreq); | ||
| 108 | $self->_computeTermNormFreq($document, $maxFreq{$document->getId}); | ||
| 109 | } | ||
| 110 | foreach $document (@{$self->_documentSet}) { | ||
| 111 | $self->_computeFreqAndDocFreqAndTfIdf(\%term2Freq, \%term2Doc, $document, $D, \%term2CanFreq) ; | ||
| 112 | } | ||
| 113 | |||
| 114 | warn "[LOG]\n"; | ||
| 115 | } | ||
| 116 | |||
| 117 | # TODO | ||
| 118 | # canFreq -> TO CHECK | ||
| 119 | # NormFreq (freq / maxFreq) | ||
| 120 | # iLong (Freq / length(term) | ||
| 121 | |||
| 122 | # CValue* | ||
| 123 | # ilnc* | ||
| 124 | # iTer | ||
| 125 | |||
| 126 | |||
| 127 | sub _computeFreqAndDocFreqAndTfIdf { | ||
| 128 | my ($self, $term2Freq, $term2Doc, $document, $D, $termCan2canFreq) = @_; | ||
| 129 | |||
| 130 | my $freq; | ||
| 131 | my $term; | ||
| 132 | my $termForm; | ||
| 133 | my $ref; | ||
| 134 | |||
| 135 | foreach $termForm (keys %$term2Freq) { | ||
| 136 | $ref = $document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("form", $termForm); | ||
| 137 | if (defined $ref) { | ||
| 138 | foreach $term (@$ref) { | ||
| 139 | if (!defined $term->weight('Freq')) { | ||
| 140 | $term->weight('Freq', $term2Freq->{$termForm}); | ||
| 141 | } | ||
| 142 | if (!defined $term->weight('idf')) { | ||
| 143 | $term->weight('idf', $D / $term2Doc->{$termForm}); | ||
| 144 | } | ||
| 145 | if (!defined $term->weight('tf-idf')) { | ||
| 146 | $term->weight('tf-idf', $term->weight('Freq') * $term->weight('idf')); | ||
| 147 | } | ||
| 148 | if (!defined $term->weight('canFreq')) { | ||
| 149 | $term->weight('canFreq', $termCan2canFreq->{$self->_getCanonicalForm($term, $document)}); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | # sub _computeDocFreq { | ||
| 157 | # my ($self, $term, $document) = @_; | ||
| 158 | |||
| 159 | |||
| 160 | # } | ||
| 161 | |||
| 162 | sub _computeTermFreq { | ||
| 163 | my ($self, $term, $document) = @_; | ||
| 164 | |||
| 165 | my $freq; | ||
| 166 | my $ref; | ||
| 167 | |||
| 168 | if (!defined $term->weight('tf')) { | ||
| 169 | $ref = $document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("form", $term->getForm); | ||
| 170 | if (defined $ref) { | ||
| 171 | $freq = scalar(@{$ref}); | ||
| 172 | # warn $term->getForm . " : $freq\n"; | ||
| 173 | $term->weight('tf', $freq); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | sub _computeTermNormFreq { | ||
| 179 | my ($self, $document, $maxFreq) = @_; | ||
| 180 | |||
| 181 | my $term; | ||
| 182 | |||
| 183 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElements}) { | ||
| 184 | if ($term->isTerm) { | ||
| 185 | $term->weight('NormFreq', $term->weight('tf') / $maxFreq); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | sub _computeTermCanFreq { | ||
| 191 | my ($self, $document, $termCan2canFreq) = @_; | ||
| 192 | |||
| 193 | my %termCanForm; | ||
| 194 | my %termForm; | ||
| 195 | my $elmt; | ||
| 196 | my $term; | ||
| 197 | my $canForm; | ||
| 198 | |||
| 199 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElements}) { | ||
| 200 | if ($term->isTerm) { | ||
| 201 | $canForm = $self->_getCanonicalForm($term, $document); | ||
| 202 | # warn $term->getForm . " : $canForm\n"; | ||
| 203 | if (!defined $termCanForm{$canForm}) { | ||
| 204 | $termCanForm{$canForm} = []; | ||
| 205 | } | ||
| 206 | push @{$termCanForm{$canForm}}, $term; | ||
| 207 | # warn $term->getForm . " ($canForm)\n"; | ||
| 208 | if (!exists $termForm{$term->getForm . "_" . $canForm}) { | ||
| 209 | $termForm{$term->getForm . "_" . $canForm}++; | ||
| 210 | # warn $term->getForm . "\n"; | ||
| 211 | $termCan2canFreq->{$canForm} += $term->weight('tf'); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | } | ||
| 215 | # warn "====\n"; | ||
| 216 | foreach $canForm (keys %termCanForm) { | ||
| 217 | foreach $term (@{$termCanForm{$canForm}}) { | ||
| 218 | $term->weight('cantf', $termCan2canFreq->{$canForm}); | ||
| 219 | # warn $term->getForm . "($canForm) : " . $term->weight('cantf') . "\n"; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | sub _computeTermCanFreq1 { | ||
| 225 | my ($self, $term, $document) = @_; | ||
| 226 | |||
| 227 | my $freq; | ||
| 228 | my $ref; | ||
| 229 | |||
| 230 | if (!defined $term->weight('cantf')) { | ||
| 231 | $ref = $document->getAnnotations->getSemanticUnitLevel->getElementFromIndex("canonical_form", $term->getForm); | ||
| 232 | if (defined $ref) { | ||
| 233 | $freq = scalar(@{$ref}); | ||
| 234 | $term->weight('cantf', $freq); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | sub _computeTermPositionCos { | ||
| 240 | my ($self, $term, $document) = @_; | ||
| 241 | |||
| 242 | my $freq; | ||
| 243 | my $ref; | ||
| 244 | my $last_token_addr = $document->getAnnotations->getTokenLevel->getLastElement->getFrom; | ||
| 245 | |||
| 246 | if (!defined $term->weight('positionCos')) { | ||
| 247 | $term->weight('positionCos', abs(cos(deg2rad($term->start_token->getFrom / $last_token_addr * 180)))); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | sub _computeTermPositionDistMid { | ||
| 252 | my ($self, $term, $document) = @_; | ||
| 253 | |||
| 254 | my $freq; | ||
| 255 | my $ref; | ||
| 256 | my $last_token_addr = $document->getAnnotations->getTokenLevel->getLastElement->getFrom; | ||
| 257 | |||
| 258 | |||
| 259 | if (!defined $term->weight('positionDistMid')) { | ||
| 260 | # ($last_token_addr/2) - | ||
| 261 | $term->weight('positionDistMid', (abs($last_token_addr/2 - $term->start_token->getFrom))); | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | sub _inputTermMeasures { | ||
| 266 | my ($self) = @_; | ||
| 267 | |||
| 268 | warn "[LOG] making TermMeasures input\n"; | ||
| 269 | |||
| 270 | warn "[LOG] done\n"; | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | sub _outputTermMeasures { | ||
| 275 | my ($self) = @_; | ||
| 276 | |||
| 277 | warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 278 | |||
| 279 | warn "[LOG] done\n"; | ||
| 280 | } | ||
| 281 | |||
| 282 | sub run { | ||
| 283 | my ($self, $documentSet) = @_; | ||
| 284 | |||
| 285 | # Set variables according the the configuration | ||
| 286 | |||
| 287 | $self->_documentSet($documentSet); | ||
| 288 | |||
| 289 | warn "[LOG] " . $self->_config->comments . " ... \n"; | ||
| 290 | |||
| 291 | # $self->_inputTermMeasures; | ||
| 292 | |||
| 293 | my $command_line = $self->_processTermMeasures; | ||
| 294 | |||
| 295 | # if ($self->_position eq "last") { | ||
| 296 | # # TODO | ||
| 297 | if (($self->_position eq "last") && ($self->_no_standard_output)) { | ||
| 298 | warn "print no standard output\n"; | ||
| 299 | } else { | ||
| 300 | # $self->_outputTermMeasures; | ||
| 301 | } | ||
| 302 | # $self->_outputParsing; | ||
| 303 | |||
| 304 | |||
| 305 | # Put log information | ||
| 306 | |||
| 307 | my $information = { 'software_name' => $self->_config->name, | ||
| 308 | 'comments' => $self->_config->comments, | ||
| 309 | 'command_line' => $command_line, | ||
| 310 | 'list_modified_level' => [''], | ||
| 311 | }; | ||
| 312 | $self->_log($information); | ||
| 313 | |||
| 314 | |||
| 315 | # die "You call the 'rum' method of the wrapper class base\n | ||
| 316 | # You should define a 'run' method for your wrapper\n"; | ||
| 317 | warn "[LOG] done\n"; | ||
| 318 | } | ||
| 319 | |||
| 320 | |||
| 321 | 1; | ||
| 322 | |||
| 323 | __END__ |