-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstem.go
591 lines (511 loc) · 13.2 KB
/
stem.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package tashaphyne
import (
"cmp"
"regexp"
"slices"
"strings"
)
type Stemmer struct {
prefixLetters string
suffixLetters string
infixLetters string
maxPrefixLength int
maxSuffixLength int
minStemLength int
joker string
prefixList []string
suffixList []string
rootList []string
validAffixesList []string
word Runes
starWord Runes
unvocalized Runes
normalized Runes
root Runes
left int
right int
stemLeft int
stemRight int
segmentList [][2]int
prefixesTree map[string]any
suffixesTree map[string]any
tokenRegex *regexp.Regexp
notPrefixRegex *regexp.Regexp
notInfixRegex *regexp.Regexp
notSuffixRegex *regexp.Regexp
notPrefixSuffixRegex *regexp.Regexp
}
func New() *Stemmer {
s := &Stemmer{
prefixLetters: DefaultPrefixLetters,
suffixLetters: DefaultSuffixLetters,
infixLetters: DefaultInfixLetters,
maxPrefixLength: DefaultMaxPrefix,
maxSuffixLength: DefaultMaxSuffix,
minStemLength: DefaultMinStem,
joker: DefaultJoker,
prefixList: DefaultPrefixList,
suffixList: DefaultSuffixList,
rootList: Roots,
validAffixesList: AffixList,
tokenRegex: re("[^\\w\u064b-\u0652']+"),
}
s.notPrefixRegex = re("[^%s]", s.prefixLetters)
s.notInfixRegex = re("[^%s]", s.infixLetters)
s.notSuffixRegex = re("[^%s]", s.suffixLetters)
s.notPrefixSuffixRegex = re("[^%s]", s.prefixLetters+s.suffixLetters)
s.prefixesTree = s.createPrefixesTree(s.prefixList)
s.suffixesTree = s.createSuffixesTree(s.suffixList)
return s
}
func (s *Stemmer) LightStem(word string) string {
if word == "" {
return ""
}
s.transformToStars(Runes(word))
s.segment(Runes(word))
return string(s.getStem(-1, -1))
}
func (s *Stemmer) createPrefixesTree(prefixes []string) map[string]any {
prefixesTree := make(map[string]any)
for _, prefix := range prefixes {
branch := prefixesTree
for _, ch := range Runes(prefix) {
char := string(ch)
if _, exists := branch[char]; !exists {
branch[char] = make(map[string]any)
}
branch = branch[char].(map[string]any)
}
if endMarker, exists := branch["#"]; exists {
endMarker.(map[string]any)[prefix] = "#"
} else {
branch["#"] = map[string]any{prefix: "#"}
}
}
return prefixesTree
}
func (s *Stemmer) createSuffixesTree(suffixes []string) map[string]any {
suffixesTree := make(map[string]any)
for _, suffix := range suffixes {
branch := suffixesTree
for _, ch := range Runes(suffix) {
char := string(ch)
if _, exists := branch[char]; !exists {
branch[char] = make(map[string]any)
}
branch = branch[char].(map[string]any)
}
if endMarker, exists := branch["#"]; exists {
endMarker.(map[string]any)[suffix] = "#"
} else {
branch["#"] = map[string]any{suffix: "#"}
}
}
return suffixesTree
}
func (s *Stemmer) getRoot(preIdx, suIdx int) Runes {
if preIdx >= 0 || suIdx >= 0 {
s.extractRoot(preIdx, suIdx)
} else {
s.root = s.chooseRoot()
}
return s.root
}
func (s *Stemmer) chooseRoot() Runes {
if IsStop(string(s.word)) {
return Runes(StopStem(string(s.word)))
}
if len(s.segmentList) == 0 {
s.segment(s.word)
}
affixList := s.getAffixList(nil)
var roots []string
for i := range affixList {
roots = append(roots, string(affixList[i].root))
}
rootsTmp := roots
var accepted []string
for i := range rootsTmp {
if s.isRootLengthValid(Runes(rootsTmp[i])) {
accepted = append(accepted, rootsTmp[i])
}
}
if len(accepted) > 0 {
rootsTmp = accepted
}
accepted = nil
for i := range rootsTmp {
if s.isRoot(Runes(rootsTmp[i])) {
accepted = append(accepted, rootsTmp[i])
}
}
if len(accepted) > 0 {
rootsTmp = accepted
}
return s.mostCommon(rootsTmp)
}
func (s *Stemmer) chooseStem() Runes {
if IsStop(string(s.word)) {
return Runes(StopStem(string(s.word)))
}
if len(s.segmentList) == 0 {
s.segment(s.word)
}
var segList [][2]int
for i := range s.segmentList {
if s.verifyAffix(s.segmentList[i][0], s.segmentList[i][1]) {
segList = append(segList, s.segmentList[i])
}
}
if len(segList) == 0 {
return s.unvocalized
}
left, right := s.getLeftRight(segList)
return s.unvocalized.Slice(left, right)
}
func (s *Stemmer) getStem(preIdx, suIdx int) Runes {
var left, right int
if preIdx >= 0 || suIdx >= 0 {
if preIdx > 0 {
left = preIdx
} else {
left = s.stemLeft
}
if suIdx > 0 {
right = suIdx
} else {
right = s.stemRight
}
return s.unvocalized.Slice(left, right)
}
return s.chooseStem()
}
func (s *Stemmer) handleTehInflix(word Runes, left, right int) Runes {
if keyStem := word.Replace(TehMarbuta, ""); len(keyStem) != 4 {
return word.ReplaceRegex(tehTahDalRegex, s.joker)
}
word = append(word[:2], word[2:].ReplaceRegex(tehRegex, s.joker)...)
if s.word.Slice(left, right).HasPrefix("ضط") {
word = append(word[:2], word[2:].ReplaceRegex(tahRegex, s.joker)...)
} else {
word = word.ReplaceRegex(tahRegex, s.joker)
}
if s.word[left:right].HasPrefix("زد") {
word = append(word[:2], word[2:].ReplaceRegex(dalRegex, s.joker)...)
} else {
word = word.ReplaceRegex(dalRegex, s.joker)
}
return word
}
func (s *Stemmer) getStarStem(preIdx, suIdx int) Runes {
word := s.word
if preIdx < 0 && suIdx < 0 {
return word.Slice(s.left, s.right)
}
left, right := s.left, s.right
if preIdx >= 0 {
left = preIdx
}
if suIdx >= 0 {
right = suIdx
}
if s.infixLetters != "" {
return s.handleTehInflix(word.Slice(left, right).ReplaceRegex(re("[^%s]", s.infixLetters+TehMarbuta), s.joker), left, right)
}
return Runes(strings.Repeat(s.joker, len(word.Slice(left, right))))
}
func (s *Stemmer) getPrefix(preIdx int) Runes {
if preIdx >= 0 {
return s.unvocalized.SliceTo(preIdx)
}
return s.unvocalized.SliceTo(s.left)
}
func (s *Stemmer) getSuffix(suIdx int) Runes {
if suIdx >= 0 {
return s.unvocalized.SliceFrom(suIdx)
}
return s.unvocalized.SliceFrom(s.right)
}
func (s *Stemmer) getAffix(preIdx, suIdx int) Runes {
return Runes(string(s.getPrefix(preIdx)) + "-" + string(s.getSuffix(suIdx)))
}
type affixTuple struct {
prefix Runes
suffix Runes
stem Runes
starStem Runes
root Runes
}
func (s *Stemmer) getAffixTuple(preIdx, suIdx int) affixTuple {
return affixTuple{
prefix: s.getPrefix(preIdx),
suffix: s.getSuffix(suIdx),
stem: s.getStem(preIdx, suIdx),
starStem: s.getStarStem(preIdx, suIdx),
root: s.getRoot(preIdx, suIdx),
}
}
func (s *Stemmer) transformToStars(word Runes) (Runes, int, int) {
s.word = word
word = word.StripTashkeel()
s.unvocalized = word
word = word.ReplaceRegex(alefMaddaRegex, Hamza+Alef)
word = word.ReplaceRegex(s.notPrefixSuffixRegex, s.joker)
left := word.IndexOf(s.joker)
right := word.LastIndexOf(s.joker)
if left >= 0 {
left = Min(left, s.maxPrefixLength-1)
right = max(right+1, len(word)-s.maxSuffixLength)
prefix := word.SliceTo(left)
stem := s.word.Slice(left, right)
suffix := word.SliceFrom(right)
if s.infixLetters != "" {
stem = stem.ReplaceRegex(s.notInfixRegex, s.joker)
}
word = Runes(string(prefix) + string(stem) + string(suffix))
}
left = word.IndexOf(s.joker)
right = word.LastIndexOf(s.joker)
if left < 0 {
left = Min(s.maxPrefixLength, len(word)-2)
}
if left >= 0 {
prefix := word.SliceTo(left)
for len(prefix) > 0 && !prefix.In(s.prefixList...) {
prefix = prefix.SliceTo(-1)
}
if right < 0 {
right = max(len(prefix), len(word)-s.maxSuffixLength)
}
suffix := word.SliceFrom(right)
for len(suffix) > 0 && !suffix.In(s.suffixList...) {
suffix = suffix.SliceFrom(1)
}
left = len(prefix)
right = len(word) - len(suffix)
stem := s.word.Slice(left, right)
if s.infixLetters != "" {
stem = stem.ReplaceRegex(s.notInfixRegex, s.joker)
}
word = Runes(string(prefix) + string(stem) + string(suffix))
}
s.stemLeft = left
s.stemRight = right
s.starWord = word
return word, left, right
}
func (s *Stemmer) extractRoot(preIdx, suIdx int) Runes {
stem := s.getStem(preIdx, suIdx)
root := Runes("")
if len(stem) == 3 {
s.root = s.adjustRoot(root, stem)
return s.root
}
starStem := s.getStarStem(preIdx, suIdx)
if len(starStem) == len(stem) {
for i, ch := range stem {
if string(starStem[i]) == s.joker {
root = append(root, ch)
}
}
} else {
root = stem
}
root = s.normalizeRoot(root)
if len(root) == 2 {
root = s.adjustRoot(root, starStem)
}
s.root = root
return root
}
func (s *Stemmer) adjustRoot(root, starStem Runes) Runes {
if len(starStem) == 0 {
return root
}
if len(starStem) == 3 {
return starStem.Replace(Alef, Waw).Replace(AlefMaksura, Yeh)
}
first := starStem.At(0)
last := starStem.At(-1)
if first == Alef || first == Waw {
root = root.Prepend(Waw)
} else if first == Yeh {
root = root.Prepend(Yeh)
} else if first == s.joker && In(last, Alef, Waw) {
root = root.Append(Waw)
} else if first == s.joker && In(last, AlefMaksura, Yeh) {
root = root.Append(Waw)
} else if first == s.joker && last == s.joker {
if len(starStem) == 2 {
root = root.Append(root.At(-1))
} else {
root = Runes(root.At(0) + Waw + root.At(1))
}
}
return root
}
func (s *Stemmer) lookupPrefixes(word Runes) []int {
branch := s.prefixesTree
lefts := []int{0}
i := 0
for ; i < len(word); i++ {
ch := word.At(i)
if _, ok := branch[ch]; !ok {
break
}
if _, hashOk := branch["#"]; hashOk {
lefts = append(lefts, i)
}
branch = branch[ch].(map[string]any)
}
if _, hashExist := branch["#"]; hashExist && i < len(word) {
lefts = append(lefts, i)
}
return lefts
}
func (s *Stemmer) lookupSuffixes(word Runes) []int {
branch := s.suffixesTree
suffix := Runes("")
rights := []int{}
i := len(word) - 1
for ; i >= 0; i-- {
ch := word.At(i)
if _, ok := branch[ch]; !ok {
break
}
suffix = suffix.Prepend(ch)
if _, hashOk := branch["#"]; hashOk {
rights = append(rights, i+1)
}
branch = branch[ch].(map[string]any)
}
if _, hashExist := branch["#"]; hashExist && i >= 0 {
rights = append(rights, i+1)
}
return rights
}
func (s *Stemmer) segment(word Runes) [][2]int {
s.word = word
s.unvocalized = word.StripTashkeel()
word = word.ReplaceRegex(alefMaddaRegex, Hamza+Alef)
lefts := s.lookupPrefixes(word)
rights := s.lookupSuffixes(word)
if len(lefts) > 0 {
s.left = Max(lefts...)
} else {
s.left = -1
}
if len(rights) > 0 {
s.right = Min(rights...)
} else {
s.right = -1
}
s.segmentList = [][2]int{{0, len(word)}}
for _, i := range lefts {
for _, j := range rights {
if j >= i+2 {
s.segmentList = append(s.segmentList, [2]int{i, j})
}
}
}
s.left, s.right = s.getLeftRight(s.segmentList)
return s.segmentList
}
func (s *Stemmer) getAffixList(segList [][2]int) []affixTuple {
if len(segList) == 0 {
segList = s.segmentList
}
var affixList []affixTuple
for i := range segList {
affixList = append(affixList, s.getAffixTuple(segList[i][0], segList[i][1]))
}
return affixList
}
func (s *Stemmer) validStem(stem Runes, tag string, prefix Runes) bool {
if len(stem) == 0 {
return false
}
if tag == Noun {
return len(stem) < 8
}
// verb
if len(stem) > 6 || len(stem) < 2 {
return false
} else if stem.Contains(TehMarbuta) {
return false
} else if len(stem) == 6 && !stem.HasPrefix(Alef) {
return false
} else if len(stem) == 5 && !(stem.At(0) == Alef || stem.At(0) == Teh) {
if prefix.SliceFrom(-1).In(Yeh, Teh, Noon, AlefHamzaAbove) {
return false
}
} else if stem.HasPrefix(Alef) && prefix.SliceFrom(-1).In(Yeh, Noon, Teh, AlefHamzaAbove, Alef) {
return false
}
if !IsVerbStamp(string(stem)) {
return false
}
return true
}
func (s *Stemmer) verifyAffix(preIdx, suIdx int) bool {
prefix := s.getPrefix(preIdx)
suffix := s.getSuffix(suIdx)
affix := string(prefix) + "-" + string(suffix)
stem := s.getStem(preIdx, suIdx)
if slices.Contains(VerbAffixList, affix) && s.validStem(stem, Verb, prefix) {
if slices.Contains(NounAffixList, affix) && s.validStem(stem, Noun, Runes("")) {
return true
}
return true
}
if slices.Contains(NounAffixList, affix) && s.validStem(stem, Noun, Runes("")) {
return true
}
return false
}
func (s *Stemmer) normalizeRoot(word Runes) Runes {
word = word.Replace(AlefMadda, Hamza+Alef)
word = word.Replace(TehMarbuta, "")
word = word.Replace(AlefMaksura, Yeh)
return Runes(NormalizeHamza(string(word)))
}
func (s *Stemmer) isRootLengthValid(root Runes) bool {
return len(root) >= 2 && len(root) <= 4
}
func (s *Stemmer) mostCommon(lst []string) Runes {
var triroots []string
for i := range lst {
if len(lst[i]) == 3 {
triroots = append(triroots, lst[i])
}
}
if len(triroots) > 0 {
lst = triroots
}
return Runes(slices.MaxFunc(lst, func(a, b string) int { return cmp.Compare(len(a), len(b)) }))
}
func (s *Stemmer) isRoot(word Runes) bool {
return slices.Contains(s.rootList, string(word))
}
func (s *Stemmer) getLeftRight(segList [][2]int) (int, int) {
if len(segList) == 0 {
return -1, -1
}
var l, r int
for i := range segList {
if i == 0 {
l = segList[i][0]
} else if segList[i][0] > l {
l = segList[i][0]
}
}
for i := range segList {
if i == 0 {
r = segList[i][1]
} else if segList[i][0] == l && segList[i][1] < r {
r = segList[i][1]
}
}
return l, r
}