-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstemmer.go
474 lines (455 loc) · 14.3 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package stemmer
import "bytes"
const (
vowel_state = iota
consonant_state
)
func measure(word []byte) int {
m := 0
if len(word) > 0 {
var state int = consonant_state
if vowel(word, 0) {
state = vowel_state
}
for i := 0; i < len(word); i++ {
if vowel(word, i) && state == consonant_state {
state = vowel_state
} else if consonant(word, i) && state == vowel_state {
state = consonant_state
m++
}
}
}
return m
}
//
// A \consonant\ in a word is a letter other than A, E, I, O or U, and other
// than Y preceded by a consonant.
//
func consonant(word []byte, i int) bool {
switch word[i] {
case byte('a'), byte('e'), byte('i'), byte('o'), byte('u'):
return false
case byte('y'):
if 0 == i {
return true
} else {
return (i > 0 && !consonant(word, i-1))
}
default:
return true
}
}
func vowel(word []byte, i int) bool {
return !consonant(word, i)
}
//
// *v* - the stem contains a vowel
//
func containVowel(word []byte) bool {
for i, _ := range word {
if vowel(word, i) {
return true
}
}
return false
}
//
// Step 1a
//
// SSES -> SS caresses -> caress
// IES -> I ponies -> poni
// ties -> ti
// SS -> SS caress -> caress
// S -> cats -> cat
//
func firstA(word []byte) []byte {
if bytes.HasSuffix(word, []byte("s")) {
if bytes.HasSuffix(word, []byte("sses")) || bytes.HasSuffix(word, []byte("ies")) {
return word[:len(word)-2]
} else if bytes.HasSuffix(word, []byte("ss")) {
return word
}
return word[:len(word)-1]
}
return word
}
//
// Step 1b
//
// (m>0) EED -> EE feed -> feed
// agreed -> agree
// (*v*) ED -> plastered -> plaster
// bled -> bled
// (*v*) ING -> motoring -> motor
// sing -> sing
//
func firstB(word []byte) []byte {
l := len(word)
if bytes.HasSuffix(word, []byte("ed")) {
if bytes.HasSuffix(word, []byte("eed")) {
if m := measure(word[:l-3]); m > 0 {
return bytes.TrimSuffix(word, []byte("d"))
}
} else {
if containVowel(word[:l-2]) {
return firstB2(bytes.TrimSuffix(word, []byte("ed")))
}
}
} else if bytes.HasSuffix(word, []byte("ing")) {
if containVowel(word[:l-3]) {
return firstB2(bytes.TrimSuffix(word, []byte("ing")))
}
}
return word
}
//
// AT -> ATE conflat(ed) -> conflate
// BL -> BLE troubl(ed) -> trouble
// IZ -> IZE siz(ed) -> size
// (*d and not (*L or *S or *Z))
// -> single letter
// hopp(ing) -> hop
// tann(ed) -> tan
// fall(ing) -> fall
// hiss(ing) -> hiss
// fizz(ed) -> fizz
// (m=1 and *o) -> E fail(ing) -> fail
// fil(ing) -> file
//
func firstB2(word []byte) []byte {
l := len(word)
if bytes.HasSuffix(word, []byte("at")) || bytes.HasSuffix(word, []byte("iz")) || bytes.HasSuffix(word, []byte("bl")) {
return append(word, 'e')
// (*d and not (*L or *S or *Z)) -> single letter
} else if consonant(word, l-1) && word[l-1] == word[l-2] {
if !bytes.HasSuffix(word, []byte("l")) && !bytes.HasSuffix(word, []byte("s")) && !bytes.HasSuffix(word, []byte("z")) {
return word[:l-1]
}
} else if m := measure(word); m == 1 {
//*o - the stem ends cvc, where the second c is not W, X or Y (e.g. -WIL, -HOP).
//(m=1 and *o) -> E
if isCVCSuffix(word) {
return append(word, 'e')
}
}
return word
}
//
//*o - the stem ends cvc, where the second c is not W, X or Y (e.g.
// -WIL, -HOP)
//
func isCVCSuffix(word []byte) bool {
size := len(word) - 1
if size >= 2 && consonant(word, size-2) && vowel(word, size-1) && consonant(word, size) && word[size] != 'w' && word[size] != 'x' && word[size] != 'y' {
return true
}
return false
}
//
// Step 1c
//
// (*v*) Y -> I happy -> happi
// sky -> sky
//
func firstC(word []byte) []byte {
l := len(word)
if bytes.HasSuffix(word, []byte("y")) && containVowel(word[:l-1]) {
word[l-1] = byte('i')
return word
}
return word
}
//
// Step 2
//
// (m>0) ATIONAL -> ATE relational -> relate
// (m>0) TIONAL -> TION conditional -> condition
// rational -> rational
// (m>0) ENCI -> ENCE valenci -> valence
// (m>0) ANCI -> ANCE hesitanci -> hesitance
// (m>0) IZER -> IZE digitizer -> digitize
// (m>0) ABLI -> ABLE conformabli -> conformable
// (m>0) ALLI -> AL radicalli -> radical
// (m>0) ENTLI -> ENT differentli -> different
// (m>0) ELI -> E vileli - > vile
// (m>0) OUSLI -> OUS analogousli -> analogous
// (m>0) IZATION -> IZE vietnamization -> vietnamize
// (m>0) ATION -> ATE predication -> predicate
// (m>0) ATOR -> ATE operator -> operate
// (m>0) ALISM -> AL feudalism -> feudal
// (m>0) IVENESS -> IVE decisiveness -> decisive
// (m>0) FULNESS -> FUL hopefulness -> hopeful
// (m>0) OUSNESS -> OUS callousness -> callous
// (m>0) ALITI -> AL formaliti -> formal
// (m>0) IVITI -> IVE sensitiviti -> sensitive
// (m>0) BILITI -> BLE sensibiliti -> sensible
//
func second(word []byte) []byte {
l := len(word)
if bytes.HasSuffix(word, []byte("tional")) {
if bytes.HasSuffix(word, []byte("ational")) {
if m := measure(word[:l-7]); m > 0 {
return append(bytes.TrimSuffix(word, []byte("ional")), 'e')
}
} else if m := measure(word[:l-2]); m > 0 {
return bytes.TrimSuffix(word, []byte("al"))
}
} else if bytes.HasSuffix(word, []byte("nci")) {
if bytes.HasSuffix(word, []byte("enci")) || bytes.HasSuffix(word, []byte("anci")) {
if m := measure(word[:l-4]); m > 0 {
return append(word[:len(word)-1], 'e')
}
}
} else if bytes.HasSuffix(word, []byte("li")) {
if bytes.HasSuffix(word, []byte("abli")) {
if m := measure(word[:l-4]); m > 0 {
return append(word[:len(word)-1], 'e')
}
} else if bytes.HasSuffix(word, []byte("bli")) {
if m := measure(word[:l-3]); m > 0 {
return append(word[:l-1], 'e')
}
} else if bytes.HasSuffix(word, []byte("alli")) {
if m := measure(word[:l-4]); m > 0 {
return word[:l-2]
}
} else if bytes.HasSuffix(word, []byte("entli")) || bytes.HasSuffix(word, []byte("ousli")) {
if m := measure(word[:l-5]); m > 0 {
return word[:l-2]
}
} else if bytes.HasSuffix(word, []byte("eli")) {
if m := measure(word[:l-3]); m > 0 {
return word[:l-2]
}
}
} else if bytes.HasSuffix(word, []byte("ti")) {
if bytes.HasSuffix(word, []byte("iviti")) {
if m := measure(word[:l-5]); m > 0 {
return append(word[:l-3], 'e')
}
} else if bytes.HasSuffix(word, []byte("aliti")) {
if m := measure(word[:l-5]); m > 0 {
return word[:l-3]
}
} else if bytes.HasSuffix(word, []byte("biliti")) {
if m := measure(word[:l-6]); m > 0 {
return append(word[:l-5], []byte("le")...)
}
}
} else if bytes.HasSuffix(word, []byte("ation")) {
if bytes.HasSuffix(word, []byte("ization")) {
if m := measure(word[:l-7]); m > 0 {
return append(word[:len(word)-5], 'e')
}
} else if m := measure(word[:l-5]); m > 0 {
return append(word[:l-3], 'e')
}
} else if bytes.HasSuffix(word, []byte("ness")) {
if bytes.HasSuffix(word, []byte("iveness")) || bytes.HasSuffix(word, []byte("fulness")) || bytes.HasSuffix(word, []byte("ousness")) {
if m := measure(word[:l-7]); m > 0 {
return word[:l-4]
}
}
} else if bytes.HasSuffix(word, []byte("izer")) {
if m := measure(word[:l-4]); m > 0 {
return word[:l-1]
}
} else if bytes.HasSuffix(word, []byte("alism")) {
if m := measure(word[:l-5]); m > 0 {
return word[:l-3]
}
} else if bytes.HasSuffix(word, []byte("ator")) {
if m := measure(word[:l-4]); m > 0 {
return append(word[:len(word)-2], 'e')
}
} else if bytes.HasSuffix(word, []byte("logi")) {
if m := measure(word[:l-4]); m > 0 {
return append(word[:l-1])
}
}
return word
}
//
// Step 3
//
// (m>0) ICATE -> IC triplicate -> triplic
// (m>0) ATIVE -> formative -> form
// (m>0) ALIZE -> AL formalize -> formal
// (m>0) ICITI -> IC electriciti -> electric
// (m>0) ICAL -> IC electrical -> electric
// (m>0) FUL -> hopeful -> hope
// (m>0) NESS -> goodness -> good
//
func third(word []byte) []byte {
l := len(word)
if bytes.HasSuffix(word, []byte("e")) {
if bytes.HasSuffix(word, []byte("icate")) {
if measure(word[:l-5]) > 0 {
return word[:l-3]
}
} else if bytes.HasSuffix(word, []byte("ative")) {
if measure(word[:l-5]) > 0 {
return word[:l-5]
}
} else if bytes.HasSuffix(word, []byte("alize")) {
if measure(word[:l-5]) > 0 {
return word[:l-3]
}
}
} else if bytes.HasSuffix(word, []byte("iciti")) {
if measure(word[:l-5]) > 0 {
return word[:l-3]
}
} else if bytes.HasSuffix(word, []byte("ical")) {
if measure(word[:l-4]) > 0 {
return word[:l-2]
}
} else if bytes.HasSuffix(word, []byte("ful")) {
if measure(word[:l-3]) > 0 {
return word[:l-3]
}
} else if bytes.HasSuffix(word, []byte("ness")) {
if measure(word[:l-4]) > 0 {
return word[:l-4]
}
}
return word
}
//
// Step 4
//
// (m>1) AL -> revival -> reviv
// (m>1) ANCE -> allowance -> allow
// (m>1) ENCE -> inference -> infer
// (m>1) ER -> airliner -> airlin
// (m>1) IC -> gyroscopic -> gyroscop
// (m>1) ABLE -> adjustable -> adjust
// (m>1) IBLE -> defensible -> defens
// (m>1) ANT -> irritant -> irrit
// (m>1) EMENT -> replacement -> replac
// (m>1) MENT -> adjustment -> adjust
// (m>1) ENT -> dependent -> depend
// (m>1 and (*S or *T)) ION -> adoption -> adopt
// (m>1) OU -> homologou -> homolog
// (m>1) ISM -> communism -> commun
// (m>1) ATE -> activate -> activ
// (m>1) ITI -> angulariti -> angular
// (m>1) OUS -> homologous -> homolog
// (m>1) IVE -> effective -> effect
// (m>1) IZE -> bowdlerize -> bowdler
//
func four(word []byte) []byte {
l := len(word)
if bytes.HasSuffix(word, []byte("al")) || bytes.HasSuffix(word, []byte("er")) || bytes.HasSuffix(word, []byte("ic")) {
if m := measure(word[:l-2]); m > 1 {
return word[:l-2]
}
} else if bytes.HasSuffix(word, []byte("nce")) {
//if word[l-4] == 'a' || word[l-4] == 'e' {
if bytes.HasSuffix(word, []byte("ance")) || bytes.HasSuffix(word, []byte("ence")) {
if m := measure(word[:l-4]); m > 1 {
return word[:l-4]
}
}
} else if bytes.HasSuffix(word, []byte("ble")) {
if word[l-4] == 'a' || word[l-4] == 'i' {
//if bytes.HasSuffix(word, []byte("able")) || bytes.HasSuffix(word, []byte("ible")) {
if m := measure(word[:l-4]); m > 1 {
return word[:l-4]
}
}
} else if bytes.HasSuffix(word, []byte("ent")) {
if bytes.HasSuffix(word, []byte("ement")) {
if m := measure(word[:l-5]); m > 1 {
return word[:l-5]
}
} else if bytes.HasSuffix(word, []byte("ment")) {
if m := measure(word[:l-4]); m > 1 {
return word[:l-4]
}
} else if m := measure(word[:l-3]); m > 1 {
return word[:l-3]
}
} else if bytes.HasSuffix(word, []byte("ant")) {
if m := measure(word[:l-3]); m > 1 {
return word[:l-3]
}
} else if bytes.HasSuffix(word, []byte("e")) {
if bytes.HasSuffix(word, []byte("ate")) || bytes.HasSuffix(word, []byte("ive")) || bytes.HasSuffix(word, []byte("ize")) {
if m := measure(word[:l-3]); m > 1 {
return word[:l-3]
}
}
} else if bytes.HasSuffix(word, []byte("ism")) {
if m := measure(word[:l-3]); m > 1 {
return word[:l-3]
}
} else if bytes.HasSuffix(word, []byte("ous")) || bytes.HasSuffix(word, []byte("iti")) {
if m := measure(word[:l-3]); m > 1 {
return word[:l-3]
}
} else if bytes.HasSuffix(word, []byte("ou")) {
if m := measure(word[:l-2]); m > 1 {
return word[:l-2]
}
} else if bytes.HasSuffix(word, []byte("ion")) {
// *S - the stem ends with S (and similarly for the other letters). (m>1 and (*S or *T)) ION ->
l := len(word)
if measure(word[:l-3]) > 1 {
if l > 4 && (word[l-4] == 's' || word[l-4] == 't') {
return word[:l-3]
}
}
}
return word
}
//
// Step 5a
//
// (m>1) E -> probate -> probat
// rate -> rate
// (m=1 and not *o) E -> cease -> ceas
//
func fiveA(word []byte) []byte {
l := len(word)
if bytes.HasSuffix(word, []byte("e")) {
if m := measure(word[:l-1]); m > 1 {
return word[:l-1]
} else if m := measure(word[:l-1]); m == 1 {
if !isCVCSuffix(word[:l-1]) {
return word[:l-1]
}
}
}
return word
}
//
// Step 5b
//
// (m > 1 and *d and *L) -> single letter
// controll -> control
// roll -> roll
//
func fiveB(word []byte) []byte {
l := len(word)
if measure(word) > 1 && consonant(word, l-1) && consonant(word, l-2) && word[l-1] == word[l-2] && word[l-1] == 'l' {
return word[:l-1]
}
return word
}
func Stem(word []byte) []byte {
word = bytes.TrimSpace(bytes.ToLower(word))
if len(word) < 3 {
return word
}
word = firstA(word)
word = firstB(word)
word = firstC(word)
word = second(word)
word = third(word)
word = four(word)
word = fiveA(word)
word = fiveB(word)
return word
}