敏感词过滤(Asp.Net三层)
说到敏感词,小伙伴们可能就要吐糟了吧!
刚好最近做一个电商评论模块,简单的总结一下!
由于天朝的规则比较复杂,在评论的时候要求稍微会高一些,所以评论区都要进行后台敏感词过滤。
而对于敏感词的大致分为:禁用词(BNNED)、审查词(MOD)、替换词(REPLACE)三种。
而对于这些词最令人印象深刻就是:Java/JavaScript了,这里可以猜猜为什么会把这两个词语列入敏感词的行列里呢?
恐怕也不是这么容易就能想到的吧,理由很简单-那就是因为艾微了(自行脑补吧),这不多说,说多就犯规了(毕竟我们天朝规则复杂);
下面就进入正题了:
首先接手这个项目:我先去down了网上现有的敏感词库.txt;然后写了个html界面用异步实现来向数据库添加敏感词(当然数据库是先前就建好的),写html界面的原因也是为了后续方便添加产生新的敏感词;
一、异步添加敏感词的代码:
1、DAL层(数据库层):
public int add(Sensitive_words model) { string sql = "insert into Sensitive_words(wordPattern,IsForbid,IsMod,Replace) values(@wordpattern,@isforbid,@ismod,@replace)"; SqlParameter[] parameter = { new SqlParameter("@wordpattern",Model.wordPattern), new SqlParameter("@wordpattern",Model.IsForbid), new SqlParameter("@wordpattern",Model.IsMod), new SqlParameter("@wordpattern",Model.Replace), }; return DbSqlHelper.ExecuteSql(sql, parameter); }
2、BLL层(逻辑层)
public bool Add(Sensitive_words model) { return dal.Add(model); }
1、Web层(一般处理程序)
public void ProcessRequest(HttpContext context) { string msg = context.Request["Msg"];//获取传过来的敏感词 msg = msg.Trim();//移除空白 string[] words = msg.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);//对读入的数据进行分割 BLL.Sensitive_Word bll = new BLL.Sensitive_Word(); foreach (string item in words) { string[] word = item.Split('='); Model.Sensitive_Word model = new Model.Sensitive_Word(); model.WordPattern = word[0]; //WORD = {XXX} 读入的数据格式 if (word[1]=="{BANNED}")//禁止 { model.IsForbid = true; } else if (word[1] == "{MOD}")//审查 { model.IsMod = true; } else if (word[1]=="{REPLACE}")//替换 { model.Replace = word[1]; } bll.Add(model); } }
到此完成了敏感词的数据库添加
二、对于用户的输入进行敏感词过滤
1、获取数据库相关敏感词
public class Sensitive_Word { /// <summary> /// 添加敏感词 /// </summary> /// <param name="model"></param> /// <returns></returns> public int Add(Model.Sensitive_Word model) { string sql = "insert into Sensitive_words(wordPattern,IsForbid,IsMod,ReplaceWord) values(@wordpattern,@isforbid,@ismod,@replace)"; SqlParameter[] parameter = { new SqlParameter("@wordpattern",model.WordPattern), new SqlParameter("@wordpattern",model.IsForbid), new SqlParameter("@wordpattern",model.IsMod), new SqlParameter("@wordpattern",model.ReplaceWord), }; return DbHelperSQL.ExecuteSql(sql, parameter); } /// <summary> /// 获取所有禁用词 /// </summary> /// <returns></returns> public List<string> GetBanned() { string sql = "select wordPattern form Sensitive_words where IsForbid=1"; List<string> list = null; using (SqlDataReader reader = DbHelperSQL.ExecuteReader(sql)) { if (reader.HasRows) { if (reader.Read()) { list.Add(reader.GetString(0)); } } } return list; } /// <summary> /// 获取所有审查词 /// </summary> /// <returns></returns> public List<string> GetMod() { string sql = "select wordPattern form Sensitive_words where IsMod=1"; List<string> list = null; using (SqlDataReader reader = DbHelperSQL.ExecuteReader(sql)) { if (reader.HasRows) { if (reader.Read()) { list.Add(reader.GetString(0)); } } } return list; } /// <summary> /// 获取所有替换词 /// </summary> /// <returns></returns> public List<Model.Sensitive_Word> GetReplace() { string sql = "select wordPattern,ReplaceWord form Sensitive_words where IsForbid=0 and IsMod=0"; List<Model.Sensitive_Word> list = null; using (SqlDataReader reader = DbHelperSQL.ExecuteReader(sql)) { if (reader.HasRows) { list = new List<Model.Sensitive_Word>(); if (reader.Read()) { Model.Sensitive_Word model = new Model.Sensitive_Word(); model.WordPattern = reader.GetString(0); model.ReplaceWord = reader.GetString(1); list.Add(model); } } } return list; } }
2、校验敏感词方法
我这里把对敏感词的操作封装到一个类中,当然首先你的实现添加评论的功能(代码实现类似插入敏感词操作)
Sensitive_WoedManager.cs
public class Sensitive_WordManager { DAL.Sensitive_Word dal = new DAL.Sensitive_Word(); /// <summary> /// 校验禁用词 /// </summary> /// <param name="msg"></param> /// <returns></returns> public bool CheckBanned(string msg) { //获取所有的禁用词 List<string> list = dal.GetBanned(); string regex = string.Join("|", list.ToArray());//将list中的数据的存入数组中,并在数组中以|分割元素 return Regex.IsMatch(msg, regex); } /// <summary> /// 校验审查词 /// </summary> /// <param name="msg"></param> /// <returns></returns> public bool CheckMod(string msg) { //获取所有的审查词 List<string> list = dal.GetMod(); string regex = string.Join("|", list.ToArray());//将list中的数据的存入数组中,并在数组中以|分割元素 regex.Replace(@"\", @"\\").Replace("{2}", "{0,2}");//如果审查词格式存在\,需要转义 return Regex.IsMatch(msg, regex); } /// <summary> /// 校验替换词 /// </summary> /// <param name="msg"></param> /// <returns></returns> public string CheckReplace(string msg) { //获取所有的替换词 List<Model.Sensitive_Word> list = dal.GetReplace(); foreach (Model.Sensitive_Word model in list) { msg.Replace(model.WordPattern, model.ReplaceWord);//替换词 } return msg; } }
3、校验用户名评论
public class Comment : IHttpHandler { BLL.Sensitive_WordManager sensitive = new BLL.Sensitive_WordManager(); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("Hello World"); } public void AddComment(HttpContext context) { string msg = context.Request["msg"]; if (sensitive.CheckBanned(msg))//禁用词过滤 { context.Response.Write("no:评论中含有禁用词!"); } else if (sensitive.CheckMod(msg))//审查词过滤 { context.Response.Write("no:评论中含有审查词!"); AddComment(context,msg);//审查词允许插入评论 } else//替换词过滤 { msg = sensitive.CheckReplace(msg); AddComment(context,msg);//替换以后添加到评论 } } /// <summary> /// 添加评论 /// </summary> /// <param name="context"></param> /// <param name="msg">用户评论内容</param> public void AddComment(HttpContext context,string msg) { Model.Comment comment = new Model.Comment(); BLL.CommentManager commentManager = new BLL.CommentManager(); comment.commentContent = msg; comment.productId = Convert.ToInt32(context.Request["productId"]); comment.commentTime = DateTime.Now; if (commentManager.Add(comment)) { context.Response.Write("ok"); } } public bool IsReusable { get { return false; } } }
相关推荐
taishanduba 2020-09-12
sunnyJam 2019-12-03
haohong 2019-10-29
natloc 2011-12-27
燕哥带你学算法 2019-06-29
cuiweisaidelike 2010-12-07
luokingfly 2018-09-24
crayydog 2018-10-26
郭岚 2018-07-24
展翅飞翔phpBoy00 2019-04-17