sensfilter

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2023 License: MIT Imports: 18 Imported by: 1

README

敏感词过滤

简介

敏感词过滤是一个使用 Go 语言编写基于AC自动机算法简单易用的敏感词过滤库。 该库支持敏感词搜索和替换功能,可以用于敏感信息过滤、垃圾邮件过滤等场景。

功能特点

  • 支持中英文敏感词过滤
  • 支持敏感词搜索和替换
  • 支持用户自定义跳过字符列表
  • 支持快速很多快捷方式使用:字符串数组、文件、MySQL、网页.详情使用请看example
  • 支持当成一个单独http服务器启动,是基于gin

快速开始

安装

使用如下命令安装:

shell

go get github.com/king133134/sensfilter
快速开始

以下是一个简单的示例代码:

package main

import (
	"fmt"
	"github.com/king133134/sensfilter"
)

func main() {
	words := []string{"林茹", "林如", "临蓐", "空子", "霸王龙", "我是个SB", "是我", "TMD", "他妈的", "他妈"}
	filter := sensfilter.Strings(words)
	str := []byte("我空ss子sss我是霸**王*龙,我是我我是个(S)(B)真的,TMD,他妈的")
	fmt.Println(filter.Find(str))
	fmt.Println(string(filter.Replace(str, '*')))
	fmt.Println(string(filter.ReplaceRune(str, '*')))
}

输出:

[word:霸王龙 mathced:霸**王*龙 start:20 end:31; word:是我 mathced:是我 start:36 end:41; word:我是个SB mathced:我是个(S)(B start:42 end:55; word:TMD mathced:TMD start:64 end:66; word:他妈的 mathced:他妈的 start:70 end:78;]
我空ss子sss我是************,我********************)真的,***,*********
我空ss子sss我是******,我**********)真的,***,***
带有自定义跳过字符列表

以下是一个简单的示例代码:

package main

import (
	"fmt"
	"github.com/king133134/sensfilter"
)

func main() {
	words := []string{"林茹", "林如", "临蓐", "空子", "霸王龙", "我是个SB", "是我", "TMD", "他妈的", "他妈"}
	filter := sensfilter.Strings(words, "*!")
	str := []byte("我空ss子sss我是霸**王*龙,我是我我是个(S)(B)真的,TMD,他妈的")
	fmt.Println(filter.Find(str))
	fmt.Println(string(filter.Replace(str, '*')))
	fmt.Println(string(filter.ReplaceRune(str, '*')))
}

输出:

[word:霸王龙 mathced:霸**王*龙 start:20 end:31; word:是我 mathced:是我 start:36 end:41; word:TMD mathced:TMD start:64 end:66; word:他妈的 mathced:他妈的 start:70 end:78;]
我空ss子sss我是************,我******我是个(S)(B)真的,***,*********
我空ss子sss我是******,我**我是个(S)(B)真的,***,***

许可证

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMySQLTable

func CreateMySQLTable(conf *DatabaseConf) error

func GetCurrentAbPath

func GetCurrentAbPath() string

GetCurrentAbPath 最终方案-全兼容

func SortedSkipList

func SortedSkipList() string

Types

type DatabaseConf

type DatabaseConf struct {
	DSN       string `json:"dataSourceName" default:"username:password@tcp(127.0.0.1:3306)/db_name?charset=utf8mb4&parseTime=True&loc=Local"`
	TableName string `json:"tableName" default:"sensitive_word"`
}

type DefaultResultWriter

type DefaultResultWriter struct {
	// contains filtered or unexported fields
}

func (*DefaultResultWriter) Len

func (_this *DefaultResultWriter) Len() int

func (*DefaultResultWriter) List

func (_this *DefaultResultWriter) List() []*Result

func (*DefaultResultWriter) MarshalJSON

func (_this *DefaultResultWriter) MarshalJSON() ([]byte, error)

func (*DefaultResultWriter) String

func (_this *DefaultResultWriter) String() string

func (*DefaultResultWriter) Write

func (_this *DefaultResultWriter) Write(res *Result) (stop bool)

type Option

type Option func(options *options)

func SetSkip

func SetSkip(s string) Option

func SetSortedRunesSkip

func SetSortedRunesSkip(s []rune) Option

func SetSortedSkip

func SetSortedSkip(s string) Option

func SetWriter

func SetWriter(w *TrieWriter) Option

type Result

type Result struct {
	Word    string `json:"word"`    // 匹配到的敏感词
	Matched string `json:"matched"` // 匹配到的字符串
	Start   int    `json:"start"`   // 原始字符串中匹配到的起始位置
	End     int    `json:"end"`     // 原始字符串中匹配到的结束位置
}

func (*Result) String

func (_this *Result) String() string

type ResultWriter

type ResultWriter interface {
	// Write 写入结果,stop是否停止写入
	Write(res *Result) (stop bool)
	Len() int
}
type Search struct {
	// contains filtered or unexported fields
}

Search 表示一个 tireRoot 树的搜索器

func File

func File(filename string, skip ...string) (search *Search, err error)

func MySQL

func MySQL(conf *DatabaseConf, skip ...string) (search *Search, err error)

func Network

func Network(pageUrl string, skip ...string) (search *Search, err error)

func NewSearch

func NewSearch(opts ...Option) *Search

func Strings

func Strings(words []string, skip ...string) *Search

Strings 将输入的敏感词列表转换成 tireRoot 树

func (*Search) Find

func (_this *Search) Find(s []byte) []*Result

Find 在 tireRoot 树中搜索敏感词并将结果写入 w

func (*Search) HasSens

func (_this *Search) HasSens(s []byte) (has bool)

HasSens 检查字节数组 s 是否包含敏感词

func (*Search) Replace

func (_this *Search) Replace(s []byte, new byte) []byte

Replace 将字节数组 s 中的所有敏感词替换为 new 并返回替换后的字节数组

func (*Search) ReplaceRune

func (_this *Search) ReplaceRune(s []byte, new rune) []byte

ReplaceRune 将字节数组 s 中的所有敏感词替换为 new 并返回替换后的字节数组

func (*Search) TrieWriter

func (_this *Search) TrieWriter() *TrieWriter

TrieWriter 返回关联的 TrieWriter

type SensitiveWord

type SensitiveWord struct {
	ID        uint32    `gorm:"primaryKey;autoIncrement"`
	Word      string    `gorm:"not null;type:varchar(128);uniqueIndex"`
	CreatedAt time.Time `gorm:"column:created_at;type:TIMESTAMP;default:CURRENT_TIMESTAMP"`
}

type Skip

type Skip struct {
	// contains filtered or unexported fields
}

func (*Skip) Set

func (_this *Skip) Set(s string)

func (*Skip) SetSorted

func (_this *Skip) SetSorted(s string)

func (*Skip) ShouldSkip

func (_this *Skip) ShouldSkip(r rune) bool

func (*Skip) String

func (_this *Skip) String() string

type TrieWriter

type TrieWriter struct {
	// contains filtered or unexported fields
}

TrieWriter 表示一个Trie写入器,包含了一些trie树的相关操作。

func NewTrieWriter

func NewTrieWriter() *TrieWriter

NewTrieWriter 返回一个新的TrieWriter对象,其中tireRoot属性为一个空的trie树根节点。

func (*TrieWriter) Array added in v0.2.6

func (t *TrieWriter) Array() []string

func (*TrieWriter) BuildFail

func (t *TrieWriter) BuildFail() int

BuildFail 用于构建trie树中每个节点的失败指针,返回当前对象。

func (*TrieWriter) Insert

func (t *TrieWriter) Insert(word string) *TrieWriter

Insert 向trie树中插入一个单词,返回当前对象。

func (*TrieWriter) InsertBytes added in v0.2.4

func (t *TrieWriter) InsertBytes(p []byte, delim byte) (n int)

InsertBytes 将一个字节数组写入到trie树中,返回写入的字节数和nil错误。在遍历字节数组的过程中,跳过被定义在skip属性中的字符,如果遇到换行符则在该单词的结尾节点标记为end

func (*TrieWriter) InsertFile added in v0.2.4

func (t *TrieWriter) InsertFile(filename string)

func (*TrieWriter) InsertReader added in v0.2.9

func (t *TrieWriter) InsertReader(reader io.Reader, delim byte) (n int, err error)

func (*TrieWriter) InsertScanner added in v0.2.5

func (t *TrieWriter) InsertScanner(scanner *bufio.Scanner)

func (*TrieWriter) InsertWords

func (t *TrieWriter) InsertWords(words []string) *TrieWriter

InsertWords 向trie树中插入一个字符串数组中的所有单词,返回当前对象。调用了Insert(word string)方法。

func (*TrieWriter) Size

func (t *TrieWriter) Size() int

Size 获取trie树中单词的数量。

func (*TrieWriter) Skip

func (t *TrieWriter) Skip() *Skip

Skip 获取需要跳过的字符集合。

func (*TrieWriter) String

func (t *TrieWriter) String() string

func (*TrieWriter) WriteString

func (t *TrieWriter) WriteString(s string) (n int, err error)

WriteString 将一个字符串写入到trie树中,返回写入的字节数和nil错误。调用了Insert(word string)方法。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL