敏感词过滤__Lua版本

过滤脚本 wordfilter.lua

-- 敏感词过滤脚本

local wordfilter = {
    _tree = {}
}

local strSub = string.sub
local strLen = string.len

local _maskWord = '*'

function _word2Tree(root, word)
    if strLen(word) == 0 or type(word) ~= 'string' then
        return
    end

    local function _byte2Tree(r, ch, tail)
        if tail then
            if type(r[ch]) == 'table' then
                r[ch].isTail = true
            else
                r[ch] = true
            end
        else
            if r[ch] == true then
                r[ch] = { isTail = true }
            else
                r[ch] = r[ch] or {}
            end
        end
        return r[ch]
    end

    local tmpparent = root
    local len = strLen(word)
    for i = 1, len do
        tmpparent = _byte2Tree(tmpparent, strSub(word, i, i), i == len)
    end
end

function _check(parent, word, idx)
    local len = strLen(word)

    local ch = strSub(word, 1, 1)
    local child = parent[ch]
    if not child then
    elseif type(child) == 'table' then
        if len > 1 then
            if child.isTail then
                return _check(child, strSub(word, 2), idx + 1) or idx
            else
                return _check(child, strSub(word, 2), idx + 1)
            end
        elseif len == 1 then
            if child.isTail == true then
                return idx
            end
        end
    elseif (child == true) then
        return idx
    end
    return false
end

-- 添加敏感词
function wordfilter:addWord(word)
    _word2Tree(self._tree, word)
end

-- 添加敏感词(配置表)
function wordfilter:addWords(words)
    if type(words) == 'table' then
        for _, word in pairs(words) do
            _word2Tree(self._tree, word)
        end
    end
end

-- 过滤敏感词
function wordfilter:maskString(s)
    if type(s) ~= 'string' then return end

    local i = 1
    local len = strLen(s)
    local word, idx, tmps

    while i <= len do
        word = strSub(s, i)
        idx = _check(self._tree, word, i)

        if idx then
            tmps = strSub(s, 1, i - 1)
            for j = 1, idx - i + 1 do
                tmps = tmps .. _maskWord
            end
            s = tmps .. strSub(s, idx + 1)
            i = idx + 1
        else
            i = i + 1
        end
    end
    return s
end

-- 初始化
function _Init()
    -- wordfilter = require 'Lua/LuaCommon/wordfilter'
	-- 敏感词的表
    local words = require("SensitiveCfg")
    wordfilter:addWords(words);
end

_Init();

return wordfilter

敏感词配置表:SensitiveCfg.lua 

这里只配置如下了几个,其他需要自己添加。

local SensitiveCfg =
{
	"外 挂",
	"外挂",
	"外/挂",
	"外\\挂",
	"外_挂",
	"外挂",
	"外-挂",
	"外—挂",
}

return SensitiveCfg

测试脚本:test.lua

local filter = require("wordfilter")

-- 添加敏感词
--filter:addWord("xxx")
--filter:addWord("tmd")

--local dirtywords =
--{
--	"abc",
--	"abcde",
--}
-- 添加敏感词
--filter:addWords(dirtywords)
local s = "abcyyabcdefghixxxzzzxxxyyytmd,外挂!"
local ret = ''
local loop = 10000
local t1 = os.clock()

for i=1,loop do
	ret = filter:maskString(s)
end

local t2 = os.clock()

print(string.format("%d times call maskString(\"%s\" -> \"%s\"), time used: (%f)\n", loop, s, ret, t2-t1))

 

说明

这是看到一个 别人写的lua版本敏感词过滤,觉得挺不错,亲测可用。

转载地址:https://blog.csdn.net/fanzhang1990/article/details/62227958

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值