blib/lib/Lingua/Ogmios/NLPWrappers/Distribution.pm untested

filter: Statementstmt 0.0% 0 / 252 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 453 CC 41 SCAR 44.0
CC
41
Cov
0%
new
210.0
getContextBefore
72.0
getContextAfter
72.0
CRAP 81.6
1 package Lingua::Ogmios::NLPWrappers::Distribution;
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 Distribution\n";
21  
22 my $Distribution = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output);
23  
24 if (defined $Distribution->_config->configuration->{'CONFIG'}) {
25 foreach $lang (keys %{$Distribution->_config->configuration->{'CONFIG'}}) {
26 if ($lang =~ /language=([\w]+)/io) {
27 $lang2 = $1;
28 if (defined $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"PRINTTERM"}) {
29 $Distribution->{'printTerm'}->{$lang2} = $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"PRINTTERM"};
30 }
31 if (defined $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"ALLTAGS"}) {
32 $Distribution->{'allTags'}->{$lang2} = $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"ALLTAGS"};
33 } else {
34 $Distribution->{'allTags'}->{$lang2} = 1;
35 }
36 if (defined $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"ALLTAGSCTXT"}) {
37 $Distribution->{'allTagsCtxt'}->{$lang2} = $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"ALLTAGSCTXT"};
38 } else {
39 $Distribution->{'allTagsCtxt'}->{$lang2} = 1;
40 }
41 if (defined $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"ALLWORDCOUNT"}) {
42 $Distribution->{'allWordCount'}->{$lang2} = $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"ALLWORDCOUNT"};
43 } else {
44 $Distribution->{'allWordCount'}->{$lang2} = 1;
45 }
46 if (defined $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"BEFOREWINDOWSIZE"}) {
47 $Distribution->{'BeforeWindowSize'}->{$lang2} = $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"BEFOREWINDOWSIZE"};
48 }
49 if (defined $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"AFTERWINDOWSIZE"}) {
50 $Distribution->{'AfterWindowSize'}->{$lang2} = $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"AFTERWINDOWSIZE"};
51 }
52 if (defined $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"TERMINCONTEXT"}) {
53 $Distribution->{'termInContext'}->{$lang2} = $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"TERMINCONTEXT"};
54 }
55 if ((defined $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"POSTAGLIST"}) &&
56 (defined $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"POSTAGLIST"}->{'POSTAG'})) {
57 if (ref($Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"POSTAGLIST"}->{'POSTAG'}) eq "ARRAY") {
58 $Distribution->{'POSTAGS'}->{$lang2} = [];
59 push @{$Distribution->{'POSTAGS'}->{$lang2}}, @{$Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"POSTAGLIST"}->{'POSTAG'}};
60 } else {
61 push @{$Distribution->{'POSTAGS'}->{$lang2}}, $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"POSTAGLIST"}->{'POSTAG'};
62 }
63 } else {
64 $Distribution->{'allTags'}->{$lang2} = 1;
65 }
66  
67 if ((defined $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"POSTAGCTXTLIST"}) &&
68 (defined $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"POSTAGCTXTLIST"}->{'POSTAG'})) {
69 if (ref($Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"POSTAGCTXTLIST"}->{'POSTAG'}) eq "ARRAY") {
70 $Distribution->{'POSTAGCTXT'}->{$lang2} = [];
71 push @{$Distribution->{'POSTAGCTXT'}->{$lang2}}, @{$Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"POSTAGCTXTLIST"}->{'POSTAG'}};
72 } else {
73 push @{$Distribution->{'POSTAGCTXT'}->{$lang2}}, $Distribution->_config->configuration->{'CONFIG'}->{$lang}->{"POSTAGCTXTLIST"}->{'POSTAG'};
74 }
75 } else {
76 $Distribution->{'allTagsCtxt'}->{$lang2} = 1;
77 }
78 }
79 }
80 # } else {
81 # $Distribution->{'sentenceSeparatorCharList'}->{"FR"} = "?.!";
82 # $Distribution->{'sentenceSeparatorCharList'}->{"EN"} = "?.!";
83 }
84  
85 $Distribution->_input_filename($tmpfile_prefix . ".Distribution.in");
86 $Distribution->_output_filename($tmpfile_prefix . ".Distribution.out");
87  
88  
89 return($Distribution);
90  
91 }
92  
93 sub _processDistribution {
94 my ($self, $lang) = @_;
95  
96 warn "[LOG] Distribution\n";
97  
98 $lang = $self->_documentSet->[0]->getAnnotations->getLanguage;
99  
100 return($self->_exec_command($self->_defineCommandLine($self->_config->commands($lang)->{Distribution_CMD} . " < " . $self->_input_filename . ">" . $self->_output_filename)));
101  
102 warn "[LOG]\n";
103 }
104  
105 sub _inputDistribution {
106 my ($self) = @_;
107  
108 warn "[LOG] making Distribution input\n";
109
110 warn "[LOG] done\n";
111 }
112  
113  
114 sub _outputDistribution {
115 my ($self) = @_;
116  
117 warn "[LOG] . Parsing " . $self->_output_filename . "\n";
118  
119 warn "[LOG] done\n";
120 }
121  
122 sub run {
123 my ($self, $documentSet) = @_;
124  
125 # Set variables according the the configuration
126  
127 $self->_documentSet($documentSet);
128  
129 warn "[LOG] " . $self->_config->comments . " ... \n";
130  
131 # $self->_inputDistribution;
132  
133 my $command_line = "";
134 # my $command_line = $self->_processDistribution;
135  
136 # if ($self->_position eq "last") {
137 # # TODO
138 if (($self->_position eq "last") && ($self->_no_standard_output)) {
139 warn "print no standard output\n";
140 if ($self->_no_standard_output eq "GW") {
141 $self->graphicalWindow();
142 }
143 } else {
144 # $self->_outputDistribution;
145 }
146 # $self->_outputParsing;
147  
148  
149 # Put log information
150  
151 my $information = { 'software_name' => $self->_config->name,
152 'comments' => $self->_config->comments,
153 'command_line' => $command_line,
154 'list_modified_level' => [''],
155 };
156 $self->_log($information);
157  
158  
159 # die "You call the 'rum' method of the wrapper class base\n
160 # You should define a 'run' method for your wrapper\n";
161 warn "[LOG] done\n";
162 }
163  
164 sub graphicalWindow {
165 my ($self) = @_;
166  
167 my $token;
168 my $currentToken;
169 my $document;
170 # my $doc_idx;
171 my @context;
172 my $word;
173 my $semanticUnit;
174 my $currentUnit;
175 my $i;
176 my $type;
177 my $canonical_form;
178 my $postag;
179 my $form;
180 my $lang;
181  
182 foreach $document (@{$self->_documentSet}) {
183 $lang = $document->getAnnotations->getLanguage;
184 $currentToken = $document->getAnnotations->getTokenLevel->getElements->[0];
185 while(defined $currentToken) {
186 # for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) {
187 # $currentToken = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
188 if (($self->_printTerm($lang)) && ($document->getAnnotations->getSemanticUnitLevel->existsElementByToken($currentToken))) {
189 $currentUnit = $self->_getLargerTerm($document->getAnnotations->getSemanticUnitLevel->getElementByToken($currentToken));
190 $token = $currentUnit->end_token;
191 $type = "TERM";
192 $canonical_form = $self->_getCanonicalForm($currentUnit, $document);
193 # if (!defined $currentUnit->canonical_form) {
194 # $canonical_form = "";
195 # } else {
196 # $canonical_form = $currentUnit->canonical_form;
197 # }
198 if ($currentUnit->isNamedEntity) {
199 $postag = "named_entity";
200 } else {
201 $postag = "term";
202 }
203 } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $currentToken->getId)) {
204 $currentUnit = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $currentToken->getId)->[0];
205 # $doc_idx += $currentUnit->getReferenceSize - 1;
206 $token = $currentUnit->end_token;
207 $type = "WORD";
208 $canonical_form = $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $currentUnit->getId)->[0]->canonical_form;
209 $postag = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $currentUnit->getId)->[0]->syntactic_category;
210 if (!($self->_selectedPOSTAG($lang, $postag))) {
211 $currentUnit = undef;
212 $token = $currentToken;
213 }
214 } else {
215 $token = $currentToken;
216 }
217 if (defined $currentUnit) {
218 @context = @{$self->getContextBefore($document, $currentToken)};
219 for($i = 0; $i < scalar(@context) ; $i++) {
220 print $currentUnit->getForm . "\t$postag\t$canonical_form\t" . join("\t", @{$context[$i]}) . "\tBefore\t" . ($i+1). "\t$type\n"
221 }
222 # @context = @{$self->getContextAfter($document, $currentToken)};
223 @context = @{$self->getContextAfter($document, $currentUnit->end_token)};
224 for($i = 0; $i < scalar(@context) ; $i++) {
225 print $currentUnit->getForm . "\t$postag\t$canonical_form\t" . join("\t", @{$context[$i]}) . "\tAfter\t" . ($i+1). "\t$type\n"
226 }
227 print "\n";
228 $currentUnit = undef;
229 }
230 $currentToken = $token->next;
231 }
232 }
233  
234 }
235  
236 sub getContextBefore {
237 my ($self, $document, $currentToken) = @_;
238  
239 my $token;
240 my @context;
241 my $size = 0;
242 my $currentUnit;
243 my $postag;
244 my $canonical_form;
245 my $lang = $document->getAnnotations->getLanguage;
246 my $windowSize = $self->{'BeforeWindowSize'}->{$lang};
247  
248 $token = $currentToken;
249 while((defined $token->previous) &&
250 ($size < $windowSize) &&
251 (!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_start_token", $token->getId))
252 ){
253 do {
254 $token = $token->previous;
255
256 } while((defined $token->previous) &&
257 # ($token->isSep) &&
258 (!$document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) &&
259 (!(($self->_termInContext($lang)) && ($document->getAnnotations->getSemanticUnitLevel->existsElementByToken($token)))) &&
260 (!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_start_token", $token->getId))
261 );
262  
263 # ->getElementByToken($token)
264  
265 if (defined $token->previous) {
266 if (($self->_termInContext($lang)) && ($document->getAnnotations->getSemanticUnitLevel->existsElementByToken($token))) {
267 $currentUnit = $self->_getLargerTerm($document->getAnnotations->getSemanticUnitLevel->getElementByToken($token));
268  
269 if ($currentUnit->isNamedEntity) {
270 $postag = "named_entity";
271 $canonical_form = $self->_getCanonicalForm($currentUnit, $document);
272 # $canonical_form = $currentUnit->getForm;
273 } else {
274 $postag = "term";
275 $canonical_form = $self->_getCanonicalForm($currentUnit, $document);
276 # $canonical_form = $currentUnit->canonical_form;
277 }
278 push @context, [$currentUnit->getForm, $postag, $canonical_form];
279  
280 $size++;
281 $token = $currentUnit->start_token;
282 } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
283 # warn "$size\n";
284 $currentUnit = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0];
285 $postag = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $currentUnit->getId)->[0]->syntactic_category;
286 if ($self->_selectedPOSTAGCTXT($lang, $postag)) {
287 push @context, [$currentUnit->getForm,
288 $postag,
289 $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $currentUnit->getId)->[0]->canonical_form];
290 if ($self->{'allWordCount'}->{$lang} == 0) {$size++;}
291 }
292 if ($self->{'allWordCount'}->{$lang} == 1) {$size++;}
293 $token = $currentUnit->start_token;
294 }
295 }
296 }
297  
298 return(\@context);
299 }
300  
301 sub getContextAfter {
302 my ($self, $document, $currentToken) = @_;
303  
304 my $token;
305 my @context;
306 my $size = 0;
307 my $currentUnit;
308 my $postag;
309 my $canonical_form;
310 my $lang = $document->getAnnotations->getLanguage;
311 my $windowSize = $self->{'AfterWindowSize'}->{$lang};
312  
313 $token = $currentToken;
314 while((defined $token->next) &&
315 ($size < $windowSize) &&
316 (!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId))
317 ) {
318 do {
319 $token = $token->next;
320 # print $token->getContent;
321 } while((defined $token->next) &&
322 # ($token->isSep) &&
323 (!$document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) &&
324 ((!($self->_termInContext($lang)) && ($document->getAnnotations->getSemanticUnitLevel->existsElementByToken($token)))) &&
325 (!$document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId))
326
327 );
328 if (defined $token->next) {
329 if (($self->_termInContext($lang)) && ($document->getAnnotations->getSemanticUnitLevel->existsElementByToken($token))) {
330 $currentUnit = $self->_getLargerTerm($document->getAnnotations->getSemanticUnitLevel->getElementByToken($token));
331 if ($currentUnit->isNamedEntity) {
332 $postag = "named_entity";
333 $canonical_form = $self->_getCanonicalForm($currentUnit, $document);
334 # $canonical_form = $currentUnit->getForm;
335 } else {
336 $postag = "term";
337 $canonical_form = $self->_getCanonicalForm($currentUnit, $document);
338 # $canonical_form = $currentUnit->canonical_form;
339 }
340 push @context, [$currentUnit->getForm, $postag, $canonical_form];
341  
342 $size++;
343 $token = $currentUnit->end_token;
344 } elsif ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
345 # print " $size ";
346 # warn "$size\n";
347 $currentUnit = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0];
348 $postag = $document->getAnnotations->getMorphosyntacticFeaturesLevel->getElementFromIndex("refid_word", $currentUnit->getId)->[0]->syntactic_category;
349 if ($self->_selectedPOSTAGCTXT($lang, $postag)) {
350 push @context, [$currentUnit->getForm,
351 $postag,
352 $document->getAnnotations->getLemmaLevel->getElementFromIndex("refid_word", $currentUnit->getId)->[0]->canonical_form];
353
354 if ($self->{'allWordCount'}->{$lang} == 0) {$size++;}
355 }
356 if ($self->{'allWordCount'}->{$lang} == 1) {$size++;}
357 $token = $currentUnit->end_token;
358  
359 }
360 }
361 }
362 # print "\n";
363 return(\@context);
364 }
365  
366 sub _printTerm {
367 my ($self, $lang) = @_;
368  
369 if (exists $self->{'printTerm'}->{$lang}) {
370 return($self->{'printTerm'}->{$lang});
371 } else {
372 return(0);
373 }
374 }
375  
376 sub _termInContext {
377 my ($self, $lang) = @_;
378  
379 if (exists $self->{'termInContext'}->{$lang}) {
380 return($self->{'termInContext'}->{$lang});
381 } else {
382 return(0);
383 }
384 }
385  
386 sub _selectedPOSTAG {
387 my ($self, $lang, $postag) = @_;
388  
389 my $postagRE;
390  
391 if ((!defined($self->{'allTags'}->{$lang})) || ($self->{'allTags'}->{$lang} == 1)) {
392 return(1);
393 } else {
394 foreach $postagRE (@{$self->{'POSTAGS'}->{$lang}}) {
395 if ($postag =~ /^$postagRE$/) {
396 return(1);
397 }
398 }
399 }
400 return(0);
401 }
402  
403 sub _selectedPOSTAGCTXT {
404 my ($self, $lang, $postag) = @_;
405  
406 my $postagRE;
407  
408 if ((!defined($self->{'allTagsCtxt'}->{$lang})) || ($self->{'allTagsCtxt'}->{$lang} == 1)) {
409 return(1);
410 } else {
411 foreach $postagRE (@{$self->{'POSTAGCTXT'}->{$lang}}) {
412 if ($postag =~ /^$postagRE$/) {
413 return(1);
414 }
415 }
416 }
417 return(0);
418 }
419  
420 1;
421  
422  
423 __END__