在评价的时候供给某些会高一些,在言三语四的时候须要某些会高1些威尼斯人娱乐

聊到敏感词,小伙伴们恐怕就要吐糟了吗!

敏感词过滤(Asp.Net叁层),词过滤asp.net三层

谈到敏感词,小伙伴们或然就要吐糟了吧!

正要方今做3个电商评论模块,不难的下结论一下!

由于天朝的平整相比较复杂,在指指点点的时候需求有些会高壹些,所以评论区都要实行后台敏感词过滤。

而对此敏感词的光景分成:禁止使用词(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);
}

一、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);
            }
}

到此形成了敏感词的数据库添加

贰、对于用户的输入实行敏感词过滤

一、获取数据库相关敏感词

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;
        }
    }

 

二、校验敏感词方法

自作者那边把对敏感词的操作封装到3个类中,当然首先你的贯彻增进评论的机能(代码完成类似插入敏感词操作)

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;
        }
    }            

三、校验用户名评论

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;
            }
        }
    }

 

http://www.bkjia.com/Javascript/1168236.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javascript/1168236.htmlTechArticle敏感词过滤(Asp.Net三层),词过滤asp.net三层
说起敏感词,小伙伴们只怕就要吐糟了呢!
刚好近来做2个电商评论模块,简单的总括一下!…

正要近来做三个电商评论模块,简单的计算一下!

鉴于天朝的规则相比较复杂,在评论的时候供给有个别会高壹些,所以评论区都要开始展览后台敏感词过滤。

而对中国“氢弹之父”感词的光景分成:禁止使用词(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);
}

一、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);
            }
}

到此形成了敏感词的数据库添加

2、对于用户的输入实行敏感词过滤

壹、获取数据库相关敏感词

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;
        }
    }

 

二、校验敏感词方法

本身那边把对敏感词的操作封装到多少个类中,当然首先你的贯彻拉长评论的机能(代码完结类似插入敏感词操作)

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;
            }
        }
    }