-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathflashtext.go
149 lines (135 loc) · 3.15 KB
/
flashtext.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
package flashtext
import (
"strings"
"sync"
)
// keywordProcessor is the processor of keyword extract
type keywordProcessor struct {
// dicts store the keyword => cleanName
dicts map[string]string
// keytrie is the trie struct
keytrie *trie
// caseSensitive or not
caseSensitive bool
// noboundaryWords default to a-zA-Z0-9_
noboundaryWords map[rune]bool
// lock for the map write
sync.RWMutex
}
type ExtractResult struct {
Keyword string
// StartIndex is the keyword index in the sentences
StartIndex int
}
type Option struct {
// Longest set to true will just match the longest keyword,
Longest bool
SpanInfo bool
}
var (
defaultOption = &Option{
Longest: true,
SpanInfo: false,
}
)
func NewKeywordProcessor() *keywordProcessor {
p := &keywordProcessor{
dicts: make(map[string]string),
noboundaryWords: make(map[rune]bool),
keytrie: NewTrie('r'),
}
for i := 0; i < 26; i++ {
p.AddNoBoundaryWords(rune('a' + i))
p.AddNoBoundaryWords(rune('A' + i))
}
for i := 0; i < 10; i++ {
p.AddNoBoundaryWords(rune('0' + i))
}
p.AddNoBoundaryWords('-')
return p
}
func (p *keywordProcessor) SetCaseSenstive(caseSenstive bool) {
p.caseSensitive = caseSenstive
}
func (p *keywordProcessor) AddNoBoundaryWords(noboundaryWords ...rune) {
for _, w := range noboundaryWords {
p.noboundaryWords[w] = true
}
}
func (p *keywordProcessor) AddKeywords(keywords ...string) {
for _, keyword := range keywords {
p.AddKeywordAndName(keyword, keyword)
}
}
func (p *keywordProcessor) AddKeywordAndName(keyword string, cleanName string) {
p.Lock()
defer p.Unlock()
if !p.caseSensitive {
keyword = strings.ToLower(keyword)
}
p.keytrie.addKeyword(keyword)
p.dicts[keyword] = cleanName
}
func (p *keywordProcessor) ExtractKeywords(sentence string, option ...*Option) (res []*ExtractResult) {
extractOption := defaultOption
if len(option) > 0 {
extractOption = option[0]
}
res = make([]*ExtractResult, 0, 20)
if !p.caseSensitive {
sentence = strings.ToLower(sentence)
}
runes := []rune(sentence)
size := len(runes)
idx := 0
begin := true
var curTrie *trie
for idx < size {
curTrie = p.keytrie
c := runes[idx]
if _, ok := p.noboundaryWords[c]; !ok {
idx++
begin = true
} else if !begin {
idx++
} else {
var j = idx
foundKeyword := ""
for j = idx; j < size; j++ {
c = runes[j]
curTrie = curTrie.next[c]
if curTrie == nil {
break
}
if curTrie.word != "" && (j == size-1 || !p.noboundaryWords[runes[j+1]]) {
foundKeyword = curTrie.word
if !extractOption.Longest {
res = append(res, &ExtractResult{p.dicts[foundKeyword], idx})
idx = j
}
}
}
if foundKeyword == "" {
idx++
} else if extractOption.Longest {
res = append(res, &ExtractResult{p.dicts[foundKeyword], idx})
idx = j
}
begin = false
}
}
return res
}
func (p *keywordProcessor) RemoveKeywords(keywords ...string) {
p.Lock()
defer p.Unlock()
for _, keyword := range keywords {
if !p.caseSensitive {
keyword = strings.ToLower(keyword)
}
p.keytrie.removeKeyword(keyword)
}
}
func (p *keywordProcessor) Exists(keyword string) bool {
return p.keytrie.exists(keyword)
}