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