加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-lua敏感词过滤函数

发布时间:2020-12-14 16:46:50 所属栏目:百科 来源:网络整理
导读:function cc.splitStringToWords(str) local words = {} for uchar in string.gfind(str,"[%z1-127194-244][128-191]*") do words[#words+1] = uchar end return wordsendfunction cc.filterSensitiveWord(str) local filter = { [" "] = true,["。"]
function cc.splitStringToWords(str)
    local words = {}
    for uchar in string.gfind(str,"[%z1-127194-244][128-191]*") do
        words[#words+1] = uchar
    end
    return words
end

function cc.filterSensitiveWord(str)
    local filter = {
        [" "] = true,["。"] = true,[","] = true,["、"] = true,[";"] = true,[":"] = true,["’"] = true,["‘"] = true,["."] = true,[","] = true,["/"] = true,[";"] = true,[":"] = true,["'"] = true,}

    print("原字符:",str)
    local words = cc.splitStringToWords(str)
    local exist = false
    local pos = 1
    while pos <= #words do
        local tmp = pos
        local t = cc.csvData["sensitive_words"][words[pos]]
        while t ~= nil do
            if next(t) then
                tmp = tmp +1
                if filter[words[tmp]] then
                    tmp = tmp +1
                end
                t = t[words[tmp]]
            else
                print("find",pos,tmp)
                exist = true
                for i=pos,tmp do
                    if filter[words[i]] ~= true then
                        words[i] = "*"
                    end
                end
                break
            end
        end
        if tmp ~= pos then
            pos = tmp
        else
            pos = pos +1
        end
    end
    local newStr = ""
    if exist then
        for i=1,#words do
            newStr = newStr .. words[i]
        end
    else
        newStr = str
    end
    print("过滤后:",newStr)
    return newStr
end



csv = path.."config/sensitive_words.csv"
    cc.csvData["sensitive_words"] = {}
    cc.praseCSV(csv,function (idx,vals)
            local words = cc.splitStringToWords(vals["code"])
            --print("word",vals["code"],#words)
            local t = cc.csvData["sensitive_words"]
            local pos = 1
            while pos <= #words do
                if not t[words[pos]] then
                    t[words[pos]] = {}
                end
                t = t[words[pos]]
                pos = pos+1
            end
        end)
单词
code
习大大

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读