敏感词(小敏感饶舌麦词)


【点击查看】低成本上班族靠谱副业好项目 | 拼多多无货源创业7天起店爆单玩法

【点击查看】逆林创业记 | 拼多多电商店铺虚拟类项目新玩法(附完整词表&检测工具)

【点击查看】逆林创业记 | 小白ai写作一键生成爆文速成课

领300个信息差项目,见公众号【逆林创业记】(添加请备注:网站)

过滤脚本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版本敏感词过滤敏感词敏感词,觉得挺不错,亲测可用。

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站不拥有所有权,不承担相关法律责任。如发现有侵权/违规的内容, 联系QQ3361245237,本站将立刻清除。