line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::NLP; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
$VERSION = '0.1'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
31389
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
453
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub new |
9
|
|
|
|
|
|
|
{ |
10
|
0
|
|
|
0
|
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
|
|
|
|
|
|
|
{ |
23
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
24
|
0
|
|
|
|
|
|
my $args = shift; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
foreach ( keys %{$args} ) { |
|
0
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
$self->{_wordsObj}->addCategory( $_, $args->{$_} ); |
28
|
|
|
|
|
|
|
}; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
return; |
31
|
|
|
|
|
|
|
}; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub process |
35
|
|
|
|
|
|
|
{ |
36
|
0
|
|
|
0
|
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
|
|
|
|
|
|
|
{ |
53
|
0
|
|
|
0
|
|
|
my $self = shift; |
54
|
0
|
|
|
|
|
|
my $words = shift; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
my $weight = Text::NLP::Weight->new; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
foreach ( @{$words} ) { |
|
0
|
|
|
|
|
|
|
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
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8990
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub new |
75
|
|
|
|
|
|
|
{ |
76
|
0
|
|
|
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
|
|
|
|
|
|
|
{ |
93
|
0
|
|
|
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
|
|
|
|
|
|
|
{ |
105
|
0
|
|
|
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}} ) { |
|
0
|
|
|
|
|
|
|
115
|
0
|
0
|
|
|
|
|
if( $string =~ /$_/ ) { |
116
|
0
|
|
|
|
|
|
push @{$neg}, $_; |
|
0
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
}; |
118
|
|
|
|
|
|
|
}; |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
foreach ( @{$self->{_emph}->{POS}} ) { |
|
0
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
|
if( $string =~ /$_/ ) { |
122
|
0
|
|
|
|
|
|
push @{$pos}, $_; |
|
0
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
}; |
124
|
|
|
|
|
|
|
}; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
foreach my $key ( keys %{$self->{_data}} ) { |
|
0
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
ITERATION: |
129
|
0
|
|
|
|
|
|
foreach ( @{$self->{_data}->{$key}} ) { |
130
|
0
|
|
|
|
|
|
foreach my $bob ( @{$neg} ) { |
|
0
|
|
|
|
|
|
|
131
|
0
|
0
|
|
|
|
|
if( $string =~ /(^$bob $_ | $bob $_ | $bob $_$)/ ) { |
132
|
0
|
|
|
|
|
|
next ITERATION; |
133
|
|
|
|
|
|
|
}; |
134
|
|
|
|
|
|
|
}; |
135
|
0
|
|
|
|
|
|
foreach my $bob ( @{$pos} ) { |
|
0
|
|
|
|
|
|
|
136
|
0
|
0
|
|
|
|
|
if( $string =~ /(^$bob $_ | $bob $_ | $bob $_$)/ ) { |
137
|
0
|
|
|
|
|
|
push @{$words}, ( $key, $key ); |
|
0
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
next ITERATION; |
139
|
|
|
|
|
|
|
}; |
140
|
|
|
|
|
|
|
}; |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
my @temp = $string =~ /(^$_ | $_ | $_$)/g; |
143
|
0
|
|
|
|
|
|
foreach( @temp ) { |
144
|
0
|
|
|
|
|
|
push @{$words}, $key; |
|
0
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
}; |
146
|
|
|
|
|
|
|
}; |
147
|
|
|
|
|
|
|
}; |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
return( $words ); |
150
|
|
|
|
|
|
|
}; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
package Text::NLP::Weight; |
155
|
|
|
|
|
|
|
|
156
|
1
|
|
|
1
|
|
9
|
use strict; |
|
1
|
|
|
|
|
14
|
|
|
1
|
|
|
|
|
261
|
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub new |
159
|
|
|
|
|
|
|
{ |
160
|
0
|
|
|
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
|
|
|
|
|
|
|
{ |
172
|
0
|
|
|
0
|
|
|
my $self = shift; |
173
|
0
|
|
|
|
|
|
my $word = shift; |
174
|
|
|
|
|
|
|
|
175
|
0
|
0
|
|
|
|
|
if( defined($self->{_words}->{$word}) ) { |
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
|
|
|
|
|
|
|
{ |
188
|
0
|
|
|
0
|
|
|
my $self = shift; |
189
|
|
|
|
|
|
|
|
190
|
0
|
|
|
|
|
|
my $max = []; |
191
|
0
|
|
|
|
|
|
my $updated = 1; |
192
|
|
|
|
|
|
|
|
193
|
1
|
|
|
1
|
|
4112
|
use Data::Dumper; print Dumper( $self->{_words} ); |
|
1
|
|
|
|
|
32981
|
|
|
1
|
|
|
|
|
229
|
|
|
0
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
|
195
|
0
|
|
|
|
|
|
foreach ( keys %{$self->{_words}} ) { |
|
0
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
push @{$max}, $_; |
|
0
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
}; |
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
while( $updated ) { |
200
|
0
|
|
|
|
|
|
$updated = 0; |
201
|
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
for ( my $count = 0; $count < (@{$max} - 1); $count++ ) { |
|
0
|
|
|
|
|
|
|
203
|
0
|
|
|
|
|
|
my $priority_a = $self->{_words}->{$max->[$count]}; |
204
|
0
|
|
|
|
|
|
my $priority_b = $self->{_words}->{$max->[$count+1]}; |
205
|
|
|
|
|
|
|
|
206
|
0
|
0
|
|
|
|
|
if( $priority_a < $priority_b ) { |
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; |
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, |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=cut |
260
|
|
|
|
|
|
|
|