This commit is contained in:
xxjwxc
2022-04-01 16:00:35 +08:00
parent f6b3d2a143
commit 886f51993e
3 changed files with 20 additions and 7 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -83,7 +83,7 @@ loop:
words = root words = root
i = e i = e
// Maximum Matching Principle, Matching Backwards First // Maximum Matching Principle, Matching Backwards First
for ; i < l; i ++ { for ; i < l; i++ {
word := string(textr[i]) word := string(textr[i])
if n, ok := words[word]; ok { if n, ok := words[word]; ok {
back = append(back, n) back = append(back, n)
@@ -136,20 +136,31 @@ func (node *Node) contains(text string, root map[string]*Node) bool {
if root == nil || text == "" { if root == nil || text == "" {
return false return false
} }
point := root
index := 0
jump := false
textr := []rune(text) textr := []rune(text)
end := len(textr) - 1 end := len(textr) - 1
for i := 0; i <= end; i++ { for i := 0; i <= end; i++ {
word := string(textr[i]) word := string(textr[i])
if n, ok := root[word]; ok { if n, ok := point[word]; ok {
if i == end { if i == end {
return n.Placeholders != "" return n.Placeholders != ""
} else { } else {
if len(n.Child) == 0 { // last if len(n.Child) == 0 { // last
return true return true
} }
root = n.Child point = n.Child
index = i
jump = true
} }
} else { } else {
if jump {
point = root //重头来
i = index
jump = false
}
continue continue
} }
} }

View File

@@ -6,15 +6,17 @@ import (
func TestWordsFilter(t *testing.T) { func TestWordsFilter(t *testing.T) {
texts := []string{ texts := []string{
"Miyamoto Musashi", "爱女人",
"妲己", "爱液",
"アンジェラ", "你他妈",
"ความรุ่งโรจน์", "ความรุ่งโรจน์",
} }
wf := New() wf := New()
// wf.StripSpace = false
// wf.Placeholder = ""
root := wf.Generate(texts) root := wf.Generate(texts)
wf.Remove("shif", root) wf.Remove("shif", root)
c1 := wf.Contains("アン", root) // 是否有敏感词 c1 := wf.Contains("你爱液", root) // 是否有敏感词
if c1 != false { if c1 != false {
t.Errorf("Test Contains expect false, get %T, %v", c1, c1) t.Errorf("Test Contains expect false, get %T, %v", c1, c1)
} }