File Coverage

blib/lib/Lingua/Stem/Sv.pm
Criterion Covered Total %
statement 44 61 72.1
branch 12 26 46.1
condition 1 3 33.3
subroutine 7 9 77.7
pod 3 3 100.0
total 67 102 65.6


line stmt bran cond sub pod time code
1             package Lingua::Stem::Sv;
2              
3             # $RCSfile: De.pm,v $ $Revision: 1.4 $ $Date: 1999/06/24 23:33:37 $ $Author: snowhare $
4              
5             =head1 NAME
6              
7             Lingua::Stem::Sv - Stemming algorithm for Swedish
8              
9             =head1 SYNOPSIS
10              
11             use Lingua::Stem::Sv;
12             my $stems = Lingua::Stem::Sv::stem({ -words => $word_list_reference,
13             -locale => 'sv',
14             -exceptions => $exceptions_hash,
15             });
16              
17             =head1 DESCRIPTION
18              
19             This routine applies a stemming slgorithm to a passed anon array of Swedish words,
20             returning the stemmed words as an anon array.
21              
22             It is a 'convienence' wrapper for 'Lingua::Stem::Snowball::Se' that provides
23             a standardized interface and caching.
24              
25             =head1 CHANGES
26              
27             1.01 2003.09.28 - Documentation fix
28              
29             1.00 2003.04.05 - Initial release
30              
31             =cut
32              
33             #######################################################################
34             # Initialization
35             #######################################################################
36              
37 1     1   6 use strict;
  1         2  
  1         46  
38              
39 1     1   969 use Lingua::Stem::Snowball::Se;
  1         1749  
  1         39  
40              
41 1     1   9 use Exporter;
  1         3  
  1         52  
42 1     1   7 use Carp;
  1         3  
  1         85  
43 1     1   7 use vars qw (@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS $VERSION);
  1         3  
  1         121  
44             BEGIN {
45 1     1   21 @ISA = qw (Exporter);
46 1         3 @EXPORT = ();
47 1         3 @EXPORT_OK = qw (stem clear_stem_cache stem_caching);
48 1         563 %EXPORT_TAGS = ();
49             }
50             $VERSION = "1.01";
51              
52             my $Stem_Caching = 0;
53             my $Stem_Cache = {};
54              
55             =head1 METHODS
56              
57             =cut
58              
59             #######################################################################
60              
61             =over 4
62              
63             =item stem({ -words => \@words, -locale => 'sv', -exceptions => \%exceptions });
64              
65             Stems a list of passed words using the rules of Swedish. Returns
66             an anonymous array reference to the stemmed words.
67              
68             Example:
69              
70             my $stemmed_words = Lingua::Stem::Sv::stem({ -words => \@words,
71             -locale => 'sv',
72             -exceptions => \%exceptions,
73             });
74              
75             =back
76              
77             =cut
78              
79             sub stem {
80 1 50   1 1 6 return [] if ($#_ == -1);
81 1         2 my $parm_ref;
82 1 50       5 if (ref $_[0]) {
83 1         2 $parm_ref = shift;
84             } else {
85 0         0 $parm_ref = { @_ };
86             }
87            
88 1         2 my $words = [];
89 1         2 my $locale = 'sv';
90 1         3 my $exceptions = {};
91 1         4 foreach (keys %$parm_ref) {
92 3         7 my $key = lc ($_);
93 3 100       15 if ($key eq '-words') {
    100          
    50          
94 1         2 @$words = @{$parm_ref->{$key}};
  1         4  
95             } elsif ($key eq '-exceptions') {
96 1         4 $exceptions = $parm_ref->{$key};
97             } elsif ($key eq '-locale') {
98 1         3 $locale = $parm_ref->{$key};
99             } else {
100 0         0 croak (__PACKAGE__ . "::stem() - Unknown parameter '$key' with value '$parm_ref->{$key}'\n");
101             }
102             }
103              
104 1         2 local $_;
105 1 50       2 my $use_cache = ($Stem_Caching > 1) ? 1 : 0;
106 1         5 my $stemmer = Lingua::Stem::Snowball::Se->new( use_cache => $use_cache );
107 1         15 foreach (@$words) {
108              
109             # Check against exceptions list
110 1 50       4 if (exists $exceptions->{$_}) {
111 0         0 $_ = $exceptions->{$_};
112 0         0 next;
113             }
114              
115             # Check against cache of stemmed words
116 1         2 my $original_word = $_;
117 1 50 33     5 if ($Stem_Caching && defined ($Stem_Cache->{$original_word})) {
118 0         0 $_ = $Stem_Cache->{$original_word};
119 0         0 next;
120             }
121              
122 1         5 $_ = $stemmer->stem("$_");
123 1 50       197 $Stem_Cache->{$original_word} = $_ if $Stem_Caching;
124             }
125 1 50       6 $Stem_Cache = {} if ($Stem_Caching < 2);
126            
127 1         12 return $words;
128             }
129              
130             ##############################################################
131              
132             =over 4
133              
134             =item stem_caching({ -level => 0|1|2 });
135              
136             Sets the level of stem caching.
137              
138             '0' means 'no caching'. This is the default level.
139              
140             '1' means 'cache per run'. This caches stemming results during a single
141             call to 'stem'.
142              
143             '2' means 'cache indefinitely'. This caches stemming results until
144             either the process exits or the 'clear_stem_cache' method is called.
145              
146             =back
147              
148             =cut
149              
150             sub stem_caching {
151 0     0 1   my $parm_ref;
152 0 0         if (ref $_[0]) {
153 0           $parm_ref = shift;
154             } else {
155 0           $parm_ref = { @_ };
156             }
157 0           my $caching_level = $parm_ref->{-level};
158 0 0         if (defined $caching_level) {
159 0 0         if ($caching_level !~ m/^[012]$/) {
160 0           croak(__PACKAGE__ . "::stem_caching() - Legal values are '0','1' or '2'. '$caching_level' is not a legal value");
161             }
162 0           $Stem_Caching = $caching_level;
163             }
164 0           return $Stem_Caching;
165             }
166            
167             ##############################################################
168              
169             =over 4
170              
171             =item clear_stem_cache;
172              
173             Clears the cache of stemmed words
174              
175             =back
176              
177             =cut
178              
179             sub clear_stem_cache {
180 0     0 1   $Stem_Cache = {};
181             }
182              
183             ##############################################################
184              
185             =head1 NOTES
186              
187             This code is a wrapper around Lingua::Stem::Snowball::Sv written by
188             Ask Solem Hoel,
189              
190             =head1 SEE ALSO
191              
192             Lingua::Stem Lingua::Stem::Snowball::Sv
193              
194             =head1 AUTHOR
195              
196             Integration in Lingua::Stem by
197             Benjamin Franz, FreeRun Technologies,
198             snowhare@nihongo.org or http://www.nihongo.org/snowhare/
199              
200             =head1 COPYRIGHT
201              
202             Benjamin Franz, FreeRun Technologies
203              
204             This code is freely available under the same terms as Perl.
205              
206             =head1 BUGS
207              
208             =head1 TODO
209              
210             =cut
211              
212             1;