-
Notifications
You must be signed in to change notification settings - Fork 0
/
stemmer.go
227 lines (200 loc) · 6.86 KB
/
stemmer.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package stemmer
import (
"regexp"
"strings"
)
//roots represents standalone root words
var roots = []string{"ĉar", "ĉi", "ĉu", "kaj", "ke", "la", "minus", "plus",
"se", "ĉe", "da", "de", "el", "ekster", "en", "ĝis", "je", "kun", "na",
"po", "pri", "pro", "sen", "tra", "ajn", "do", "ja", "jen", "ju", "ne",
"pli", "tamen", "tre", "tro", "ci", "ĝi", "ili", "li", "mi", "ni", "oni",
"ri", "si", "ŝi", "ŝli", "vi", "unu", "du", "tri", "kvin", "ĵus", "nun", "plu",
"tuj", "amen", "bis", "boj", "fi", "ha", "he", "ho", "hu", "hura", "nu", "ve",
"esperanto",
}
//correlative roots
var correlatives = []string{
"kia", "kial", "kiam", "kie", "kiel", "kies", "kio", "kiom", "kiu",
"tia", "tial", "tiam", "tie", "tiel", "ties", "tio", "tiom", "tiu",
"ia", "ial", "iam", "ie", "iel", "ies", "io", "iom", "iu",
"ĉia", "ĉial", "ĉiam", "ĉie", "ĉiel", "ĉies", "ĉio", "ĉiom", "ĉiu",
"nenia", "nenial", "neniam", "nenie", "neniel", "nenies", "nenio", "neniom", "neniu",
}
//Stem returns the word's base form.
func Stem(word string) string {
word = strings.TrimSpace(strings.ToLower(word))
//standalone roots
for _, root := range roots {
if word == root {
return word
}
}
//l’ l' → la
if word == "l’" || word == "l'" {
return "la"
}
//un’ un' unuj → unu
if word == "un’" || word == "un'" || word == "unuj" {
return "unu"
}
//-’ -' → -o
word = replaceSuffix(word, "'", "o")
word = replaceSuffix(word, "’", "o")
//’st- 'st- → est-
word = replacePrefix(word, "’st", "est")
word = replacePrefix(word, "'st", "est")
// nouns, adjectives, -u correlatives:
// -oj -on -ojn → o
// -aj -an -ajn → a
// -uj -un -ujn → u
word = replaceSuffix(word, "oj", "o")
word = replaceSuffix(word, "on", "o")
word = replaceSuffix(word, "ojn", "o")
word = replaceSuffix(word, "aj", "a")
word = replaceSuffix(word, "an", "a")
word = replaceSuffix(word, "ajn", "a")
word = replaceSuffix(word, "uj", "u")
word = replaceSuffix(word, "un", "u")
word = replaceSuffix(word, "ujn", "u")
//correlatives: -en → -e
for _, s := range []string{"kien", "tien", "ien", "nenien", "ĉien"} {
if word == s {
return word[:len(word)-1]
}
}
//correlative roots
for _, s := range correlatives {
if word == s {
return word
}
}
//accusative pronouns: -in → -i
if strings.HasSuffix(word, "in") {
return replaceSuffix(word, "in", "i")
}
//accusative adverbs: -en → -o
word = replaceSuffix(word, "en", "o")
//verbs: -is -as -os -us -u → -i
word = replaceSuffix(word, "is", "i")
word = replaceSuffix(word, "as", "i")
word = replaceSuffix(word, "os", "i")
word = replaceSuffix(word, "us", "i")
word = replaceSuffix(word, "u", "i")
//lexical aspect: ek- el-
if !strings.HasPrefix(word, "ekscit") {
word = strings.TrimPrefix(word, "ek")
}
if !strings.HasPrefix(word, "elefant") {
word = strings.TrimPrefix(word, "el")
}
//imperfective verbs & action nouns: -adi -ado → -i
if strings.HasSuffix(word, "adi") {
return replaceSuffix(word, "adi", "i")
} else if strings.HasSuffix(word, "ado") {
return replaceSuffix(word, "ado", "i")
}
// compound verbs:
// -inti -anti -onti -iti -ati -oti → -i
// -inte -ante -onte -ite -ate -ote → -i
// -inta -anta -onta -ita -ata -ota → -i
switch {
case strings.HasSuffix(word, "inti"):
return replaceSuffix(word, "inti", "i")
case strings.HasSuffix(word, "anti"):
return replaceSuffix(word, "anti", "i")
case strings.HasSuffix(word, "onti"):
return replaceSuffix(word, "onti", "i")
case strings.HasSuffix(word, "iti"):
return replaceSuffix(word, "iti", "i")
case strings.HasSuffix(word, "ati"):
return replaceSuffix(word, "ati", "i")
case strings.HasSuffix(word, "oti"):
return replaceSuffix(word, "oti", "i")
case strings.HasSuffix(word, "inte"):
return replaceSuffix(word, "inte", "i")
case strings.HasSuffix(word, "ante"):
return replaceSuffix(word, "ante", "i")
case strings.HasSuffix(word, "onte"):
return replaceSuffix(word, "onte", "i")
case strings.HasSuffix(word, "ite"):
return replaceSuffix(word, "ite", "i")
case strings.HasSuffix(word, "ate"):
return replaceSuffix(word, "ate", "i")
case strings.HasSuffix(word, "ote"):
return replaceSuffix(word, "ote", "i")
case strings.HasSuffix(word, "inta"):
return replaceSuffix(word, "inta", "i")
case strings.HasSuffix(word, "anta"):
return replaceSuffix(word, "anta", "i")
case strings.HasSuffix(word, "onta"):
return replaceSuffix(word, "onta", "i")
case strings.HasSuffix(word, "ita"):
return replaceSuffix(word, "ita", "i")
case strings.HasSuffix(word, "ata"):
return replaceSuffix(word, "ata", "i")
case strings.HasSuffix(word, "ota"):
return replaceSuffix(word, "ota", "i")
}
// participle nouns:
// -into -anto -onto → -anto
// -ito -ato -oto → -ato
switch {
case strings.HasSuffix(word, "into"):
return replaceSuffix(word, "into", "anto")
case strings.HasSuffix(word, "anto"):
return replaceSuffix(word, "anto", "anto")
case strings.HasSuffix(word, "onto"):
return replaceSuffix(word, "onto", "anto")
case strings.HasSuffix(word, "ito"):
return replaceSuffix(word, "ito", "ato")
case strings.HasSuffix(word, "ato"):
return replaceSuffix(word, "ato", "ato")
case strings.HasSuffix(word, "oto"):
return replaceSuffix(word, "oto", "ato")
}
return word
}
//StemAggressive performs a aggressive stemming on the word and returns the simplest
//form of it's root form.
func StemAggressive(word string) string {
word = Stem(word)
//root words
for _, root := range roots {
if word == root {
return word
}
}
//remove final suffix if it's a vowel
if strings.HasSuffix(word, "a") || strings.HasSuffix(word, "e") || strings.HasSuffix(word, "i") ||
strings.HasSuffix(word, "o") || strings.HasSuffix(word, "u") {
word = word[:len(word)-1]
}
//remove suffix for participle nouns:
//-int- -ant- -ont- -it- -at- -ot-
matched, err := regexp.MatchString("[aeiou].*(int|ant|ont|it|at|ot)$", word)
if err == nil && matched {
switch {
case strings.HasSuffix(word, "int") || strings.HasSuffix(word, "ant") || strings.HasSuffix(word, "ont"):
word = word[:len(word)-3] // I chose not to use strings.TrimSuffix because because it calls strings.HasSuffix again.
case strings.HasSuffix(word, "it") || strings.HasSuffix(word, "at") || strings.HasSuffix(word, "ot"):
word = word[:len(word)-2]
}
}
return word
}
//replaceSuffix returns s with the trailing oldSuffix replaced with the newSuffix.
//If s doesn't end with the suffix, s is returned unchanged.
func replaceSuffix(s, oldSuffix, newSuffix string) string {
if strings.HasSuffix(s, oldSuffix) {
return s[:len(s)-len(oldSuffix)] + newSuffix
}
return s
}
//replacePrefix returns s with the leading oldPrefix replaced with the newPrefix.
//If s doesn't start with the prefix, s is returned unchanged.
func replacePrefix(s, oldPrefix, newPrefix string) string {
if strings.HasPrefix(s, oldPrefix) {
return newPrefix + s[len(oldPrefix):]
}
return s
}