-
Notifications
You must be signed in to change notification settings - Fork 0
/
tok.go
104 lines (83 loc) · 2.61 KB
/
tok.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
// Tokenization utilities, primarily for use as a tokenizer in a full-text search engine.
package txt
import (
_ "embed"
"strings"
"unicode"
)
//go:embed dicts/colors.txt
var COLORS_DICT_EMBED string
// Loads a replacer file from a given path.
// Files must be in the format:
// original,replaced value
// with each tuple on a new line.
// The values are loaded into memory; the replaced value is assigned the `Data` field on the final
// node of a trie branch, which can be accessed using Trie.NodeAt(x).
func LoadDictionary(data string, caseSensitive bool) *Node {
t := NewTrie()
for _, v := range strings.Split(data, "\n") {
tuple := strings.Split(v, ",")
if len(tuple) < 2 {
break
}
original := tuple[0]
if !caseSensitive {
original = strings.ToLower(original)
}
replaced := tuple[1]
t.Insert(string(original), []byte(replaced))
}
return t
}
var (
// English colors and their hex equivalents.
Colors *Node = LoadDictionary(COLORS_DICT_EMBED, false)
)
// Normalizes a list of tokens.
type Tokenizer func(tokens []string) []string
var (
// Removes stopwords from a token list using the default stopword list.
TokenizerStopwords Tokenizer = func(tokens []string) []string { return FilterStopwords(tokens) }
// Stems tokens using a Porter stemmer.
TokenizerStemmer Tokenizer = func(tokens []string) []string { return StemTokens(tokens) }
// Normalizes color names (e.g. `red`, `navy`) into their hex values.
TokenizerColors Tokenizer = func(tokens []string) []string {
for i, v := range tokens {
if n, exists := Colors.Find(v); exists {
tokens[i] = string(n.Data.([]byte))
}
}
return tokens
}
)
var (
// Splits a string at non-alphanumeric characters (whitespace, punctuation, etc).
SplitNonAlphanumeric Splitter = func(text string) []string {
return strings.FieldsFunc(text, func(r rune) bool {
return !unicode.IsNumber(r) && !unicode.IsLetter(r)
})
}
DefaultSplitter Splitter = SplitNonAlphanumeric
)
// DefaultTokenizer removes stopwords and stems tokens.
var DefaultTokenizer = []Tokenizer{
TokenizerStopwords,
TokenizerStemmer,
}
// Splits a string into individual tokens.
type Splitter func(text string) []string
// Produces a list of normalized text tokens. If no options are provided, the DefaultTokenizer is used.
func Tokenize(text string, splitter Splitter, normalizer []Normalizer, options ...Tokenizer) []string {
if len(text) == 0 {
return make([]string, 0)
}
if len(options) == 0 {
options = append(options, DefaultTokenizer...)
}
text = Normalize(text, normalizer...)
tokens := splitter(text)
for _, v := range options {
tokens = v(tokens)
}
return tokens
}