sensitive_words_match

package module
v0.0.0-...-60fa1f1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2021 License: MIT Imports: 3 Imported by: 0

README

sensitive-words-match

支持三种匹配方式

  1. 敏感词 使用DFA匹配算法;不支持标点符号;
  2. 组合词的方式 加你|微信 表示同时包含 加你微信 才会命中; 象牙#猛犸象牙 包含象牙,同时不包含猛犸象牙时才会命中;其中#后面的一个词必须包含前面一个词;
  3. 正则方式 以reg@打头,后面跟正则字符串;
Installation
go get -u https://github.com/dongweifly/sensitive-words-match
Usage
import (
	"fmt"
	match "github.com/dongweifly/sensitive-words-match"
)

func main() {
	service := match.NewMatchService()
	service.Build([]string{
		"fuck",
		"加你|微",
		"象牙#猛犸象牙#非象牙#象牙不准卖",
		`reg@(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}`,
	})

	fmt.Println(service.Match("阿斯蒂芬fuck", '*'))
	fmt.Println(service.Match("Hello,微信,加你吗", '*'))
	fmt.Println(service.Match("Hello, 加你吗, 微信", '*'))
	fmt.Println(service.Match("买象牙吗", '*'))
	fmt.Println(service.Match("猛犸象牙筷子", '*'))
	fmt.Println(service.Match("电话号码:15210753706@@@@@", '*'))
}

一个简单的,开箱即用的API服务

RoadMap

Contact

Documentation

Index

Constants

View Source
const (
	NON = iota
	AND
)
View Source
const (
	DFA      = 0
	ASSEMBLE = 1
	REGEXP   = 2
)

Variables

This section is empty.

Functions

func NewRegRule

func NewRegRule(str string) (*regRule, error)

func TrimString

func TrimString(s string) string

TrimString returns s without leading and trailing ASCII space.

Types

type AssembleMather

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

func NewAssembleMather

func NewAssembleMather() *AssembleMather

func (*AssembleMather) Build

func (a *AssembleMather) Build(words []string)

func (*AssembleMather) Match

func (a *AssembleMather) Match(text string, repl rune) (word []string, desensitization string)

type DFAMatcher

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

func NewDFAMather

func NewDFAMather() *DFAMatcher

func (*DFAMatcher) Build

func (d *DFAMatcher) Build(words []string)

Build 构造DFA树

func (*DFAMatcher) Match

func (d *DFAMatcher) Match(text string, repl rune) (sensitiveWords []string, replaceText string)

Match 查找替换发现的敏感词

type DFANode

type DFANode struct {
	End  bool
	Next map[rune]*DFANode
}

func (*DFANode) AddChild

func (n *DFANode) AddChild(c rune) *DFANode

func (*DFANode) AddWord

func (n *DFANode) AddWord(word string)

func (*DFANode) FindChild

func (n *DFANode) FindChild(c rune) *DFANode

type MatchService

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

func NewMatchService

func NewMatchService() *MatchService

func (*MatchService) Build

func (m *MatchService) Build(words []string)

Build 同一个配置中支持三种配置

func (*MatchService) Match

func (m *MatchService) Match(text string, repl rune) (sensitiveWords []string, replaceText string)

Match 只要有一个规则就返回;

type Matcher

type Matcher interface {
	//Build build Matcher
	Build(words []string)

	//Match return match sensitive words
	Match(text string, repl rune) ([]string, string)
}

type RegexpMather

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

func NewRegexpMatcher

func NewRegexpMatcher() *RegexpMather

func (*RegexpMather) Build

func (a *RegexpMather) Build(words []string)

func (*RegexpMather) Match

func (a *RegexpMather) Match(text string, repl rune) (word []string, desensitization string)

Match 所有的正则只有命中一个就返回;

Jump to

Keyboard shortcuts

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