官方教程
goimport (
"github.com/syyongx/go-wordsfilter"
)
func main() {
texts := []string{
"Miyamoto Musashi",
"妲己",
"アンジェラ",
"ความรุ่งโรจน์",
}
wf := wordsfilter.New()
// Generate
root := wf.Generate(texts)
// Generate with file
// root := wf.GenerateWithFile(path)
// Contains
c1 := wf.Contains("アン", root)
// c1: false
c2 := wf.Contains("アンジェラ", root)
// c2: true
// Remove
wf.Remove("アンジェラ", root)
c3 := wf.Contains("アンジェラ", root)
// c3: false
// Replace
r1 := wf.Replace("Game ความรุ่งโรจน์ i like 妲己 heroMiyamotoMusashi", root)
// r1: Game*************ilike**hero***************
}
快速开始
gopackage wordsfilter
import (
"bufio"
"github.com/syyongx/go-wordsfilter"
"log"
"os"
)
var Wf *wordsfilter.WordsFilter
var samples []string
var root map[string]*wordsfilter.Node
//敏感词过滤
func SetTexts() {
f, err := os.Open("sample.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
s := scanner.Text()
samples = append(samples, s)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
Wf = wordsfilter.New()
root = Wf.Generate(samples)
return
}
func MsgFilter(val string) bool {
return Wf.Contains(val, root)
}
本文作者:yowayimono
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!