-
Notifications
You must be signed in to change notification settings - Fork 72
/
nodefilter.go
261 lines (242 loc) · 5.08 KB
/
nodefilter.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package filter
import (
"bufio"
"bytes"
"unicode"
"io"
)
// NewNodeReaderFilter 创建节点过滤器,实现敏感词的过滤
// 从可读流中读取敏感词数据(以指定的分隔符读取数据)
func NewNodeReaderFilter(rd io.Reader, delim byte) DirtyFilter {
nf := &nodeFilter{
root: newNode(),
}
buf := new(bytes.Buffer)
io.Copy(buf, rd)
buf.WriteByte(delim)
for {
line, err := buf.ReadString(delim)
if err != nil {
break
}
if line == "" {
continue
}
nf.addDirtyWords(line)
}
buf.Reset()
return nf
}
// NewNodeChanFilter 创建节点过滤器,实现敏感词的过滤
// 从通道中读取敏感词数据
func NewNodeChanFilter(text <-chan string) DirtyFilter {
nf := &nodeFilter{
root: newNode(),
}
for v := range text {
nf.addDirtyWords(v)
}
return nf
}
// NewNodeFilter 创建节点过滤器,实现敏感词的过滤
// 从切片中读取敏感词数据
func NewNodeFilter(text []string) DirtyFilter {
nf := &nodeFilter{
root: newNode(),
}
for i, l := 0, len(text); i < l; i++ {
nf.addDirtyWords(text[i])
}
return nf
}
func newNode() *node {
return &node{
child: make(map[rune]*node),
}
}
type node struct {
end bool
child map[rune]*node
}
type nodeFilter struct {
root *node
}
func (nf *nodeFilter) addDirtyWords(text string) {
n := nf.root
uchars := []rune(text)
for i, l := 0, len(uchars); i < l; i++ {
if unicode.IsSpace(uchars[i]) {
continue
}
if _, ok := n.child[uchars[i]]; !ok {
n.child[uchars[i]] = newNode()
}
n = n.child[uchars[i]]
}
n.end = true
}
func (nf *nodeFilter) Filter(text string, excludes ...rune) ([]string, error) {
buf := bytes.NewBufferString(text)
defer buf.Reset()
return nf.FilterReader(buf, excludes...)
}
func (nf *nodeFilter) FilterResult(text string, excludes ...rune) (map[string]int, error) {
buf := bytes.NewBufferString(text)
defer buf.Reset()
return nf.FilterReaderResult(buf, excludes...)
}
func (nf *nodeFilter) FilterReader(reader io.Reader, excludes ...rune) ([]string, error) {
data, err := nf.FilterReaderResult(reader, excludes...)
if err != nil {
return nil, err
}
var result []string
for k := range data {
result = append(result, k)
}
return result, nil
}
func (nf *nodeFilter) FilterReaderResult(reader io.Reader, excludes ...rune) (map[string]int, error) {
var (
uchars []rune
)
data := make(map[string]int)
bi := bufio.NewReader(reader)
for {
ur, _, err := bi.ReadRune()
if err != nil {
if err != io.EOF {
return nil, err
}
break
}
if nf.checkExclude(ur, excludes...) {
continue
}
if (unicode.IsSpace(ur) || unicode.IsPunct(ur)) && len(uchars) > 0 {
nf.doFilter(uchars[:], data)
uchars = nil
continue
}
uchars = append(uchars, ur)
}
if len(uchars) > 0 {
nf.doFilter(uchars, data)
}
return data, nil
}
func (nf *nodeFilter) Replace(text string, delim rune, excludes ...rune) (string, error) {
uchars := []rune(text)
idexs := nf.doIndexes(uchars, excludes...)
if len(idexs) == 0 {
return "", nil
}
for i := 0; i < len(idexs); i++ {
uchars[idexs[i]] = rune(delim)
}
return string(uchars), nil
}
func (nf *nodeFilter) checkExclude(u rune, excludes ...rune) bool {
if len(excludes) == 0 {
return false
}
var exist bool
for i, l := 0, len(excludes); i < l; i++ {
if u == excludes[i] {
exist = true
break
}
}
return exist
}
func (nf *nodeFilter) doFilter(uchars []rune, data map[string]int) {
var result []string
ul := len(uchars)
buf := new(bytes.Buffer)
n := nf.root
for i := 0; i < ul; i++ {
if _, ok := n.child[uchars[i]]; !ok {
continue
}
n = n.child[uchars[i]]
buf.WriteRune(uchars[i])
if n.end {
result = append(result, buf.String())
}
for j := i + 1; j < ul; j++ {
if _, ok := n.child[uchars[j]]; !ok {
break
}
n = n.child[uchars[j]]
buf.WriteRune(uchars[j])
if n.end {
result = append(result, buf.String())
}
}
buf.Reset()
n = nf.root
}
for i, l := 0, len(result); i < l; i++ {
var c int
if v, ok := data[result[i]]; ok {
c = v
}
data[result[i]] = c + 1
}
}
func (nf *nodeFilter) doIndexes(uchars []rune, excludes ...rune) (idexs []int) {
var (
tIdexs []int
ul = len(uchars)
n = nf.root
)
for i := 0; i < ul; i++ {
if nf.checkExclude(uchars[i], excludes...) {
continue
}
if _, ok := n.child[uchars[i]]; !ok {
continue
}
n = n.child[uchars[i]]
tIdexs = append(tIdexs, i)
if n.end {
idexs = nf.appendTo(idexs, tIdexs)
tIdexs = nil
}
for j := i + 1; j < ul; j++ {
if nf.checkExclude(uchars[j], excludes...) {
tIdexs = append(tIdexs, j)
} else {
if _, ok := n.child[uchars[j]]; !ok {
break
}
n = n.child[uchars[j]]
tIdexs = append(tIdexs, j)
if n.end {
idexs = nf.appendTo(idexs, tIdexs)
}
}
}
if tIdexs != nil {
tIdexs = nil
}
n = nf.root
}
return
}
func (nf *nodeFilter) appendTo(dst, src []int) []int {
var t []int
for i, il := 0, len(src); i < il; i++ {
var exist bool
for j, jl := 0, len(dst); j < jl; j++ {
if src[i] == dst[j] {
exist = true
break
}
}
if !exist {
t = append(t, src[i])
}
}
return append(dst, t...)
}