您好,欢迎来到步遥情感网。
搜索
您的当前位置:首页.NET脏字过滤算法 — IT传媒网技术文档

.NET脏字过滤算法 — IT传媒网技术文档

来源:步遥情感网

导读:
  .NET脏字过滤算法
  收藏本文
  经作者测试,此算法非常的高效
  主要算法如代码所示
  private static Dictionary dic = new Dictionary();
  private static BitArray fastcheck = new BitArray(char.MaxValue);
  static void Prepare()
  {
  string[] badwords = // read from file
  foreach (string word in badwords)
  {
  if (!dic.ContainsKey(word))
  {
  dic.Add(word, null);
  maxlength = Math.Max(maxlength, word.Length); fastcheck[word[0]] = true }
  }
  }
  使用的时候
  int index = 0;
  
  while (index < target.Length)
  
  {
  
  if (!fastcheck[target[index]])
  
  {
  
  while (index < target.Length - 1 && !fastcheck[target[++index]]) ;
  
  }
  
  
  for (int j = 0; j < Math.Min(maxlength, target.Length - index); j++)
  
  {
  
  string sub = target.Substring(index, j);
  
  if (dic.ContainsKey(sub))
  
  {
  
  sb.Replace(sub, "***", index, j);
  
  index += j;
  
  break;
  
  }
  
  }
  
  
  index++;
  
  }


以上内容属网上转载,如有侵权,请来信告知,我们将立即删除
本文转自

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- obuygou.com 版权所有 赣ICP备2024042798号-5

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务