« blib/lib/Lingua/Ogmios/Annotations/Level.pm
↑ Summary
blib/lib/Lingua/Ogmios/Annotations/MorphosyntacticFeatures.pm »
| 1 | package Lingua::Ogmios::NLPWrappers::SmileyRecognition; | ||
| 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 | |||
| 20 | warn "[LOG] Creating a wrapper of the SmileyRecognition\n"; | ||
| 21 | |||
| 22 | my $SmileyRecognition = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output); | ||
| 23 | |||
| 24 | if (defined $SmileyRecognition->_config->configuration->{'CONFIG'}) { | ||
| 25 | foreach $lang (keys %{$SmileyRecognition->_config->configuration->{'CONFIG'}}) { | ||
| 26 | if ($lang =~ /language=([\w]+)/io) { | ||
| 27 | $lang2 = $1; | ||
| 28 | |||
| 29 | # $SmileyRecognition->_setOption($lang2, "MODE", "mode", "eval"); | ||
| 30 | # if (defined $SmileyRecognition->_config->configuration->{'CONFIG'}->{$lang}->{"OPTION"}) { | ||
| 31 | # $SmileyRecognition->{'parameter'}->{$lang2} = $SmileyRecognition->_config->configuration->{'CONFIG'}->{$lang}->{"OPTION"}; | ||
| 32 | # } else { | ||
| 33 | # $SmileyRecognition->{'parameter'}->{"FR"} = "?.!"; | ||
| 34 | # $SmileyRecognition->{'parameter'}->{"EN"} = "?.!"; | ||
| 35 | # warn "*** OPTION is not set ***\n"; | ||
| 36 | # } | ||
| 37 | # } else { | ||
| 38 | # $SmileyRecognition->{'parameter'}->{"FR"} = "?.!"; | ||
| 39 | # $SmileyRecognition->{'parameter'}->{"EN"} = "?.!"; | ||
| 40 | # warn "*** OPTION is not set ***\n"; | ||
| 41 | $SmileyRecognition->_setOption($lang2, "EXCEPTIONS", "exceptions", undef); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | # } else { | ||
| 45 | # $SmileyRecognition->{'parameter'}->{"FR"} = "?.!"; | ||
| 46 | # $SmileyRecognition->{'parameter'}->{"EN"} = "?.!"; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | $SmileyRecognition->_input_filename($tmpfile_prefix . ".SmileyRecognition.in"); | ||
| 51 | $SmileyRecognition->_output_filename($tmpfile_prefix . ".SmileyRecognition.out"); | ||
| 52 | |||
| 53 | return($SmileyRecognition); | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | sub _inputSmileyRecognition { | ||
| 58 | my ($self) = @_; | ||
| 59 | |||
| 60 | my $token; | ||
| 61 | my $corpus_in = ""; | ||
| 62 | my $document; | ||
| 63 | my $docOffset = 0; | ||
| 64 | |||
| 65 | $self->{'documentLastOffset'} = []; | ||
| 66 | |||
| 67 | warn "[LOG] making SmileyRecognition input\n"; | ||
| 68 | |||
| 69 | foreach $document (@{$self->_documentSet}) { | ||
| 70 | $docOffset += $document->getAnnotations->getTokenLevel->getLastElement->getTo; | ||
| 71 | push @{$self->{'documentLastOffset'}}, [$docOffset, $document, $document->getAnnotations->getTokenLevel->getLastElement->getTo]; | ||
| 72 | |||
| 73 | # warn scalar(@{$self->{'documentLastOffset'}}) . " / $docOffset\n"; | ||
| 74 | foreach $token (@{$document->getAnnotations->getTokenLevel->getElements}) { | ||
| 75 | # warn "$token: " . $token->getContent . ";\n"; | ||
| 76 | $corpus_in .= $token->getContent; | ||
| 77 | if ($document->getAnnotations->getSectionLevel->existsElementFromIndex("to", $token->getId)) { | ||
| 78 | # $corpus_in .= "\n"; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | # $corpus_in =~ s/[\x{A0}\x{2000}-\x{200B}]/ /go; | ||
| 83 | # $corpus_in =~ s/\x{0153}/oe/go; | ||
| 84 | # $corpus_in =~ s/\x{0152}/OE/go; | ||
| 85 | # $corpus_in =~ s/[\x{2019}\x{2032}]/\'/go; | ||
| 86 | |||
| 87 | warn "[LOG] Write in " . $self->_input_filename . "\n"; | ||
| 88 | |||
| 89 | ##XXX TH | ||
| 90 | open(FILE_IN, ">:utf8", $self->_input_filename) or die "No such file " . $self->_output_filename . "\n"; | ||
| 91 | |||
| 92 | |||
| 93 | # print FILE_IN Encode::encode("iso-8859-1", $corpus_in, Encode::FB_DEFAULT); #$corpus_in; | ||
| 94 | |||
| 95 | print FILE_IN $corpus_in; | ||
| 96 | |||
| 97 | close FILE_IN; | ||
| 98 | |||
| 99 | warn "[LOG] done\n"; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _processSmileyRecognition { | ||
| 103 | my ($self, $lang) = @_; | ||
| 104 | |||
| 105 | warn "[LOG] SmileyRecognition\n"; | ||
| 106 | |||
| 107 | $lang = $self->_documentSet->[0]->getAnnotations->getLanguage; | ||
| 108 | |||
| 109 | return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{SmileyRecognition_CMD} . " " . $self->{'exceptions'}->{$lang} . " " . $self->_input_filename . ">" . $self->_output_filename))); | ||
| 110 | |||
| 111 | warn "[LOG]\n"; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | sub _outputSmileyRecognition { | ||
| 116 | my ($self) = @_; | ||
| 117 | |||
| 118 | my $line; | ||
| 119 | my $file; | ||
| 120 | my $start; | ||
| 121 | my $end; | ||
| 122 | my $start2; | ||
| 123 | my $end2; | ||
| 124 | my $string; | ||
| 125 | my $semtags; | ||
| 126 | my $semtag; | ||
| 127 | my $document; | ||
| 128 | my $startToken; | ||
| 129 | my $endToken; | ||
| 130 | my @tokenList; | ||
| 131 | my $token; | ||
| 132 | my $semFeatures; | ||
| 133 | my $i; | ||
| 134 | my $termUnit; | ||
| 135 | warn "[LOG] . Parsing " . $self->_output_filename . "\n"; | ||
| 136 | |||
| 137 | |||
| 138 | open(FILE_OUT, "<:utf8", $self->_output_filename) or die "No such file " . $self->_output_filename . "\n"; | ||
| 139 | while($line = <FILE_OUT>) { | ||
| 140 | # warn $line; | ||
| 141 | chomp $line; | ||
| 142 | if ($line !~ /\#EXCEPT/o) { | ||
| 143 | ($file, $start, $end, $string, $semtags) = split /\|/, $line; | ||
| 144 | # warn "************************\n"; | ||
| 145 | # warn "string: $string\n"; | ||
| 146 | # warn "\tstart (a): $start\n"; | ||
| 147 | # warn "\tend (a): $end\n"; | ||
| 148 | # warn "\tsemf: $semtags\n"; | ||
| 149 | # get document | ||
| 150 | $i = 0; | ||
| 151 | $start2 = $start; | ||
| 152 | $end2 = $end; | ||
| 153 | while(($i < scalar(@{$self->{'documentLastOffset'}})) && ($start > $self->{'documentLastOffset'}->[$i]->[0])) { | ||
| 154 | # warn "\n\tstart (c): $start\n"; | ||
| 155 | # warn "\tend (c): $end\n"; | ||
| 156 | # warn "\tstart2 (c): $start2\n"; | ||
| 157 | # warn "\tend2 (c): $end2\n"; | ||
| 158 | # warn "\ti = $i\n"; | ||
| 159 | # warn "\t " . $self->{'documentLastOffset'}->[$i]->[0] . "\n"; | ||
| 160 | # warn "\t " . $self->{'documentLastOffset'}->[$i]->[2] . "\n"; | ||
| 161 | $start2 -= $self->{'documentLastOffset'}->[$i]->[2] + 1; | ||
| 162 | $end2 -= $self->{'documentLastOffset'}->[$i]->[2] + 1; | ||
| 163 | # warn "\tstart2 (d): $start2\n"; | ||
| 164 | # warn "\tend2 (d): $end2\n"; | ||
| 165 | $i++; | ||
| 166 | } | ||
| 167 | if ($i < scalar(@{$self->{'documentLastOffset'}})) { | ||
| 168 | $document = $self->{'documentLastOffset'}->[$i]->[1]; | ||
| 169 | # warn "found\n"; | ||
| 170 | # warn "\t" . $document->getId . "\n"; | ||
| 171 | } | ||
| 172 | # warn "\tstart (b): $start\n"; | ||
| 173 | # warn "\tend (b): $end\n"; | ||
| 174 | # warn "\tstart2 (b): $start2\n"; | ||
| 175 | # warn "\tend2 (b): $end2\n"; | ||
| 176 | # warn "\ti = $i\n"; | ||
| 177 | # $self->{'documentLastOffset'} | ||
| 178 | |||
| 179 | if ($document->getAnnotations->getTokenLevel->existsElementFromIndex("from", $start2 )) { | ||
| 180 | $startToken = $document->getAnnotations->getTokenLevel->getElementFromIndex("from", $start2 )->[0]; | ||
| 181 | if ($document->getAnnotations->getTokenLevel->existsElementFromIndex("to", $end2 )) { | ||
| 182 | $endToken = $document->getAnnotations->getTokenLevel->getElementFromIndex("to", $end2 )->[0]; | ||
| 183 | # warn "\t" . $startToken->getId . "\n"; | ||
| 184 | # warn "\t" . $endToken->getId . "\n"; | ||
| 185 | $token = $startToken; | ||
| 186 | @tokenList = (); | ||
| 187 | push @tokenList, $token; | ||
| 188 | while(!$token->equals($endToken)) { | ||
| 189 | $token = $token->next; | ||
| 190 | push @tokenList, $token; | ||
| 191 | }; | ||
| 192 | $termUnit = $self->_createTerm($document, \@tokenList, $string); | ||
| 193 | |||
| 194 | if (defined $termUnit) { | ||
| 195 | $document->getAnnotations->addSemanticUnit($termUnit); | ||
| 196 | } | ||
| 197 | |||
| 198 | if (defined $semtags) { | ||
| 199 | $semFeatures = $self->_createSemanticFeaturesFromString($semtags, $termUnit->getId); | ||
| 200 | if (defined $semFeatures) { | ||
| 201 | $document->getAnnotations->addSemanticFeatures($semFeatures); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | } | ||
| 211 | close FILE_OUT; | ||
| 212 | |||
| 213 | warn "[LOG] done\n"; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub run { | ||
| 217 | my ($self, $documentSet) = @_; | ||
| 218 | |||
| 219 | # Set variables according the the configuration | ||
| 220 | |||
| 221 | $self->_documentSet($documentSet); | ||
| 222 | |||
| 223 | warn "[LOG] " . $self->_config->comments . " ... \n"; | ||
| 224 | |||
| 225 | $self->_inputSmileyRecognition; | ||
| 226 | |||
| 227 | my $command_line = $self->_processSmileyRecognition; | ||
| 228 | |||
| 229 | # if ($self->_position eq "last") { | ||
| 230 | # # TODO | ||
| 231 | $self->_outputSmileyRecognition; | ||
| 232 | if (($self->_position eq "last") && ($self->_no_standard_output)) { | ||
| 233 | warn "print no standard output\n"; | ||
| 234 | if ($self->_no_standard_output eq "HTML") { | ||
| 235 | warn "print HTML output\n"; | ||
| 236 | $self->HTMLoutput; | ||
| 237 | } | ||
| 238 | if ($self->_no_standard_output eq "TXT") { | ||
| 239 | warn "print TXT output\n"; | ||
| 240 | $self->_TXToutput; | ||
| 241 | } | ||
| 242 | # if ($self->_no_standard_output eq "TXTwSectionTitle") { | ||
| 243 | # warn "print TXT output\n"; | ||
| 244 | # $self->_TXToutputWithSectionTitle; | ||
| 245 | # } | ||
| 246 | |||
| 247 | if ($self->_no_standard_output eq "TAGGEDSENT") { | ||
| 248 | warn "print Tagged Sentence output (TAGGEDSENT)\n"; | ||
| 249 | $self->_makeTaggedSentences({}, \*STDOUT, "/", " "); | ||
| 250 | } | ||
| 251 | |||
| 252 | # if ($self->_no_standard_output eq "CRFINPUT") { | ||
| 253 | # warn "print Tagged Sentence output (CRFINPUT)\n"; | ||
| 254 | # $self->_makeTaggedSentences([], \*STDOUT, "\t", "\n"); | ||
| 255 | # } | ||
| 256 | } | ||
| 257 | # $self->_outputParsing; | ||
| 258 | |||
| 259 | |||
| 260 | # Put log information | ||
| 261 | |||
| 262 | my $information = { 'software_name' => $self->_config->name, | ||
| 263 | 'comments' => $self->_config->comments, | ||
| 264 | 'command_line' => $command_line, | ||
| 265 | 'list_modified_level' => [''], | ||
| 266 | }; | ||
| 267 | $self->_log($information); | ||
| 268 | |||
| 269 | |||
| 270 | # die "You call the 'rum' method of the wrapper class base\n | ||
| 271 | # You should define a 'run' method for your wrapper\n"; | ||
| 272 | warn "[LOG] done\n"; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub _TXToutput { | ||
| 276 | my ($self) = @_; | ||
| 277 | |||
| 278 | my $i; | ||
| 279 | my $semUnit; | ||
| 280 | my $semf; | ||
| 281 | my $document; | ||
| 282 | |||
| 283 | # for ($i = 0; $i < scalar(@{$self->_documentSet}); $i++) { | ||
| 284 | foreach $document (@{$self->_documentSet}) { | ||
| 285 | foreach $semUnit (@{$document->getAnnotations->getSemanticUnitLevel->getElements}) { | ||
| 286 | print $semUnit->getId . "\t" . $semUnit->getForm . "\t"; | ||
| 287 | if ($document->getAnnotations->getSemanticFeaturesLevel->existsElementFromIndex("refid_semantic_unit", $semUnit->getId)) { | ||
| 288 | $semf = $document->getAnnotations->getSemanticFeaturesLevel->getElementFromIndex("refid_semantic_unit", $semUnit->getId)->[0]; | ||
| 289 | if (defined $semf) { | ||
| 290 | print $semf->toString; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | print "\n"; | ||
| 294 | |||
| 295 | } | ||
| 296 | } | ||
| 297 | } | ||
| 298 | |||
| 299 | 1; | ||
| 300 | |||
| 301 | __END__ |