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. 条款27:尽量少做转型动作(Minimize casting)

    NOTE : 1.如果可以,尽量避免转型,特别是在注重效率的代码中避免dynamic_casts. 如果有个设计需要转型动作,试着发展无需转型的替代设计. 2.如果转型是必须要的,试着将它隐藏于某个函 ...

  2. Python 函数的初识

    1.函数的初识 函数的作用:以功能为导向 减少代码重复 # 函数试编程 # 函数以功能(完成一件事)为导向,登录 注册, # 一个函数就是一个功能,一个函数只能写一个功能 # 何时需要 何时调用,随调 ...

  3. pwnable.kr cmd1之write up

    看一下源代码: #include <stdio.h> #include <string.h> int filter(char* cmd){ ; r += strstr(cmd, ...

  4. 算法导论 第六章 2 优先队列(python)

    优先队列:     物理结构: 顺序表(典型的是数组){python用到list}     逻辑结构:似完全二叉树 使用的特点是:动态的排序..排序的元素会增加,减少#和快速排序对比 快速一次排完 增 ...

  5. spring源码深度解析—Spring的整体架构和环境搭建

    概述 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的Java 开发框 ...

  6. INFO main org.springframework.context.support.AbstractApplicationContext

    原因, spring-framework-5.0.2.RELEASE  需要使用 jdk8.

  7. 什么是TLS?

    最近在Istio实验中经常遇到HTTP,HTTPS,TLS等名词,感觉忘得差不多,需要复习一下计算机网络的知识了. 本文参考   http://www.techug.com/post/https-ss ...

  8. hdu 1824 2-sat问题(判断)

    /* 题意:u,v,w队长,队员,队长留下两个队员可以回家,两个队员留下,队长回家 2-sat问题,把两个队员看成一个整体就变成一个简单2-sat问题了 */ #include<stdio.h& ...

  9. captcha库报错"OSError: cannot open resource"

    问题描述 在win平台上python虚拟环境下使用captcha库生成验证码报错OSError: cannot open resource 代码 from captcha.image import I ...

  10. 《TCP/IP详解卷1:协议》——第3章 IP:网际协议(转载)

    1.引言 IP是TCP/IP协议族中最核心的协议.所有的TCP.UDP.ICMP及IGMP数据都以IP数据报格式传输.IP提供不可靠. 无连接的数据报传送服务. (1)不可靠 它不能保证IP数据报能成 ...