filter

package
v3.7.17 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SortedSkipList

func SortedSkipList() string

Types

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 struct {
	// contains filtered or unexported fields
}

func (*ResultWriter) Len

func (_this *ResultWriter) Len() int

func (*ResultWriter) Write

func (_this *ResultWriter) Write(_ *Result) (stop bool)
type Search struct {
	// contains filtered or unexported fields
}

Search 表示一个 tireRoot 树的搜索器

func File

func File(filename 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 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

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

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

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

func (*TrieWriter) InsertFile

func (t *TrieWriter) InsertFile(filename string)

func (*TrieWriter) InsertReader

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

func (*TrieWriter) InsertScanner

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)方法。

Jump to

Keyboard shortcuts

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