File Coverage

blib/lib/Lingua/Stem.pm
Criterion Covered Total %
statement 113 162 69.7
branch 29 56 51.7
condition 0 3 0.0
subroutine 11 13 84.6
pod 10 10 100.0
total 163 244 66.8


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