forked from sitanshunanda/shield
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ru_tokenizer.go
35 lines (29 loc) · 852 Bytes
/
ru_tokenizer.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
package shield
import (
"regexp"
"strings"
"strconv"
"github.com/legion-zver/shield/porterstemmers"
)
type ruTokenizer struct {
}
// NewRussianTokenizer - new Russian tokenizer
func NewRussianTokenizer() Tokenizer {
return &ruTokenizer{}
}
func (t *ruTokenizer) Tokenize(text string) (words map[string]int64) {
words = make(map[string]int64)
rusPS := porterstemmers.RussianPorterStemmer{}
for _, w := range strings.Split(replaceWSpacesRx.ReplaceAllString(text, " ")," ") {
if len(w) > 2 {
if _, err := strconv.Atoi(w); err != nil {
stem := rusPS.StemString(w)
if len(stem) > 2 {
words[stem]++
}
}
}
}
return
}
var replaceWSpacesRx = regexp.MustCompile(`[\s|\n]+`)