filter

package module
v0.0.0-...-23c7aad Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

README

Golang Dirty Filter

GoDoc

基于DFA算法; 支持动态修改敏感词,同时支持特殊字符的筛选; 敏感词的存储支持内存存储及MongoDB存储。

获取

$ go get -v github.com/antlinker/go-dirtyfilter

使用

package main

import (
  "fmt"

  "github.com/antlinker/go-dirtyfilter"
  "github.com/antlinker/go-dirtyfilter/store"
)

var (
  filterText = `我是需要过滤的内容,内容为:**文@@件,需要过滤。。。`
)

func main() {
  memStore, err := store.NewMemoryStore(store.MemoryConfig{
    DataSource: []string{"文件"},
  })
  if err != nil {
    panic(err)
  }
  filterManage := filter.NewDirtyManager(memStore)
  result, err := filterManage.Filter().Filter(filterText, '*', '@')
  if err != nil {
    panic(err)
  }
  fmt.Println(result)
}

输出结果

[文件]

License

Copyright 2016.All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

View Source
const (
	// DefaultCheckInterval 敏感词检查频率(默认5秒检查一次)
	DefaultCheckInterval = time.Second * 5
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DirtyFilter

type DirtyFilter interface {
	// Filter 文本过滤函数
	// excludes 表示排除指定的字符
	// 返回文本中出现的敏感词,如果敏感词不存在则返回nil
	// 如果出现异常,则返回error
	Filter(text string, excludes ...rune) ([]string, error)

	// FilterResult 文本过滤函数
	// excludes 表示排除指定的字符
	// 返回文本中出现的敏感词及出现次数,如果敏感词不存在则返回nil
	// 如果出现异常,则返回error
	FilterResult(text string, excludes ...rune) (map[string]int, error)

	// FilterReader 从可读流中过滤敏感词
	// excludes 表示排除指定的字符
	// 返回可读流中出现的敏感词,如果敏感词不存在则返回nil
	// 如果出现异常,则返回error
	FilterReader(reader io.Reader, excludes ...rune) ([]string, error)

	// FilterReaderResult 从可读流中过滤敏感词
	// excludes 表示排除指定的字符
	// 返回可读流中出现的敏感词及出现次数,如果敏感词不存在则返回nil
	// 如果出现异常,则返回error
	FilterReaderResult(reader io.Reader, excludes ...rune) (map[string]int, error)

	// Replace 使用字符替换文本中的敏感词
	// delim 替换的字符
	// 如果出现异常,则返回error
	Replace(text string, delim rune) (string, error)
}

DirtyFilter 提供敏感词过滤接口

func NewNodeChanFilter

func NewNodeChanFilter(text <-chan string, intrusion map[rune]bool) DirtyFilter

NewNodeChanFilter 创建节点过滤器,实现敏感词的过滤 从通道中读取敏感词数据

func NewNodeFilter

func NewNodeFilter(text []string) DirtyFilter

NewNodeFilter 创建节点过滤器,实现敏感词的过滤 从切片中读取敏感词数据

func NewNodeReaderFilter

func NewNodeReaderFilter(rd io.Reader, delim byte) DirtyFilter

NewNodeReaderFilter 创建节点过滤器,实现敏感词的过滤 从可读流中读取敏感词数据(以指定的分隔符读取数据)

type DirtyManager

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

DirtyManager 提供敏感词的管理

func NewDirtyManager

func NewDirtyManager(store DirtyStore, intrusion map[rune]bool, checkInterval ...time.Duration) *DirtyManager

NewDirtyManager 使用敏感词存储接口创建敏感词管理的实例

func (*DirtyManager) Filter

func (dm *DirtyManager) Filter() DirtyFilter

Filter 获取敏感词过滤接口

func (*DirtyManager) Store

func (dm *DirtyManager) Store() DirtyStore

Store 获取敏感词存储接口

type DirtyStore

type DirtyStore interface {
	// Write 将敏感词写入存储区,如果写入失败则返回error
	Write(words ...string) error

	// Read 以迭代的方式读取敏感词
	Read() <-chan string

	// ReadAll 获取所有的敏感词数据,如果获取失败则返回error
	ReadAll() ([]string, error)

	// Remove 移除敏感词,如果移除失败则返回error
	Remove(words ...string) error

	// Version 数据存储版本号
	Version() uint64
}

DirtyStore 提供敏感词的读取、写入存储接口

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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