word_filter

package module
v0.0.0-...-f396ded Latest Latest
Warning

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

Go to latest
Published: May 27, 2017 License: Apache-2.0 Imports: 17 Imported by: 0

README

go-word-filter

基于trie树的敏感词过滤服务, 支持grpc调用

安装

$ go get github.com/cyuxlif/go-word-filter

$ cd $GOPATH/src/github.com/cyuxlif/go-word-filter/conf
$ cp app.yaml.example app.yaml
$ vim app.yaml #配置字典路径, 监听地址
$ cd $GOPATH/src/github.com/cyuxlif/go-word-filter/cmd/word_filter
$ go build
$ ./word_filter -c /yourpath/conf/app.yaml

使用

参考 $GOPATH/src/github.com/cyuxlif/go-word-filter/cmd/grpc_client

部分代码如:

	// 连接
	conn, err := grpc.Dial(Addr, grpc.WithInsecure())

	if err != nil {
		pp.Println(err)
	}

	defer conn.Close()

	// 初始化客户端
	c := pb.NewWordFilterClient(conn)

	// 调用方法
	Text := new(pb.Text)
	Text.Text = `有了 gRPC, 我们可以一次性的在一个 .proto 文件中定义服务并使用任何支持它的操你妹语言去实现客户端和服务器,反过来,
	它们可以在各种环境中,你妈了个逼从Google的服务器到你自己的平板电脑—— gRPC 帮你解决了性吧春暖花开不同语言及环境间通信的复杂性.
	使用 protocol buffers 还能获得波多野结衣全集其他好处,包括高效的序列号,简单的 IDL 以及容易进行操你妈接口更新`
	r, err := c.FindKeyWords(context.Background(), Text)
	if err != nil {
		pp.Println(err)
	}
	pp.Println(r.KeyWords)

将会打印

[]*word_filter.KeyWord{
  &word_filter.KeyWord{
    Word:    "操你妹",
    Attr:    1,
    Replace: "我傻x",
  },
  &word_filter.KeyWord{
    Word:    "你妈了个逼",
    Attr:    2,
    Replace: "",
  },
  &word_filter.KeyWord{
    Word:    "性吧春暖花开",
    Attr:    1,
    Replace: "",
  },
  &word_filter.KeyWord{
    Word:    "波多野结衣全集",
    Attr:    1,
    Replace: "",
  },
  &word_filter.KeyWord{
    Word:    "操你妈",
    Attr:    1,
    Replace: "",
  },
}

字典格式

这里的字典格式指的是data目录下的txt的字典格式 词典格式为文本格式:

关键词|词性(可选,默认是替换词 1代表替换词,2代表审核词, 3代表禁止词)|替换词(可选)

如:

操你妈|1 //替换词

你妈逼|1|你妹妹 //替换词

卖毒品|3 //禁止词

你妈了个逼|2 //审核词

Documentation

Index

Constants

View Source
const SPLIT_FLAG = "|"
View Source
const VERSION = "v1.0.0"

Variables

This section is empty.

Functions

This section is empty.

Types

type App

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

func New

func New(opts *Options) *App

func (*App) EditUserDict

func (app *App) EditUserDict(ctx context.Context, t *pb.Text) (*pb.Empty, error)

rpc handler 修改用户词典内容

func (*App) FindKeyWords

func (app *App) FindKeyWords(ctx context.Context, t *pb.Text) (*pb.FindKeyWordsRes, error)

rpc handler 根据文本查找关键词

func (*App) GetUserDict

func (app *App) GetUserDict(ctx context.Context, empty *pb.Empty) (*pb.Text, error)

rpc handler 获取用户词典内容

func (*App) Run

func (app *App) Run(addr string)

type AttrType

type AttrType uint8
const (
	//替换关键词
	REPLACE AttrType = iota + 1 //1
	//审核关键词
	REVIEW //2
	//禁止关键词
	BAN //3
)

type Dict

type Dict struct {
	//默认脏词库
	DefaultDictDataPath string
	//自定义脏词库
	UserDictDataPath string

	MaxWordLength int
	//trie 树
	Trie *Trie
	sync.RWMutex
}

字典

func NewDict

func NewDict(opts *Options) *Dict

func (*Dict) EditUserDict

func (d *Dict) EditUserDict(userDictContent []byte) error

更新用户词库

func (*Dict) FindKeyWords

func (d *Dict) FindKeyWords(text string) []*KeyWord

从文本中找出 所有关键词 采用正向最大匹配算法

func (*Dict) GetUserDict

func (d *Dict) GetUserDict() ([]byte, error)

获取用户自定义词库

func (*Dict) Load

func (d *Dict) Load(reader io.Reader)

func (*Dict) LoadDict

func (d *Dict) LoadDict()

初始化字典 trie树 词典格式为文本格式 关键词|词性(可选,默认是替换词)|替换词(可选) 如: 彭丽媛 //替换词 江泽民|1 //替换词 你妈逼|1|你妹妹 //替换词 叫小姐|2 //审核词 卖毒品|3 //禁止词

func (*Dict) ReloadDict

func (d *Dict) ReloadDict()

重新加载词库

type KeyWord

type KeyWord struct {
	Word    string
	Attr    AttrType
	Replace string
}

type Node

type Node struct {
	KeyWord *KeyWord //节点末才有数据, 非节点末为nil
	Nodes   map[rune]*Node
}

type Options

type Options struct {
	//默认脏词库
	DefaultDictDataPath string `yaml:"DefaultDictDataPath"`
	//自定义脏词库
	UserDictDataPath string `yaml:"UserDictDataPath"`

	//TCP监听地址
	TCPAddr string `yaml:"TCPAddr"`
}

func NewOptions

func NewOptions(configPath string) *Options

type Trie

type Trie struct {
	Root *Node
}

Directories

Path Synopsis
cmd
pb
word_filter
Package word_filter is a generated protocol buffer package.
Package word_filter is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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