-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlp.go
30 lines (25 loc) · 635 Bytes
/
nlp.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
package nlp
//go:generate sh -c "go run gen_stop.go < stop_words.txt > stop_words.go"
//go:generate gofmt -w stop_words.go
import (
"regexp"
"strings"
"github.com/353solutions/nlp/stemmer"
)
var (
// "Who's on first" -> ["Who", "s", "on", "first"]
wordRe = regexp.MustCompile("[[:alpha:]]+")
)
// Tokenize returns list of tokens in text
func Tokenize(text string) []string {
words := wordRe.FindAllString(text, -1)
var tokens []string
for _, w := range words {
token := strings.ToLower(w)
token = stemmer.Stem(token)
if len(token) > 0 && !StopWords[token] {
tokens = append(tokens, token)
}
}
return tokens
}