diff --git a/README.md b/README.md index e88cc7c..fc7767e 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![GoDoc](https://godoc.org/github.com/antlinker/go-dirtyfilter?status.svg)](https://godoc.org/github.com/antlinker/go-dirtyfilter) [![wercker status](https://app.wercker.com/status/02cb69dcbba94ee4274f1c0c06ce68f9/s "wercker status")](https://app.wercker.com/project/bykey/02cb69dcbba94ee4274f1c0c06ce68f9) +> 基于DFA算法; > 支持动态修改敏感词,同时支持特殊字符的筛选; > 敏感词的存储支持内存存储及MongoDB存储。 @@ -25,7 +26,7 @@ import ( ) var ( - filterText = `我是需要过滤的内容,内容为:**文**件,需要过滤。。。` + filterText = `我是需要过滤的内容,内容为:**文@@件,需要过滤。。。` ) func main() { @@ -36,7 +37,7 @@ func main() { panic(err) } filterManage := filter.NewDirtyManager(memStore) - result, err := filterManage.Filter().Filter(&filterText, '*') + result, err := filterManage.Filter().Filter(filterText, '*', '@') if err != nil { panic(err) } diff --git a/filter.go b/filter.go index b1a74cc..8fe2ac1 100644 --- a/filter.go +++ b/filter.go @@ -10,13 +10,13 @@ type DirtyFilter interface { // excludes 表示排除指定的字符 // 返回文本中出现的敏感词,如果敏感词不存在则返回nil // 如果出现异常,则返回error - Filter(text *string, excludes ...rune) ([]string, error) + Filter(text string, excludes ...rune) ([]string, error) // FilterResult 文本过滤函数 // excludes 表示排除指定的字符 // 返回文本中出现的敏感词及出现次数,如果敏感词不存在则返回nil // 如果出现异常,则返回error - FilterResult(text *string, excludes ...rune) (map[string]int, error) + FilterResult(text string, excludes ...rune) (map[string]int, error) // FilterReader 从可读流中过滤敏感词 // excludes 表示排除指定的字符 diff --git a/nodefilter.go b/nodefilter.go index 464bda9..0f7b1cb 100644 --- a/nodefilter.go +++ b/nodefilter.go @@ -85,14 +85,14 @@ func (nf *nodeFilter) addDirtyWords(text string) { n.end = true } -func (nf *nodeFilter) Filter(text *string, excludes ...rune) ([]string, error) { - buf := bytes.NewBufferString(*text) +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) +func (nf *nodeFilter) FilterResult(text string, excludes ...rune) (map[string]int, error) { + buf := bytes.NewBufferString(text) defer buf.Reset() return nf.FilterReaderResult(buf, excludes...) } diff --git a/sample/memory/main.go b/sample/memory/main.go index 67832f5..dddb36a 100644 --- a/sample/memory/main.go +++ b/sample/memory/main.go @@ -8,7 +8,7 @@ import ( ) var ( - filterText = `我是需要过滤的内容,内容为:**文**件,需要过滤。。。` + filterText = `我是需要过滤的内容,内容为:**文@@件,需要过滤。。。` ) func main() { @@ -19,7 +19,7 @@ func main() { panic(err) } filterManage := filter.NewDirtyManager(memStore) - result, err := filterManage.Filter().Filter(&filterText, '*') + result, err := filterManage.Filter().Filter(filterText, '*', '@') if err != nil { panic(err) }