public class BadWordFilter

{

#region 变量

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;

private string _replaceString = "*";

private string _newWord;

#endregion

#region 单例模式创建实例

private static BadWordFilter badWordFilter = null;

/// <summary>

/// 构造函数

/// </summary>

private BadWordFilter() { }

/// <summary>

/// 单例

/// </summary>

/// <returns></returns>

public static BadWordFilter CreateBadWordsFilter()

{

if (badWordFilter == null)

{

badWordFilter = new BadWordFilter();

}

return badWordFilter;

}

#endregion

#region 初始化数据,将List集合类型敏感词放入HashSet中

/// <summary>

/// 初始化数据,将敏感词放入HashSet中

/// </summary>

/// <param name="badwords"></param>

public void Init(List<BadWordEntity> badwords)

{

foreach (BadWordEntity word in badwords)

{

maxWordLength = Math.Max(maxWordLength, word.BadWord.Length);

minWordLength = Math.Min(minWordLength, word.BadWord.Length);

for (int i = 0; i < 7 && i < word.BadWord.Length; i++)

{

fastCheck[word.BadWord[i]] |= (byte)(1 << i);

}

for (int i = 7; i < word.BadWord.Length; i++)

{

fastCheck[word.BadWord[i]] |= 0x80;

}

if (word.BadWord.Length == 1)

{

charCheck[word.BadWord[0]] = true;

}

else

{

fastLength[word.BadWord[0]] |= (byte)(1 << (Math.Min(7, word.BadWord.Length - 2)));

endCheck[word.BadWord[word.BadWord.Length - 1]] = true;

hash.Add(word.BadWord);

}

}

}

#endregion

#region 初始化数据,将String[]类型敏感词放入HashSet中

/// <summary>

/// 初始化数据,将敏感词放入HashSet中

/// </summary>

/// <param name="badwords"></param>

private void Init(string[] badwords)

{

foreach (string word in badwords)

{

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

}

}

}

#endregion

#region 检查是否有敏感词

/// <summary>

/// 检查是否有敏感词

/// </summary>

/// <param name="text"></param>

/// <returns></returns>

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;

}

#endregion

#region 替换敏感词

/// <summary>

/// 替换敏感词

/// </summary>

/// <param name="text"></param>

/// <returns></returns>

public string ReplaceBadWord(string text)

{

int index = 0;

for (index = 0; index < text.Length; index++)

{

if ((fastCheck[text[index]] & 1) == 0)

{

while (index < text.Length - 1 && (fastCheck[text[++index]] & 1) == 0) ;

}

//单字节检测

if (minWordLength == 1 && charCheck[text[index]])

{

text = text.Replace(text[index], _replaceString[0]);

continue;

}

//多字节检测

for (int j = 1; j <= Math.Min(maxWordLength, text.Length - index - 1); j++)

{

//快速排除

if ((fastCheck[text[index + j]] & (1 << Math.Min(j, 7))) == 0)

{

break;

}

if (j + 1 >= minWordLength)

{

string sub = text.Substring(index, j + 1);

if (hash.Contains(sub))

{

//替换字符操作

char cc = _replaceString[0];

string rp = _replaceString.PadRight((j + 1), cc);

text = text.Replace(sub, rp);

//记录新位置

index += j;

break;

}

}

}

}

_newWord = text;

return text;

}

#endregion

}

#region 敏感词实体类

/// <summary>

/// 敏感词实体

/// </summary>

public class BadWordEntity

{

/// <summary>

/// 敏感词

/// </summary>

public string BadWord { get; set; }

}

#endregion

C# 敏感词过滤的更多相关文章

  1. java实现敏感词过滤(DFA算法)

    小Alan在最近的开发中遇到了敏感词过滤,便去网上查阅了很多敏感词过滤的资料,在这里也和大家分享一下自己的理解. 敏感词过滤应该是不用给大家过多的解释吧?讲白了就是你在项目中输入某些字(比如输入xxo ...

  2. 用php实现一个敏感词过滤功能

    周末空余时间撸了一个敏感词过滤功能,下边记录下实现过程. 敏感词,一方面是你懂的,另一方面是我们自己可能也要过滤一些人身攻击或者广告信息等,具体词库可以google下,有很多. 过滤敏感词,使用简单的 ...

  3. 浅析敏感词过滤算法(C++)

    为了提高查找效率,这里将敏感词用树形结构存储,每个节点有一个map成员,其映射关系为一个string对应一个TreeNode. STL::map是按照operator<比较判断元素是否相同,以及 ...

  4. Java实现敏感词过滤

    敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有必要的.前段时间我一个朋友(马上毕业,接触编程不久)要我帮他看一个文字过滤的东西,它说检索效率非常慢.我把它程序拿过来 ...

  5. php敏感词过滤

    在项目开发中发现有个同事在做敏感词过滤的时候用循环在判断,其实是不用这样做的,用php的数组函数和字符串函数即可实现 function filterNGWords($string) { $badwor ...

  6. 转:鏖战双十一-阿里直播平台面临的技术挑战(webSocket, 敏感词过滤等很不错)

    转自:http://www.infoq.com/cn/articles/alibaba-broadcast-platform-technology-challenges 鏖战双十一-阿里直播平台面临的 ...

  7. java敏感词过滤

    敏感词过滤在网站开发必不可少.一般用DFA,这种比较好的算法实现的. 参考链接:http://cmsblogs.com/?p=1031 一个比较好的代码实现: import java.io.IOExc ...

  8. Java实现敏感词过滤(转)

    敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有必要的.前段时间我一个朋友(马上毕业,接触编程不久)要我帮他看一个文字过滤的东西,它说检索效率非常慢.我把它程序拿过来 ...

  9. DFA和trie特里实现敏感词过滤(python和c语言)

    今天的项目是与完成python开展,需要使用做关键词检查,筛选分类,使用前c语言做这种事情.有了线索,非常高效,内存小了,检查快. 到达python在,第一个想法是pip基于外观的c语言python特 ...

  10. Jsp敏感词过滤

    Jsp敏感词过滤 大部分论坛.网站等,为了方便管理,都进行了关于敏感词的设定. 在多数网站,敏感词一般是指带有敏感政治倾向(或反执政党倾向).暴力倾向.不健康色彩的词或不文明语,也有一些网站根据自身实 ...

随机推荐

  1. linux网络原理

    1.ipconfig命令使用 显示所有正在启动的网卡的详细信息或设定系统中网卡的IP地址. 某一块网卡信息 打开或者关闭某一块网卡 2.ifup和ifdown ifup和ifdown分别是加载网卡信息 ...

  2. 《算法导论》 — Chapter 7 快速排序

    序 快速排序(QuickSort)也是一种排序算法,对包含n个数组的输入数组,最坏情况运行时间为O(n^2).虽然这个最坏情况运行时间比较差,但是快速排序通常是用于排序的最佳实用选择,这是因为其平均性 ...

  3. HDU 2462 The Luckiest number

    The Luckiest number Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Ori ...

  4. 从零到一,使用实时音视频 SDK 一起开发一款 Zoom 吧

    zoom(zoom.us) 是一款受到广泛使用的在线会议软件.相信各位一定在办公.会议.聊天等各种场景下体验或者使用过,作为一款成熟的商业软件,zoom 提供了稳定的实时音视频通话质量,以及白板.聊天 ...

  5. SpringMVC(8) - 处理器映射

    在以前的Spring版本中,用户需要在Web应用程序上下文中定义一个或多个HandlerMapping bean,以将传入的Web请求映射到适当的处理器.通过引入带注解的控制器,就不需要像之前那样定义 ...

  6. CSU 1605 数独

    题目大意: 9宫格每个位置都有对应的分数,填完数独后根据对应位置的分数相加之和求个最大值,不存在输出-1 说什么用位运算加速可以解决问题,但是对着标程还是T,最近学了dlx,发现这样解决数独快了很多 ...

  7. 【Floyd最短路】第七届福建省赛 FZU Problem 2271 X

    http://acm.fzu.edu.cn/problem.php?pid=2271 [题意] 给定一个n个点和m条边的无向连通图,问最多可以删去多少条边,使得每两个点之间的距离(最短路长度)不变. ...

  8. 跳石头(codevs 4768)

    题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有N ...

  9. 6572平台上关于wifi热点切换跳的坑

    最近在做一个无屏的项目,需要开启设备的wifi热点,通过连接热点设置设备wifi,本来看起来很容易完成的一件事情,遇到了一下的坑 在wifi切换状态时,大概率出现不能切换的问题,比如从wifi状态切换 ...

  10. linux命令2——进程相关

    (1)ps  -ef :可以看到内核的线程.