欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-09 23:13:57
本文实例讲述了高效.net脏字过滤算法。分享给大家供大家参考,具体如下: badwordsfilter.cs类 using system; using sys...

本文实例讲述了高效.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程序设计有所帮助。