--过滤敏感词(如果onlyKnowHas为true,表示只想知道是否存在敏感词,不会返回过滤后的敏感词,比如用户注册的时候,我们程序是只想知道用户取的姓名是否包含敏感词的(这样也能提高效率,检测到有一个敏感词就直接返回),而聊天模块是要返回过滤之后的内容的,那么onlyKnowHas可以不设,但这需要遍历所有可能)
local function filterSensitiveWords( content , onlyKnowHas)
if content == nil or content == '' then
return ''
end --获取每一个字符
local wordlist = {}
local q =
for w in string.gmatch(content, ".[\128-\191]*") do
wordlist[q]= w
q=q+
end --获取字符串中从起始位置到结束位置的字符
local function findWord( wordTable, startpos,endpos )
local result = ''
for i=startpos,endpos do
result = result..wordTable[i]
end
return result
end local length = #(string.gsub(content, "[\128-\191]", "")) --计算字符串的字符数(而不是字节数)
local i,j = ,
local replaceList={}
local mgc = {['敏感词1']=true,['敏感词2']=true,['敏感词3']=true}
local function check( )
local v = findWord(wordlist,i,j)
local item = mgc[v]
if item == true then
if onlyKnowHas == true then
return true
end
table.insert(replaceList,v)
j = j+
i = j
else
j = j+
end
local limit = (j-i) >= and true or (j > length and true or false)
if limit == true then --因为一个敏感词最多15个字,不会太长,目的提高效率
i = i +
j = i
end
if i <= length then
check()
end
end
check() if onlyKnowHas == true then
return false
end --模式串中的特殊字符 ( ) . % + - * ? [ ^ $
-- % 用作特殊字符的转义字符,比如%%匹配字符% %[匹配字符[
local specialChar = {['(']=true,[')']=true,['.']=true,['%']=true,['+']=true,['-']=true,['*']=true,['?']=true,['[']=true,['^']=true,['$']=true}
--检测是否有特殊字符
local function checkSpecialChar( msg )
local tArray = string.gmatch(msg, ".[\128-\191]*")
local contentArray = {}
for w in tArray do
table.insert(contentArray,w)
end
local ck = {}
for i=,#contentArray do
local v = contentArray[i]
if specialChar[v] == true then
table.insert(ck,'%')
end
table.insert(ck,v)
end
local result=''
for i,v in ipairs(ck) do
result = result..v
end
return result
end for i,v in ipairs(replaceList) do
    -- --这里我没用,主要还是为了效率
-- local count = #(string.gsub(content, "[\128-\191]", "")) --判断多少个字符(用于计算要显示的*个数)
-- local star = ''
-- for i=1,count do
-- star = star..'*'
-- end
v = checkSpecialChar(v)
content = string.gsub( content , v , '***' )
end
return content
end

目前认为最优算法如下:

local function filterSensitiveWords( content , onlyKnowHas)
if content == nil or content == '' then
return ''
end --模式串中的特殊字符 ( ) . % + - * ? [ ^ $
-- % 用作特殊字符的转义字符,比如%%匹配字符% %[匹配字符[
local specialChar = {['(']=true,[')']=true,['.']=true,['%']=true,['+']=true,['-']=true,['*']=true,['?']=true,['[']=true,['^']=true,['$']=true}
--检测是否有特殊字符
local function checkSpecialChar( msg )
local tArray = string.gmatch(msg, ".[\128-\191]*")
local contentArray = {}
for w in tArray do
table.insert(contentArray,w)
end
local ck = {}
for i=,#contentArray do
local v = contentArray[i]
if specialChar[v] == true then
table.insert(ck,'%')
end
table.insert(ck,v)
end
local result=''
for i,v in ipairs(ck) do
result = result..v
end
return result
end --因为找不到方案禁用虚拟键盘的回车键,所以只能代码移除回车键(游戏中虚拟键盘不应有换行键的)
--如果可以使用回车键的话,那么就可以发布竖着的敏感词文字了,显示的很明显,没有阅读障碍,但明文规定不能出现很明显的敏感词
--用字符隔开的敏感词是可以接受的,因为这种用字符隔开的敏感词情况太多,根本无法避免,所以是可以接受的
--InputField有一个枚举类型keyboardType来设置键盘的,具体没试,也许也是一种解决方案
local tempContent = ''
for w in contentArray do
if string.byte(w) ~= then --表示回车(换行)
tempContent = tempContent..w
end
end
content = tempContent
contentArray = string.gmatch(tempContent, ".[\128-\191]*") local mgc = {'敏'={'敏1','敏2','敏3'},,'党'={'党1'}} local contentArray = string.gmatch(content, ".[\128-\191]*")
local value,startpos,endpos,length,star
local starChar ='*'
--循环每一个字符
for w in contentArray do
value = mgc[w]
if w ~= starChar and value ~= nil then
for i,v in ipairs(value) do
local z = checkSpecialChar(v)
startpos,endpos = content:find(z)
if startpos ~= nil and endpos ~= nil then
if onlyKnowHas == true then
return true
end
length = #(string.gsub(v, "[\128-\191]", ""))
star = ''
for i=,length do
star = star..starChar
end
content = string.gsub( content , z , star )
break
end
end
end
end
if onlyKnowHas == true then
return false
end
return content
end

lua敏感词过滤的更多相关文章

  1. java实现敏感词过滤(DFA算法)

    小Alan在最近的开发中遇到了敏感词过滤,便去网上查阅了很多敏感词过滤的资料,在这里也和大家分享一下自己的理解. 敏感词过滤应该是不用给大家过多的解释吧?讲白了就是你在项目中输入某些字(比如输入xxo ...

  2. 用php实现一个敏感词过滤功能

    周末空余时间撸了一个敏感词过滤功能,下边记录下实现过程. 敏感词,一方面是你懂的,另一方面是我们自己可能也要过滤一些人身攻击或者广告信息等,具体词库可以google下,有很多. 过滤敏感词,使用简单的 ...

  3. 浅析敏感词过滤算法(C++)

    为了提高查找效率,这里将敏感词用树形结构存储,每个节点有一个map成员,其映射关系为一个string对应一个TreeNode. STL::map是按照operator<比较判断元素是否相同,以及 ...

  4. Java实现敏感词过滤

    敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有必要的.前段时间我一个朋友(马上毕业,接触编程不久)要我帮他看一个文字过滤的东西,它说检索效率非常慢.我把它程序拿过来 ...

  5. php敏感词过滤

    在项目开发中发现有个同事在做敏感词过滤的时候用循环在判断,其实是不用这样做的,用php的数组函数和字符串函数即可实现 function filterNGWords($string) { $badwor ...

  6. 转:鏖战双十一-阿里直播平台面临的技术挑战(webSocket, 敏感词过滤等很不错)

    转自:http://www.infoq.com/cn/articles/alibaba-broadcast-platform-technology-challenges 鏖战双十一-阿里直播平台面临的 ...

  7. java敏感词过滤

    敏感词过滤在网站开发必不可少.一般用DFA,这种比较好的算法实现的. 参考链接:http://cmsblogs.com/?p=1031 一个比较好的代码实现: import java.io.IOExc ...

  8. Java实现敏感词过滤(转)

    敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有必要的.前段时间我一个朋友(马上毕业,接触编程不久)要我帮他看一个文字过滤的东西,它说检索效率非常慢.我把它程序拿过来 ...

  9. DFA和trie特里实现敏感词过滤(python和c语言)

    今天的项目是与完成python开展,需要使用做关键词检查,筛选分类,使用前c语言做这种事情.有了线索,非常高效,内存小了,检查快. 到达python在,第一个想法是pip基于外观的c语言python特 ...

随机推荐

  1. 在VS2010 中使用subversion 进行代码的分支与合并

    在实际开发总,遇到了这种情况: 开发版本1,开发版本2 ,更新产品时要求1在前,2在后. 但是因为时间要求,必须2个版本同时开发.这时就想到了在svn的版本分支合并. 创建分支之前,首先把当前版本代码 ...

  2. ContentProvider 、 ContentResolver 、 ContentObserver

    说说ContentProvider . ContentResolver . ContentObserver 之间的关系**a. ContentProvider 内容提供者,用于对外提供数据 b. Co ...

  3. HTML5读取input[type=file]中的图片

    转载 https://blog.csdn.net/fd214333890/article/details/71250488

  4. Percona-Tookit工具包之pt-online-schema-change

      Preface       As we all know,it's really a troublesome thing to DBA in scenario of changing table ...

  5. 爬虫——Handler处理器 和 自定义Opener

    我们之前一直都在使用的urlopen,这是一个特殊的opener(也就是模块帮我们构建好的). 但是基本的urlopen()方法不支持代理.cookie等其他的HTTP/HTTPS高级功能.所以要支持 ...

  6. python的pymysql模块简介

    一.介绍 在python中用pymysql模块来对mysql进行操作,该模块本质就是一个套接字客户端软件,使用前需要事先安装 pip3 install pymysql 二.操作简介 import py ...

  7. Spring常见面试题

    本文是通过收集网上各种面试指南题目及答案然后经过整理归纳而来,仅仅是为了方便以后回顾,无意冒犯各位原创作者. Spring框架 1. 什么是Spring? Spring 是个java企业级应用的开源开 ...

  8. mysql帐号不允许从远程登陆

    默认情况下,mysql帐号不允许从远程登陆,只能在localhost登录.本文提供了二种方法设置mysql可以通过远程主机进行连接. 一.改表法 在localhost登入mysql后,更改 “mysq ...

  9. laravel自定义返回错误方法

    返回视图传递错误信息 function withInfoErr($msg){ return back()->with('error',$msg); } 返回视图提示消息 function wit ...

  10. hdu6370 并查集+dfs

    Werewolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...