-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathczech_stemmer.py
executable file
·174 lines (153 loc) · 5.79 KB
/
czech_stemmer.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
Czech stemmer
Copyright © 2010 Luís Gomes <[email protected]>.
Ported from the Java implementation available at:
http://members.unine.ch/jacques.savoy/clef/index.html
"""
import re
import sys
def cz_stem(word, aggressive=False):
if not re.match("^\\w+$", word):
return word
if not word.islower() and not word.istitle() and not word.isupper():
print("warning: skipping word with mixed case: {}".format(word),
file=sys.stderr)
return word
s = word.lower() # all our pattern matching is done in lowercase
s = _remove_case(s)
s = _remove_possessives(s)
if aggressive:
s = _remove_comparative(s)
s = _remove_diminutive(s)
s = _remove_augmentative(s)
s = _remove_derivational(s)
if word.isupper():
return s.upper()
if word.istitle():
return s.title()
return s
def _remove_case(word):
if len(word) > 7 and word.endswith("atech"):
return word[:-5]
if len(word) > 6:
if word.endswith("ětem"):
return _palatalise(word[:-3])
if word.endswith("atům"):
return word[:-4]
if len(word) > 5:
if word[-3:] in {"ech", "ich", "ích", "ého", "ěmi", "emi", "ému",
"ete", "eti", "iho", "ího", "ími", "imu"}:
return _palatalise(word[:-2])
if word[-3:] in {"ách", "ata", "aty", "ých", "ama", "ami",
"ové", "ovi", "ými"}:
return word[:-3]
if len(word) > 4:
if word.endswith("em"):
return _palatalise(word[:-1])
if word[-2:] in {"es", "ém", "ím"}:
return _palatalise(word[:-2])
if word[-2:] in {"ům", "at", "ám", "os", "us", "ým", "mi", "ou"}:
return word[:-2]
if len(word) > 3:
if word[-1] in "eiíě":
return _palatalise(word)
if word[-1] in "uyůaoáéý":
return word[:-1]
return word
def _remove_possessives(word):
if len(word) > 5:
if word[-2:] in {"ov", "ův"}:
return word[:-2]
if word.endswith("in"):
return _palatalise(word[:-1])
return word
def _remove_comparative(word):
if len(word) > 5:
if word[-3:] in {"ejš", "ějš"}:
return _palatalise(word[:-2])
return word
def _remove_diminutive(word):
if len(word) > 7 and word.endswith("oušek"):
return word[:-5]
if len(word) > 6:
if word[-4:] in {"eček", "éček", "iček", "íček", "enek", "ének",
"inek", "ínek"}:
return _palatalise(word[:-3])
if word[-4:] in {"áček", "aček", "oček", "uček", "anek", "onek",
"unek", "ánek"}:
return _palatalise(word[:-4])
if len(word) > 5:
if word[-3:] in {"ečk", "éčk", "ičk", "íčk", "enk", "énk",
"ink", "ínk"}:
return _palatalise(word[:-3])
if word[-3:] in {"áčk", "ačk", "očk", "učk", "ank", "onk",
"unk", "átk", "ánk", "ušk"}:
return word[:-3]
if len(word) > 4:
if word[-2:] in {"ek", "ék", "ík", "ik"}:
return _palatalise(word[:-1])
if word[-2:] in {"ák", "ak", "ok", "uk"}:
return word[:-1]
if len(word) > 3 and word[-1] == "k":
return word[:-1]
return word
def _remove_augmentative(word):
if len(word) > 6 and word.endswith("ajzn"):
return word[:-4]
if len(word) > 5 and word[-3:] in {"izn", "isk"}:
return _palatalise(word[:-2])
if len(word) > 4 and word.endswith("ák"):
return word[:-2]
return word
def _remove_derivational(word):
if len(word) > 8 and word.endswith("obinec"):
return word[:-6]
if len(word) > 7:
if word.endswith("ionář"):
return _palatalise(word[:-4])
if word[-5:] in {"ovisk", "ovstv", "ovišt", "ovník"}:
return word[:-5]
if len(word) > 6:
if word[-4:] in {"ásek", "loun", "nost", "teln", "ovec", "ovík",
"ovtv", "ovin", "štin"}:
return word[:-4]
if word[-4:] in {"enic", "inec", "itel"}:
return _palatalise(word[:-3])
if len(word) > 5:
if word.endswith("árn"):
return word[:-3]
if word[-3:] in {"ěnk", "ián", "ist", "isk", "išt", "itb", "írn"}:
return _palatalise(word[:-2])
if word[-3:] in {"och", "ost", "ovn", "oun", "out", "ouš",
"ušk", "kyn", "čan", "kář", "néř", "ník",
"ctv", "stv"}:
return word[:-3]
if len(word) > 4:
if word[-2:] in {"áč", "ač", "án", "an", "ář", "as"}:
return word[:-2]
if word[-2:] in {"ec", "en", "ěn", "éř", "íř", "ic", "in", "ín",
"it", "iv"}:
return _palatalise(word[:-1])
if word[-2:] in {"ob", "ot", "ov", "oň", "ul", "yn", "čk", "čn",
"dl", "nk", "tv", "tk", "vk"}:
return word[:-2]
if len(word) > 3 and word[-1] in "cčklnt":
return word[:-1]
return word
def _palatalise(word):
if word[-2:] in {"ci", "ce", "či", "če"}:
return word[:-2] + "k"
if word[-2:] in {"zi", "ze", "ži", "že"}:
return word[:-2] + "h"
if word[-3:] in {"čtě", "čti", "čtí"}:
return word[:-3] + "ck"
if word[-3:] in {"ště", "šti", "ští"}:
return word[:-3] + "sk"
return word[:-1]
if __name__ == '__main__':
if len(sys.argv) != 2 or sys.argv[1] not in ("light", "aggressive"):
sys.exit("usage: {} light|aggressive".format(sys.argv[0]))
aggressive = sys.argv[1] == "aggressive"
for line in sys.stdin:
print(*[cz_stem(word, aggressive=aggressive)
for word in line.split()])