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; ...
随机推荐
- JSP具体条款——response对象
response对象 response为响应对象client要求.输出信息到客户.他封装JSP反应生成.发送client在回应client要求. 1.重定向网页 使用response对象的sendRe ...
- C#记录日志、获取枚举值 等通用函数列表
) { ] >= && ipvals[] <= && ipval ...
- android 应用程序框架
携带Android软件开发时间,由开发商开发Android应用程序是通过应用程序框架和Android底层交互,因此,发展以达到最大的部分是应用程序框架. 应用集成框架 那里4一个重要组成部分,以下. ...
- MongoDB在window下的安装
1.下载mongodb的windows版本号,有32位和64位版本号,依据系统情况下载,下载地址:http://www.mongodb.org/downloads 2.解压缩至D:/mongodb就可 ...
- 【设计模式】Abstract Factory模式
抽象工厂模式是工厂方法模式的进一步强化.当工厂函数仅仅须要产生一种类型的产品(全部产品都继承自同一抽象基类)时,使用工厂方法模式就可以. 可是.当用户程序须要创建多种类型的产品,而这些产品又有一定的内 ...
- Android结构分析Android智能指针(两)
笔者:刘蒿羽 博客:http://blog.csdn.net/liuhaoyutz Android版本号:4.4.2 在上一篇文章中,我们分析了Android智能指针中的强指针sp,本文我们来分析弱指 ...
- CSS3 选择器读解
文章资料来自于W3Cfuns CSS3.0 四个基本的结构性伪类选择器:root 此选择器将绑定到页面的根元素中,所谓根元素,是指文档树中最顶层的元素,也就是<html>部 分. < ...
- SQL点滴32—Excel中CONCATENATE函数生成SQL语句
原文:SQL点滴32-Excel中CONCATENATE函数生成SQL语句 当拿到一个Excel的时候需要将这里面的数据插入到数据库里面,该怎么办,除了使用SSIS数据导入之外还可以使用Excel中的 ...
- 5款新颖的ReSharper插件
ReSharper是著名的代码生成工具.自ReSharper 8.0版本发布以来,新的扩展管理器.模板设置.分发安装等功能改革使得对插件的要求变高.接下来小编整理了5款新颖插件. JetBox 当开发 ...
- js获取编辑框游标的位置
代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...