lua敏感词过滤
--过滤敏感词(如果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敏感词过滤的更多相关文章
- java实现敏感词过滤(DFA算法)
小Alan在最近的开发中遇到了敏感词过滤,便去网上查阅了很多敏感词过滤的资料,在这里也和大家分享一下自己的理解. 敏感词过滤应该是不用给大家过多的解释吧?讲白了就是你在项目中输入某些字(比如输入xxo ...
- 用php实现一个敏感词过滤功能
周末空余时间撸了一个敏感词过滤功能,下边记录下实现过程. 敏感词,一方面是你懂的,另一方面是我们自己可能也要过滤一些人身攻击或者广告信息等,具体词库可以google下,有很多. 过滤敏感词,使用简单的 ...
- 浅析敏感词过滤算法(C++)
为了提高查找效率,这里将敏感词用树形结构存储,每个节点有一个map成员,其映射关系为一个string对应一个TreeNode. STL::map是按照operator<比较判断元素是否相同,以及 ...
- Java实现敏感词过滤
敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有必要的.前段时间我一个朋友(马上毕业,接触编程不久)要我帮他看一个文字过滤的东西,它说检索效率非常慢.我把它程序拿过来 ...
- php敏感词过滤
在项目开发中发现有个同事在做敏感词过滤的时候用循环在判断,其实是不用这样做的,用php的数组函数和字符串函数即可实现 function filterNGWords($string) { $badwor ...
- 转:鏖战双十一-阿里直播平台面临的技术挑战(webSocket, 敏感词过滤等很不错)
转自:http://www.infoq.com/cn/articles/alibaba-broadcast-platform-technology-challenges 鏖战双十一-阿里直播平台面临的 ...
- java敏感词过滤
敏感词过滤在网站开发必不可少.一般用DFA,这种比较好的算法实现的. 参考链接:http://cmsblogs.com/?p=1031 一个比较好的代码实现: import java.io.IOExc ...
- Java实现敏感词过滤(转)
敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有必要的.前段时间我一个朋友(马上毕业,接触编程不久)要我帮他看一个文字过滤的东西,它说检索效率非常慢.我把它程序拿过来 ...
- DFA和trie特里实现敏感词过滤(python和c语言)
今天的项目是与完成python开展,需要使用做关键词检查,筛选分类,使用前c语言做这种事情.有了线索,非常高效,内存小了,检查快. 到达python在,第一个想法是pip基于外观的c语言python特 ...
随机推荐
- ubuntu安装完整版的vim
apt-get remove vim-commonapt-get install vim
- Android学习笔记_7_使用 sax 或者 dom 或者 pull 解析XML文件
一.Pull解析介绍: Android上使用SAX和DOM方式解析XML的方法,并且对两种做了简单的比较,通过比较我们知道对在往往内存比较稀缺的移动设备上运行的Android系统来说,SAX是一种比较 ...
- lucene&solr学习——创建和查询索引(理论)
1.Lucene基础 (1) 简介 Lucene是apache下的一个开放源代码的全文检索引擎工具包.提供完整的查询引擎和索引引擎:部分文本分析引擎. Lucene的目的是为软件开发人员提供一个简单易 ...
- ASP.NET MVC中使用表单上传文件时的注意事项
最近了好久没写ASP.NET 使用HTML的FORM来上传文件了,结果写了个文件上传发现ASP.NET MVC的Controller中老是读取不到上传的文件. MVC的View(Index.cshtm ...
- oracle删除一个表内的重复数据,
查询以及删除一个数据库表内的重复数据. 1.查询表中的多余的重复记录,重复记录是根据单个字段来判断的. select * from biao where id in (select id from b ...
- NSString+JSON - iOS
日常开发中常用的一个相互转换的方法; 直接创建对应的类,引用如下方法即可实现; 具体 code 如下: 声明: #import <Foundation/Foundation.h> @int ...
- zTree的核心处理逻辑
zTree 是一个前端树形结构的插件. 使用起来很简单,我们重点关注一下插件的核心代码. 首先,zTree需要如下的数据结构: let areaData = [ { "id": & ...
- python的字典数据类型及常用操作
字典的定义与特性 字典是Python语言中唯一的映射类型. 定义:{key1: value1, key2: value2} 1.键与值用冒号“:”分开: 2.项与项用逗号“,”分开: 特性: 1.ke ...
- sql server,mysql,oracle平时用法的区别
由于工作的原因,上家公司一直使用的oracle,后来接触了的几个项目,既有使用mysql的又有使用sqlserver,自己在使用sqlserver及mysql要实现某功能时,经常要在网上找来找去,所以 ...
- 使用hibernate框架连接oracle数据库进行简单的增删改
初始化配置和session 关于配置文件这里就不在赘述了,假设配置文件配好后我们需要加载配置和sessionFactory,并获取session,因为每次进行增删改查时都需要session,所以封装成 ...