Lua语言的屏蔽词过滤
参考了DFA算法的简单说明与案例实现
构造过滤器
local FilterData = {}local FilterTree = {}--构建时候的临时变量local IsEnd = "IsEnd"function FilterData.Load()--构建过滤器for _, v in ipairs(exd["GT_".."FilterText"]) do --这个循环是已经读取的屏蔽字表local FilterStr = v.MaskWordlocal CurMap = FilterTree--这个函数是循环遍历字符串ForEachString(FilterStr,function(Str)if CurMap[Str] == nil thenCurMap[Str] = {}CurMap[Str][IsEnd] = falseCurMap = CurMap[Str]elseCurMap = CurMap[Str]endend,true)CurMap[IsEnd] = trueend
end
查找
--基础的屏蔽词表匹配过滤
function FilterData.MaskWordFilter(Str)local BeginIndex = 1local MatchingTable = {}while BeginIndex < #Str dolocal NowMap = FilterTree--local MatchingNum = 0local CurStr = string.sub(Str, BeginIndex, #Str)local CurIndex = BeginIndexlocal LeftMatchIndex = 0local RightMatchIndex = 0local i = 1local AllowSpecialSymbols = falsewhile true dolocal char = string.sub(CurStr,i,i)local Ascll = string.byte(char)if Ascll > 128 thenchar = string.sub(CurStr,i,i+2)i = i + 3if NowMap[char] ~=nil thenif LeftMatchIndex == 0 thenLeftMatchIndex = CurIndexendif NowMap[char][IsEnd] == true thenRightMatchIndex = CurIndex+2BeginIndex = CurIndex+3endNowMap = NowMap[char]else--判断是否已经有一个字匹配if LeftMatchIndex ~= 0 and RightMatchIndex ~= 0 thenMatchingTable[LeftMatchIndex] = RightMatchIndexbreakelseif LeftMatchIndex ~= 0 thenBeginIndex = BeginIndex+3breakelseBeginIndex = BeginIndex+3endendCurIndex = CurIndex +3elsei = i + 1if 32 < Ascll thenif NowMap[char] ~=nil thenif LeftMatchIndex == 0 thenLeftMatchIndex = CurIndexendif NowMap[char][IsEnd] == true thenRightMatchIndex = CurIndexBeginIndex = CurIndex+1endNowMap = NowMap[char]elseif LeftMatchIndex ~= 0 and RightMatchIndex ~= 0 thenMatchingTable[LeftMatchIndex] = RightMatchIndexbreakelseif LeftMatchIndex ~= 0 thenBeginIndex = BeginIndex+1breakelseBeginIndex = BeginIndex+1endendelseif AllowSpecialSymbols thenendCurIndex = CurIndex +1endif i > #CurStr thenif LeftMatchIndex ~= 0 and RightMatchIndex ~= 0 thenMatchingTable[LeftMatchIndex] = RightMatchIndexelseBeginIndex = BeginIndex + #CurStrendbreakendendendlocal res = trueif next(MatchingTable) ~= nil thenres = falseendreturn res, MatchingTable
end--调用前面的,把字符串先提取成中文和英文再分别过滤,替换成**
function FilterData.FineMaskWordFilter(Str)local Chi = {}local Eng = {}local k = 1while k < #Str dolocal c = string.byte(Str,k)if not c then break end--英文字母 (c>=48 and c<=57) orif (c>= 65 and c<=90) or (c>=97 and c<=122) thenk = k + 1Eng[#Eng+1] = string.char(c)elseif c>=228 and c<=233 then--中文local c1 = string.byte(Str,k+1)local c2 = string.byte(Str,k+2)if c1 and c2 thenlocal a1,a2,a3,a4 = 128,191,128,191if c == 228 then a1 = 184elseif c == 233 then a2,a4 = 190,c1 ~= 190 and 191 or 165endif c1>=a1 and c1<=a2 and c2>=a3 and c2<=a4 thenk = k + 3Chi[#Chi+1] = string.char(c,c1,c2)endendelse--都不符合时候不加入表k = k + 1endendlocal ChiStr = table.concat(Chi)local EngStr = table.concat(Eng)local _,r =FilterData.MaskWordFilter(ChiStr)for key,v in pairs(r) dolocal tmp =string.sub(ChiStr,key,v)ForEachString(tmp, function(SubStr) Str = string.gsub(Str,SubStr,"*") end)endlocal _,q = FilterData.MaskWordFilter(EngStr)for key,v in pairs(q) dolocal tmp =string.sub(EngStr,key,v)ForEachString(tmp, function(SubStr) Str = string.gsub(Str,SubStr,"*") end)endreturn Str
end
判断玩家名字是否只含有中英文数字和常用标点
function FilterData.PlayerNameFilter(Str)local ss = {}local k = 1while k < #Str dolocal c = string.byte(Str,k)if not c then break end--英文字母符号if (c>=48 and c<=57) or (c>= 65 and c<=90) or (c>=97 and c<=122) thenk = k + 1ss[#ss+1] = string.char(c)elseif c>=228 and c<=233 then--中文local c1 = string.byte(Str,k+1)local c2 = string.byte(Str,k+2)if c1 and c2 thenlocal a1,a2,a3,a4 = 128,191,128,191if c == 228 then a1 = 184elseif c == 233 then a2,a4 = 190,c1 ~= 190 and 191 or 165endif c1>=a1 and c1<=a2 and c2>=a3 and c2<=a4 thenk = k + 3ss[#ss+1] = string.char(c,c1,c2)endendelse--都不符合时候不加入表k = k + 1endendif #ss > 0 thenlocal res = table.concat(ss)local len = #resif #Str == len thenreturn true,reselsereturn false,resendelsereturn false , ""end
end
字符串遍历的代码
--[[字符串遍历
@Str: 被遍历的字符串
@Function:每个char需要调用的函数
@bAllowSpecialSymbols:是否允许空格遍历
@...:Function的其他参数
--]]
function ForEachString(Str , Function ,bAllowSpecialSymbols,...)if Str==nil or Function==nil ortype(Str)~= "string" or type(Function) ~= "function"thenreturnendlocal i = 1local AllowSpecialSymbolsif bAllowSpecialSymbols then AllowSpecialSymbols = bAllowSpecialSymbolselseAllowSpecialSymbols = falseendwhile true dolocal char = string.sub(Str,i,i)local Ascll = string.byte(char)if Ascll > 128 thenchar = string.sub(Str,i,i+2)i = i + 3Function(char,...)elseif 32 < Ascll thenFunction(char,...)elseif AllowSpecialSymbols thenFunction(char,...)endi = i + 1endif i > #Str thenbreakendend
end
Lua语言的屏蔽词过滤相关推荐
- 游戏敏感词屏蔽词过滤
游戏和网站,都需要敏感词(屏蔽词)过滤,你懂的.而且敏感词库不断的增长,从十几年前的3000多词,已经增长到14000以上(各渠道获取的词库略有不同). 这1万多词的扫描处理,开销的性能就有点客观了. ...
- python敏感词过滤replace_Serverless 实战:3 分钟实现文本敏感词过滤
敏感词过滤是随着互联网社区一起发展起来的一种阻止网络犯罪和网络暴力的技术手段,通过对可能存在犯罪或网络暴力的关键词进行有针对性的筛查和屏蔽,能够防患于未然,将后果严重的犯罪行为扼杀于萌芽之中. 随着各 ...
- 38个敏感词_Serverless 实战:3 分钟实现文本敏感词过滤
敏感词过滤是随着互联网社区一起发展起来的一种阻止网络犯罪和网络暴力的技术手段,通过对可能存在犯罪或网络暴力的关键词进行有针对性的筛查和屏蔽,能够防患于未然,将后果严重的犯罪行为扼杀于萌芽之中. 随着各 ...
- dfa算法c语言,DFA跟trie字典树实现敏感词过滤(python和c语言)
DFA和trie字典树实现敏感词过滤(python和c语言) 现在做的项目都是用python开发,需要用做关键词检查,过滤关键词,之前用c语言做过这样的事情,用字典树,蛮高效的,内存小,检查快. 到了 ...
- Android前端敏感词(屏蔽词)过滤
转载请标明出处:http://blog.csdn.net/donkor_/article/details/81232529 前言: 开发中经常要处理用户一些文字的提交,所以涉及到了敏感词过滤的功能,参 ...
- java敏感词屏蔽器,“敏感词过滤”功能设置
"敏感词过滤"功能设置 "敏感词过滤"是指对互联网发布的言论和文章中含有的敏感词进行过滤.敏感词经过在互联网的传播和扩散会影响社会的稳定和用户的使用.云锁的&q ...
- 敏感词过滤算法 为内容保驾护航 Java/.Net/C++/c/Python等语言是如何进行敏感词打码限制的 高效防范违规内容
有人的地方,就有江湖,有输入框的地方,就有注入风险!有输入框的地方,就有敏感词!敏感词就像一个平台杀手,可能直接导致平台被封锁! 敏感词是一个APP.一个网站.一个内容平台的"杀手" ...
- 网站是怎么屏蔽脏话的呢:简单学会SpringBoot项目敏感词、违规词过滤方案
一个社区最重要的就是交流氛围与审查违规,而这两者都少不了对于敏感词进行过滤的自动维护措施.基于这样的措施,我们才能基本保证用户在使用社区的过程中,不至于被敏感违规词汇包围,才能够正常的进行发布帖子和评 ...
- Java实现敏感词过滤 - IKAnalyzer中文分词工具
IKAnalyzer 是一个开源的,基于java语言开发的轻量级的中文分词工具包. 官网: https://code.google.com/archive/p/ik-analyzer/ 本用例借助 I ...
最新文章
- SharePoint Server 2016 部署安装(三)—— 安装SharePoint Server
- 2021年春季学期-信号与系统-第十五次作业参考答案-第二小题参考答案
- Android之旅---广播(BroadCast)
- 原生js 基于canvas写一个简单的前端 截图工具
- Redis-哨兵机制,实现高可用
- day4 数组的初始化和练习
- C ++ 指针 | 指针与函数_7
- 罗永浩:锤子手机比索尼、夏普、微软、LG都好
- fiddler证书 iphone_【详细】Mac使用Fiddler实现IPhone抓包(支持https)
- jboss6启动报错
- 利用ACIS、HOOPS开发三维软件
- 量子纠缠:万物皆有默契
- 一种永不止步的进取精神的勤奋
- 这一季绿色智能制造“英雄联盟”已集结, 有哪些新鲜点?
- CSS中hover出现不生效的几个原因 ?
- 为TreeView正色
- java 切图_java用pdfbox切图并重绘宽高
- 基于FPGA的 DS18B20多功能温度显示
- codecombat极客战记地牢蓝色练习关通关代码
- ONNX 模型的静态量化和动态量化
热门文章
- ssh用于验证,https://github.com/?需要,https://www.cloudcontrol.com/onboarding/ssh也需要。...
- 【Tomcat】——纯手写实现一个简单的Tomcat
- 支付宝退款接口(即时到帐有密退款)
- 林志玲为何无法拯救都市丽人的遇冷?
- linux制作windows光盘,Windows光盘制作U盘安装系统的方法
- 阿里云DataWorks学习之平台实践
- 营改增来了新税制下金融做好准备了吗
- 演绎大制作电影的“撼人心魄”——飞利浦全景声回音壁B8967分享
- 偏向锁、轻量级锁及重量级锁
- python 类的简单应用--一个简单的扑克游戏