Skip to content

Commit

Permalink
Modify filter text,remove pointer.
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed May 14, 2016
1 parent 786627d commit 217389a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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存储。
Expand All @@ -25,7 +26,7 @@ import (
)

var (
filterText = `我是需要过滤的内容,内容为:**文**件,需要过滤。。。`
filterText = `我是需要过滤的内容,内容为:**文@@件,需要过滤。。。`
)

func main() {
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 表示排除指定的字符
Expand Down
8 changes: 4 additions & 4 deletions nodefilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down
4 changes: 2 additions & 2 deletions sample/memory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var (
filterText = `我是需要过滤的内容,内容为:**文**件,需要过滤。。。`
filterText = `我是需要过滤的内容,内容为:**文@@件,需要过滤。。。`
)

func main() {
Expand All @@ -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)
}
Expand Down

0 comments on commit 217389a

Please sign in to comment.