--过滤敏感词(如果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. 【luogu P2194 HXY烧情侣】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2194 第一问:缩点并且统计其强连通分量里的最小耗费.把所有强连通分量的最小耗费加起来. 第二问:统计在每个强 ...

  2. 深入浅出C指针

    http://bbs.9ria.com/blog-164422-18039.html 初学者在学习C语言时,通常会遇到两个瓶颈,一个是“递归”,一个是“指针”.大学老师在讲述这两个知识点时通常都是照本 ...

  3. 推荐几个Mac/Linux下比较好用的工具

    1.Tmux,连接开发机可以让在任务在开发机一直执行,不用nohup &这种了也相对稳定,还有session可以记录当时的状态. 常用命令: tmux new -s name 指定名字开启一个 ...

  4. PL/SQL语句快捷输入设置

    设置PL/SQL语句快捷输入的方法,让你成为高效率的人. 1.打开PL/SQL,输入用户并登录 2.并打开Tools->Preferences->Editor->AutoReplac ...

  5. c# 说说开发通用通信库,尤其是分布式服务的通信

    来,牛皮需要吹起,IT行业需要自娱自乐.开篇吹牛..... 现在我们通信真是各种各样,各种组件,但是就我的看法,功能越完善,封装越完善,牺牲的性能可能就越大,代码量就越大. 当然这不能阻挡IT大军的脚 ...

  6. 小胖办证 wzoi

    小胖办证 题目描述: xuzhenyi要办个签证.办证处是一座M层的大楼,1<=M<=100. 每层楼都有N个办公室,编号为1..N(1<=N<=500).每个办公室有一个签证 ...

  7. ABAP术语-ALE

    ALE 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/13/993351.html Application Link Enabling (A ...

  8. Django---定义、MVC和MTV模式、命令行工具、配置文件settings

    1.什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有的 ...

  9. 【tp5.1】微信公众号授权登录及获取信息录入数据库

    微信公众号开发文档链接:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432 微信公众号授权登录分为两种: 1.以 ...

  10. 图的遍历(Python实现)

    图的遍历(Python实现) 记录两种图的遍历算法——广度优先(BFS)与深度优先(DFS). 图(graph)在物理存储上采用邻接表,而邻接表是用python中的字典来实现的. 两种遍历方式的代码如 ...