forked from pranjali2018201029/Megathon19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
porter.py
188 lines (144 loc) · 4.35 KB
/
porter.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
175
176
177
178
179
180
181
182
183
184
185
186
187
"""
Reimplementation of the
`Porter stemming algorithm <http://tartarus.org/~martin/PorterStemmer/>`_
in Python.
In my quick tests, this implementation about 3.5 times faster than the
seriously weird Python linked from the official page.
"""
import re
# Suffix replacement lists
_step2list = {
"ational": "ate",
"tional": "tion",
"enci": "ence",
"anci": "ance",
"izer": "ize",
"bli": "ble",
"alli": "al",
"entli": "ent",
"eli": "e",
"ousli": "ous",
"ization": "ize",
"ation": "ate",
"ator": "ate",
"alism": "al",
"iveness": "ive",
"fulness": "ful",
"ousness": "ous",
"aliti": "al",
"iviti": "ive",
"biliti": "ble",
"logi": "log",
}
_step3list = {
"icate": "ic",
"ative": "",
"alize": "al",
"iciti": "ic",
"ical": "ic",
"ful": "",
"ness": "",
}
_cons = "[^aeiou]"
_vowel = "[aeiouy]"
_cons_seq = "[^aeiouy]+"
_vowel_seq = "[aeiou]+"
# m > 0
_mgr0 = re.compile("^(" + _cons_seq + ")?" + _vowel_seq + _cons_seq)
# m == 0
_meq1 = re.compile("^(" + _cons_seq + ")?" + _vowel_seq + _cons_seq + "(" + _vowel_seq + ")?$")
# m > 1
_mgr1 = re.compile("^(" + _cons_seq + ")?" + _vowel_seq + _cons_seq + _vowel_seq + _cons_seq)
# vowel in stem
_s_v = re.compile("^(" + _cons_seq + ")?" + _vowel)
# ???
_c_v = re.compile("^" + _cons_seq + _vowel + "[^aeiouwxy]$")
# Patterns used in the rules
_ed_ing = re.compile("^(.*)(ed|ing)$")
_at_bl_iz = re.compile("(at|bl|iz)$")
_step1b = re.compile("([^aeiouylsz])\\1$")
_step2 = re.compile("^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$")
_step3 = re.compile("^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$")
_step4_1 = re.compile("^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$")
_step4_2 = re.compile("^(.+?)(s|t)(ion)$")
_step5 = re.compile("^(.+?)e$")
# Stemming function
def stem(w):
"""Uses the Porter stemming algorithm to remove suffixes from English
words.
>>> stem("fundamentally")
"fundament"
"""
if len(w) < 3: return w
first_is_y = w[0] == "y"
if first_is_y:
w = "Y" + w[1:]
# Step 1a
if w.endswith("s"):
if w.endswith("sses"):
w = w[:-2]
elif w.endswith("ies"):
w = w[:-2]
elif w[-2] != "s":
w = w[:-1]
# Step 1b
if w.endswith("eed"):
s = w[:-3]
if _mgr0.match(s):
w = w[:-1]
else:
m = _ed_ing.match(w)
if m:
stem = m.group(1)
if _s_v.match(stem):
w = stem
if _at_bl_iz.match(w):
w += "e"
elif _step1b.match(w):
w = w[:-1]
elif _c_v.match(w):
w += "e"
# Step 1c
if w.endswith("y"):
stem = w[:-1]
if _s_v.match(stem):
w = stem + "i"
# Step 2
m = _step2.match(w)
if m:
stem = m.group(1)
suffix = m.group(2)
if _mgr0.match(stem):
w = stem + _step2list[suffix]
# Step 3
m = _step3.match(w)
if m:
stem = m.group(1)
suffix = m.group(2)
if _mgr0.match(stem):
w = stem + _step3list[suffix]
# Step 4
m = _step4_1.match(w)
if m:
stem = m.group(1)
if _mgr1.match(stem):
w = stem
else:
m = _step4_2.match(w)
if m:
stem = m.group(1) + m.group(2)
if _mgr1.match(stem):
w = stem
# Step 5
m = _step5.match(w)
if m:
stem = m.group(1)
if _mgr1.match(stem) or (_meq1.match(stem) and not _c_v.match(stem)):
w = stem
if w.endswith("ll") and _mgr1.match(w):
w = w[:-1]
if first_is_y:
w = "y" + w[1:]
return w
if __name__ == '__main__':
print(stem("fundamentally"))