forked from LeonieWeissweiler/CISTEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cistem.pm
163 lines (116 loc) · 3.88 KB
/
Cistem.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/perl
use utf8;
package Cistem;
sub stem{
$word = shift;
$case_insensitive = shift;
$word =~ s/Ü/U/g;
$word =~ s/Ö/O/g;
$word =~ s/Ä/A/g;
$word =~ s/üü/uu/g; #necessary because of Perl Unicode problems
$word =~ s/öö/oo/g;
$word =~ s/ää/aa/g;
$word =~ s/ü/u/g;
$word =~ s/ö/o/g;
$word =~ s/ä/a/g;
$upper = (ucfirst $word eq $word);
$word = lc($word);
$word =~ s/ß/ss/g;
$word =~ s/^ge(.{4,})/\1/;
$word =~s/sch/\$/g;
$word =~s/ei/\%/g;
$word =~s/ie/\&/g;
$word =~ s/(.)\1/\1\*/g;
while(length($word)>3){
if(length($word)>5 && ($word =~ s/e[mr]$// || $word =~ s/nd$//)){
pass;
}
elsif((!($upper) || $case_insensitive) && $word =~ s/t$//){
pass;
}
elsif($word =~ s/[esn]$//){
pass;
}
else{
last;
}
}
$word =~s/(.)\*/\1\1/g;
$word =~s/\$/sch/g;
$word =~s/\%/ei/g;
$word =~s/\&/ie/g;
return $word;
}
sub segment{
$word = shift;
$case_insensitive = shift;
$rest_length = 0;
$upper = (ucfirst $word eq $word);
$word = lc($word);
$original = $word;
$word =~s/sch/\$/g;
$word =~s/ei/\%/g;
$word =~s/ie/\&/g;
$word =~ s/(.)\1/\1\*/g;
while(length($word)>3){
if(length($word)>5 && ($word =~ s/(e[mr])$// || $word =~ s/(nd)$//)){
$rest_length += 2;
}
elsif((!($upper) || $case_insensitive) && $word =~ s/t$//){
$rest_length++;
}
elsif($word =~ s/([esn])$//){
$rest_length++;
}
else{
last;
}
}
$word =~s/(.)\*/\1\1/g;
$word =~s/\$/sch/g;
$word =~s/\%/ei/g;
$word =~s/\&/ie/g;
if($rest_length){
$rest = substr($original, - $rest_length);
}
else{
$rest = "";
}
return ($word,$rest);
}
1;
=pod
=head1 NAME
CISTEM Stemmer for German
=head1 SYNOPSIS
use Cistem;
my $stemmed_word = stem($word);
or, for segmentation:
my @segments = segment($word);
=head1 DESCRIPTION
This is the official Perl implementation of the CISTEM stemmer.
It is based on the paper
Leonie Weißweiler, Alexander Fraser (2017). Developing a Stemmer for German Based on a Comparative Analysis of Publicly Available Stemmers. In Proceedings of the German Society for Computational Linguistics and Language Technology (GSCL)
which can be read here:
http://www.cis.lmu.de/~weissweiler/cistem/
In the paper, we conducted an analysis of publicly available stemmers, developed
two gold standards for German stemming and evaluated the stemmers based on the
two gold standards. We then proposed the stemmer implemented here and show
that it achieves slightly better f-measure than the other stemmers and is
thrice as fast as the Snowball stemmer for German while being about as fast as
most other stemmers.
=head1 METHODS
=over 8
=item stem($word, $case_insensitivity)
This method takes the word to be stemmed and a boolean specifiying if case-insensitive stemming should be used and returns the stemmed word. If only the word
is passed to the method or the second parameter is 0, normal case-sensitive stemming is used, if the second parameter is 1, case-insensitive stemming is used.
Case sensitivity improves performance only if words in the text may be incorrectly upper case.
For all-lowercase and correctly cased text, best performance is achieved by
using the case-sensitive version.
=item segment($word, $case_insensitivity)
This method works very similarly to stem. The only difference is that in
addition to returning the stem, it also returns the rest that was removed at
the end. To be able to return the stem unchanged so the stem and the rest
can be concatenated to form the original word, all subsitutions that altered
the stem in any other way than by removing letters at the end were left out.
=cut