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

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 182 CC 15 SCAR 32.7
CC
15
Cov
0%
_inputPorterStemmer
42.0
_outputParsing
42.0
run
20.0
CRAP 26.3
1 package Lingua::Ogmios::NLPWrappers::PorterStemmer;
2  
3  
4 our $VERSION='0.1';
5  
6 use Lingua::Ogmios::NLPWrappers::Wrapper;
7  
8 use Lingua::Ogmios::Annotations::Stem;
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 " . $config->comments . "\n";
19  
20  
21 my $PorterStemmer = $class->SUPER::new($config, $tmpfile_prefix, $logfile, $position, $no_standard_output);
22  
23 my @tmpin;
24 # my @tmpout;
25  
26 $PorterStemmer->_input_array(\@tmpin);
27 # $PorterStemmer->_output_array(\@tmpout);
28  
29 return($PorterStemmer);
30  
31 }
32  
33 sub _processPorterStemmer {
34 my ($self, $lang) = @_;
35  
36 warn "[LOG] " . $self->_config->comments . "\n";
37  
38 $lang = $self->_documentSet->[0]->getAnnotations->getLanguage;
39  
40 my $perlModule = $self->_config->commands($lang)->{PerlModule};
41  
42 eval "require $perlModule";
43 if ($@) {
44 warn $@ . "\n";
45 die "Problem while loading perlModule $perlModule - Abort\n\n";
46 } else {
47 warn "Run Module $perlModule\n";
48  
49 my $stemmer = Lingua::Stem->new(-locale => $lang);
50 $stemmer->stem_caching({ -level => 2 });
51 my $tmparray = $stemmer->stem(@{$self->_input_array});
52 $self->_output_array($tmparray);
53  
54 # foreach my $stem (@$tmparray) {
55 # warn "$stem\n";
56 # }
57  
58 }
59 warn "[LOG]\n";
60 return($perlModule);
61 }
62  
63 sub _inputPorterStemmer {
64 my ($self) = @_;
65  
66 my $token;
67 my $next_token;
68 my $document;
69 my $doc_idx;
70  
71 my @corpus_in_t;
72  
73 my $word;
74 warn "[LOG] making input\n";
75
76 foreach $document (@{$self->_documentSet}) {
77 for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) {
78 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
79 if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
80 $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0];
81 push @corpus_in_t, $word->getForm;
82  
83 $doc_idx += $word->getReferenceSize - 1;
84 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
85 } else {
86 if (!($token->isSep)) {
87 push @corpus_in_t, $token->getContent;
88 }
89 }
90 if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) {
91 if ($token->isSymb) {
92 if (!($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId))) {
93 $corpus_in_t[$#corpus_in_t] .= "\tSENT";
94 } else {
95 push @corpus_in_t, $token->getContent . "\tSENT";
96 }
97 } else {
98 push @corpus_in_t, ".\tSENT";
99 }
100 }
101 }
102 }
103  
104 push @{$self->_input_array}, @corpus_in_t;
105
106 warn "[LOG] done\n";
107 }
108  
109  
110 sub _outputParsing {
111 my ($self) = @_;
112  
113 my $line;
114  
115 my $stems;
116  
117 my $doc_idx;
118 my $word_idx;
119 my $document;
120  
121 my $word;
122 my $token;
123  
124 my $posInWord;
125 my $posInLemma;
126  
127 my $substringBefore;
128 my $substringAfter;
129  
130 warn "[LOG] . Parsing Output Array\n";
131  
132 $stems = $self->_output_array;
133  
134 my $StemOutput_idx = 0;
135  
136 foreach $document (@{$self->_documentSet}) {
137 for($doc_idx = 0; $doc_idx < scalar(@{$document->getAnnotations->getTokenLevel->getElements});$doc_idx++) {
138 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
139 if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
140 $word = $document->getAnnotations->getWordLevel->getElementFromIndex("list_refid_token", $token->getId)->[0];
141 # Correct POSTag if it's named entity
142 my $Stem = Lingua::Ogmios::Annotations::Stem->new(
143 {'refid_word' => $word,
144 'stem_form' => $stems->[$StemOutput_idx],
145 });
146 $document->getAnnotations->addStem($Stem);
147  
148 $StemOutput_idx++;
149  
150 $doc_idx += $word->getReferenceSize - 1;
151 $token = $document->getAnnotations->getTokenLevel->getElements->[$doc_idx];
152  
153 } else {
154 if (!($token->isSep)) {
155 $StemOutput_idx++;
156 }
157 }
158 if ($document->getAnnotations->getSentenceLevel->existsElementFromIndex("refid_end_token", $token->getId)) {
159 if ($token->isSymb) {
160 if ($document->getAnnotations->getWordLevel->existsElementFromIndex("list_refid_token", $token->getId)) {
161 $StemOutput_idx++;
162 }
163 } else {
164 $StemOutput_idx++;
165 }
166 }
167 }
168 }
169 warn "[LOG] done\n";
170 }
171  
172 sub run {
173 my ($self, $documentSet) = @_;
174  
175 # Set variables according the the configuration
176 $self->_documentSet($documentSet);
177  
178 if ($documentSet->[0]->getAnnotations->existsStemLevel) {
179 warn "stems exist in the first document\n";
180 warn " Assuming that no stem idenfication is required for the current document set\n";
181 # return(0);
182 } else {
183 warn "[LOG] " . $self->_config->comments . " ... \n";
184
185 $self->_inputPorterStemmer;
186
187 my $command_line = $self->_processPorterStemmer;
188 $self->_outputParsing;
189  
190 # Put log information
191 my $information = { 'software_name' => $self->_config->name,
192 'comments' => $self->_config->comments,
193 'command_line' => $command_line,
194 'list_modified_level' => ['stem_level'],
195 };
196
197 $self->_log($information);
198  
199 my $document;
200 foreach $document (@{$documentSet}) {
201 $document->getAnnotations->addLogProcessing(
202 Lingua::Ogmios::Annotations::LogProcessing->new(
203 { 'comments' => 'Found ' . $document->getAnnotations->getStemLevel->getSize . ' stem\n',
204 }
205 )
206 );
207 # basic check
208 if ($document->getAnnotations->getStemLevel->getSize != $document->getAnnotations->getWordLevel->getSize) {
209 warn "Document " . $document->getId . ": there is difference between word and stem size\n";
210 }
211 }
212 }
213 if (($self->_position eq "last") && ($self->_no_standard_output)) {
214 warn "print no standard output\n";
215 }
216 warn "[LOG] done\n";
217 }
218  
219  
220 1;
221  
222 __END__