File Coverage

blib/lib/Lingua/Stem.pm
Criterion Covered Total %
statement 116 165 70.3
branch 29 56 51.7
condition 0 3 0.0
subroutine 12 14 85.7
pod 10 10 100.0
total 167 248 67.3


line stmt bran cond sub pod time code
1             package Lingua::Stem;
2              
3 2     2   3818 use strict;
  2         8  
  2         57  
4 2     2   9 use warnings;
  2         3  
  2         62  
5             require Exporter;
6 2     2   797 use Lingua::Stem::AutoLoader;
  2         4  
  2         197  
7              
8             BEGIN {
9 2     2   6 $Lingua::Stem::VERSION = '2.31';
10 2         34 @Lingua::Stem::ISA = qw (Exporter);
11 2         7 @Lingua::Stem::EXPORT = ();
12 2         6 @Lingua::Stem::EXPORT_OK = qw (stem stem_in_place clear_stem_cache stem_caching add_exceptions delete_exceptions get_exceptions set_locale get_locale);
13 2         5400 %Lingua::Stem::EXPORT_TAGS = ( 'all' => [qw (stem stem_in_place stem_caching clear_stem_cache add_exceptions delete_exceptions get_exceptions set_locale get_locale)],
14             'stem' => [qw (stem)],
15             'stem_in_place' => [qw (stem_in_place)],
16             'caching' => [qw (stem_caching clear_stem_cache)],
17             'locale' => [qw (set_locale get_locale)],
18             'exceptions' => [qw (add_exceptions delete_exceptions get_exceptions)],
19             );
20             }
21              
22             my $defaults = {
23             -locale => 'en',
24             -stemmer => \&Lingua::Stem::En::stem,
25             -stem_in_place => \&Lingua::Stem::En::stem,
26             -stem_caching => \&Lingua::Stem::En::stem_caching,
27             -clear_stem_cache => \&Lingua::Stem::En::clear_stem_cache,
28             -exceptions => {},
29             -known_locales => {
30             'da' => { -stemmer => \&Lingua::Stem::Da::stem,
31             -stem_caching => \&Lingua::Stem::Da::stem_caching,
32             -clear_stem_cache => \&Lingua::Stem::Da::clear_stem_cache,
33             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'da' locale"); },
34             },
35             'de' => { -stemmer => \&Lingua::Stem::De::stem,
36             -stem_caching => \&Lingua::Stem::De::stem_caching,
37             -clear_stem_cache => \&Lingua::Stem::De::clear_stem_cache,
38             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'de' locale"); },
39             },
40             'en' => { -stemmer => \&Lingua::Stem::En::stem,
41             -stem_caching => \&Lingua::Stem::En::stem_caching,
42             -clear_stem_cache => \&Lingua::Stem::En::clear_stem_cache,
43             -stem_in_place => \&Lingua::Stem::En::stem,
44             },
45             'en_us' => { -stemmer => \&Lingua::Stem::En::stem,
46             -stem_caching => \&Lingua::Stem::En::stem_caching,
47             -clear_stem_cache => \&Lingua::Stem::En::clear_stem_cache,
48             -stem_in_place => \&Lingua::Stem::En::stem,
49             },
50             'en-us' => { -stemmer => \&Lingua::Stem::En::stem,
51             -stem_caching => \&Lingua::Stem::En::stem_caching,
52             -clear_stem_cache => \&Lingua::Stem::En::clear_stem_cache,
53             -stem_in_place => \&Lingua::Stem::En::stem,
54             },
55             'en_uk' => { -stemmer => \&Lingua::Stem::En::stem,
56             -stem_caching => \&Lingua::Stem::En::stem_caching,
57             -clear_stem_cache => \&Lingua::Stem::En::clear_stem_cache,
58             -stem_in_place => \&Lingua::Stem::En::stem,
59             },
60             'en-uk' => { -stemmer => \&Lingua::Stem::En::stem,
61             -stem_caching => \&Lingua::Stem::En::stem_caching,
62             -clear_stem_cache => \&Lingua::Stem::En::clear_stem_cache,
63             -stem_in_place => \&Lingua::Stem::En::stem,
64             },
65             'en-broken' => { -stemmer => \&Lingua::Stem::En_Broken::stem,
66             -stem_caching => \&Lingua::Stem::En_Broken::stem_caching,
67             -clear_stem_cache => \&Lingua::Stem::En_Broken::clear_stem_cache,
68             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'en-broken' locale"); },
69             },
70             'fr' => { -stemmer => \&Lingua::Stem::Fr::stem,
71             -stem_caching => \&Lingua::Stem::Fr::stem_caching,
72             -clear_stem_cache => \&Lingua::Stem::Fr::clear_stem_cache,
73             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'fr' locale"); },
74             },
75             'gl' => { -stemmer => \&Lingua::Stem::Gl::stem,
76             -stem_caching => \&Lingua::Stem::Gl::stem_caching,
77             -clear_stem_cache => \&Lingua::Stem::Gl::clear_stem_cache,
78             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'gl' locale"); },
79             },
80             'it' => { -stemmer => \&Lingua::Stem::It::stem,
81             -stem_caching => \&Lingua::Stem::It::stem_caching,
82             -clear_stem_cache => \&Lingua::Stem::It::clear_stem_cache,
83             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'it' locale"); },
84             },
85             'no' => { -stemmer => \&Lingua::Stem::No::stem,
86             -stem_caching => \&Lingua::Stem::No::stem_caching,
87             -clear_stem_cache => \&Lingua::Stem::No::clear_stem_cache,
88             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'no' locale"); },
89             },
90             'pt' => { -stemmer => \&Lingua::Stem::Pt::stem,
91             -stem_caching => \&Lingua::Stem::Pt::stem_caching,
92             -clear_stem_cache => \&Lingua::Stem::Pt::clear_stem_cache,
93             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'pt' locale"); },
94             },
95             'sv' => { -stemmer => \&Lingua::Stem::Sv::stem,
96             -stem_caching => \&Lingua::Stem::Sv::stem_caching,
97             -clear_stem_cache => \&Lingua::Stem::Sv::clear_stem_cache,
98             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'sv' locale"); },
99             },
100             'ru' => { -stemmer => \&Lingua::Stem::Ru::stem,
101             -stem_caching => \&Lingua::Stem::Ru::stem_caching,
102             -clear_stem_cache => \&Lingua::Stem::Ru::clear_stem_cache,
103             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'ru' locale"); },
104             },
105             'ru_ru' => {
106             -stemmer => \&Lingua::Stem::Ru::stem,
107             -stem_caching => \&Lingua::Stem::Ru::stem_caching,
108             -clear_stem_cache => \&Lingua::Stem::Ru::clear_stem_cache,
109             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'ru_ru' locale"); },
110             },
111             'ru-ru' => {
112             -stemmer => \&Lingua::Stem::Ru::stem,
113             -stem_caching => \&Lingua::Stem::Ru::stem_caching,
114             -clear_stem_cache => \&Lingua::Stem::Ru::clear_stem_cache,
115             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'ru-ru' locale"); },
116             },
117             'ru-ru.koi8-r' => {
118             -stemmer => \&Lingua::Stem::Ru::stem,
119             -stem_caching => \&Lingua::Stem::Ru::stem_caching,
120             -clear_stem_cache => \&Lingua::Stem::Ru::clear_stem_cache,
121             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'ru-ru.koi8-r' locale"); },
122             },
123             'ru_ru.koi8-r' => {
124             -stemmer => \&Lingua::Stem::Ru::stem,
125             -stem_caching => \&Lingua::Stem::Ru::stem_caching,
126             -clear_stem_cache => \&Lingua::Stem::Ru::clear_stem_cache,
127             -stem_in_place => sub { require Carp; Carp::croak("'stem_in_place' not available for 'ru_ru.koi8-r' locale"); },
128             },
129             },
130             };
131              
132             ###
133              
134             sub new {
135 3     3 1 249 my $proto = shift;
136 3         7 my $package = __PACKAGE__;
137 3         6 my $proto_ref = ref($proto);
138 3         5 my $class;
139 3 50       9 if ($proto_ref) {
    50          
140 0         0 $class = $proto_ref;
141             } elsif ($proto) {
142 3         6 $class = $proto;
143             } else {
144 0         0 $class = $package;
145             }
146 3         6 my $self = bless {},$class;
147              
148             # Set the defaults
149 3         6 %{$self->{'Lingua::Stem'}->{-exceptions}} = %{$defaults->{-exceptions}};
  3         14  
  3         9  
150 3         8 $self->{'Lingua::Stem'}->{-locale} = $defaults->{-locale};
151 3         5 $self->{'Lingua::Stem'}->{-stemmer} = $defaults->{-stemmer};
152 3         6 $self->{'Lingua::Stem'}->{-stem_in_place} = $defaults->{-stem_in_place};
153 3         7 $self->{'Lingua::Stem'}->{-stem_caching} = $defaults->{-stem_caching};
154 3         5 $self->{'Lingua::Stem'}->{-clear_stem_cache} = $defaults->{-clear_stem_cache};
155              
156             # Handle any passed parms
157 3         5 my @errors = ();
158 3 50       10 if ($#_ > -1) {
159 0         0 my $parm_ref = $_[0];
160 0 0       0 if (not ref $parm_ref) {
161 0         0 $parm_ref = {@_};
162             }
163 0         0 foreach my $key (keys %$parm_ref) {
164 0         0 my $lc_key = lc ($key);
165 0 0       0 if ($lc_key eq '-locale') { $self->set_locale($parm_ref->{$key}); }
  0 0       0  
166 0         0 elsif ($lc_key eq '-default_locale') { set_locale($parm_ref->{$key}); }
167 0         0 else { push (@errors," '$key' => '$parm_ref->{$key}'"); }
168             }
169             }
170 3 50       8 if ($#errors > -1) {
171 0         0 require Carp;
172 0         0 Carp::croak ($package . "::new() - unrecognized parameters passed:" . join(', ',@errors));
173             }
174              
175 3         10 return $self;
176             }
177              
178             ###
179              
180             sub set_locale {
181 47     47 1 16028 my ($self) = shift;
182              
183 47         57 my ($locale);
184 47 100       76 if (ref $self) {
185 14         22 ($locale) = @_;
186 14         23 $locale = lc $locale;
187 14 50       34 if (not exists $defaults->{-known_locales}->{$locale}) {
188 0         0 require Carp;
189 0         0 Carp::croak (__PACKAGE__ . "::set_locale() - Unknown locale '$locale'");
190             }
191 14         21 $self->{'Lingua::Stem'}->{-locale} = $locale;
192 14         28 $self->{'Lingua::Stem'}->{-stemmer} = $defaults->{-known_locales}->{$locale}->{-stemmer};
193 14         23 $self->{'Lingua::Stem'}->{-stem_in_place} = $defaults->{-known_locales}->{$locale}->{-stem_in_place};
194 14         18 $self->{'Lingua::Stem'}->{-stem_caching} = $defaults->{-known_locales}->{$locale}->{-stem_caching};
195 14         23 $self->{'Lingua::Stem'}->{-clear_stem_cache} = $defaults->{-known_locales}->{$locale}->{-clear_stem_cache};
196             } else {
197 33         47 $locale = lc $self;
198 33 50       76 if (not exists $defaults->{-known_locales}->{$locale}) {
199 0         0 require Carp;
200 0         0 Carp::croak (__PACKAGE__ . "::set_locale() - Unknown locale '$locale'");
201             }
202 33         43 $defaults->{-locale} = $locale;
203 33         56 $defaults->{-stemmer} = $defaults->{-known_locales}->{$locale}->{-stemmer};
204 33         43 $defaults->{-stem_in_place} = $defaults->{-known_locales}->{$locale}->{-stem_in_place};
205 33         36 $defaults->{-stem_caching} = $defaults->{-known_locales}->{$locale}->{-stem_caching};
206 33         44 $defaults->{-clear_stem_cache} = $defaults->{-known_locales}->{$locale}->{-clear_stem_cache};
207             }
208 47         75 return;
209             }
210              
211             ###
212              
213             sub get_locale {
214 54     54 1 455 my $self = shift;
215              
216 54 100       89 if (ref $self) {
217 17         40 return $self->{'Lingua::Stem'}->{-locale};
218             } else {
219 37         85 return $defaults->{-locale};
220             }
221             }
222              
223             ###
224              
225             sub add_exceptions {
226 6     6 1 46 my $self;
227              
228 6         11 my ($exceptions, $exception_list);
229 6         11 my $reference = ref $_[0];
230 6 100       16 if ($reference eq 'HASH') {
    50          
231 3         5 ($exceptions) = @_;
232 3         5 $exception_list = $defaults->{-exceptions};
233             } elsif (not $reference) {
234 0         0 $exceptions = { @_ };
235 0         0 $exception_list = $defaults->{-exceptions};
236             } else {
237 3         4 $self = shift;
238 3         5 ($exceptions) = @_;
239 3         6 $exception_list = $self->{'Lingua::Stem'}->{-exceptions};
240             }
241 6         26 while (my ($exception,$replace_with) = each %$exceptions) {
242 6         23 $exception_list->{$exception} = $replace_with;
243             }
244 6         12 return;
245             }
246              
247             ###
248              
249             sub delete_exceptions {
250 6     6 1 234 my $self;
251              
252 6         10 my ($exception_list,$exceptions);
253 6 50       16 if ($#_ == -1) {
254 0         0 $defaults->{-exceptions} = {};
255 0         0 return;
256             }
257 6         26 my $reference =ref $_[0];
258 6 50       22 if ($reference eq 'ARRAY') {
    100          
259 0         0 ($exceptions) = @_;
260 0         0 $exception_list = $defaults->{-exceptions};
261             } elsif (not $reference) {
262 3         7 $exceptions = [@_];
263 3         5 $exception_list = $defaults->{-exceptions};
264             } else {
265 3         6 $self = shift;
266 3 50       8 if ($#_ == -1) {
267 0         0 $self->{'Lingua::Stem'}->{-exceptions} = {};
268             } else {
269 3         4 $reference = ref $_[0];
270 3 50       7 if ($reference eq 'ARRAY') {
271 0         0 ($exceptions) = @_;
272 0         0 $exception_list = $self->{'Lingua::Stem'}->{-exceptions};
273             } else {
274 3         7 ($exceptions) = [@_];
275 3         6 $exception_list = $self->{'Lingua::Stem'}->{-exceptions};
276             }
277             }
278             }
279              
280 6         13 foreach (@$exceptions) { delete $exception_list->{$_}; }
  6         12  
281 6         14 return;
282             }
283              
284             ###
285              
286             sub get_exceptions {
287              
288 12     12 1 39 my $exception_list = {};
289 12 100       24 if ($#_ == -1) {
290 6         7 %$exception_list = %{$defaults->{-exceptions}};
  6         15  
291 6         13 return $exception_list;
292             }
293 6         11 my $reference = ref $_[0];
294 6 50       23 if ($reference eq 'ARRAY') {
    50          
295 0         0 %$exception_list = %{$defaults->{-exceptions}};
  0         0  
296             } elsif ($reference) {
297 6         13 my $self = shift;
298 6 50       12 if ($#_ > -1) {
299 0         0 foreach (@_) {
300 0         0 $exception_list->{$_} = $self->{'Lingua::Stem'}->{-exceptions}->{$_};
301             }
302             } else {
303 6         9 %$exception_list = %{$self->{'Lingua::Stem'}->{-exceptions}};
  6         25  
304             }
305             } else {
306 0         0 foreach (@_) {
307 0         0 $exception_list->{$_} = $_;
308             }
309             }
310 6         16 return $exception_list;
311             }
312              
313             ####
314              
315             sub stem {
316 22     22 1 187 my $self;
317 22 50       50 return [] if ($#_ == -1);
318 22         32 my ($exceptions,$locale,$stemmer);
319 22 100       35 if (ref $_[0]) {
320 6         9 my $self = shift;
321 6         10 $exceptions = $self->{'Lingua::Stem'}->{-exceptions};
322 6         9 $stemmer = $self->{'Lingua::Stem'}->{-stemmer};
323 6         10 $locale = $self->{'Lingua::Stem'}->{-locale};
324             } else {
325 16         20 $exceptions = $defaults->{-exceptions};
326 16         16 $stemmer = $defaults->{-stemmer};
327 16         24 $locale = $defaults->{-locale};
328             }
329 22         365 &$stemmer({ -words => \@_,
330             -locale => $locale,
331             -exceptions => $exceptions });
332             }
333              
334             ###
335              
336             sub stem_in_place {
337 3     3 1 24 my $self;
338 3 50       8 return [] if ($#_ == -1);
339 3         6 my ($exceptions,$locale,$stemmer);
340 3 50       6 if (ref $_[0]) {
341 0         0 my $self = shift;
342 0         0 $exceptions = $self->{'Lingua::Stem'}->{-exceptions};
343 0         0 $stemmer = $self->{'Lingua::Stem'}->{-stem_in_place};
344 0         0 $locale = $self->{'Lingua::Stem'}->{-locale};
345             } else {
346 3         4 $exceptions = $defaults->{-exceptions};
347 3         5 $stemmer = $defaults->{-stem_in_place};
348 3         4 $locale = $defaults->{-locale};
349             }
350 3         13 &$stemmer({ -words => [\@_],
351             -locale => $locale,
352             -exceptions => $exceptions });
353             }
354              
355             ###
356              
357             sub clear_stem_cache {
358 0     0 1   my $clear_stem_cache_sub;
359 0 0         if (ref $_[0]) {
360 0           my $self = shift;
361 0           $clear_stem_cache_sub = $self->{'Lingua::Stem'}->{-clear_stem_cache};
362             } else {
363 0           $clear_stem_cache_sub = $defaults->{-clear_stem_cache};
364             }
365 0           &$clear_stem_cache_sub;
366             }
367              
368             ###
369              
370             sub stem_caching {
371 0     0 1   my $stem_caching_sub;
372 0           my $first_parm_ref = ref $_[0];
373 0 0 0       if ($first_parm_ref && ($first_parm_ref ne 'HASH')) {
374 0           my $self = shift;
375 0           $stem_caching_sub = $self->{'Lingua::Stem'}->{-stem_caching};
376             } else {
377 0           $stem_caching_sub = $defaults->{-stem_caching};
378             }
379 0           &$stem_caching_sub(@_);
380             }
381              
382             1;