-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringSearch.go
221 lines (204 loc) · 4.3 KB
/
StringSearch.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
package ToolGood
import "github.com/CloudStriver/ToolGood/tool"
type StringSearch struct {
_first []*tool.TrieNode2
_keywords []string
}
func NewStringSearch() *StringSearch {
return &StringSearch{
_first: make([]*tool.TrieNode2, 0),
}
}
func (this *StringSearch) SetKeywords(keywords []string) {
this._keywords = keywords
root := tool.NewTrieNode()
allNodeLayers := make(map[int]*tool.TrieNodes, 0)
for r, p := range keywords {
length := len(p)
if length > 0 {
var nd *tool.TrieNode
nd = root
for i, ch := range p {
nd = nd.Add(int(ch))
if nd.Layer == 0 {
nd.Layer = i + 1
if trieNodes, ok := allNodeLayers[nd.Layer]; ok {
trieNodes.Items = append(trieNodes.Items, nd)
} else {
trieNodes = tool.NewTrieNodes()
allNodeLayers[nd.Layer] = trieNodes
trieNodes.Items = append(trieNodes.Items, nd)
}
}
}
nd.SetResults(r)
}
}
allNode := make([]*tool.TrieNode, 1)
allNode[0] = root
for i := 0; i < len(allNodeLayers); i++ {
nodes := allNodeLayers[i+1].Items
for j := 0; j < len(nodes); j++ {
allNode = append(allNode, nodes[j])
}
}
for i := 1; i < len(allNode); i++ {
var nd *tool.TrieNode
var r *tool.TrieNode
nd = allNode[i]
nd.Index = i
r = nd.Parent.Failure
c := nd.Char
for {
if r != nil {
if _, ok := r.M_values[c]; ok {
break
} else {
r = r.Failure
}
} else {
break
}
}
if r == nil {
nd.Failure = root
} else {
nd.Failure = r.M_values[c]
for j := 0; j < len(nd.Failure.Results); j++ {
nd.SetResults(nd.Failure.Results[j])
}
}
}
root.Failure = root
allNode2 := make([]*tool.TrieNode2, len(allNode))
for i := 0; i < len(allNode); i++ {
allNode2[i] = tool.NewTrieNode2()
}
for i := 0; i < len(allNode); i++ {
oldNode := allNode[i]
newNode := allNode2[i]
for key, val := range oldNode.M_values {
var index = val.Index
newNode.Add(int32(key), allNode2[index])
}
for j := 0; j < len(oldNode.Results); j++ {
newNode.SetResults(oldNode.Results[j])
}
oldNode = oldNode.Failure
for oldNode != root {
for key, val := range oldNode.M_values {
if newNode.HasKey(int32(key)) == false {
var index = val.Index
newNode.Add(int32(key), allNode2[index])
}
}
for j := 0; j < len(oldNode.Results); j++ {
newNode.SetResults(oldNode.Failure.Results[j])
}
oldNode = oldNode.Failure
}
}
first := make([]*tool.TrieNode2, 0x10000)
for key, val := range allNode2[0].M_values {
first[key] = val
}
this._first = first
}
func (this *StringSearch) FindFirst(text string) string {
var ptr *tool.TrieNode2
for _, t := range text {
var tn *tool.TrieNode2
if ptr == nil {
tn = this._first[t]
} else {
var b bool
b, tn = ptr.TryGetValue(t)
if b == false {
tn = this._first[t]
}
}
if tn != nil {
if tn.End == true {
return this._keywords[tn.Results[0]]
}
}
ptr = tn
}
return ""
}
func (this *StringSearch) FindAll(text string) []string {
list := make([]string, 0)
var ptr *tool.TrieNode2
for _, t := range text {
var tn *tool.TrieNode2
if ptr == nil {
tn = this._first[t]
} else {
var b bool
b, tn = ptr.TryGetValue(t)
if b == false {
tn = this._first[t]
}
}
if tn != nil {
if tn.End == true {
for _, item := range tn.Results {
list = append(list, this._keywords[item])
}
}
}
ptr = tn
}
return list
}
func (this *StringSearch) ContainsAny(text string) bool {
var ptr *tool.TrieNode2
for _, t := range text {
var tn *tool.TrieNode2
if ptr == nil {
tn = this._first[t]
} else {
var b bool
b, tn = ptr.TryGetValue(t)
if b == false {
tn = this._first[t]
}
}
if tn != nil {
if tn.End == true {
return true
}
}
ptr = tn
}
return false
}
func (this *StringSearch) Replace(text string, replaceChar int32) string {
result := []rune(text)
var ptr *tool.TrieNode2
var i int
for _, t := range text {
var tn *tool.TrieNode2
if ptr == nil {
tn = this._first[t]
} else {
var b bool
b, tn = ptr.TryGetValue(t)
if b == false {
tn = this._first[t]
}
}
if tn != nil {
if tn.End == true {
maxLength := len([]rune(this._keywords[tn.Results[0]]))
start := i + 1 - maxLength
for j := start; j <= i; j++ {
result[j] = replaceChar
}
}
}
ptr = tn
i++
}
return string(result)
}