forked from importcjj/sensitive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.go
63 lines (53 loc) · 1.32 KB
/
filter.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
package sensitive
import (
"io/ioutil"
"regexp"
"strings"
)
// Filter 敏感词过滤器
type Filter struct {
trie *Trie
noise *regexp.Regexp
}
// New 返回一个敏感词过滤器
func New() *Filter {
return &Filter{
trie: NewTrie(),
noise: regexp.MustCompile(`[\s&%$@*]+`),
}
}
// UpdateNoisePattern 更新去噪模式
func (filter *Filter) UpdateNoisePattern(pattern string) {
filter.noise = regexp.MustCompile(pattern)
}
// LoadWordDict 加载敏感词字典
func (filter *Filter) LoadWordDict(path string) error {
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
words := strings.Split(string(content), "\n")
filter.trie.Add(words...)
return nil
}
// AddWord 添加敏感词
func (filter *Filter) AddWord(words ...string) {
filter.trie.Add(words...)
}
// Filter 过滤敏感词
func (filter *Filter) Filter(text string) string {
return filter.trie.Filter(text)
}
// Replace 和谐敏感词
func (filter *Filter) Replace(text string, repl rune) string {
return filter.trie.Replace(text, repl)
}
// FindIn 检测敏感词
func (filter *Filter) FindIn(text string) (bool, string) {
text = filter.RemoveNoise(text)
return filter.trie.FindIn(text)
}
// RemoveNoise 去除空格等噪音
func (filter *Filter) RemoveNoise(text string) string {
return filter.noise.ReplaceAllString(text, "")
}