public class CheckStreamReader
{
//使用的数据:
private static HashSet<string> hash = new HashSet<string>();
private byte[] fastCheck = new byte[char.MaxValue];
private BitArray charCheck = new BitArray(char.MaxValue);
private int maxWordLength = 0;
private int minWordLength = int.MaxValue;
private static string[] badwords = { }; public CheckStreamReader()
{
if (hash == null || hash.Count <= 0)
{
//添加敏感词
string path = HttpContext.Current.Server.MapPath("~/config") + "/StreamReader.txt";
StreamReader sr = new StreamReader(path, Encoding.GetEncoding("utf-8"));
string strText = sr.ReadToEnd();
badwords = strText.Split('|');
InitializationText();
}
} //初始化数据的代码:将敏感词加入的hash表中
private void InitializationText()
{
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
{
hash.Add(word);
}
}
} //判断是否包含脏字的代码:
public bool HasBadWord(string text)
{
if (hash == null || hash.Count<=0)
{
string path = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["ConfigPath"]) + "/StreamReader.txt";
StreamReader sr = new StreamReader(path, Encoding.GetEncoding("utf-8"));
string strText = sr.ReadToEnd();
badwords = strText.Split('|');
InitializationText();
} int index = 0; while (index < text.Length)
{
if ((fastCheck[text[index]] & 1) == 0)
{
while (index < text.Length - 1 && (fastCheck[text[++index]] & 1) == 0) ;
} if (minWordLength == 1 && charCheck[text[index]])
{
return true;
} 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))
{
return true;
}
}
} index++;
} return false;
}
}

  

C# 添加敏感词的更多相关文章

  1. java敏感词过滤

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

  2. (转)两种高效过滤敏感词算法--DFA算法和AC自动机算法

    原文:https://blog.csdn.net/u013421629/article/details/83178970 一道bat面试题:快速替换10亿条标题中的5万个敏感词,有哪些解决思路? 有十 ...

  3. 建站时注意敏感词的添加_seo优化禁忌

    之前接手一个站点,网站标题中出现一个“三级医院”的词,虽然这个词在我们看来是没有问题的,医院评级中“三级医院”算是等级很高的,很多医院为了体现等级会在明显的地方着重加注.但是我们要考虑一下搜索引擎的分 ...

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

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

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

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

  6. ckeditor 敏感词标记显示处理方法

    直接在原型添加方法: (function () { /* * 取消所有高亮 */ CKEDITOR.editor.prototype.CancleSensitiveWordsHighlight = f ...

  7. Android敏感词过滤主要类

    package com.tradeaider.app.utils; import com.tradeaider.app.activity.MyApplication;import java.util. ...

  8. 使用DFA算法对敏感词进行过滤

    项目目录结构如下: 其中resources资源目录中: stopwd.txt :停顿词,匹配时间直接过滤. wd.txt:敏感词库. 1.WordFilter敏感词过滤类: package com.s ...

  9. java实现文章敏感词过滤检测

    SensitivewordFilter.java import java.util.HashSet; import java.util.Iterator; import java.util.Map; ...

随机推荐

  1. Net下无敌的ORM

    Dapper ORM 用法—Net下无敌的ORM http://www.renfb.com/blog/2011/Article/335 假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上 ...

  2. sql事务,在sql2000里判断执行是否成功用@@ERROR 判断

    原文:sql事务,在sql2000里判断执行是否成功用@@ERROR 判断 贴个sql事务,在sql2000里判断执行是否成功用@@ERROR 判断 这个东西多少还是有点问题,sql2005了可以用t ...

  3. 使用Vs2005打造简单分页浏览器(1)原创

    原文:使用Vs2005打造简单分页浏览器(1)原创 使用Vs2005打造简单分页浏览器(1)原创1引言2功能3实现过程以及关键点4总结5不足之处6其他7 代码下载 1    引言很早就有搞一个浏览器的 ...

  4. 修改 dll

    由于没有源码,想改dll,就要依靠反汇编了. 输入 ildasm.exe 据说也可以直接 C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin 找到该软件 ...

  5. Modernizr

    Modernizr 目录 概述 CSS的新增class JavaScript侦测 加载器 参考链接 概述 随着HTML5和CSS3加入越来越多的模块,检查各种浏览器是否支持这些模块,成了一大难题.Mo ...

  6. POJ 3071-Football(可能性dp)

    Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3145   Accepted: 1591 Descript ...

  7. bzoj 1799: [Ahoi2009]self 类似的分布 解读

    [原标题] 1799: [Ahoi2009]self 同类分布 Time Limit: 50 Sec  Memory Limit: 64 MB Submit: 554  Solved: 194 [id ...

  8. C语言移位运算

    移位运算有两种:>>(右移),<<(左移). a>>b表示将a的二进制值右移b位. a<<b 表示将a的二进制值左移 b位.要求 a和 b都是整型, b ...

  9. C# & WPF 随手小记之一 ——初探async await 实现多线程处理

    嗯...我也是在园子待了不短时间的人了,一直以来汲取着园友的知识,感觉需要回馈什么. 于是以后有空我都会把一些小技巧小知识写下来,有时候可能会很短甚至很简单,但希望能帮到大家咯. 第一篇文章来说说as ...

  10. 安装Visual Studio 2010 - 初学者系列 - 学习者系列文章

    本文讲述如何安装Visual Studio 2010开发工具. 首先,通过下列地址获取Visual Studio 2010的副本 1.开始页面 2.欢迎页 3.这里选择 自定义 ,选择安装路径 4.这 ...