Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 1.06 KB

readme.md

File metadata and controls

44 lines (31 loc) · 1.06 KB

SensitiveWords

golang实现DFA算法的敏感词查找、过滤替换、敏感词匹配检测

用法

package main

import (
	"fmt"

	"github.com/feiin/sensitivewords"
)

func main() {
    sensitive := sensitivewords.New()
    /*
     * keywords.txt:
     * 尼玛
     * 哈哈
     */
    sensitive.LoadFromFile("./keywords.txt") 
    
	sensitive.AddWord("测试")
	sensitive.AddWords("+q", "+v")

	s, keyword := sensitive.Find("测试啊+q/+v,尼玛,哈哈")
	fmt.Printf("Find:%v, %v\n", s, keyword) //true,测试
	s, results := sensitive.FindAll("测试啊+q/+v,尼玛,哈哈")
	fmt.Printf("FindAll:%v, %v\n", s, results) //true, [测试 +q +v 尼玛 哈哈哈]
	s, results = sensitive.FindAny("测试啊+q/+v,尼玛,哈哈", 3)
	fmt.Printf("FindAny:%v, %v\n", s, results) //true, [测试 +q +v]

	s = sensitive.Check("测试啊+q/+v,尼玛,哈哈")
	fmt.Printf("Check: %v\n", s) //true

	str := sensitive.Filter("测试啊+q/+v,尼玛,哈哈")
	fmt.Printf("Filter:%v\n", str) //**啊**/**,**,**
}