当前位置:Gxlcms > asp.net > 高效.NET脏字过滤算法与应用实例

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

时间:2021-07-01 10:21:17 帮助过:8人阅读

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

BadWordsFilter.cs类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Collections;
  6. using System.Data;
  7. namespace WNF
  8. {
  9. public class BadWordsFilter
  10. {
  11. private HashSet<string> hash = new HashSet<string>(); //关键字
  12. private byte[] fastCheck = new byte[char.MaxValue];
  13. private byte[] fastLength = new byte[char.MaxValue];
  14. private BitArray charCheck = new BitArray(char.MaxValue);
  15. private BitArray endCheck = new BitArray(char.MaxValue);
  16. private int maxWordLength = 0;
  17. private int minWordLength = int.MaxValue;
  18. public BadWordsFilter()
  19. {
  20. }
  21. //初始化关键字
  22. public void Init(DataTable badwords)
  23. {
  24. for (int j = 0; j < badwords.Rows.Count; j++)
  25. {
  26. string word = badwords.Rows[j][0].ToString();
  27. maxWordLength = Math.Max(maxWordLength, word.Length);
  28. minWordLength = Math.Min(minWordLength, word.Length);
  29. for (int i = 0; i < 7 && i < word.Length; i++)
  30. {
  31. fastCheck[word[i]] |= (byte)(1 << i);
  32. }
  33. for (int i = 7; i < word.Length; i++)
  34. {
  35. fastCheck[word[i]] |= 0x80;
  36. }
  37. if (word.Length == 1)
  38. {
  39. charCheck[word[0]] = true;
  40. }
  41. else
  42. {
  43. fastLength[word[0]] |= (byte)(1 << (Math.Min(7, word.Length - 2)));
  44. endCheck[word[word.Length - 1]] = true;
  45. hash.Add(word);
  46. }
  47. }
  48. }
  49. public string Filter(string text, string mask)
  50. {
  51. throw new NotImplementedException();
  52. }
  53. //检查是否有关键字
  54. public bool HasBadWord(string text)
  55. {
  56. int index = 0;
  57. while (index < text.Length)
  58. {
  59. int count = 1;
  60. if (index > 0 || (fastCheck[text[index]] & 1) == 0)
  61. {
  62. while (index < text.Length - 1 && (fastCheck[text[++index]] & 1) == 0) ;
  63. }
  64. char begin = text[index];
  65. if (minWordLength == 1 && charCheck[begin])
  66. {
  67. return true;
  68. }
  69. for (int j = 1; j <= Math.Min(maxWordLength, text.Length - index - 1); j++)
  70. {
  71. char current = text[index + j];
  72. if ((fastCheck[current] & 1) == 0)
  73. {
  74. ++count;
  75. }
  76. if ((fastCheck[current] & (1 << Math.Min(j, 7))) == 0)
  77. {
  78. break;
  79. }
  80. if (j + 1 >= minWordLength)
  81. {
  82. if ((fastLength[begin] & (1 << Math.Min(j - 1, 7))) > 0 && endCheck[current])
  83. {
  84. string sub = text.Substring(index, j + 1);
  85. if (hash.Contains(sub))
  86. {
  87. return true;
  88. }
  89. }
  90. }
  91. }
  92. index += count;
  93. }
  94. return false;
  95. }
  96. }
  97. }

引用:

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

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

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

人气教程排行