前端之家收集整理的这篇文章主要介绍了
cocos2d-lua敏感词过滤函数,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
function cc.splitStringToWords(str)
local words = {}
for uchar in string.gfind(str,"[%z\1-\127\194-\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
习大大
原文链接:https://www.f2er.com/cocos2dx/340650.html