cocos2d-lua敏感词过滤函数

前端之家收集整理的这篇文章主要介绍了cocos2d-lua敏感词过滤函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. function cc.splitStringToWords(str)
  2. local words = {}
  3. for uchar in string.gfind(str,"[%z\1-\127\194-\244][\128-\191]*") do
  4. words[#words+1] = uchar
  5. end
  6. return words
  7. end
  8.  
  9. function cc.filterSensitiveWord(str)
  10. local filter = {
  11. [" "] = true,["。"] = true,[","] = true,["、"] = true,[";"] = true,[":"] = true,["’"] = true,["‘"] = true,["."] = true,[","] = true,["/"] = true,[";"] = true,[":"] = true,["'"] = true,}
  12.  
  13. print("原字符:",str)
  14. local words = cc.splitStringToWords(str)
  15. local exist = false
  16. local pos = 1
  17. while pos <= #words do
  18. local tmp = pos
  19. local t = cc.csvData["sensitive_words"][words[pos]]
  20. while t ~= nil do
  21. if next(t) then
  22. tmp = tmp +1
  23. if filter[words[tmp]] then
  24. tmp = tmp +1
  25. end
  26. t = t[words[tmp]]
  27. else
  28. print("find",pos,tmp)
  29. exist = true
  30. for i=pos,tmp do
  31. if filter[words[i]] ~= true then
  32. words[i] = "*"
  33. end
  34. end
  35. break
  36. end
  37. end
  38. if tmp ~= pos then
  39. pos = tmp
  40. else
  41. pos = pos +1
  42. end
  43. end
  44. local newStr = ""
  45. if exist then
  46. for i=1,#words do
  47. newStr = newStr .. words[i]
  48. end
  49. else
  50. newStr = str
  51. end
  52. print("过滤后:",newStr)
  53. return newStr
  54. end



  1. csv = path.."config/sensitive_words.csv"
  2. cc.csvData["sensitive_words"] = {}
  3. cc.praseCSV(csv,function (idx,vals)
  4. local words = cc.splitStringToWords(vals["code"])
  5. --print("word",vals["code"],#words)
  6. local t = cc.csvData["sensitive_words"]
  7. local pos = 1
  8. while pos <= #words do
  9. if not t[words[pos]] then
  10. t[words[pos]] = {}
  11. end
  12. t = t[words[pos]]
  13. pos = pos+1
  14. end
  15. end)
  1. 单词
  2. code
  3. 习大大

猜你在找的Cocos2d-x相关文章