-
Notifications
You must be signed in to change notification settings - Fork 10
/
Cistem.py
144 lines (114 loc) · 4.48 KB
/
Cistem.py
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
#!/usr/bin/python3
"""
CISTEM Stemmer for German
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.
"""
import re
stripge = re.compile(r"^ge(.{4,})")
replxx = re.compile(r"(.)\1")
replxxback = re.compile(r"(.)\*");
stripemr = re.compile(r"e[mr]$")
stripnd = re.compile(r"nd$")
stript = re.compile(r"t$")
stripesn = re.compile(r"[esn]$")
"""
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.
"""
def stem(word, case_insensitive = False):
if len(word) == 0:
return word
upper = word[0].isupper()
word = word.lower()
word = word.replace("ü","u")
word = word.replace("ö","o")
word = word.replace("ä","a")
word = word.replace("ß","ss")
word = stripge.sub(r"\1", word)
word = word.replace("sch","$")
word = word.replace("ei","%")
word = word.replace("ie","&")
word = replxx.sub(r"\1*", word)
while len(word) > 3:
if len(word) > 5:
(word, success) = stripemr.subn("", word)
if success != 0:
continue
(word, success) = stripnd.subn("", word)
if success != 0:
continue
if not upper or case_insensitive:
(word, success) = stript.subn("", word)
if success != 0:
continue
(word, success) = stripesn.subn("", word)
if success != 0:
continue
else:
break
word = replxxback.sub(r"\1\1", word)
word = word.replace("%","ei")
word = word.replace("&","ie")
word = word.replace("$","sch")
return word
"""
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.
"""
def segment(word, case_insensitive = False):
rest_length = 0
if len(word) == 0:
return ("", "")
upper = word[0].isupper()
word = word.lower()
original = word[:]
word = word.replace("sch","$")
word = word.replace("ei","%")
word = word.replace("ie","&")
word = replxx.sub(r"\1*", word)
while len(word) > 3:
if len(word) > 5:
(word, success) = stripemr.subn("", word)
if success != 0:
rest_length += 2
continue
(word, success) = stripnd.subn("", word)
if success != 0:
rest_length += 2
continue
if not upper or case_insensitive:
(word, success) = stript.subn("", word)
if success != 0:
rest_length += 1
continue
(word, success) = stripesn.subn("", word)
if success != 0:
rest_length += 1
continue
else:
break
word = replxxback.sub(r"\1\1", word)
word = word.replace("%","ei")
word = word.replace("&","ie")
word = word.replace("$","sch")
if rest_length:
rest = original[-rest_length:]
else:
rest = ""
return (word,rest)