line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Dictionary; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
96364
|
use warnings; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
100
|
|
4
|
3
|
|
|
3
|
|
17
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
97
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
3862
|
use WWW::Mechanize; |
|
3
|
|
|
|
|
1188449
|
|
|
3
|
|
|
|
|
134
|
|
7
|
3
|
|
|
3
|
|
6353
|
use HTML::Strip; |
|
3
|
|
|
|
|
10346
|
|
|
3
|
|
|
|
|
1194
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
WWW::Dictionary - Interface with www.dictionary.com |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Version 0.01 |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our @unwanted; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
BEGIN { |
24
|
|
|
|
|
|
|
|
25
|
3
|
|
|
3
|
|
3617
|
our @unwanted = ( |
26
|
|
|
|
|
|
|
'CancerWEB\'s On-line Medical Dictionary', |
27
|
|
|
|
|
|
|
'Download Now or Buy the Book', |
28
|
|
|
|
|
|
|
'in Acronym Finder', |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 SYNOPSIS |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
use WWW::Dictionary; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $dictionary = WWW::Dictionary->new(); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $meaning = $dictionary->meaning( $word ); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 FUNCTIONS |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head2 new |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Creates a new WWW::Dictionary object. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
If passed an expression, sets that expression to the current one. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $dictionary = WWW::Dictionary->new(); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
or |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $dictionary = WWW::Dictionary->new('current expression'); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub new { |
58
|
3
|
|
|
3
|
1
|
35
|
my $self = shift; |
59
|
3
|
|
100
|
|
|
24
|
my $expression = shift || ''; |
60
|
|
|
|
|
|
|
|
61
|
3
|
|
|
|
|
20
|
my %dictionary = ( |
62
|
|
|
|
|
|
|
'current' => $expression, |
63
|
|
|
|
|
|
|
'dictionary' => {}, |
64
|
|
|
|
|
|
|
); |
65
|
|
|
|
|
|
|
|
66
|
3
|
|
|
|
|
18
|
bless \%dictionary => $self; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 set_expression |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Sets the current expression to look for (doesn't look, merely sets the expression). |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$dictionary->set_expression('new expression'); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Returns the same expression. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=cut |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub set_expression { |
80
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
6
|
my $expression = shift; |
83
|
|
|
|
|
|
|
|
84
|
1
|
50
|
|
|
|
9
|
if ($expression) { |
85
|
1
|
|
|
|
|
4
|
$self->{'current'} = $expression; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
9
|
return $expression; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 get_expression |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Returns the current expression. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $expression = $dictionary->get_expression(); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=cut |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub get_expression { |
100
|
3
|
|
|
3
|
1
|
23
|
my $self = shift; |
101
|
|
|
|
|
|
|
|
102
|
3
|
|
|
|
|
433
|
return $self->{'current'}; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 get_meaning |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Returns the meaning of the current expression by fetching from |
108
|
|
|
|
|
|
|
www.dictionary.com. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
If the expression has already been fetched (if it still has the |
111
|
|
|
|
|
|
|
information stored), returns what is already on memory. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
my $meaning = $dictionary->get_meaning(); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
You can also pass a new expression, which is set to be the current |
116
|
|
|
|
|
|
|
expression before fetching is made: |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
my $meaning = $dictionary->get_meaning('some other expression'); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub get_meaning { |
123
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
0
|
my $expression = shift; |
126
|
|
|
|
|
|
|
|
127
|
0
|
0
|
|
|
|
0
|
if ($expression) { |
128
|
0
|
|
|
|
|
0
|
$self->set_expression($expression); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
else { |
131
|
0
|
|
|
|
|
0
|
$expression = $self->get_expression(); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
0
|
if (defined $self->{'dictionary'}->{$expression}) { |
135
|
0
|
|
|
|
|
0
|
return $self->{'dictionary'}->{$expression}; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
else { |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# retrieve the webpage |
140
|
0
|
|
|
|
|
0
|
my $mech = WWW::Mechanize->new(); |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
0
|
$mech->get( "http://dictionary.reference.com/search?q=$expression" ); |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
0
|
my $cont = $mech->content; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# if there's no meaning |
147
|
0
|
0
|
|
|
|
0
|
if ( $cont =~ /No entry found for $expression<\/i>./ ) { |
148
|
0
|
|
|
|
|
0
|
$self->set_meaning( $expression, "Entry not found"); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
# if there's a meaning |
151
|
|
|
|
|
|
|
else { |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
# remove extra information |
154
|
0
|
|
|
|
|
0
|
$cont =~ s/(.|\n)*?1 entry found for $expression<\/i>.*//; |
155
|
0
|
|
|
|
|
0
|
$cont =~ s/(.|\n)*?entries found.*//; |
156
|
0
|
|
|
|
|
0
|
$cont =~ s/.*Perform a new search(.|\n)*//; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# strip HTML |
159
|
0
|
|
|
|
|
0
|
my $hs = HTML::Strip->new(); |
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
0
|
my $clean_text = $hs->parse( $cont ); |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
0
|
$clean_text =~ s/\nSource : .*//g; # we don't want no sources |
164
|
0
|
|
|
|
|
0
|
$clean_text =~ s/(\012\r|\r\012|\r)/\012/g; # removing trailing ^M |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
# remove unwanted things |
167
|
0
|
|
|
|
|
0
|
for (@unwanted) { |
168
|
0
|
|
|
|
|
0
|
$clean_text =~ s/.*$_.*//; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
0
|
$clean_text =~ y/ / /s; # compact spaces left by cleaning HTML |
172
|
|
|
|
|
|
|
|
173
|
0
|
|
|
|
|
0
|
$clean_text =~ s/\n\n\n+/\n\n/g; # compact empty newlines |
174
|
0
|
|
|
|
|
0
|
$clean_text =~ s/^\n+//; # remove leading newlines |
175
|
0
|
|
|
|
|
0
|
$clean_text =~ s/\n+$//; # remove trailing newlines |
176
|
|
|
|
|
|
|
|
177
|
0
|
|
|
|
|
0
|
$clean_text =~ s/\s*$expression$//; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# store the meaning |
180
|
0
|
|
|
|
|
0
|
$self->set_meaning( $expression, $clean_text); |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
0
|
return $self->{'dictionary'}->{$expression}; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 set_meaning |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Sets a meaning in the object dictionary. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
$dictionary->set_meaning( $word, $meaning ); |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
From this point on (until a C is called), retrieving |
195
|
|
|
|
|
|
|
the meaning of $word will return whatever was on $meaning. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub set_meaning { |
200
|
3
|
|
|
3
|
1
|
6
|
my $self = shift; |
201
|
|
|
|
|
|
|
|
202
|
3
|
|
|
|
|
6
|
my ($expression, $meaning) = @_; |
203
|
|
|
|
|
|
|
|
204
|
3
|
100
|
|
|
|
10
|
if ($expression) { |
205
|
2
|
|
|
|
|
12
|
$self->{'dictionary'}->{$expression} = $meaning; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
else { |
208
|
1
|
|
|
|
|
5
|
return undef; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head2 get_dictionary |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Returns the current dictionary inside the object. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
my %dictionary = %{ $dictionary->get_dictionary }; |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=cut |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub get_dictionary { |
221
|
4
|
|
|
4
|
1
|
8
|
my $self = shift; |
222
|
|
|
|
|
|
|
|
223
|
4
|
|
|
|
|
28
|
return $self->{'dictionary'}; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head2 reset_dictionary |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
Resets the current dictionary. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
$dictionary->reset_dictionary; |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=cut |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
sub reset_dictionary { |
235
|
2
|
|
|
2
|
1
|
5
|
my $self = shift; |
236
|
|
|
|
|
|
|
|
237
|
2
|
|
|
|
|
5
|
for (keys %{$self->{'dictionary'}}) { |
|
2
|
|
|
|
|
10
|
|
238
|
1
|
|
|
|
|
6
|
delete $self->{'dictionary'}->{$_}; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=head1 AUTHOR |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
Jose Castro, C<< >> |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head1 BUGS |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
249
|
|
|
|
|
|
|
C, or through the web interface at |
250
|
|
|
|
|
|
|
L. |
251
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
252
|
|
|
|
|
|
|
your bug as I make changes. |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head1 SUPPORT |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
perldoc WWW::Dictionary |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
You can also look for information at: |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=over 4 |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
L |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=item * CPAN Ratings |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
L |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
L |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=item * Search CPAN |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
L |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=back |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
Copyright 2005 Jose Castro, all rights reserved. |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
287
|
|
|
|
|
|
|
under the same terms as Perl itself. |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=cut |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
1; # End of WWW::Dictionary |