Tohrusky / sensitive-go

基于 Trie 的敏感词检查 🤓

Home Page:https://pkg.go.dev/github.com/Tohrusky/sensitive-go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sensitive-go

Fork from importcjj/sensitive. 敏感词查找,验证,过滤以及替换。内置特色10w+敏感词。CI-Test

安装

go get github.com/Tohrusky/sensitive-go

使用

package sensitive

import (
	"github.com/Tohrusky/sensitive-go/sdict"
	"github.com/Tohrusky/sensitive-go/sensitive"
	"sync"
)

var (
	filter     *sensitive.Filter
	filterBoss *sensitive.Filter
	once       sync.Once
)

// Init 初始化敏感词库,单例模式
func Init() {
	once.Do(func() {
		filter = sensitive.NewWithDefaultSDict()
		filterBoss = sensitive.NewWithBossSDict()
	})
}

// ValidateBoss 重点敏感词校验,校验一个句子是否包含“重点”敏感词,合法返回true,有敏感内容返回false
func ValidateBoss(s string) bool {
	b, _ := filterBoss.Validate(s)
	return b
}

// Replace 和谐敏感词,将敏感词替换为*号
func Replace(s string) string {
	return filter.Replace(s, '*')
}

NewWithDefaultSDict & NewWithBossSDict

初始化一个携带默认敏感词的过滤器 (github.com/Tohrusky/sensitive-go/sdict)。

filter := sensitive.NewWithDefaultSDict()
// filter := sensitive.NewWithBossSDict()

New

初始化一个敏感词过滤器,不携带敏感词,需要手动添加。

filter := sensitive.New()
filter.AddWord(sdict.DefaultSDict...)
filter.AddWord(sdict.BossSDict...)

AddWord

添加敏感词。

filter.AddWord("垃圾")

DelWord

删除敏感词。

filter.DelWord("垃圾")

Filter

过滤敏感词,直接移除词语。

filter.Filter("这篇文章真的好垃圾啊")
// output => 这篇文章真的好啊

Replace

和谐敏感词,把词语中的字符替换成指定的字符。

filter.Replace("这篇文章真的好垃圾", '*')
// output => 这篇文章真的好**

FindIn

查找并返回第一个敏感词,如果没有则返回false

filter.FindIn("这篇文章真的好垃圾")
// output => true, 垃圾

FindAll

查找内容中的全部敏感词,以数组返回。

filter.FindAll("这篇文章真的好垃圾")
// output => [垃圾]

Validate

验证内容是否ok,如果含有敏感词,则返回false和第一个敏感词。

filter.Validate("这篇文章真的好垃圾")
// output => false, 垃圾

UpdateNoisePattern

设置噪音模式,排除噪音字符。

// failed
filter.FindIn("这篇文章真的好垃x圾") // false
filter.UpdateNoisePattern(`x`)
// success
filter.FindIn("这篇文章真的好垃x圾") // true, 垃圾
filter.Validate("这篇文章真的好垃x圾") // False, 垃圾

LoadWordDict

加载本地敏感词字典。

filter.LoadWordDict("./dict.txt")

LoadNetWordDict

加载网络词库。

filter.LoadNetWordDict("https://raw.githubusercontent.com/Tohrusky/sensitive-go/main/dict/dict.txt")

License

sensitive-go is licensed under the MIT License.

About

基于 Trie 的敏感词检查 🤓

https://pkg.go.dev/github.com/Tohrusky/sensitive-go

License:MIT License


Languages

Language:Go 100.0%