高效.NET脏字过滤算法与应用实例

 更新时间:2016年08月04日 10:46:36   作者:Wilson  
这篇文章主要介绍了高效.NET脏字过滤算法与应用方法,结合实例形式分析了.NET字符串过滤操作相关技巧,需要的朋友可以参考下

本文实例讲述了高效.NET脏字过滤算法。分享给大家供大家参考,具体如下:

BadWordsFilter.cs类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data;
namespace WNF
{
  public class BadWordsFilter
  {
    private HashSet<string> hash = new HashSet<string>(); //关键字
    private byte[] fastCheck = new byte[char.MaxValue];
    private byte[] fastLength = new byte[char.MaxValue];
    private BitArray charCheck = new BitArray(char.MaxValue);
    private BitArray endCheck = new BitArray(char.MaxValue);
    private int maxWordLength = 0;
    private int minWordLength = int.MaxValue;
    public BadWordsFilter()
    {
    }
    //初始化关键字
    public void Init(DataTable badwords)
    {
      for (int j = 0; j < badwords.Rows.Count; j++)
      {
        string word = badwords.Rows[j][0].ToString();
        maxWordLength = Math.Max(maxWordLength, word.Length);
        minWordLength = Math.Min(minWordLength, word.Length);
        for (int i = 0; i < 7 && i < word.Length; i++)
        {
          fastCheck[word[i]] |= (byte)(1 << i);
        }
        for (int i = 7; i < word.Length; i++)
        {
          fastCheck[word[i]] |= 0x80;
        }
        if (word.Length == 1)
        {
          charCheck[word[0]] = true;
        }
        else
        {
          fastLength[word[0]] |= (byte)(1 << (Math.Min(7, word.Length - 2)));
          endCheck[word[word.Length - 1]] = true;
          hash.Add(word);
        }
      }
    }
    public string Filter(string text, string mask)
    {
      throw new NotImplementedException();
    }
    //检查是否有关键字
    public bool HasBadWord(string text)
    {
      int index = 0;
      while (index < text.Length)
      {
        int count = 1;
        if (index > 0 || (fastCheck[text[index]] & 1) == 0)
        {
          while (index < text.Length - 1 && (fastCheck[text[++index]] & 1) == 0) ;
        }
        char begin = text[index];
        if (minWordLength == 1 && charCheck[begin])
        {
          return true;
        }
        for (int j = 1; j <= Math.Min(maxWordLength, text.Length - index - 1); j++)
        {
          char current = text[index + j];
          if ((fastCheck[current] & 1) == 0)
          {
            ++count;
          }
          if ((fastCheck[current] & (1 << Math.Min(j, 7))) == 0)
          {
            break;
          }
          if (j + 1 >= minWordLength)
          {
            if ((fastLength[begin] & (1 << Math.Min(j - 1, 7))) > 0 && endCheck[current])
            {
              string sub = text.Substring(index, j + 1);
              if (hash.Contains(sub))
              {
                return true;
              }
            }
          }
        }
        index += count;
      }
      return false;
    }
  }
}

引用:

string sql = "select keywords from tb_keyword";
BadWordsFilter badwordfilter = new BadWordsFilter();
//初始化关键字
badwordfilter.Init(oEtb.GetDataSet(sql).Tables[0]);
//检查是否有存在关键字
bool a = badwordfilter.HasBadWord(TextBox1.Text);
if (a == true)
{
    Page.RegisterClientScriptBlock("a", "<script>alert('该评论含有不合法文字!')</script>");
}
else
{
    PingLun();//写入评论表
}

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net字符串操作技巧汇总》、《asp.net操作json技巧总结》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

相关文章

  • asp.net“服务器应用程序不可用” 解决方法

    asp.net“服务器应用程序不可用” 解决方法

    服务器应用程序不可用 您试图在此 Web 服务器上访问的 Web 应用程序当前不可用。请点击 Web 浏览器中的“刷新”按钮重试您的请求。 管理员注意事项: 详述此特定请求失败原因的错误消息可在 Web 服务器的系统事件日志中找到。请检查此日志项以查明导致该错误发生的原因。
    2008-10-10
  • ASP.NET MVC 项目直接预览PDF文件

    ASP.NET MVC 项目直接预览PDF文件

    本文主要介绍了ASP.NET MVC项目实现直接预览PDF文件的方法,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • ASP.NET微信开发(接口指南)

    ASP.NET微信开发(接口指南)

    这篇文章详细介绍了ASP.NET微信开发接口指南,微信公众平台的开发较为简单,感兴趣的小伙伴们可以参考一下
    2015-12-12
  • .NET医院公众号系统线程CPU双高问题分析

    .NET医院公众号系统线程CPU双高问题分析

    这篇文章主要介绍了.NET医院公众号系统 线程CPU双高分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • 使用aspnet_regiis.exe重新注册.NET Framework

    使用aspnet_regiis.exe重新注册.NET Framework

    本文主要介绍使用aspnet_regiis.exe重新注册.NET Framework的方法,简单实用,有需要的朋友拿去用吧。
    2016-05-05
  • c# 读取Northwind数据库image字段

    c# 读取Northwind数据库image字段

    我在写一个三层结构Demo时,使用了Northwind这个范例数据库。但是奇怪的是,读取Categories表的Picture列(image类型)无法在image控件中正常显示(解决方案在后面代码中可以看到)。
    2009-03-03
  • ASP.NET笔记之 Repeater的使用

    ASP.NET笔记之 Repeater的使用

    本篇文章小编为大家介绍,ASP.NET笔记之 Repeater的使用。需要的朋友参考下
    2013-04-04
  • 使用CefSharp在.Net程序中嵌入Chrome浏览器

    使用CefSharp在.Net程序中嵌入Chrome浏览器

    这篇文章介绍了使用CefSharp在.Net程序中嵌入Chrome浏览器的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • .net微信服务号发送红包

    .net微信服务号发送红包

    这篇文章主要为大家详细介绍了.net微信服务号发送红包的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • WPF图片按钮的实现方法

    WPF图片按钮的实现方法

    这篇文章主要为大家详细介绍了WPF图片按钮的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10

最新评论