blib/lib/Text/NLP.pm

filter: Statementstmt 12.3% 14 / 113 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 28.5% 4 / 14 Podpod 0.0% 0 / 3 totaltotal 12.3% 18 / 146 CC 19 SCAR 41.5
CC
19
Cov
11%
translateString
156.0
getMax
23.0
addSeeding
6.0
CRAP 63.7
↑ Summary
1 package Text::NLP;
2  
3 1 $VERSION = '0.1';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 sub new
9 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
10 0 my $class = shift;
11  
12 0 my $self = {
13 _wordsObj => Text::NLP::Words->new,
14 };
15 0 bless $self, $class;
16  
17 0 return( $self );
18 };
19  
20  
21 sub addSeeding
22 0 ▶{
Subroutine addSeeding: not called CC=2 SCAR=17.9
Pod undocumented
23 0 my $self = shift;
24 0 my $args = shift;
25  
26 0 foreach ( keys %{$args} ) {
27 0 $self->{_wordsObj}->addCategory( $_, $args->{$_} );
28 };
29  
30 0 return;
31 };
32  
33  
34 sub process
35 0 ▶{
Subroutine process: not called CC=1 SCAR=6.9
Pod undocumented
36 0 my $self = shift;
37 0 my $string = lc(shift);
38  
39 # Clean the string
40 0 $string =~ s/[,;:!\?\.\"\']{1,}//g;
41 0 $string =~ s/\s+/ /g;
42  
43 0 my $words = $self->{_wordsObj}->translateString( $string );
44  
45 0 my $max = $self->_weightWords( $words );
46  
47 0 return( $max );
48 };
49  
50  
51 sub _weightWords
52 0 ▶{
Subroutine _weightWords: not called CC=2 SCAR=17.9
53 0 my $self = shift;
54 0 my $words = shift;
55  
56 0 my $weight = Text::NLP::Weight->new;
57  
58 0 foreach ( @{$words} ) {
59 0 $weight->add( $_ );
60 };
61  
62 0 my $max = $weight->getMax;
63  
64 0 return( $max );
65 };
66  
67  
68  
69 package Text::NLP::Words;
70  
71 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73  
74 sub new
75 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
76 0 my $class = shift;
77
78 0 my $self = {
79 _emph => {
80 NEG => [ 'definately not', 'not' ],
81 POS => [ 'definately' ],
82 },
83 _data => {},
84 };
85 0 bless $self, $class;
86  
87 0 return( $self );
88 };
89  
90  
91 sub addCategory
92 0 ▶{
Subroutine addCategory: not called CC=1 SCAR=6.9
93 0 my $self = shift;
94 0 my $cat = shift;
95 0 my $words = shift;
96  
97 0 $self->{_data}->{$cat} = $words;
98  
99 0 return;
100 };
101  
102  
103 sub translateString
104 0 ▶{
Subroutine translateString: not called CC=12 SCAR=50.5
105 0 my $self = shift;
106 0 my $string = shift;
107  
108 0 my $neg = [];
109 0 my $pos = [];
110 0 my $words = [];
111  
112 0 study( $string );
113  
114 0 foreach ( @{$self->{_emph}->{NEG}} ) {
115 0 ▶ if( $string =~ /$_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$_/) 0 0 0
116 0 push @{$neg}, $_;
117 };
118 };
119  
120 0 foreach ( @{$self->{_emph}->{POS}} ) {
121 0 ▶ if( $string =~ /$_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$_/) 0 0 0
122 0 push @{$pos}, $_;
123 };
124 };
125  
126  
127 0 foreach my $key ( keys %{$self->{_data}} ) {
128 ITERATION:
129 0 foreach ( @{$self->{_data}->{$key}} ) {
130 0 foreach my $bob ( @{$neg} ) {
131 0 ▶ if( $string =~ /(^$bob $_ | $bob $_ | $bob $_$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /(^$bob $_ | $bob $_ | $bob $_$)/) 0 0 0
132 0 next ITERATION;
133 };
134 };
135 0 foreach my $bob ( @{$pos} ) {
136 0 ▶ if( $string =~ /(^$bob $_ | $bob $_ | $bob $_$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /(^$bob $_ | $bob $_ | $bob $_$)/) 0 0 0
137 0 push @{$words}, ( $key, $key );
138 0 next ITERATION;
139 };
140 };
141
142 0 my @temp = $string =~ /(^$_ | $_ | $_$)/g;
143 0 foreach( @temp ) {
144 0 push @{$words}, $key;
145 };
146 };
147 };
148  
149 0 return( $words );
150 };
151  
152  
153  
154 package Text::NLP::Weight;
155  
156 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 sub new
159 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
160 0 my $class = shift;
161  
162 0 my $self = {
163 _words => {},
164 };
165 0 bless $self, $class;
166
167 0 return( $self );
168 };
169  
170 sub add
171 0 ▶{
Subroutine add: not called CC=2 SCAR=17.9
172 0 my $self = shift;
173 0 my $word = shift;
174  
175 0 ▶ if( defined($self->{_words}->{$word}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_words'}{$word}) { } 0 0 0
176 0 $self->{_words}->{$word}++;
177 }
178 else {
179 0 $self->{_words}->{$word} = 1;
180 };
181  
182 0 return;
183 };
184  
185  
186 sub getMax
187 0 ▶{
Subroutine getMax: not called CC=5 SCAR=31.4
188 0 my $self = shift;
189
190 0 my $max = [];
191 0 my $updated = 1;
192  
193 0 ▶use Data::Dumper; print Dumper( $self->{_words} );
Subroutine BEGIN: called CC=1 SCAR=0.2
194  
195 0 foreach ( keys %{$self->{_words}} ) {
196 0 push @{$max}, $_;
197 };
198  
199 0 ▶ while( $updated ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($updated) 0 0 0
200 0 $updated = 0;
201  
202 0 ▶ for ( my $count = 0; $count < (@{$max} - 1); $count++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < @{$max;} - 1) 0 0 0
203 0 my $priority_a = $self->{_words}->{$max->[$count]};
204 0 my $priority_b = $self->{_words}->{$max->[$count+1]};
205  
206 0 ▶ if( $priority_a < $priority_b ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($priority_a < $priority_b) 0 0 0
207 0 my $temp = $max->[$count];
208 0 $max->[$count] = $max->[$count+1];
209 0 $max->[$count+1] = $temp;
210 0 $updated = 1;
211 };
212 };
213 };
214  
215 0 return( $max );
216 };
217  
218 1 1;
219  
220 =pod
221  
222 =head1 NAME
223  
224 Text::NLP - Perl module for Natural Language Processing
225
226 =head1 DESCRIPTION
227  
228 Initial release, documentation and updates will follow.
229  
230 =head1 SYNOPSIS
231  
232 use Text::NLP;
233
234 my $talker = Text::NLP->new;
235  
236 # setup the seeding
237 my $args = {
238 HELLO => [ 'hi', 'hello', 'good day' ],
239 BYE => [ 'bye', 'goodbye', 'laters' ],
240 }
241  
242 $talker->addSeeding( $args );
243  
244 my $string = 'Should I say goodbye or hello? I\'ll just say good day';
245  
246 # process the string
247 my $words = $talker->process( $string );
248
249 # do some post-processing
250  
251 =head1 KNOWN BUGS
252  
253 None, but that does not mean there are not any.
254  
255 =head1 AUTHOR
256  
257 Alistair Francis, <cpan@alizta.com>
258  
259 =cut
260  
↑ Summary