File Coverage

blib/lib/Lingua/Stem/De.pm
Criterion Covered Total %
statement 42 59 71.1
branch 11 24 45.8
condition 1 3 33.3
subroutine 7 9 77.7
pod 3 3 100.0
total 64 98 65.3


line stmt bran cond sub pod time code
1             package Lingua::Stem::De;
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::De - Stemming algorithm for German
8              
9             =head1 SYNOPSIS
10              
11             use Lingua::Stem::De;
12             my $stems = Lingua::Stem::De::stem({ -words => $word_list_reference,
13             -locale => 'de',
14             -exceptions => $exceptions_hash,
15             });
16              
17             =head1 DESCRIPTION
18              
19             This routine applies a stemming slgorithm to a passed anon array of German words,
20             returning the stemmed words as an anon array.
21              
22             It is a 'convienence' wrapper for 'Text::German' 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   5 use strict;
  1         1  
  1         37  
38              
39 1     1   1298 use Text::German;
  1         82953  
  1         41  
40              
41 1     1   11 use Exporter;
  1         1  
  1         45  
42 1     1   6 use Carp;
  1         2  
  1         82  
43 1     1   6 use vars qw (@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS $VERSION);
  1         2  
  1         117  
44             BEGIN {
45 1     1   23 @ISA = qw (Exporter);
46 1         3 @EXPORT = ();
47 1         3 @EXPORT_OK = qw (stem clear_stem_cache stem_caching);
48 1         555 %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 => 'de', -exceptions => \%exceptions });
64              
65             Stems a list of passed words using the rules of German Returns
66             an anonymous array reference to the stemmed words.
67              
68             Example:
69              
70             my $stemmed_words = Lingua::Stem::De::stem({ -words => \@words,
71             -locale => 'de',
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         2 my $parm_ref;
82 1 50       5 if (ref $_[0]) {
83 1         3 $parm_ref = shift;
84             } else {
85 0         0 $parm_ref = { @_ };
86             }
87            
88 1         2 my $words = [];
89 1         2 my $locale = 'de';
90 1         2 my $exceptions = {};
91 1         5 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         10  
95             } elsif ($key eq '-exceptions') {
96 1         3 $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         2 foreach (@$words) {
106              
107             # Check against exceptions list
108 14 50       32 if (exists $exceptions->{$_}) {
109 0         0 $_ = $exceptions->{$_};
110 0         0 next;
111             }
112              
113             # Check against cache of stemmed words
114 14         14 my $original_word = $_;
115 14 50 33     28 if ($Stem_Caching && exists $Stem_Cache->{$original_word}) {
116 0         0 $_ = $Stem_Cache->{$original_word};
117 0         0 next;
118             }
119              
120 14         40 $_= Text::German::reduce("$_");
121 14 50       3798 $Stem_Cache->{$original_word} = $_ if $Stem_Caching;
122             }
123 1 50       5 $Stem_Cache = {} if ($Stem_Caching < 2);
124            
125 1         8 return $words;
126             }
127              
128             ##############################################################
129              
130             =over 4
131              
132             =item stem_caching({ -level => 0|1|2 });
133              
134             Sets the level of stem caching.
135              
136             '0' means 'no caching'. This is the default level.
137              
138             '1' means 'cache per run'. This caches stemming results during a single
139             call to 'stem'.
140              
141             '2' means 'cache indefinitely'. This caches stemming results until
142             either the process exits or the 'clear_stem_cache' method is called.
143              
144             =back
145              
146             =cut
147              
148             sub stem_caching {
149 0     0 1   my $parm_ref;
150 0 0         if (ref $_[0]) {
151 0           $parm_ref = shift;
152             } else {
153 0           $parm_ref = { @_ };
154             }
155 0           my $caching_level = $parm_ref->{-level};
156 0 0         if (defined $caching_level) {
157 0 0         if ($caching_level !~ m/^[012]$/) {
158 0           croak(__PACKAGE__ . "::stem_caching() - Legal values are '0','1' or '2'. '$caching_level' is not a legal value");
159             }
160 0           $Stem_Caching = $caching_level;
161             }
162 0           return $Stem_Caching;
163             }
164            
165             ##############################################################
166              
167             =over 4
168              
169             =item clear_stem_cache;
170              
171             Clears the cache of stemmed words
172              
173             =back
174              
175             =cut
176              
177             sub clear_stem_cache {
178 0     0 1   $Stem_Cache = {};
179             }
180              
181             ##############################################################
182              
183             =head1 NOTES
184              
185             This code is almost entirely derived from Text::German
186             written by Ulrich Pfeifer
187              
188             =head1 SEE ALSO
189              
190             Lingua::Stem Text::German
191              
192             =head1 AUTHOR
193              
194             Ulrich Pfeifer
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             Ulrich Pfeifer
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;