C# 添加敏感词
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# 添加敏感词的更多相关文章
- java敏感词过滤
敏感词过滤在网站开发必不可少.一般用DFA,这种比较好的算法实现的. 参考链接:http://cmsblogs.com/?p=1031 一个比较好的代码实现: import java.io.IOExc ...
- (转)两种高效过滤敏感词算法--DFA算法和AC自动机算法
原文:https://blog.csdn.net/u013421629/article/details/83178970 一道bat面试题:快速替换10亿条标题中的5万个敏感词,有哪些解决思路? 有十 ...
- 建站时注意敏感词的添加_seo优化禁忌
之前接手一个站点,网站标题中出现一个“三级医院”的词,虽然这个词在我们看来是没有问题的,医院评级中“三级医院”算是等级很高的,很多医院为了体现等级会在明显的地方着重加注.但是我们要考虑一下搜索引擎的分 ...
- 浅析敏感词过滤算法(C++)
为了提高查找效率,这里将敏感词用树形结构存储,每个节点有一个map成员,其映射关系为一个string对应一个TreeNode. STL::map是按照operator<比较判断元素是否相同,以及 ...
- 转:鏖战双十一-阿里直播平台面临的技术挑战(webSocket, 敏感词过滤等很不错)
转自:http://www.infoq.com/cn/articles/alibaba-broadcast-platform-technology-challenges 鏖战双十一-阿里直播平台面临的 ...
- ckeditor 敏感词标记显示处理方法
直接在原型添加方法: (function () { /* * 取消所有高亮 */ CKEDITOR.editor.prototype.CancleSensitiveWordsHighlight = f ...
- Android敏感词过滤主要类
package com.tradeaider.app.utils; import com.tradeaider.app.activity.MyApplication;import java.util. ...
- 使用DFA算法对敏感词进行过滤
项目目录结构如下: 其中resources资源目录中: stopwd.txt :停顿词,匹配时间直接过滤. wd.txt:敏感词库. 1.WordFilter敏感词过滤类: package com.s ...
- java实现文章敏感词过滤检测
SensitivewordFilter.java import java.util.HashSet; import java.util.Iterator; import java.util.Map; ...
随机推荐
- linux-ubuntu关闭防火墙
SYNOPSIS iptables [-t table] {-A|-C|-D} chain rule-specification ip6tables [-t table] {-A|-C|-D} cha ...
- Advance Installer安装问题
一,在Advance Installer中注冊dll 1,首先将文件加入到Files And Folders中.此处以InstallValidate.dll为例. 2,在Custom Action处进 ...
- [Oracle] Insert All神奇
无条件插入 Oracle中间insert all它指的是相同的数据组成不同的表.如果有需求现在:该t插入数据表t1,t2,假设你不知道insert all.您可以使用insert插入2次要,例如,见下 ...
- Forms身份验证和基于Role的权限验证
Forms身份验证和基于Role的权限验证 从Membership到SimpleMembership再到ASP.NET Identity,ASP.NET每一次更换身份验证的组件,都让我更失望.Memb ...
- 【百度地图API】——国内首款团购网站的地图插件
原文:[百度地图API]--国内首款团购网站的地图插件 摘要: 本文介绍了一款应用在团购网站上的地图插件,适用于目前非常流行的团购网站.使用这款地图插件,无需任何编程技术,你就把商家的位置轻松地标注在 ...
- 项目管理实践 -- 健身小管家(Fitness housekeeper)的管理(4)
提前几天把检查更新的功能完成了.
- iOS--Swift开发中的单例设计模式
最近在开发一个小的应用,遇到了一些Objective-c上面常用的单例模式,但是swift上面还是有一定区别的,反复倒来倒去发现不能按常理(正常的oc to swift的方式)出牌,因此搜索了一些帖子 ...
- c语言发挥帕斯卡三角
我们已经确定了帕斯卡三角的规则,下面是我的代码,非常实用哦! !! #include<stdio.h> void main() { int i,j,n,k; sca ...
- android意图传递参数返回结果(六)
例如,有两页a,b. a参数传递到页面b页面,b后,将获得的参数的处理页,然后将结果传回与参数的页面a. 1.a主页MainActivity的代码类型,如以下: private Button butt ...
- 自动编译CoffeeScript的Gruntfile.js
比如把coffee文件写在coffee/controller/文件夹下,新建js/controller文件夹,使用grunt运行项目,将自动编译coffee到相应的js文件夹下. module.exp ...