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

View File

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