| 1 | package Lingua::Ogmios::NLPWrappers::MorphoSemanticTagging; | ||
| 2 | |||
| 3 | # Authors: Amandine Périnet, Natalia Grabar, Thierry Hamon | ||
| 4 | |||
| 5 | our $VERSION='0.1'; | ||
| 6 | |||
| 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 | warn "[LOG] Creating a wrapper of the TreeTagger\n"; | ||
| 19 | |||
| 20 | my $MorphoSemanticTagging = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output); | ||
| 21 | |||
| 22 | $MorphoSemanticTagging->_input_filename($tmpfile_prefix . ".MorphoSemanticTagging.in"); | ||
| 23 | $MorphoSemanticTagging->_output_filename($tmpfile_prefix . ".MorphoSemanticTagging.out"); | ||
| 24 | |||
| 25 | return($MorphoSemanticTagging); | ||
| 26 | |||
| 27 | } | ||
| 28 | |||
| 29 | sub _inputMorphoSemanticTagging { | ||
| 30 | my ($self) = @_; | ||
| 31 | |||
| 32 | warn "[LOG] making Decision MorphoSemanticTagging input\n"; | ||
| 33 | |||
| 34 | my @fields = ("WEIGHTS", "SEMANTICTYPES", "WORD_FIELDS"); | ||
| 35 | |||
| 36 | $self->_loadRCFile($self->_config->configuration->{'CONFIG'}->{"language=EN"}->{"CONFIGFILE"}, \@fields); | ||
| 37 | |||
| 38 | $self->LoadMorphoSemanticMarkResource("SEMANTICTYPES"); | ||
| 39 | |||
| 40 | warn "[LOG] done\n"; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub determineSemanticType4Word { | ||
| 44 | my ($affixList, $term, $word, $acroType) = @_; | ||
| 45 | |||
| 46 | my $semanticType; | ||
| 47 | my $affix; | ||
| 48 | |||
| 49 | my @semanticTypes; | ||
| 50 | |||
| 51 | foreach $affix (keys %$affixList) { | ||
| 52 | if ((($affixList->{$affix} eq "m") && ($word =~ /$affix/)) || | ||
| 53 | (($affixList->{$affix} eq "m-s") && ($word =~ /$affix$/)) || | ||
| 54 | (($affixList->{$affix} eq "m-p") && ($word =~ /^$affix/))) { | ||
| 55 | $semanticType = $acroType;# $affixList->{$affix}; | ||
| 56 | push @semanticTypes, {"semanticType" => $semanticType, | ||
| 57 | "originType" => "morphological", | ||
| 58 | "origin" => $affixList->{$affix}, # . " / $affix", | ||
| 59 | }; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | return(@semanticTypes); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub addSemanticType { | ||
| 66 | my ($self, $semanticTypeStruct, $semanticTypes) = @_; | ||
| 67 | |||
| 68 | # semanticTypeStruct => (semanticType, origin, originType, nboccurrences) | ||
| 69 | # semanticType => @origins,@ originType, @nboccurrences) | ||
| 70 | my $semanticType = $semanticTypeStruct->{"semanticType"}; | ||
| 71 | |||
| 72 | if (!defined $semanticTypes->{$semanticType}->{"originType"}) { | ||
| 73 | my @tmp; | ||
| 74 | $semanticTypes->{$semanticType}->{"originType"} = \@tmp; | ||
| 75 | } | ||
| 76 | push @{$semanticTypes->{$semanticType}->{"originType"}}, $semanticTypeStruct->{"originType"}; | ||
| 77 | $semanticTypes->{$semanticType}->{$semanticTypeStruct->{"originType"}}++; | ||
| 78 | |||
| 79 | if (!defined $semanticTypes->{$semanticType}->{"origin"}) { | ||
| 80 | my @tmp; | ||
| 81 | $semanticTypes->{$semanticType}->{"origin"} = \@tmp; | ||
| 82 | } | ||
| 83 | # warn "semanticType: " . $semanticType . "\n"; | ||
| 84 | # warn "OriginType: " . $semanticTypeStruct->{"originType"} . "\n"; | ||
| 85 | # warn "Origin:: " . $semanticTypeStruct->{"origin"} . "\n"; | ||
| 86 | push @{$semanticTypes->{$semanticType}->{"origin"}}, $semanticTypeStruct->{"origin"}; | ||
| 87 | $semanticTypes->{$semanticType}->{$semanticTypeStruct->{"origin"}}++; | ||
| 88 | |||
| 89 | $semanticTypes->{$semanticType}->{"nboccurrences"}++; | ||
| 90 | |||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | sub selectSemanticType { | ||
| 95 | my ($self, $semanticTypes, $term) = @_; | ||
| 96 | |||
| 97 | my @concurrentSemanticTypes = keys %$semanticTypes; | ||
| 98 | |||
| 99 | my $semanticType; | ||
| 100 | my @a; | ||
| 101 | my @r; | ||
| 102 | my $scores_hash; | ||
| 103 | |||
| 104 | if (scalar(@concurrentSemanticTypes) == 0) { | ||
| 105 | $semanticType = undef; | ||
| 106 | } elsif (scalar(@concurrentSemanticTypes) == 1) { | ||
| 107 | $semanticType = $concurrentSemanticTypes[0]; | ||
| 108 | } else { | ||
| 109 | @r = sort { $semanticTypes->{$a}->{"nboccurrences"} <=> $semanticTypes->{$a}->{"nboccurrences"}} @concurrentSemanticTypes; | ||
| 110 | if ($semanticTypes->{$r[0]}->{"nboccurrences"} != $semanticTypes->{$r[$#r]}->{"nboccurrences"}) { | ||
| 111 | return ($r[$#r]); | ||
| 112 | } else { | ||
| 113 | $scores_hash = $self->computeScores($semanticTypes); | ||
| 114 | @r = sort { $scores_hash->{$a} <=> $scores_hash->{$b} } keys %$scores_hash; | ||
| 115 | return($r[$#r]); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | return($semanticType); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub computeScores { | ||
| 123 | my ($self, $semanticTypes) = @_; | ||
| 124 | |||
| 125 | my $semanticType; | ||
| 126 | my %scores; | ||
| 127 | my $score = 0; | ||
| 128 | my $originType; | ||
| 129 | my $origin; | ||
| 130 | |||
| 131 | foreach $semanticType (keys %$semanticTypes) { | ||
| 132 | foreach $originType (@{$semanticTypes->{$semanticType}->{"originType"}}) { | ||
| 133 | if ($originType eq "morphological") { | ||
| 134 | foreach $origin (@{$semanticTypes->{$semanticType}->{"origin"}}) { | ||
| 135 | # warn "origin: $origin\n"; | ||
| 136 | # warn "semanticType: $semanticType\n"; | ||
| 137 | # warn $self->{"WEIGHTS"}->{$origin} . "\n"; | ||
| 138 | # warn $semanticTypes->{$semanticType}->{$origin} . "\n"; | ||
| 139 | $score += ($self->{"WEIGHTS"}->{$origin} * $semanticTypes->{$semanticType}->{$origin}); | ||
| 140 | } | ||
| 141 | } else { | ||
| 142 | $score += ($self->{"WEIGHTS"}->{$originType} * $semanticTypes->{$semanticType}->{$originType}); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | $scores{$semanticType} = $score; | ||
| 146 | } | ||
| 147 | return(\%scores); | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | sub determineSemanticType4Term { | ||
| 152 | my ($self, $term) = @_; | ||
| 153 | |||
| 154 | my $word; | ||
| 155 | my @words = split (/ /, $term); | ||
| 156 | |||
| 157 | my $semanticType; | ||
| 158 | |||
| 159 | my %semanticTypes; # semanticType => (origin, nboccurrences) | ||
| 160 | my @semanticTypeList; | ||
| 161 | |||
| 162 | my $field; | ||
| 163 | my $type; | ||
| 164 | |||
| 165 | foreach $word (@words) { | ||
| 166 | foreach $semanticType (keys %{$self->{"SEMANTICTYPES"}}) { | ||
| 167 | foreach $field (@{$self->{"WORD_FIELDS"}->{"field"}}) { | ||
| 168 | if (exists $self->{"RESOURCES"}->{$semanticType . "_$field"}->{$word}) { | ||
| 169 | $self->addSemanticType({"semanticType" => $semanticType, | ||
| 170 | "originType" => $field, | ||
| 171 | "origin" => $field, | ||
| 172 | }, \%semanticTypes); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | foreach $semanticType (keys %{$self->{"SEMANTICTYPES"}}) { | ||
| 177 | @semanticTypeList = &determineSemanticType4Word($self->{"RESOURCES"}->{$semanticType . "_morphological"}, $term, $word, $semanticType); | ||
| 178 | if (scalar(@semanticTypeList) > 0) { | ||
| 179 | $field = "morphological"; | ||
| 180 | foreach $type (@semanticTypeList) { | ||
| 181 | $self->addSemanticType($type, \%semanticTypes); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | $semanticType = $self->selectSemanticType(\%semanticTypes, $term); | ||
| 187 | |||
| 188 | return($semanticType); | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | sub _processMorphoSemanticTagging { | ||
| 193 | my ($self, $lang) = @_; | ||
| 194 | |||
| 195 | warn "[LOG] MorphoSemantic Tagging\n"; | ||
| 196 | |||
| 197 | $lang = $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 198 | |||
| 199 | my $document; | ||
| 200 | my $term; | ||
| 201 | my $semanticType; | ||
| 202 | |||
| 203 | foreach $document (@{$self->_documentSet}) { | ||
| 204 | foreach $term (@{$document->getAnnotations->getSemanticUnitLevel->getElements}) { | ||
| 205 | # warn "------------------------------------------------------------------------\n"; | ||
| 206 | # warn "Process " . $term->getForm . "\n"; | ||
| 207 | if (!$document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $term->getId)) { | ||
| 208 | $semanticType = $self->determineSemanticType4Term($term->getForm); | ||
| 209 | if (defined $semanticType) { | ||
| 210 | $self->_addSemanticFeature($term, $semanticType, $document); | ||
| 211 | } | ||
| 212 | |||
| 213 | } | ||
| 214 | } | ||
| 215 | } | ||
| 216 | warn "[LOG]\n"; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub _outputMorphoSemanticTagging { | ||
| 220 | my ($self) = @_; | ||
| 221 | |||
| 222 | warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 223 | |||
| 224 | warn "[LOG] done\n"; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub run { | ||
| 228 | my ($self, $documentSet) = @_; | ||
| 229 | |||
| 230 | # Set variables according the the configuration | ||
| 231 | |||
| 232 | $self->_documentSet($documentSet); | ||
| 233 | |||
| 234 | warn "[LOG] " . $self->_config->comments . " ... \n"; | ||
| 235 | |||
| 236 | $self->_inputMorphoSemanticTagging; | ||
| 237 | |||
| 238 | my $command_line = $self->_processMorphoSemanticTagging; | ||
| 239 | |||
| 240 | # if ($self->_position eq "last") { | ||
| 241 | # # TODO | ||
| 242 | if (($self->_position eq "last") && ($self->_no_standard_output)) { | ||
| 243 | warn "print no standard output\n"; | ||
| 244 | } else { | ||
| 245 | $self->_outputMorphoSemanticTagging; | ||
| 246 | } | ||
| 247 | # $self->_outputParsing; | ||
| 248 | |||
| 249 | |||
| 250 | # Put log information | ||
| 251 | |||
| 252 | my $information = { 'software_name' => $self->_config->name, | ||
| 253 | 'comments' => $self->_config->comments, | ||
| 254 | 'command_line' => $command_line, | ||
| 255 | 'list_modified_level' => ['semantic_features_level'], | ||
| 256 | }; | ||
| 257 | $self->_log($information); | ||
| 258 | |||
| 259 | |||
| 260 | # die "You call the 'rum' method of the wrapper class base\n | ||
| 261 | # You should define a 'run' method for your wrapper\n"; | ||
| 262 | warn "[LOG] done\n"; | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | 1; | ||
| 267 | |||
| 268 | __END__ |