blib/lib/Text/StemTagPOS.pm

filter: Statementstmt 77.3% 345 / 446 Branchbran 49.3% 81 / 164 Conditioncond 44.4% 12 / 27 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 90.9% 40 / 44 Podpod 100% 7 / 7 totaltotal 69.1% 488 / 706 CC 121 SCAR 35.7
CC
121
Cov
69%
getListOfStemmedWordsInAllDocuments
110.0
_getListOfMissingSubstringPositions
101.1
_getListOfPOSTagsFromPOSTypesList
90.2
CRAP 35.6
↑ Summary
1 package Text::StemTagPOS;
2  
3 3 require 5.006002;
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Lingua::Stem::Snowball;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Lingua::EN::Tagger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 $VERSION = '0.61';
16 3 @ISA = qw(Exporter);
17 3 @EXPORT = qw();
18 3 @EXPORT_OK = qw();
19 3 %EXPORT_TAGS = ();
20 }
21  
22 3 ▶use constant WORD_STEMMED => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use constant WORD_ORIGINAL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use constant WORD_POSTAG => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use constant WORD_INDEX => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use constant WORD_CHAR_POSITION => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use constant WORD_CHAR_LENGTH => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use constant WORD_SENTENCE_ID => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use constant WORD_USER_DEFINED => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 3 ▶use constant POSTAGS_PERIOD => 'PP';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 ▶use constant POSTAGS_PUNCTUATION => qw(PP PGP PPC PPD PPL PPR PPS LRB RRB SYM);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use constant POSTAGS_NOUN => qw(NN NNP NNPS NNS);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use constant POSTAGS_ADJECTIVE => qw(CD JJ JJR JJS);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use constant POSTAGS_VERB => qw(VB VBD VBG VBN VBP VBZ);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use constant POSTAGS_ADVERB => qw(RB RBR RBS RP WRB);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use constant POSTAGS_CONTENT_ADVERB => qw(RBR RBS RP);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use constant POSTAGS_ALL => qw(CC CD DET EX FW IN JJ JJR JJS LS MD NN NNP NNPS NNS PDT POS PRP PRPS RB RBR RBS RP SYM TO UH VB VBD VBG VBN VBP VBZ WDT WP WPS WRB PP PGP PPC PPD PPL PPR PPS LRB RRB);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use constant POSTAGS_CONTENT => (POSTAGS_CONTENT_ADVERB, POSTAGS_VERB, POSTAGS_ADJECTIVE, POSTAGS_NOUN);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use constant POSTAGS_TEXTRANK => (POSTAGS_NOUN, POSTAGS_ADJECTIVE);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 =head1 NAME
43  
44 C<Text::StemTagPOS> - Computes stemmed/POS tagged lists of text.
45  
46 =head1 SYNOPSIS
47  
48 use Text::StemTagPOS;
49 use Data::Dump qw(dump);
50 my $stemTagger = Text::StemTagPOS->new;
51 my $text = 'The first sentence. Sentence number two.';
52 my $listOfStemmedTaggedSentences = $stemTagger->getStemmedAndTaggedText ($text);
53 dump $listOfStemmedTaggedSentences;
54  
55 =head1 DESCRIPTION
56  
57 C<Text::StemTagPOS> uses the modules L<Lingua::Stem::Snowball> and L<Lingua::EN::Tagger>
58 to do part-of-speech tagging and stemming of English text. It was developed
59 to pre-process text for other modules. Encoding of all text
60 should be in Perl's internal format; see L<Encode> for converting text from
61 various encodes to a Perl string.
62  
63 =head1 CONSTRUCTOR
64  
65 =head2 C<new>
66  
67 The method C<new> creates an instance of the C<Text::StemTagPOS> class with the following
68 parameters:
69  
70 =over
71  
72 =item C<isoLangCode>
73  
74 isoLangCode => 'en'
75  
76 C<isoLangCode> is the ISO language code of the language that will be tagged and
77 stemmed by the object. It must be 'en', which is the default; other languages
78 may be added when POS taggers for them are added to CPAN.
79  
80 =item C<endingSentenceTag>
81  
82 endingSentenceTag => 'PP'
83  
84 C<endingSentenceTag> is the part-of-speech tag from L<Lingua::EN::Tagger>
85 that will be used to indicate
86 the end of a sentence. The default is 'PP'. The value of C<endingSentenceTag> must be
87 a tag generated by the module L<Lingua::EN::Tagger>; see method
88 C<getListOfPartOfSpeechTags> for all the possible tags; which are based on the
89 Penn Treebank tagset.
90  
91  
92 =item C<listOfPOSTypesToKeep> and/or C<listOfPOSTagsToKeep>
93  
94 listOfPOSTypesToKeep => [...], listOfPOSTagsToKeep => [...]
95  
96 The method C<getTaggedTextToKeep> uses C<listOfPOSTypesToKeep> and C<listOfPOSTagsToKeep>
97 to build the default list of the
98 parts-of-speech to be retained when filtering previously tagged text.
99 The default list is C<[qw(TEXTRANK_WORDS)]>,
100 which is all the nouns and adjectives in the text, as used in the textrank algorithm. Permitted
101 types for C<getTaggedTextToKeep> are 'ALL', 'ADJECTIVES', 'ADVERBS', 'CONTENT_WORDS', 'NOUNS', 'PUNCTUATION',
102 'TEXTRANK_WORDS', and 'VERBS'. C<listOfPOSTagsToKeep> provides finer control over the
103 parts-of-speech to be retained. For a list
104 of all the possible tags see method C<getListOfPartOfSpeechTags>.
105  
106 =back
107  
108 =cut
109  
110 sub new
111 4 ▶{
Subroutine new: called CC=6 SCAR=18.9
Pod documented
112 # create the class object.
113 4 my ($Class, %Parameters) = @_;
114 4 ▶ my $Self = bless {}, ref($Class) || $Class;
Condition33%1/3
ref $Class || $Class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $Class || $Class
ref $Class $Class
Truth table33%1/3
A || B
A= ref $ClassB= $Class
ABresult
1X1
011
000
115  
116 # get the stemmer to normalize the words, default is english.
117 4 my $isoLangCode = 'en';
118 4 ▶ $isoLangCode = lc $Parameters{isoLangCode} if (exists ($Parameters{isoLangCode}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Parameters{"isoLangCode"} 0 4 4
119 4 $Self->{stemmer} = Lingua::Stem::Snowball->new (lang => $isoLangCode, encode => 'UTF-8');
120  
121 # get the POS tagger.
122 4 ▶ if ($isoLangCode eq 'en')
Branch: 1 branch
BranchTrueFalseTotal
if ($isoLangCode eq 'en') { } 4 0 4
123 {
124 4 $Self->{tagger} = new Lingua::EN::Tagger;
125 }
126 else
127 {
128 0 croak "parameter isoLangCode must be 'en'.\n";
129 }
130  
131 # read in the part of speech tags.
132 4 $Self->_getPartOfSpeechTags ();
133  
134 # get the ending sentence part of speech tag which will be
135 # used to break text into array of sentences.
136 4 ▶ unless (exists ($Parameters{endingSentenceTag}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Parameters{"endingSentenceTag"}) 4 0 4
137 {
138 4 $Parameters{endingSentenceTag} = 'PP';
139 }
140 4 $Self->{endingSentenceTag} = uc $Parameters{endingSentenceTag};
141 4 $Self->{endingSentenceTag} =~ tr/A-Z//cd;
142 4 ▶ $Self->{endingSentenceTag} = '/' . $Self->{endingSentenceTag} if (substr ($Self->{endingSentenceTag}, 0, 1) ne '/');
Branch: 1 branch
BranchTrueFalseTotal
if substr($Self->{"endingSentenceTag"}, 0, 1) ne "/" 4 0 4
143  
144 # get the list of parts of speech to keep when filtering.
145 # the default is to keep only nouns and adjetives.
146 4 $Self->{hashOfPOSTagsToKeep} = $Self->_getHashOfPOSTagsToKeep (%Parameters, instantiation => 1);
147 4 return $Self;
148 }
149  
150 =head1 METHODS
151  
152 =head2 C<getStemmedAndTaggedText>
153  
154 getStemmedAndTaggedText (@Text, $Text, \@Text)
155  
156 The method C<getStemmedAndTaggedText> returns a hierarchy of array references containing the stemmed words,
157 the original words, their part-of-speech tag, and their word position index within the original text.
158 The hierarchy is of the form
159  
160 [
161 [ # sentence level: first sentence.
162 [ # word level: first word.
163 stemmed word, original word, part-of-speech tag, word index, word position, word length
164 ]
165 [ # word level: second word.
166 stemmed word, original word, part-of-speech tag, word index, word position, word length
167 ]
168 ...
169 ]
170 [ # sentence level: second sentence.
171 [ # word level: first word.
172 stemmed word, original word, part-of-speech tag, word index, word position, word length
173 ]
174 [ # word level: second word.
175 stemmed word, original word, part-of-speech tag, word index, word position, word length
176 ]
177 ...
178 ]
179 ]
180  
181 Its only parameters are any combination of strings of text as scalars, references to
182 scalars, arrays of strings of text, or references to arrays of strings of text, etc...
183 The following examples below show the various ways to call the method; note that the constants
184 Text::StemTagPOS::WORD_STEMMED,
185 Text::StemTagPOS::WORD_ORIGINAL,
186 Text::StemTagPOS::WORD_POSTAG,
187 Text::StemTagPOS::WORD_INDEX,
188 Text::StemTagPOS::WORD_CHAR_POSITION,
189 Text::StemTagPOS::WORD_CHAR_LENGTH,
190 Text::StemTagPOS::WORD_SENTENCE_ID, and
191 Text::StemTagPOS::WORD_USER_DEFINED,
192 are used to access the information about each word.
193  
194 use Text::StemTagPOS;
195 use Data::Dump qw(dump);
196 my $stemTagger = Text::StemTagPOS->new;
197 my $text = 'The first sentence. Sentence number two.';
198 my $listOfStemmedTaggedSentences = $stemTagger->getStemmedAndTaggedText ($text);
199 dump $listOfStemmedTaggedSentences;
200  
201 # dumps:
202 # [
203 # [
204 # ["the", "The", "/DET", 0, 0, 3, 0],
205 # [" ", " ", "/PGP", 1, 3, 1, 0],
206 # ["first", "first", "/JJ", 2, 4, 5, 0],
207 # [" ", " ", "/PGP", 3, 9, 1, 0],
208 # ["sentenc", "sentence", "/NN", 4, 10, 8, 0],
209 # [".", ".", "/PP", 5, 18, 1, 0],
210 # [" ", " ", "/PGP", 6, 19, 1, 0],
211 # ],
212 # [
213 # ["sentenc", "Sentence", "/NN", 7, 20, 8, 1],
214 # [" ", " ", "/PGP", 8, 28, 1, 1],
215 # ["number", "number", "/NN", 9, 29, 6, 1],
216 # [" ", " ", "/PGP", 10, 35, 1, 1],
217 # ["two", "two", "/CD", 11, 36, 3, 1],
218 # [".", ".", "/PP", 12, 39, 1, 1],
219 # ],
220 # ]
221  
222 my $word = $listOfStemmedTaggedSentences->[0][0];
223 print
224 'WORD_STEMMED: ' .
225 "'" . $word->[Text::StemTagPOS::WORD_STEMMED] . "'\n" .
226 'WORD_ORIGINAL: ' .
227 "'" . $word->[Text::StemTagPOS::WORD_ORIGINAL] . "'\n" .
228 'WORD_POSTAG: ' .
229 "'" . $word->[Text::StemTagPOS::WORD_POSTAG] . "'\n" .
230 'WORD_INDEX: ' .
231 $word->[Text::StemTagPOS::WORD_INDEX] . "\n" .
232 'WORD_CHAR_POSITION: ' .
233 $word->[Text::StemTagPOS::WORD_CHAR_POSITION] . "\n" .
234 'WORD_CHAR_LENGTH: ' .
235 $word->[Text::StemTagPOS::WORD_CHAR_LENGTH] . "\n";
236  
237 # prints:
238 # WORD_STEMMED: 'the'
239 # WORD_ORIGINAL: 'The'
240 # WORD_POSTAG: '/DET'
241 # WORD_INDEX: 0
242 # WORD_CHAR_POSITION: 0
243 # WORD_CHAR_LENGTH: 3
244  
245 The following example shows the various ways the text can be passed to the method:
246  
247 use Text::StemTagPOS;
248 use Data::Dump qw(dump);
249 my $stemTagger = Text::StemTagPOS->new;
250 my $text = 'This is a sentence with seven words.';
251 dump $stemTagger->getStemmedAndTaggedText ($text,
252 [$text, \$text], ($text, \$text));
253  
254 =cut
255  
256 sub getStemmedAndTaggedText
257 4349 ▶{
Subroutine getStemmedAndTaggedText: called CC=6 SCAR=17.9
Pod documented
258 4349 my ($Self, @Text) = @_;
259  
260 # convert the data to process to a list of strings.
261 4349 my $ListOfStrings = $Self->_flattenList (@Text);
262  
263 4349 my @sentences;
264 4349 my $initialIndex = 0;
265 4349 foreach my $text (@$ListOfStrings)
266 {
267 # skip undefined text.
268 4349 ▶ next unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 4349 4349
269  
270 # replace all backslashes with a space, since the tagger adds these.
271 4349 my $cleanedText = $text;
272 4349 $cleanedText =~ tr/\/\-/ /;
273 4349 ▶ if ($cleanedText !~ m/^\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($cleanedText =~ /^\s*$/) 4349 0 4349
274 {
275 # tag the text.
276 4349 my $taggedText = $Self->{tagger}->get_readable ($cleanedText);
277  
278 # convert to a list of sentence word,tag pairs.
279 4349 my $listOfSentences = $Self->_convertTextToListOfSentenceWordTags ($taggedText);
280  
281 # add the position in the original text and the length of the words.
282 4349 $listOfSentences = $Self->_addPositionLengthWordInfo ($initialIndex, $text, $listOfSentences);
283  
284 # save the list of sentences.
285 4349 push @sentences, @$listOfSentences;
286 }
287  
288 # update the initial index for the next string.
289 4349 $initialIndex += length $text;
290 }
291  
292 4349 my $wordIndex = 0;
293 4349 foreach my $sentenceList (@sentences)
294 {
295 # pull off the words in the sentence and put them in a list.
296 # words must be encoded to utf8 for stemmer.
297 4372 my @wordList = map {encode("utf8", $_->[WORD_ORIGINAL])} @$sentenceList;
298  
299 # stem the list of words.
300 4372 $Self->{stemmer}->stem_in_place (\@wordList);
301  
302 # insert the stemmed words to the list as:
303 # [stemmed word, original word, part of speech tag, word index, word position, word length]
304 4372 ▶ for (my $i = 0; $i < @wordList; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @wordList) 28735 4372 33107
305 {
306 28735 my $wordTags = $sentenceList->[$i];
307 # insert the stemmed word and convert it from utf8 to Perl's internal format.
308 28735 $wordTags->[WORD_STEMMED] = lc decode ('utf8', $wordList[$i]);
309 28735 $wordTags->[WORD_INDEX] = $wordIndex++;
310 }
311 }
312  
313 4349 return \@sentences;
314 }
315  
316 # add the position of the words in the original text to the info about each
317 # of the words. really should rewrite Lingua::EN::Tagger to provide this
318 # information; sloppy to recompute it and method used is fragile.
319 sub _addPositionLengthWordInfo
320 4349 ▶{
Subroutine _addPositionLengthWordInfo: called CC=17 SCAR=28.4
321 4349 my ($Self, $PositionOffset, $Text, $ListOfSentences) = @_;
322  
323 4349 my $startingIndex = 0;
324 4349 my @listOfWords;
325 4349 foreach my $sentence (@$ListOfSentences)
326 {
327 4372 foreach my $word (@$sentence)
328 {
329 # store all the words for subsequent analysis for missing words.
330 16587 push @listOfWords, $word;
331  
332 # get and store the length of the word.
333 16587 my $length = length ($word->[WORD_ORIGINAL]);
334 16587 $word->[WORD_CHAR_LENGTH] = $length;
335  
336 # default position is -1, like index function.
337 16587 $word->[WORD_CHAR_POSITION] = -1;
338  
339 # find the starting position of the word.
340 16587 my $position = index ($Text, $word->[WORD_ORIGINAL], $startingIndex);
341  
342 16587 ▶ if ($position >= $startingIndex)
Branch: 1 branch
BranchTrueFalseTotal
if ($position >= $startingIndex) 16573 14 16587
343 {
344 # add position and update starting position for next search.
345 16573 $word->[WORD_CHAR_POSITION] = $PositionOffset + $position;
346 16573 $startingIndex = $position + $length;
347 }
348 }
349 }
350  
351 # estimate the positions of the missing words.
352 4349 my $totalWords = @listOfWords;
353 4349 my $i = -1;
354 4349 ▶ while (++$i < $totalWords)
Branch: 1 branch
BranchTrueFalseTotal
if (++$i < $totalWords) 16573 4349 20922
355 {
356 16573 ▶ if ($listOfWords[$i]->[WORD_CHAR_POSITION] == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($listOfWords[$i][4] == -1) 12 16561 16573
357 {
358 # get the starting and ending index of the sequence of missing words.
359 12 my $noPositionStartIndex = $i;
360 12 ▶ while (++$i < $totalWords)
Branch: 1 branch
BranchTrueFalseTotal
if (++$i < $totalWords) 14 0 14
361 {
362 14 ▶ last if ($listOfWords[$i]->[WORD_CHAR_POSITION] != -1);
Branch: 1 branch
BranchTrueFalseTotal
if $listOfWords[$i][4] != -1 12 2 14
363 }
364 12 my $noPositionEndIndex = $i;
365 12 my $totalMissingWords = $noPositionEndIndex - $noPositionStartIndex;
366 12 ▶ $totalMissingWords = 0 if ($totalMissingWords < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $totalMissingWords < 0 0 12 12
367  
368 # get the substring corresponding to the missing words.
369  
370 # compute the starting index of the substring.
371 12 my $startIndexPosition = $PositionOffset;
372 12 ▶ if ($noPositionStartIndex > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($noPositionStartIndex > 0) 12 0 12
373 {
374 12 $startIndexPosition = $listOfWords[$noPositionStartIndex - 1]->[WORD_CHAR_POSITION] + $listOfWords[$noPositionStartIndex - 1]->[WORD_CHAR_LENGTH];
375 }
376  
377 # get the ending string index.
378 12 my $endIndexPosition = length $Text;
379 12 ▶ if ($noPositionEndIndex < $totalWords)
Branch: 1 branch
BranchTrueFalseTotal
if ($noPositionEndIndex < $totalWords) 12 0 12
380 {
381 12 $endIndexPosition = $listOfWords[$noPositionEndIndex]->[WORD_CHAR_POSITION];
382 }
383  
384 # get the substring.
385 12 my $substring = substr ($Text, $startIndexPosition - $PositionOffset, $endIndexPosition - $startIndexPosition);
386  
387 # extract the non-white space items.
388 12 my @listOfTokens;
389 12 ▶ while ($substring =~ m/([^\s]+)/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($substring =~ /([^\s]+)/g) 14 12 26
390 {
391 14 push @listOfTokens, [$-[0], $+[0] - $-[1], $1];
392 }
393  
394 12 my $substringsToMatch = @listOfTokens;
395 12 ▶ $substringsToMatch = $totalMissingWords if ($totalMissingWords < $substringsToMatch);
Branch: 1 branch
BranchTrueFalseTotal
if $totalMissingWords < $substringsToMatch 0 12 12
396 12 ▶ for (my $i = 0; $ i < $substringsToMatch; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $substringsToMatch) 14 12 26
397 {
398 14 $listOfWords[$noPositionStartIndex + $i]->[WORD_CHAR_POSITION] = $startIndexPosition + $listOfTokens[$i]->[0];
399 14 $listOfWords[$noPositionStartIndex + $i]->[WORD_CHAR_LENGTH] = $listOfTokens[$i]->[1];
400 }
401 }
402 }
403
404 # add the gaps.
405 # still need some work here to get the words into the list of sentences that they belong in.
406 4349 my $listOfWordsAndGaps = _addGapsToListOfWords (listOfWords => \@listOfWords, stringLength => length ($Text), positionOffset => $PositionOffset);
407
408 # add the sentence id to each gap.
409 4349 my $currentSentenceId = 0;
410 4349 my @newListOfSentences;
411 4349 foreach my $word (@$listOfWordsAndGaps)
412 {
413 28735 ▶ $newListOfSentences[$currentSentenceId] = [] unless defined $newListOfSentences[$currentSentenceId];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newListOfSentences[$currentSentenceId] 4349 24386 28735
414  
415 28735 ▶ if ($word->[WORD_POSTAG] eq '/PGP')
Branch: 1 branch
BranchTrueFalseTotal
if ($word->[2] eq '/PGP') { } 12148 16587 28735
416 {
417 12148 $word->[WORD_SENTENCE_ID] = $currentSentenceId;
418 12148 $word->[WORD_ORIGINAL] = substr ($Text, $word->[WORD_CHAR_POSITION] - $PositionOffset, $word->[WORD_CHAR_LENGTH]);
419 }
420 else
421 {
422 16587 $currentSentenceId = $word->[WORD_SENTENCE_ID];
423 }
424
425 28735 push @{$newListOfSentences[$currentSentenceId]}, $word;
426 }
427
428 4349 return \@newListOfSentences;
429 }
430  
431  
432 sub _addGapsToListOfWords
433 4349 ▶{
Subroutine _addGapsToListOfWords: called CC=9 SCAR=28.7
434 # get the parameters.
435 4349 my %Parameters = @_;
436
437 # get the length of the original string.
438 4349 my $stringLength = $Parameters{stringLength};
439  
440 # get the list of words.
441 4349 my $listOfWords = $Parameters{listOfWords};
442
443 # get the position offset if it exists and is defined.
444 4349 my $positionOffset = 0;
445 4349 ▶ $positionOffset = $Parameters{positionOffset} if (exists ($Parameters{positionOffset}) && defined ($Parameters{positionOffset}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Parameters{"positionOffset"} and defined $Parameters{"positionOffset"} 4349 0 4349
Condition33%1/3
exists $Parameters{"positionOffset"} and defined $Parameters{"positionOffset"}
!ll&&!rl&&r
004349
MC/DC0%0/2
exists $Parameters{"positionOffset"} and defined $Parameters{"positionOffset"}
exists $Parameters{"positionOffset"} defined $Parameters{"positionOffset"}
Truth table33%1/3
A and B
A= exists $Parameters{"positionOffset"}B= defined $Parameters{"positionOffset"}
ABresult
0X0
100
111
446  
447 # build the list of complete tokens.
448 4349 my @listOfWordsAndGaps = @$listOfWords;
449  
450 # get the list of missing substring positions.
451 4349 my $listOfMissingSubstrings = _getListOfMissingSubstringPositions(listOfSubstringPositions => $listOfWords, stringLength => $stringLength, positionOffset => $positionOffset);
452
453 # add the gaps to the list.
454 4349 foreach my $gapInfo (@$listOfMissingSubstrings)
455 {
456 12148 $gapInfo->[WORD_STEMMED] = ' ';
457 12148 $gapInfo->[WORD_ORIGINAL] = undef;
458 12148 $gapInfo->[WORD_POSTAG] = '/PGP';
459 12148 $gapInfo->[WORD_INDEX] = undef;
460 }
461  
462 # sort the substrings by position.
463 4349 @listOfWordsAndGaps = sort { $a->[WORD_CHAR_POSITION] <=> $b->[WORD_CHAR_POSITION] } (@listOfWordsAndGaps, @$listOfMissingSubstrings);
464  
465 # if test is true, make sure things were computed correctly.
466 4349 ▶ if (exists($Parameters{test}) && $Parameters{test})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Parameters{"test"} and $Parameters{"test"}) 0 4349 4349
Condition33%1/3
exists $Parameters{"test"} and $Parameters{"test"}
!ll&&!rl&&r
434900
MC/DC0%0/2
exists $Parameters{"test"} and $Parameters{"test"}
exists $Parameters{"test"} $Parameters{"test"}
Truth table33%1/3
A and B
A= exists $Parameters{"test"}B= $Parameters{"test"}
ABresult
0X0
100
111
467 {
468 0 my $totalSubstrings = @listOfWordsAndGaps;
469  
470 0 ▶ for (my $i = 1 ; $i < $totalSubstrings ; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $totalSubstrings) 0 0 0
471 {
472  
473 # make sure the strings are sorted.
474 0 ▶ if ($listOfWordsAndGaps[ $i - 1 ]->[WORD_CHAR_POSITION] > $listOfWordsAndGaps[$i]->[WORD_CHAR_POSITION])
Branch: 1 branch
BranchTrueFalseTotal
if ($listOfWordsAndGaps[$i - 1][4] > $listOfWordsAndGaps[$i][4]) 0 0 0
475 {
476 0 croak 'error: substrings in $listOfSubstringInfo are not sorted and should be.';
477 }
478  
479 # make sure the strings have at least one character.
480 0 ▶ if ($listOfWordsAndGaps[ $i - 1 ]->[WORD_CHAR_LENGTH] < 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($listOfWordsAndGaps[$i - 1][5] < 1) 0 0 0
481 {
482 0 croak 'error: substrings in $listOfSubstringInfo has length less than one.';
483 }
484 }
485 }
486  
487 # returns the complete list of tokens sorted by their starting index in ascending order.
488 4349 return \@listOfWordsAndGaps;
489 }
490  
491  
492  
493 # routine returns an array reference of the gaps or missing substrings given
494 # a list of substrings. for example, if listOfSubstringPositions is
495 # [[2,4], [9,2], [11,5], [20,1]] and stringLength is 25, then the list returned
496 # is [[0, 2], [6, 3], [16, 4], [21, 4]].
497 sub _getListOfMissingSubstringPositions # (listOfSubstringPositions => \@, stringLength => n, positionOffset => n)
498 4349 ▶{
Subroutine _getListOfMissingSubstringPositions: called CC=25 SCAR=46.2
499 # get the parameters.
500 4349 my %Parameters = @_;
501
502 # get the position offset if it exists and is defined.
503 4349 my $positionOffset = 0;
504 4349 ▶ $positionOffset = $Parameters{positionOffset} if (exists ($Parameters{positionOffset}) && defined ($Parameters{positionOffset}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Parameters{"positionOffset"} and defined $Parameters{"positionOffset"} 4349 0 4349
Condition33%1/3
exists $Parameters{"positionOffset"} and defined $Parameters{"positionOffset"}
!ll&&!rl&&r
004349
MC/DC0%0/2
exists $Parameters{"positionOffset"} and defined $Parameters{"positionOffset"}
exists $Parameters{"positionOffset"} defined $Parameters{"positionOffset"}
Truth table33%1/3
A and B
A= exists $Parameters{"positionOffset"}B= defined $Parameters{"positionOffset"}
ABresult
0X0
100
111
505  
506 # if listOfSubstringPositions is not defined, we have one special case.
507 4349 ▶ if (!defined($Parameters{listOfSubstringPositions}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Parameters{"listOfSubstringPositions"}) 0 4349 4349
508 {
509 0 ▶ if (!defined($Parameters{stringLength}))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $Parameters{'stringLength'}) { } 0 0 0
510 {
511  
512 # no parameters defined, so return the empty list.
513 0 return [];
514 }
515 else
516 {
517 0 ▶ if (int($Parameters{stringLength}) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (int $Parameters{'stringLength'} > 0) { } 0 0 0
518 {
519  
520 # positive string length, but no substrings, so gap is entire string.
521 0 return [ 0, int($Parameters{stringLength}) - 1 ];
522 }
523 else
524 {
525  
526 # non-positive string length, so return empty list.
527 0 return [];
528 }
529 }
530 }
531  
532 # get the list of [WORD_CHAR_POSITION, WORD_CHAR_LENGTH].
533 4349 my $listOfSubstringPositions = $Parameters{listOfSubstringPositions};
534  
535 # get the number of subtrings.
536 4349 my $totalSubstrings = $#$listOfSubstringPositions + 1;
537  
538 # skip substrings having length less than one or a negative position.
539 4349 my @filteredListOfSubstringPositions;
540 4349 ▶ for (my $i = 0 ; $i < $totalSubstrings ; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $totalSubstrings) 16587 4349 20936
541 {
542 16587 ▶ next if ($listOfSubstringPositions->[$i][WORD_CHAR_LENGTH] < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $listOfSubstringPositions->[$i][5] < 1 0 16587 16587
543 16587 ▶ next if ($listOfSubstringPositions->[$i][WORD_CHAR_POSITION] < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $listOfSubstringPositions->[$i][4] < 0 0 16587 16587
544 16587 push @filteredListOfSubstringPositions, $listOfSubstringPositions->[$i];
545 }
546 4349 $listOfSubstringPositions = \@filteredListOfSubstringPositions;
547 4349 $totalSubstrings = $#$listOfSubstringPositions + 1;
548  
549 # get the entire strings length if defined.
550 4349 my $stringLength;
551 4349 ▶ $stringLength = int abs $Parameters{stringLength} if exists $Parameters{stringLength};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Parameters{"stringLength"} 4349 0 4349
552  
553 # if $stringLength is undefined use the last substring to compute the length
554 # of the entire string; the string will not end with a gap in this case.
555 4349 ▶ if (!defined($stringLength) && $totalSubstrings)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stringLength and $totalSubstrings) 0 4349 4349
Condition33%1/3
not defined $stringLength and $totalSubstrings
!ll&&!rl&&r
434900
MC/DC0%0/2
not defined $stringLength and $totalSubstrings
not defined $stringLength $totalSubstrings
Truth table33%1/3
not(A) and B
A= not defined $stringLengthB= $totalSubstrings
ABresult
0X0
100
111
556 {
557 0 $stringLength = 0;
558 0 foreach my $currentSubstringInfo (@$listOfSubstringPositions)
559 {
560 0 my $last = $currentSubstringInfo->[WORD_CHAR_POSITION] + $currentSubstringInfo->[WORD_CHAR_LENGTH];
561 0 ▶ $stringLength = $last if $last > $stringLength;
Branch: 1 branch
BranchTrueFalseTotal
if $last > $stringLength 0 0 0
562 }
563 0 $stringLength -= $positionOffset;
564 }
565  
566 # if $stringLength is not defined at this point there are no gaps.
567 4349 ▶ return [] unless ((defined $stringLength) && ($stringLength > 0));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stringLength and $stringLength > 0 0 4349 4349
Condition33%1/3
defined $stringLength and $stringLength > 0
!ll&&!rl&&r
004349
MC/DC0%0/2
defined $stringLength and $stringLength > 0
defined $stringLength $stringLength > 0
Truth table33%1/3
A and B
A= defined $stringLengthB= $stringLength > 0
ABresult
0X0
100
111
568  
569 # if $totalSubstrings is zero, then the entire string is a gap.
570 4349 ▶ unless ($totalSubstrings)
Branch: 1 branch
BranchTrueFalseTotal
unless ($totalSubstrings) 0 4349 4349
571 {
572 0 my @substringGapInfo;
573 0 $substringGapInfo[WORD_CHAR_POSITION] = 0;
574 0 $substringGapInfo[WORD_CHAR_LENGTH] = $stringLength;
575 0 return [ \@substringGapInfo ];
576 }
577  
578 # sort the pairs by their position.
579 4349 my @listOfSubstringPositions = sort { $a->[WORD_CHAR_POSITION] <=> $b->[WORD_CHAR_POSITION] } @$listOfSubstringPositions;
580  
581 # @listOfMissingSubstringPositions holds all the gaps.
582 4349 my @listOfMissingSubstringPositions;
583  
584 # get the first substring position.
585 4349 my $currentSubstringInfo = $listOfSubstringPositions[0];
586  
587 # if the first substring does not start with position 0, add the beginning gap.
588 4349 ▶ if ($currentSubstringInfo->[WORD_CHAR_POSITION] > $positionOffset)
Branch: 1 branch
BranchTrueFalseTotal
if ($currentSubstringInfo->[4] > $positionOffset) 0 4349 4349
589 {
590 0 my @substringGapInfo;
591 0 $substringGapInfo[WORD_CHAR_POSITION] = $positionOffset;
592 0 $substringGapInfo[WORD_CHAR_LENGTH] = $currentSubstringInfo->[WORD_CHAR_POSITION] - $positionOffset;
593 0 push @listOfMissingSubstringPositions, \@substringGapInfo;
594 }
595
596 # compute the gaps.
597 4349 ▶ for (my $i = 1 ; $i < $totalSubstrings ; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $totalSubstrings) 12238 4349 16587
598 {
599  
600 # get the information about the previous and current substrings.
601 12238 my $previousSubstringInfo = $listOfSubstringPositions[ $i - 1 ];
602 12238 my $currentSubstringInfo = $listOfSubstringPositions[$i];
603  
604 # compute the starting index and length of the gap.
605 12238 my $gapStartPosition = $previousSubstringInfo->[WORD_CHAR_POSITION] + $previousSubstringInfo->[WORD_CHAR_LENGTH];
606 12238 my $gapEndPosition = $currentSubstringInfo->[WORD_CHAR_POSITION] - 1;
607 12238 my $gapLength = $gapEndPosition - $gapStartPosition + 1;
608  
609 # if the gap is not a positive size, skip it.
610 # maybe a warning should be logged since it really should not happen.
611 12238 ▶ if ($gapLength > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($gapLength > 0) 12148 90 12238
612 {
613  
614 # store the information about the gap.
615 12148 my @substringGapInfo;
616 12148 $substringGapInfo[WORD_CHAR_POSITION] = $gapStartPosition;
617 12148 $substringGapInfo[WORD_CHAR_LENGTH] = $gapLength;
618 12148 push @listOfMissingSubstringPositions, \@substringGapInfo;
619 }
620 }
621  
622 # add any trailing gap to the list.
623 4349 $currentSubstringInfo = $listOfSubstringPositions[-1];
624 4349 ▶ if ($currentSubstringInfo->[WORD_CHAR_POSITION] + $currentSubstringInfo->[WORD_CHAR_LENGTH] < $stringLength)
Branch: 1 branch
BranchTrueFalseTotal
if ($currentSubstringInfo->[4] + $currentSubstringInfo->[5] < $stringLength) 0 4349 4349
625 {
626 0 my @substringGapInfo;
627 0 $substringGapInfo[WORD_CHAR_POSITION] = $currentSubstringInfo->[WORD_CHAR_POSITION] + $currentSubstringInfo->[WORD_CHAR_LENGTH];
628 0 $substringGapInfo[WORD_CHAR_LENGTH] = $stringLength - ($currentSubstringInfo->[WORD_CHAR_POSITION] + $currentSubstringInfo->[WORD_CHAR_LENGTH]);
629 0 push @listOfMissingSubstringPositions, \@substringGapInfo;
630 }
631  
632 # if test is true, check if gaps were computed correctly.
633 4349 ▶ if (exists($Parameters{test}) && $Parameters{test})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Parameters{"test"} and $Parameters{"test"}) 0 4349 4349
Condition33%1/3
exists $Parameters{"test"} and $Parameters{"test"}
!ll&&!rl&&r
434900
MC/DC0%0/2
exists $Parameters{"test"} and $Parameters{"test"}
exists $Parameters{"test"} $Parameters{"test"}
Truth table33%1/3
A and B
A= exists $Parameters{"test"}B= $Parameters{"test"}
ABresult
0X0
100
111
634 {
635 my @allSubstrings =
636 0 sort { $a->[WORD_CHAR_POSITION] <=> $b->[WORD_CHAR_POSITION] } (@listOfSubstringPositions, @listOfMissingSubstringPositions);
637 0 my $totalSubstrings = @allSubstrings;
638  
639 0 ▶ for (my $i = 1 ; $i < $totalSubstrings ; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $totalSubstrings) 0 0 0
640 {
641  
642 # make sure the strings are sorted.
643 0 ▶ if ($allSubstrings[ $i - 1 ]->[WORD_CHAR_POSITION] + $allSubstrings[ $i - 1 ]->[WORD_CHAR_LENGTH] < $allSubstrings[$i]->[WORD_CHAR_POSITION])
Branch: 1 branch
BranchTrueFalseTotal
if ($allSubstrings[$i - 1][4] + $allSubstrings[$i - 1][5] < $allSubstrings[$i][4]) 0 0 0
644 {
645 0 my $logger = Log::Log4perl->get_logger();
646 0 $logger->logdie("error: missed computing a gap.");
647 }
648 }
649 }
650  
651 # returns the list of missing substrings.
652 4349 return \@listOfMissingSubstringPositions;
653 }
654  
655  
656 =head2 C<getTaggedTextToKeep>
657  
658 getTaggedTextToKeep (listOfStemmedTaggedSentences => [...],
659 listOfPOSTypesToKeep => [...], listOfPOSTagsToKeep => [...]);
660  
661 The method C<getTaggedTextToKeep> returns all the array references of the words
662 that have a part-of-speech tag that is of a type specified by
663 C<listOfPOSTypesToKeep> or C<listOfPOSTagsToKeep>. The word lists
664 returned have the same hierarchical sentence structure used by C<listOfStemmedTaggedSentences>.
665 Note C<listOfPOSTypesToKeep> and C<listOfPOSTagsToKeep>
666 are optional parameters, if neither is defined, then the values used when the
667 object was instantiated are used. If one of them is defined, its values override the default
668 values.
669  
670 =over
671  
672 =item C<listOfStemmedTaggedSentences>
673  
674 listOfStemmedTaggedSentences => [...]
675  
676 C<listOfStemmedTaggedSentences> is the array reference returned by
677 C<getStemmedAndTaggedText> or
678 a previous call to C<getTaggedTextToKeep>.
679  
680 =item C<listOfPOSTypesToKeep> and/or C<listOfPOSTagsToKeep>
681  
682 listOfPOSTypesToKeep => [...], listOfPOSTagsToKeep => [...]
683  
684 C<listOfPOSTypesToKeep> and C<listOfPOSTagsToKeep> define the list of
685 parts-of-speech types to be retained when filtering previously tagged text.
686 Permitted values for C<listOfPOSTypesToKeep> are
687 are 'ALL', 'ADJECTIVES', 'ADVERBS', 'CONTENT_WORDS', 'NOUNS', 'PUNCTUATION',
688 'TEXTRANK_WORDS', and 'VERBS'. For the possible value of C<listOfPOSTagsToKeep>
689 see the method C<getListOfPartOfSpeechTags>.
690 Note C<listOfPOSTypesToKeep> and C<listOfPOSTagsToKeep>
691 are optional parameters, if neither is defined, then the values used when the
692 object was instantiated are used. If one of them is defined, its values override the default
693 values.
694  
695 =back
696  
697 use Text::StemTagPOS;
698 use Data::Dump qw(dump);
699 my $stemTagger = Text::StemTagPOS->new;
700 my $text = 'This is the first sentence. This is the last sentence.';
701 my $listOfStemmedTaggedSentences = $stemTagger->getStemmedAndTaggedText ($text);
702 dump $stemTagger->getTaggedTextToKeep (
703 listOfStemmedTaggedSentences => $listOfStemmedTaggedSentences);
704  
705 # dumps:
706 # [
707 # [
708 # ["first", "first", "/JJ", 6, 12, 5, 0],
709 # ["sentenc", "sentence", "/NN", 8, 18, 8, 0],
710 # ],
711 # [
712 # ["last", "last", "/JJ", 17, 40, 4, 1],
713 # ["sentenc", "sentence", "/NN", 19, 45, 8, 1],
714 # ],
715 # ]
716  
717 =cut
718  
719 sub getTaggedTextToKeep
720 0 ▶{
Subroutine getTaggedTextToKeep: not called CC=4 SCAR=30.0
Pod documented
721 0 my ($Self, %Parameters) = @_;
722  
723 # get the list of sentences.
724 0 my $listOfStemmedTaggedSentences = $Parameters{listOfStemmedTaggedSentences};
725
726 # get the hash of tags to keep.
727 0 my $hashOfPOSToKeep = $Self->_getHashOfPOSTagsToKeep (%Parameters);
728  
729 # copy off the tokens to keep in each sentence.
730 0 my @listOfFilteredSentences;
731 0 foreach my $sentence (@$listOfStemmedTaggedSentences)
732 {
733 0 my @newSentence;
734 0 foreach my $token (@$sentence)
735 {
736 0 ▶ if (exists ($hashOfPOSToKeep->{$token->[WORD_POSTAG]}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$hashOfPOSToKeep{$token->[2]}) 0 0 0
737 {
738 0 push @newSentence, $token;
739 }
740 }
741 #push @listOfFilteredSentences, \@newSentence if (@newSentence > 0);
742 0 push @listOfFilteredSentences, \@newSentence;
743 }
744  
745 # return the list of filtered sentences.
746 0 return \@listOfFilteredSentences;
747 }
748  
749 =head2 C<getWordsPhrasesInTaggedText>
750  
751 getWordsPhrasesInTaggedText (listOfStemmedTaggedSentences => ...,
752 listOfPhrasesToFind => [...], listOfPOSTypesToKeep => [...],
753 listOfPOSTagsToKeep => [...]);
754  
755 The method C<getWordsPhrasesInTaggedText> returns a reference to an array where
756 each entry in the array corresponds
757 to the word or phrase in C<listOfPhrasesToFind>. The value of each entry is a list
758 of word indices
759 where the words or phrases were found. Each list contains integer
760 pairs of the form [first-word-index, last-word-index] where first-word-index is the index to the first
761 word of the phrase and last-word-index the index of the last word. The values of the index are those
762 assigned to the stemmed and tagged word in C<listOfStemmedTaggedSentences>.
763  
764 [
765 [ # first phrase locations
766 [first word index, last word index],
767 [first word index, last word index], ...]
768 ]
769 [ # second phrase locations
770 [first word index, last word index],
771 [first word index, last word index], ...]
772 ]
773 ...
774 ]
775  
776 =over
777  
778 =item C<listOfStemmedTaggedSentences>
779  
780 listOfStemmedTaggedSentences => [...]
781  
782 C<listOfStemmedTaggedSentences> is the array reference returned by C<getStemmedAndTaggedText> or
783 C<getTaggedTextToKeep>.
784  
785 =item C<listOfPhrasesToFind>
786  
787 listOfPhrasesToFind => [...]
788  
789 C<listOfPhrasesToFind> is an array reference containing a list of strings of
790 text that are either single words or phrases that are to be located in the text
791 provided by C<listOfStemmedTaggedSentences>. Before the words or phrases are located they are filtered
792 using C<listOfPOSTypesToKeep> or C<listOfPOSTagsToKeep>.
793  
794  
795 =item C<listOfPOSTypesToKeep> and/or C<listOfPOSTagsToKeep>
796  
797 listOfPOSTypesToKeep => [...], listOfPOSTagsToKeep => [...]
798  
799 C<listOfPOSTypesToKeep> and C<listOfPOSTagsToKeep> defines the list of
800 parts-of-speech types to be retained when filtering previously tagged text.
801 Permitted values for C<listOfPOSTypesToKeep> are
802 are 'ALL', 'ADJECTIVES', 'ADVERBS', 'CONTENT_WORDS', 'NOUNS', 'PUNCTUATION',
803 'TEXTRANK_WORDS', and 'VERBS'. For the possible value of C<listOfPOSTagsToKeep>
804 see the method C<getListOfPartOfSpeechTags>.
805 Note C<listOfPOSTypesToKeep> and C<listOfPOSTagsToKeep>
806 are optional parameters, if neither is defined, then the values used when the
807 object was instantiated are used. If one of them is defined, its values override the default
808 values.
809  
810 =back
811  
812 The code below illustrates the output format:
813  
814 use Text::StemTagPOS;
815 use Data::Dump qw(dump);
816 my $stemTagger = Text::StemTagPOS->new;
817 my $text = 'This is the first sentence. This is the last sentence.';
818 my $listOfStemmedTaggedSentences = $stemTagger->getStemmedAndTaggedText ($text);
819 dump $listOfStemmedTaggedSentences;
820 my $listOfWordsOrPhrasesToFind = ['first sentence','this is',
821 'third sentence', 'sentence'];
822 my $phraseLocations = $stemTagger->getWordsPhrasesInTaggedText (
823 listOfPOSTypesToKeep => [qw(ALL)],
824 listOfStemmedTaggedSentences => $listOfStemmedTaggedSentences,
825 listOfWordsOrPhrasesToFind => $listOfWordsOrPhrasesToFind);
826 dump $phraseLocations;
827 # [
828 # [[6, 8]], # 'first sentence'
829 # [[0, 2], [11, 13]], # 'this is': note period in text has index 5.
830 # [], # 'third sentence'
831 # [[8, 8], [19, 19]] # 'sentence'
832 # ]
833  
834 =cut
835  
836 sub getWordsPhrasesInTaggedText
837 300 ▶{
Subroutine getWordsPhrasesInTaggedText: called CC=14 SCAR=26.4
Pod documented
838 300 my ($Self, %Parameters) = @_;
839  
840 # stem and tag all of the word and phrase text.
841 # put all the listOfStemmedTaggedSentences into an array,
842 # create the suffix array of the listOfStemmedTaggedSentences array.
843  
844 # get the hash of tags to keep.
845 300 my $hashOfPOSToKeep = $Self->_getHashOfPOSTagsToKeep (%Parameters);
846  
847 # get the tagged text.
848 300 my $listOfStemmedTaggedSentences = $Parameters{listOfStemmedTaggedSentences};
849  
850 # create a list of the filtered stemmed words of the text.
851 300 my @wordsOfText;
852 300 foreach my $sentence (@$listOfStemmedTaggedSentences)
853 {
854 29285 foreach my $word (@$sentence)
855 {
856 # skip over the words types to ignore.
857 439619 ▶ next unless (exists ($hashOfPOSToKeep->{$word->[WORD_POSTAG]}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$hashOfPOSToKeep{$word->[2]} 0 439619 439619
858 439619 push @wordsOfText, $word;
859 }
860 }
861  
862 # create the suffix array of the tagged words, sorting by the stemmed word.
863 300 my $totalWords = @wordsOfText;
864  
865 # hash all the indices of the stemmed words in the suffix array of the text.
866 300 my %wordIndices;
867 300 ▶ for(my $index = 0; $index < $totalWords; $index++)
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $totalWords) 439619 300 439919
868 {
869 439619 my $word = $wordsOfText[$index];
870 439619 ▶ $wordIndices{$word->[WORD_STEMMED]} = [] unless exists $wordIndices{$word->[WORD_STEMMED]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $wordIndices{$word->[0]} 439619 0 439619
871 439619 push @{$wordIndices{$word->[WORD_STEMMED]}}, $index;
872 }
873  
874 # stem and part-of-speech tag the word or phrases to find.
875 300 my @listOfStemmedWordsInPhrasesToFind;
876 300 my $listOfWordsOrPhrasesToFind = $Parameters{listOfWordsOrPhrasesToFind};
877 300 foreach my $wordOrPhrase (@$listOfWordsOrPhrasesToFind)
878 {
879 4348 my $stemmed = $Self->getStemmedAndTaggedText ($wordOrPhrase);
880  
881 # flatten the words of a phrase into on list.
882 4348 my @allWords;
883 4348 foreach my $sentence (@$stemmed)
884 {
885 4348 foreach my $word (@$sentence)
886 {
887 # remove the words to ignore.
888 27476 ▶ next unless (exists ($hashOfPOSToKeep->{$word->[WORD_POSTAG]}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$hashOfPOSToKeep{$word->[2]} 11564 15912 27476
889 15912 push @allWords, $word;
890 }
891 }
892  
893 4348 push @listOfStemmedWordsInPhrasesToFind, \@allWords ;
894 }
895  
896 # given the starting index into the suffix array and a word list, return
897 # the number of words matching the word list at that index.
898 my $getMatchingWords = sub
899 4348 ▶ {
Subroutine __ANON__: called CC=4 SCAR=13.9
900 4348 my ($StartingIndex, $StemmedWordList) = @_;
901 4348 my $stemmedIndex = 0;
902 4348 my $textIndex = $StartingIndex;
903 4348 ▶ while (($textIndex < $totalWords) && ($stemmedIndex < @$StemmedWordList))
Branch: 1 branch
BranchTrueFalseTotal
if ($textIndex < $totalWords and $stemmedIndex < @$StemmedWordList) 15912 4348 20260
Condition100%3/3
$textIndex < $totalWords and $stemmedIndex < @$StemmedWordList
!ll&&!rl&&r
34431415912
MC/DC100%2/2
$textIndex < $totalWords and $stemmedIndex < @$StemmedWordList
$textIndex < $totalWords $stemmedIndex < @$StemmedWordList
Truth table100%3/3
A and B
A= $textIndex < $totalWordsB= $stemmedIndex < @$StemmedWordList
ABresult
0X0
100
111
904 {
905 15912 ▶ last if ($wordsOfText[$textIndex]->[WORD_STEMMED] cmp $StemmedWordList->[$stemmedIndex][WORD_STEMMED]);
Branch: 1 branch
BranchTrueFalseTotal
if $wordsOfText[$textIndex][0] cmp $StemmedWordList->[$stemmedIndex][0] 0 15912 15912
906 15912 $textIndex++;
907 15912 $stemmedIndex++;
908 }
909 4348 return $textIndex - $StartingIndex;
910 300 };
911  
912 # find all the phrases in the filtered text.
913 300 my @phrasesFound;
914 300 my $phraseIndex = 0;
915 300 foreach my $phraseStemmedWordList (@listOfStemmedWordsInPhrasesToFind)
916 {
917 4348 my @fullPhrasesFound;
918 4348 my @stemmedPhraseWords = @$phraseStemmedWordList;
919 4348 my $wordsInPhrase = $#stemmedPhraseWords + 1;
920  
921 # get the first word in the phrase.
922 4348 my $firstWord = $stemmedPhraseWords[0];
923 4348 ▶ if (exists ($wordIndices{$firstWord->[WORD_STEMMED]}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $wordIndices{$firstWord->[0]}) 4348 0 4348
924 {
925 # get all the indices in the suffix array that the first word occurs at.
926 4348 my $listOfStartingIndices = $wordIndices{$firstWord->[WORD_STEMMED]};
927  
928 # compute the number of matching phrase words at each position.
929 4348 foreach my $startingIndex (@$listOfStartingIndices)
930 {
931 4348 my $matchingLength = &$getMatchingWords ($startingIndex, \@stemmedPhraseWords);
932 4348 my $firstWordIndex = $wordsOfText[$startingIndex]->[WORD_INDEX];
933 4348 my $lastWordIndex = $wordsOfText[$startingIndex + $matchingLength - 1]->[WORD_INDEX];
934  
935 4348 ▶ if ($matchingLength == $wordsInPhrase)
Branch: 1 branch
BranchTrueFalseTotal
if ($matchingLength == $wordsInPhrase) 4348 0 4348
936 {
937 # full match of the phrase.
938 4348 push @fullPhrasesFound, [$firstWordIndex, $lastWordIndex];
939 }
940 }
941 }
942 4348 $phrasesFound[$phraseIndex++] = \@fullPhrasesFound;
943 }
944 300 return \@phrasesFound;
945 }
946  
947 =head2 C<getListOfPartOfSpeechTags>
948  
949 The method C<getListOfPartOfSpeechTags> takes no parameters. It returns an array
950 reference where each item in the list is of the form C<[part of speech tag, description, examples]>.
951 It is meant for getting the part-of-speech tags that can be used to populate C<listOfPOSTagsToKeep>.
952  
953 use Text::StemTagPOS;
954 use Data::Dump qw(dump);
955 my $stemTagger = Text::StemTagPOS->new;
956 dump $stemTagger->getListOfPartOfSpeechTags;
957  
958 =cut
959  
960 # this method takes no parameters. it reads in the information about the
961 # part of speech tags stored in the DATA section of this file. the information
962 # is stored in the object at posTags as an array reference containing the
963 # array [POS tag, description, examples] for each possible tag.
964 sub getListOfPartOfSpeechTags
965 0 ▶{
Subroutine getListOfPartOfSpeechTags: not called CC=1 SCAR=6.9
Pod documented
966 0 my $Self = shift;
967  
968 # return a copy of the posTags array.
969 0 my @partOfSpeechTags = map {[@$_]} @{$Self->{posTags}};
970 0 return \@partOfSpeechTags;
971 }
972  
973  
974 # _convertTextToListOfSentenceWordTags->('string of tagged words returned from xxxx');
975 sub _convertTextToListOfSentenceWordTags
976 4349 ▶{
Subroutine _convertTextToListOfSentenceWordTags: called CC=11 SCAR=24.0
977 # get the method class object.
978 4349 my $Self = $_[0];
979  
980 # split the string of tagged words into a list of the words and tags. note,
981 # all POS tags are a slash followed by 2, 3 or 4 uppercase letters.
982 4349 my @list = split (/(\/[A-Z]{2,4})/, $_[1]);
983  
984 # trim off any leading or trailing spaces from each word or tag.
985 4349 foreach my $item (@list)
986 {
987 33174 $item =~ s/^\s*//;
988 33174 $item =~ s/\s+$//;
989 }
990  
991 # restructure the list so each item is of the form [word, tag].
992 4349 my $wordOrPhrase;
993 my @taggerWordList;
994 4349 foreach my $item (@list)
995 {
996 33174 ▶ if (substr ($item, 0, 1) eq '/')
Branch: 1 branch
BranchTrueFalseTotal
if (substr($item, 0, 1) eq '/') { } 16587 16587 33174
997 {
998 # at this point the item in the list is a tag, so form the new [word,tag] pair
999 # and save it.
1000 16587 my $wordInfo = [];
1001 16587 $wordInfo->[WORD_ORIGINAL] = $wordOrPhrase;
1002 16587 $wordInfo->[WORD_POSTAG] = $item;
1003 16587 push @taggerWordList, $wordInfo;
1004 16587 $wordOrPhrase = undef;
1005 }
1006 else
1007 {
1008 # at this point we have a new word, so append it to $wordOrPhrase.
1009 16587 ▶ if (!defined ($wordOrPhrase))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $wordOrPhrase) { } 16587 0 16587
1010 {
1011 16587 $wordOrPhrase = $item;
1012 }
1013 else
1014 {
1015 0 $wordOrPhrase .= ' ' . $item;
1016 }
1017 }
1018 }
1019  
1020 # now partition the list into sentences.
1021 4349 my @sentences;
1022 4349 my $currentSentence = [];
1023 4349 foreach my $wordTag (@taggerWordList)
1024 {
1025 16587 ▶ if ($wordTag->[WORD_POSTAG] eq $Self->{endingSentenceTag})
Branch: 1 branch
BranchTrueFalseTotal
if ($wordTag->[2] eq $Self->{'endingSentenceTag'}) { } 24 16563 16587
1026 {
1027 # if the sentence has no words, skip it.
1028 24 ▶ next unless ($#$currentSentence > -1);
Branch: 1 branch
BranchTrueFalseTotal
unless $currentSentence->$#* > -1 0 24 24
1029  
1030 # add the POS sentence ender tag to the end of the sentence list.
1031 24 push @$currentSentence, $wordTag;
1032  
1033 # add the sentence to the list of sentences.
1034 24 push @sentences, $currentSentence;
1035 24 $currentSentence = [];
1036 }
1037 else
1038 {
1039 16563 push @$currentSentence, $wordTag;
1040 }
1041 }
1042  
1043 # if there is a sentence left, save it.
1044 4349 ▶ if ($#$currentSentence > -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($currentSentence->$#* > -1) 4348 1 4349
1045 {
1046 4348 push @sentences, $currentSentence;
1047 }
1048
1049 # add the sentence id to each word.
1050 4349 ▶ for (my $i = 0; $i < @sentences; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @sentences) 4372 4349 8721
1051 {
1052 4372 foreach my $word (@{$sentences[$i]})
1053 {
1054 16587 $word->[WORD_SENTENCE_ID] = $i;
1055 }
1056 }
1057  
1058 # return the list of sentences of word,tag pairs.
1059 4349 return \@sentences;
1060 }
1061  
1062  
1063 # converts a list of strings, string refs, arrays of strings, etc... to a
1064 # simpler list of strings; essentially flattening a list.
1065 sub _flattenList
1066 4349 ▶{
Subroutine _flattenList: called CC=6 SCAR=19.4
1067 4349 my ($Self, @Data) = @_;
1068  
1069 4349 my @ListOfStrings;
1070 4349 foreach my $item (@_)
1071 {
1072 8698 my $type = ref ($item);
1073 8698 ▶ if ($type eq '')
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq '') { } 4348 4350 8698
elsif ($type eq 'SCALAR') { } 1 4349 4350
elsif ($type eq 'ARRAY') { } 0 4349 4349
elsif ($type eq 'REF') { } 0 4349 4349
1074 {
1075 4348 push @ListOfStrings, $item;
1076 }
1077 elsif ($type eq 'SCALAR')
1078 {
1079 1 push @ListOfStrings, $$item;
1080 }
1081 elsif ($type eq 'ARRAY')
1082 {
1083 0 push @ListOfStrings, @{$Self->_flattenList (@$item)};
1084 }
1085 elsif ($type eq 'REF')
1086 {
1087 0 push @ListOfStrings, @{$Self->_flattenList ($$item)};
1088 }
1089 }
1090 4349 return \@ListOfStrings;
1091 }
1092  
1093  
1094 # this method takes no parameters. it reads in the information about the
1095 # part of speech tags stored in the DATA section of this file. the information
1096 # is stored in the object at posTags as an array reference containing the
1097 # array [POS tag, description, examples] for each possible tag.
1098 sub _getPartOfSpeechTags
1099 4 ▶{
Subroutine _getPartOfSpeechTags: called CC=1 SCAR=0.0
1100 4 my $Self = shift;
1101  
1102 4 my $posTags =
1103 [
1104 ['CC', 'Conjunction, coordinating', 'and, or'],
1105 ['CD', 'Adjective, cardinal number', '3, fifteen'],
1106 ['DET', 'Determiner', 'this, each, some'],
1107 ['EX', 'Pronoun, existential there', 'there'],
1108 ['FW', 'Foreign words', ''],
1109 ['IN', 'Preposition / Conjunction', 'for, of, although, that'],
1110 ['JJ', 'Adjective', 'happy, bad'],
1111 ['JJR', 'Adjective, comparative', 'happier, worse'],
1112 ['JJS', 'Adjective, superlative', 'happiest, worst'],
1113 ['LS', 'Symbol, list item', 'A, A.'],
1114 ['MD', 'Verb, modal', "can, could, 'll"],
1115 ['NN', 'Noun', 'aircraft, data'],
1116 ['NNP', 'Noun, proper', 'London, Michael'],
1117 ['NNPS', 'Noun, proper, plural', 'Australians, Methodists'],
1118 ['NNS', 'Noun, plural', 'women, books'],
1119 ['PDT', 'Determiner, prequalifier', 'quite, all, half'],
1120 ['POS', 'Possessive', "'s"],
1121 ['PRP', 'Determiner, possessive second', 'mine, yours'],
1122 ['PRPS', 'Determiner, possessive', 'their, your'],
1123 ['RB', 'Adverb', 'often, not, very, here'],
1124 ['RBR', 'Adverb, comparative', 'faster'],
1125 ['RBS', 'Adverb, superlative', 'fastest'],
1126 ['RP', 'Adverb, particle', 'up, off, out'],
1127 ['SYM', 'Symbol', '*'],
1128 ['TO', 'Preposition', 'to'],
1129 ['UH', 'Interjection', 'oh, yes, mmm'],
1130 ['VB', 'Verb, infinitive', 'take, live'],
1131 ['VBD', 'Verb, past tense', 'took, lived'],
1132 ['VBG', 'Verb, gerund', 'taking, living'],
1133 ['VBN', 'Verb, past/passive participle', 'taken, lived'],
1134 ['VBP', 'Verb, base present form', 'take, live'],
1135 ['VBZ', 'Verb, present 3SG -s form', 'takes, lives'],
1136 ['WDT', 'Determiner, question', 'which, whatever'],
1137 ['WP', 'Pronoun, question', 'who, whoever'],
1138 ['WPS', 'Determiner, possessive & question', 'whose'],
1139 ['WRB', 'Adverb, question', 'when, how, however'],
1140 ['PP', 'Punctuation, sentence ender', '., !, ?'],
1141 ['PPC', 'Punctuation, comma', ','],
1142 ['PGP', 'Punctuation, whitespace, gap', ' '],
1143 ['PPD', 'Punctuation, dollar sign', '$'],
1144 ['PPL', 'Punctuation, quotation mark left', '``'],
1145 ['PPR', 'Punctuation, quotation mark right', "''"],
1146 ['PPS', 'Punctuation, colon, semicolon, elipsis', ':, ..., -'],
1147 ['LRB', 'Punctuation, left bracket', '(, {, ['],
1148 ['RRB', 'Punctuation, right bracket', '), }, ]']
1149 ];
1150  
1151 # store the pos array.
1152 4 $Self->{posTags} = $posTags;
1153 4 return;
1154 }
1155  
1156  
1157 sub _getListOfPOSTagsFromPOSTypesList
1158 1 ▶{
Subroutine _getListOfPOSTagsFromPOSTypesList: called CC=12 SCAR=45.0
1159 1 my ($Self, %Parameters) = @_;
1160  
1161 1 my $listOfPOSTypesToKeep = [];
1162 1 ▶ $listOfPOSTypesToKeep = $Parameters{listOfPOSTypesToKeep} if exists $Parameters{listOfPOSTypesToKeep};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Parameters{"listOfPOSTypesToKeep"} 0 1 1
1163  
1164 # ALL, NOUNS, VERBS, PUNCTUATION, ADJECTIVES, ADVERBS, CONTENT_WORDS, TEXTRANK_WORDS, VERBS
1165 1 my @listOfPOSTagsToKeep;
1166 1 foreach my $type (@$listOfPOSTypesToKeep)
1167 {
1168 0 my $ucType = uc $type;
1169 0 $ucType =~ tr/A-Z\_//cd;
1170 0 ▶ if (($ucType cmp 'NOUNS') == 0) { push @listOfPOSTagsToKeep, POSTAGS_NOUN; }
Branch: 9 branches
BranchTrueFalseTotal
if (($ucType cmp 'NOUNS') == 0) { } 0 0 0
elsif (($ucType cmp 'VERBS') == 0) { } 0 0 0
elsif (($ucType cmp 'ADJECTIVES') == 0) { } 0 0 0
elsif (($ucType cmp 'ADVERBS') == 0) { } 0 0 0
elsif (($ucType cmp 'CONTENT_ADVERBS') == 0) { } 0 0 0
elsif (($ucType cmp 'PUNCTUATION') == 0) { } 0 0 0
elsif (($ucType cmp 'CONTENT_WORDS') == 0) { } 0 0 0
elsif (($ucType cmp 'TEXTRANK_WORDS') == 0) { } 0 0 0
elsif (($ucType cmp 'ALL') == 0) { } 0 0 0
1171 0 elsif (($ucType cmp 'VERBS') == 0) { push @listOfPOSTagsToKeep, POSTAGS_VERB; }
1172 0 elsif (($ucType cmp 'ADJECTIVES') == 0) { push @listOfPOSTagsToKeep, POSTAGS_ADJECTIVE; }
1173 0 elsif (($ucType cmp 'ADVERBS') == 0) { push @listOfPOSTagsToKeep, POSTAGS_ADVERB; }
1174 0 elsif (($ucType cmp 'CONTENT_ADVERBS') == 0) { push @listOfPOSTagsToKeep, POSTAGS_CONTENT_ADVERB; }
1175 0 elsif (($ucType cmp 'PUNCTUATION') == 0) { push @listOfPOSTagsToKeep, POSTAGS_PUNCTUATION; }
1176 0 elsif (($ucType cmp 'CONTENT_WORDS') == 0) { push @listOfPOSTagsToKeep, POSTAGS_CONTENT; }
1177 0 elsif (($ucType cmp 'TEXTRANK_WORDS') == 0) { push @listOfPOSTagsToKeep, POSTAGS_TEXTRANK; }
1178 0 elsif (($ucType cmp 'ALL') == 0) { push @listOfPOSTagsToKeep, POSTAGS_ALL; }
1179 }
1180  
1181 1 return \@listOfPOSTagsToKeep;
1182 }
1183  
1184  
1185 # returns a hash reference of the part of speech tags that are
1186 # provided in listOfPOSTagsToKeep.
1187 sub _getHashOfPOSTagsToKeep
1188 304 ▶{
Subroutine _getHashOfPOSTagsToKeep: called CC=6 SCAR=18.0
1189 304 my ($Self, %Parameters) = @_;
1190  
1191 # get the list of POS tags to keep.
1192 304 my $listOfPOSTagsToKeep;
1193 304 ▶ if (!exists ($Parameters{listOfPOSTagsToKeep}) && !exists ($Parameters{listOfPOSTypesToKeep}))
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $Parameters{'listOfPOSTagsToKeep'} || exists $Parameters{'listOfPOSTypesToKeep'}) { } 303 1 304
Condition66%2/3
exists $Parameters{'listOfPOSTagsToKeep'} || exists $Parameters{'listOfPOSTypesToKeep'}
l!l&&r!l&&!r
10303
MC/DC50%1/2
exists $Parameters{'listOfPOSTagsToKeep'} || exists $Parameters{'listOfPOSTypesToKeep'}
exists $Parameters{'listOfPOSTagsToKeep'} exists $Parameters{'listOfPOSTypesToKeep'}
Truth table66%2/3
A || B
A= exists $Parameters{'listOfPOSTagsToKeep'}B= exists $Parameters{'listOfPOSTypesToKeep'}
ABresult
1X1
011
000
1194 {
1195 303 ▶ return $Self->{hashOfPOSTagsToKeep} unless exists $Parameters{instantiation};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Parameters{"instantiation"} 300 3 303
1196 3 $listOfPOSTagsToKeep = [POSTAGS_TEXTRANK];
1197 }
1198 else
1199 {
1200 1 $listOfPOSTagsToKeep = $Self->_getListOfPOSTagsFromPOSTypesList (%Parameters);
1201 1 ▶ push @$listOfPOSTagsToKeep, @{$Parameters{listOfPOSTagsToKeep}} if (exists ($Parameters{listOfPOSTagsToKeep}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Parameters{"listOfPOSTagsToKeep"} 1 0 1
1202 }
1203  
1204 4 my %hashOfPOSTagsToKeep;
1205 4 foreach my $pos (@$listOfPOSTagsToKeep)
1206 {
1207 68 my $posClean = uc $pos;
1208 68 $posClean =~ tr/A-Z0-9//cd;
1209  
1210 # prefix with a slash.
1211 68 $hashOfPOSTagsToKeep{'/' . $posClean} = 1;
1212 }
1213  
1214 4 return \%hashOfPOSTagsToKeep;
1215 }
1216  
1217 =head2 C<getListOfStemmedWordsInText>
1218  
1219 The method C<getListOfStemmedWordsInText> returns an array reference of the sorted stemmed
1220 words in the text given by C<listOfStemmedTaggedSentences>.
1221  
1222 =over
1223  
1224 =item C<listOfStemmedTaggedSentences>
1225  
1226 listOfStemmedTaggedSentences => [...]
1227  
1228 C<listOfStemmedTaggedSentences> is the array reference returned by C<getStemmedAndTaggedText> or
1229 C<getTaggedTextToKeep> of the text.
1230  
1231 =back
1232  
1233 use Text::StemTagPOS;
1234 use Data::Dump qw(dump);
1235 my $stemTagger = Text::StemTagPOS->new;
1236 my $text = 'The first sentence. Sentence number two.';
1237 my $listOfStemmedTaggedSentences = $stemTagger->getStemmedAndTaggedText ($text);
1238 dump $listOfStemmedTaggedSentences;
1239  
1240 =cut
1241  
1242 sub getListOfStemmedWordsInText
1243 0 ▶{
Subroutine getListOfStemmedWordsInText: not called CC=4 SCAR=30.0
Pod documented
1244 0 my ($Self, %Parameters) = @_;
1245  
1246 # get the hash of tags to keep.
1247 0 my $hashOfPOSToKeep = $Self->_getHashOfPOSTagsToKeep (%Parameters);
1248  
1249 # get the tagged text.
1250 0 my $listOfStemmedTaggedSentences = $Parameters{listOfStemmedTaggedSentences};
1251  
1252 # create a list of the filtered stemmed words of the text.
1253 0 my %wordsOfText;
1254 0 foreach my $sentence (@$listOfStemmedTaggedSentences)
1255 {
1256 0 foreach my $word (@$sentence)
1257 {
1258 # skip over the words types to ignore.
1259 0 ▶ next unless (exists ($hashOfPOSToKeep->{$word->[WORD_POSTAG]}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$hashOfPOSToKeep{$word->[2]} 0 0 0
1260 0 $wordsOfText{$word->[WORD_STEMMED]} = 1;
1261 }
1262 }
1263 0 my @words = sort keys %wordsOfText;
1264 0 return \@words;
1265 }
1266  
1267 =head2 C<getListOfStemmedWordsInAllDocuments>
1268  
1269 The method C<getListOfStemmedWordsInAllDocuments> returns an array reference of the sorted stemmed
1270 words of the intersection of all the words in the documents given by C<listOfStemmedTaggedDocuments>;
1271  
1272 =over
1273  
1274 =item C<listOfStemmedTaggedDocuments>
1275  
1276 listOfStemmedTaggedDocuments => [...]
1277  
1278 C<listOfStemmedTaggedDocuments> is a list of document references returned by C<getStemmedAndTaggedText> or
1279 C<getTaggedTextToKeep>.
1280  
1281 =back
1282  
1283 =cut
1284  
1285 sub getListOfStemmedWordsInAllDocuments
1286 0 ▶{
Subroutine getListOfStemmedWordsInAllDocuments: not called CC=10 SCAR=47.0
Pod documented
1287 0 my ($Self, %Parameters) = @_;
1288  
1289 # get the list of documents to process.
1290 0 my $listOfStemmedTaggedDocuments = $Parameters{listOfStemmedTaggedDocuments};
1291 0 my $totalDocuments = @$listOfStemmedTaggedDocuments;
1292 0 my %wordOccurence;
1293  
1294 # put the words of the first document into a hash.
1295 0 my $sentences = $listOfStemmedTaggedDocuments->[0];
1296 0 foreach my $sentence (@$sentences)
1297 {
1298 0 foreach my $word (@$sentence)
1299 {
1300 0 $wordOccurence{$word->[WORD_STEMMED]} = 1;
1301 }
1302 }
1303  
1304 # add the words from the remaining documents, only if the word
1305 # occurred in all previous documents.
1306 0 ▶ for (my $i = 1; $i < $totalDocuments; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $totalDocuments) 0 0 0
1307 {
1308 0 my $sentences = $listOfStemmedTaggedDocuments->[$i];
1309 0 foreach my $sentence (@$sentences)
1310 {
1311 0 foreach my $word (@$sentence)
1312 {
1313 # if the word has not occurred in a previous document, skip it.
1314 0 ▶ next unless exists $wordOccurence{$word->[WORD_STEMMED]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $wordOccurence{$word->[0]} 0 0 0
1315  
1316 # if the word has not occurred in all previous documents, delete it.
1317 0 ▶ if ($wordOccurence{$word->[WORD_STEMMED]} < $i)
Branch: 1 branch
BranchTrueFalseTotal
if ($wordOccurence{$word->[0]} < $i) 0 0 0
1318 {
1319 0 delete $wordOccurence{$word->[WORD_STEMMED]};
1320 0 next;
1321 }
1322  
1323 # set the words occurence.
1324 0 $wordOccurence{$word->[WORD_STEMMED]} = $i + 1;
1325 }
1326 }
1327 }
1328  
1329 # return the words that occured in all the documents.
1330 0 my @wordsInAll;
1331 0 ▶ while (my ($word, $occurence) = each %wordOccurence)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %wordOccurence) 0 0 0
1332 {
1333 0 ▶ push @wordsInAll, $word if ($occurence == $totalDocuments);
Branch: 1 branch
BranchTrueFalseTotal
if $occurence == $totalDocuments 0 0 0
1334 }
1335 0 @wordsInAll = sort @wordsInAll;
1336 0 return \@wordsInAll;
1337 }
1338  
1339 =head1 INSTALLATION
1340  
1341 To install the module run the following commands:
1342  
1343 perl Makefile.PL
1344 make
1345 make test
1346 make install
1347  
1348 If you are on a windows box you should use 'nmake' rather than 'make'.
1349  
1350 =head1 AUTHOR
1351  
1352 Jeff Kubina<jeff.kubina@gmail.com>
1353
1354 =head1 BUGS
1355  
1356 Please email bugs reports or feature requests to C<bug-text-stemtagpos@rt.cpan.org>, or through
1357 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-StemTagPOS>. The author
1358 will be notified and you can be automatically notified of progress on the bug fix or feature request.
1359  
1360 =head1 COPYRIGHT
1361  
1362 Copyright (c) 2010 Jeff Kubina. All rights reserved.
1363 This program is free software; you can redistribute
1364 it and/or modify it under the same terms as Perl itself.
1365  
1366 The full text of the license can be found in the
1367 LICENSE file included with this module.
1368  
1369 =head1 KEYWORDS
1370  
1371 natural language processing, NLP, part of speech tagging, POS, stemming
1372  
1373 =head1 SEE ALSO
1374  
1375 L<Encode>, L<Lingua::Stem::Snowball>, L<Lingua::EN::Tagger>, L<perlunicode>,
1376 L<Text::Iconv>, L<utf8>
1377  
1378 =begin html
1379  
1380 See the Lingua::EN::Tagger <a href="http://cpansearch.perl.org/src/ACOBURN/Lingua-EN-Tagger-0.15/README">README</a>
1381 file for a list of the part-of-speech tags.
1382  
1383 =end html
1384  
1385 =cut
1386  
1387 3 1;
1388 # The preceding line will help the module return a true value
↑ Summary