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特 ...
随机推荐
- 用jquery写的json省市县三级联动下拉
<form action="#" name="myform"> <label>省</label><select nam ...
- spring 跨域 CORS (Cross Origin Resources Share) 跨域
Spring提供了三种方式跨域 1.CorsFilter 过滤器 2.<mvc:cors> Bean(全局,推荐使用) 3.@CrossOrigin注解 以上三种方式本质都是用来配置Cor ...
- webstorm开发nodejs的技巧--代码提示DefinitelyTyped
先发视频“WebStorm - MEAN Stack Walkthrough and Tips”,地址:http://www.tudou.com/programs/view/txhBUA7fcNA/? ...
- Question 20171115 String&&StringBuffer&&StringBuilder的区别与联系?
Question 20171114 String&&StringBuffer&&StringBuilder的区别和联系 创建成功的String对象,其长度是固定的,内容 ...
- group by 注意的细节 ,
1. GROUP BY子句必须出现在WHERE子句之后,ORDER BY子句之前. HAVING语句必须在ORDER BY子句之后.(where先执行,再groupby分组:groupby先分组,ha ...
- Flask—01-轻松入门Flask
Flask入门 WEB工作原理 C/S与B/S架构:客户端-服务器,浏览器-服务器 B/S架构工作原理 客户端(浏览器) <=> WEB服务器(nginx) <=> WSGI( ...
- WebGL学习笔记(3)
根据上篇笔记,在对3D对象可进行普通的控制后,以及学习了http://hiwebgl.com的教程第10章内容:世界模型的载入以及控制镜头移动,经过多次调试矩阵代码,已经可以实现在世界中旋转镜头/控制 ...
- v-if和v-show的区别以及callback回调函数的体会
今天总结一下最近一周碰到的一些问题 一.v-if和v-show的区别 v-show用的是css属性中的display="block/none",元素被隐藏了但是节点还在页面中,但是 ...
- CF1042C Array Product(贪心,模拟)
题目描述 You are given an array aa consisting of nn integers. You can perform the following operations w ...
- MySql Connector/C++8结果集处理Demo
#include <iostream> #include <exception> #include <mysqlx/xdevapi.h> using std::co ...