File Coverage

blib/lib/Lingua/Stem/Snowball.pm
Criterion Covered Total %
statement 58 69 84.0
branch 26 40 65.0
condition 3 6 50.0
subroutine 12 13 92.3
pod 7 7 100.0
total 106 135 78.5


line stmt bran cond sub pod time code
1             package Lingua::Stem::Snowball;
2 4     4   69086 use strict;
  4         8  
  4         177  
3 4     4   20 use warnings;
  4         7  
  4         105  
4 4     4   106 use 5.006002;
  4         16  
  4         151  
5              
6 4     4   23 use Carp;
  4         5  
  4         381  
7 4     4   17 use Exporter;
  4         6  
  4         214  
8 4         3886 use vars qw(
9             $VERSION
10             @ISA
11             @EXPORT_OK
12             $AUTOLOAD
13             %EXPORT_TAGS
14             $stemmifier
15             %instance_vars
16 4     4   19 );
  4         6  
17              
18             $VERSION = '0.952';
19              
20             @ISA = qw( Exporter DynaLoader );
21             %EXPORT_TAGS = ( 'all' => [qw( stemmers stem )] );
22             @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
23              
24             require DynaLoader;
25             __PACKAGE__->bootstrap($VERSION);
26              
27             # Ensure that C symbols are exported so that other shared libaries (e.g.
28             # KinoSearch) can use them. See Dynaloader docs.
29 4     4 1 3077 sub dl_load_flags {0x01}
30              
31             # A shared home for the actual struct sb_stemmer C modules.
32             $stemmifier = Lingua::Stem::Snowball::Stemmifier->new;
33              
34             %instance_vars = (
35             lang => '',
36             encoding => undef,
37             locale => undef,
38             stemmer_id => -1,
39             strip_apostrophes => 0,
40             );
41              
42             sub new {
43 1441     1441 1 16429 my $class = shift;
44 1441   33     13350 my $self = bless { %instance_vars, @_ }, ref($class) || $class;
45              
46             # Validate lang, validate/guess encoding, and get an sb_stemmer.
47 1441         3898 $self->lang( $self->{lang} );
48 1441 50       3834 if ( !defined $self->{encoding} ) {
49 1441 100       4977 $self->{encoding}
    100          
    100          
50             = $self->{lang} eq 'ru' ? 'KOI8-R'
51             : $self->{lang} eq 'ro' ? 'ISO-8859-2'
52             : $self->{lang} eq 'tr' ? 'UTF-8'
53             : 'ISO-8859-1';
54             }
55 1441         6866 $self->_derive_stemmer;
56              
57 1441         2798 return $self;
58             }
59              
60             sub stem {
61 7282     7282 1 4339413 my ( $self, $lang, $words, $locale, $is_stemmed );
62              
63             # Support lots of DWIMmery.
64 7282 100       27396 if ( UNIVERSAL::isa( $_[0], 'HASH' ) ) {
65 5864         11998 ( $self, $words, $is_stemmed ) = @_;
66             }
67             else {
68 1418         3074 ( $lang, $words, $locale, $is_stemmed ) = @_;
69 1418         3922 $self = __PACKAGE__->new( lang => $lang );
70             }
71              
72             # Bail if we don't have a valid lang.
73 7282 100       18770 return undef unless $self->{lang};
74              
75             # Bail if there's no input.
76 7280 50 66     39499 return undef unless ( ref($words) or length($words) );
77              
78             # Duplicate the input array and transform it into an array of stems.
79 7280 100       20639 $words = ref($words) ? $words : [$words];
80 7280         12129 my @stems = map {lc} @$words;
  14414         35028  
81 7280         73224 $self->stem_in_place( \@stems );
82              
83             # Determine whether any stemming took place, if requested.
84 7280 50       16551 if ( ref($is_stemmed) ) {
85 0         0 $$is_stemmed = 0;
86 0 0       0 if ( $self->{stemmer_id} == -1 ) {
87 0         0 $$is_stemmed = 1;
88             }
89             else {
90 0         0 for ( 0 .. $#stems ) {
91 0 0       0 next if $stems[$_] eq $words->[$_];
92 0         0 $$is_stemmed = 1;
93 0         0 last;
94             }
95             }
96             }
97              
98 7280 100       34330 return wantarray ? @stems : $stems[0];
99             }
100              
101             sub lang {
102 1466     1466 1 46096 my ( $self, $lang ) = @_;
103 1466 100       2900 if ( defined $lang ) {
104 1460         10508 $lang = lc($lang);
105 1460 50       2579 $lang = $lang eq 'dk' ? 'nl' : $lang; # backwards compat
106 1460 100       4648 if ( _validate_language($lang) ) {
107 1454         3679 $self->{lang} = $lang;
108             # Force stemmer_id regen at next call to stem_in_place().
109 1454         2280 $self->{stemmer_id} = -1;
110             }
111             else {
112 6         16 $@ = "Language '$lang' does not exist";
113             }
114             }
115 1466         2297 return $self->{lang};
116             }
117              
118             sub encoding {
119 29     29 1 37927 my ( $self, $encoding ) = @_;
120 29 50       86 if ( defined $encoding ) {
121 29 50       219 croak("Invalid value for encoding: '$encoding'")
122             unless $encoding =~ /^(?:UTF-8|KOI8-R|ISO-8859-[12])$/;
123 29         60 $self->{encoding} = $encoding;
124             # Force stemmer_id regen at next call to stem_in_place().
125 29         60 $self->{stemmer_id} = -1;
126             }
127 29         68 return $self->{encoding};
128             }
129              
130             # Deprecated, has no effect on stemming behavior.
131             sub strip_apostrophes {
132 1     1 1 11 my ( $self, $boolean ) = @_;
133 1 50       4 if ( defined $boolean ) {
134 1 50       5 $self->{strip_apostrophes} eq $boolean ? 1 : 0;
135             }
136 1         3 return $self->{strip_apostrophes};
137             }
138              
139             # Deprecated, has no effect on stemming behavior.
140             sub locale {
141 0     0 1   my ( $self, $locale ) = @_;
142 0 0         if ($locale) {
143 0           $self->{locale} = $locale;
144             }
145 0           return $self->{locale};
146             }
147              
148             1;
149              
150             __END__