Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/wildcard #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dict/dict2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
刘*上*台
3 changes: 1 addition & 2 deletions examples/readme/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package main

import (
"fmt"

"github.com/importcjj/sensitive"
"sensitive"
)

func main() {
Expand Down
3 changes: 1 addition & 2 deletions examples/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package main

import (
"fmt"

"github.com/importcjj/sensitive"
"sensitive"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/test_issue_3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"github.com/importcjj/sensitive"
"sensitive"
)

func keywordFilterSearch(content string) (bool, string) {
Expand Down
10 changes: 6 additions & 4 deletions examples/test_issue_4/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"fmt"

"github.com/importcjj/sensitive"
"sensitive"
)

func main() {
filter := sensitive.New()
filter.LoadWordDict("../../dict/dict.txt")
fmt.Println(filter.Replace("xC4x", '*'))
filter.LoadWordDict("../../dict/dict2.txt")
fmt.Println(filter.ValidateWithWildcard("刘一上三台啊", '*'))
fmt.Println(filter.ValidateWithWildcard("哈哈哈刘一上三", '*'))
fmt.Println(filter.ValidateWithWildcard("哈哈哈刘一上三台", '*'))
fmt.Println(filter.ValidateWithWildcard("哈哈哈刘一上三台,你是个小白鼠", '*'))
}
5 changes: 5 additions & 0 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@ func (filter *Filter) Validate(text string) (bool, string) {
func (filter *Filter) RemoveNoise(text string) string {
return filter.noise.ReplaceAllString(text, "")
}

func (filter *Filter) ValidateWithWildcard(text string, wildcard rune) (bool, string) {
text = filter.RemoveNoise(text)
return filter.trie.ValidateWithWildcard(text, wildcard)
}
52 changes: 52 additions & 0 deletions trie_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,58 @@ func (tree *Trie) Validate(text string) (bool, string) {
return true, Empty
}

func (tree *Trie) ValidateWithWildcard(text string, wildcard rune) (bool, string) {

runes := []rune(text)

for curl := 0; curl < len(runes); curl++ {

patter := ""
parent := tree.Root
if tree.dfs(runes, parent, curl, wildcard, "", &patter) {
return false, patter
}
}
return true, ""
}

func (tree *Trie) dfs(runes []rune, parent *Node, curl int, wildcard rune, str string, patter *string) bool {

if parent == nil {
return false
}
if parent.IsPathEnd() {
*patter = str
return true
}
if curl >= len(runes) {
return false
}

// 匹配到了
if current, found := parent.Children[runes[curl]]; found {
if is1 := tree.dfs(runes, current, curl+1, wildcard, str+string(runes[curl]), patter); is1 {
return true
}
}

// 先看有没有*
if current1, found1 := parent.Children[wildcard]; found1 {

if is2 := tree.dfs(runes, current1, curl+1, wildcard, str+string(wildcard), patter); is2 {
return true
}

if current2, found2 := current1.Children[runes[curl]]; found2 {
if is3 := tree.dfs(runes, current2, curl+1, wildcard, str+string(wildcard)+string(runes[curl]), patter); is3 {
return true
}
}
}
return false

}

// FindIn 判断text中是否含有词库中的词
func (tree *Trie) FindIn(text string) (bool, string) {
validated, first := tree.Validate(text)
Expand Down