File Coverage

blib/lib/Lingua/Stem/Da.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::Da;
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::Da - Stemming algorithm for Danish
8              
9             =head1 SYNOPSIS
10              
11             use Lingua::Stem::Da;
12             my $stems = Lingua::Stem::Da::stem({ -words => $word_list_reference,
13             -locale => 'da',
14             -exceptions => $exceptions_hash,
15             });
16              
17             =head1 DESCRIPTION
18              
19             This routine applies a stemming slgorithm to a passed anon array of Danish words,
20             returning the stemmed words as an anon array.
21              
22             It is a 'convienence' wrapper for 'Lingua::Stem::Snowball::Da' 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   7 use strict;
  1         3  
  1         43  
38              
39 1     1   876 use Lingua::Stem::Snowball::Da;
  1         9842  
  1         66  
40              
41 1     1   15 use Exporter;
  1         2  
  1         59  
42 1     1   8 use Carp;
  1         2  
  1         85  
43 1     1   5 use vars qw (@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS $VERSION);
  1         3  
  1         139  
44             BEGIN {
45 1     1   21 @ISA = qw (Exporter);
46 1         2 @EXPORT = ();
47 1         3 @EXPORT_OK = qw (stem clear_stem_cache stem_caching);
48 1         561 %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 => 'da', -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::Da::stem({ -words => \@words,
71             -locale => 'da',
72             -exceptions => \%exceptions,
73             });
74              
75             =back
76              
77             =cut
78              
79             sub stem {
80 1 50   1 1 4 return [] if ($#_ == -1);
81 1         1 my $parm_ref;
82 1 50       6 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         3 my $locale = 'en';
90 1         2 my $exceptions = {};
91 1         6 foreach (keys %$parm_ref) {
92 3         5 my $key = lc ($_);
93 3 100       11 if ($key eq '-words') {
    100          
    50          
94 1         2 @$words = @{$parm_ref->{$key}};
  1         5  
95             } elsif ($key eq '-exceptions') {
96 1         4 $exceptions = $parm_ref->{$key};
97             } elsif ($key eq '-locale') {
98 1         4 $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       3 my $use_cache = ($Stem_Caching > 1) ? 1 :0;
106 1         5 my $stemmer = Lingua::Stem::Snowball::Da->new( use_cache => $use_cache );
107 1         17 foreach (@$words) {
108              
109             # Check against exceptions list
110 3 50       10 if (exists $exceptions->{$_}) {
111 0         0 $_ = $exceptions->{$_};
112 0         0 next;
113             }
114              
115             # Check against cache of stemmed words
116 3         6 my $original_word = $_;
117 3 50 33     10 if ($Stem_Caching && exists $Stem_Cache->{$original_word}) {
118 0         0 $_ = $Stem_Cache->{$original_word};
119 0         0 next;
120             }
121              
122 3         16 $_ = $stemmer->stem("$_");
123 3 50       330 $Stem_Cache->{$original_word} = $_ if $Stem_Caching;
124             }
125 1 50       6 $Stem_Cache = {} if ($Stem_Caching < 2);
126            
127 1         15 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::Da written by
188              
189             Dennis Haney
190              
191             and
192              
193             Ask Solem Hoel, (Swedish version)
194              
195             =head1 SEE ALSO
196              
197             Lingua::Stem Lingua::Stem::Snowball::Da
198              
199             =head1 AUTHOR
200              
201             Integration in Lingua::Stem by
202             Benjamin Franz, FreeRun Technologies,
203             snowhare@nihongo.org or http://www.nihongo.org/snowhare/
204              
205             =head1 COPYRIGHT
206              
207             Benjamin Franz, FreeRun Technologies
208              
209             This code is freely available under the same terms as Perl.
210              
211             =head1 BUGS
212              
213             =head1 TODO
214              
215             =cut
216              
217             1;