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