File Coverage

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