import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject; public class DFA {
/**
* 根节点
*/
private static TreeNode rootNode = new TreeNode(); /**
* 创建关键词树时的临时根节点
*/
private static TreeNode rootNodeTemp = new TreeNode(); /**
* 关键词缓存
*/
private static ByteBuffer keywordBuffer = ByteBuffer.allocate(1024); /**
* 关键词编码
*/
private static String charset = "utf-8"; /**
* 关键词树是否初始化
*/
private static boolean treeIsInit = false; /**
* 创建DFA树
* @param keywordList
* @throws UnsupportedEncodingException
*/
public static void createKeywordTree(List<String> keywordList) throws UnsupportedEncodingException{
for (String keyword : keywordList) {
if(keyword == null) continue;
keyword = keyword.trim().toLowerCase();
byte[] bytes = keyword.getBytes(charset);
TreeNode tempNode = rootNodeTemp;
for (int i = 0; i < bytes.length; i++) {
int index = bytes[i] & 0xff;
TreeNode node = tempNode.getSubNode(index);
if(node == null){
node = new TreeNode();
tempNode.setSubNode(index, node);
}
tempNode = node;
if(i == bytes.length - 1){
tempNode.setKeywordEnd(true);
}
}
}
synchronized (DFA.class)
{
rootNode = rootNodeTemp;
if(!treeIsInit)treeIsInit = true;
} } /**
* 检索关键词
* @param text
* @return
* @throws UnsupportedEncodingException
*/
public static JSONObject searchKeyword(String text) throws UnsupportedEncodingException{
if(StringUtils.isEmpty(text)){
JSONObject json = new JSONObject();
try
{
json.put("islegal", "1");
json.put("deststr", "");
}
catch (JSONException e)
{
e.printStackTrace();
}
return json;
}
if(!treeIsInit)createKeywordTree();
System.out.println("searchKeyword:"+text);
return searchKeyword(text.getBytes(charset),text.toLowerCase().getBytes(charset));
} /**
* 检索关键词
* @param oldBytes
* @param bytes
* @return
*/
public static JSONObject searchKeyword(byte[] oldBytes,byte[] bytes){
String is_legal = "1";
JSONObject json = new JSONObject();
String result = null;
if(bytes == null || bytes.length == 0){
result = "";
}else{
synchronized (DFA.class)
{
TreeNode tempNode = rootNode;
int rollback = 0;
int position = 0;
while (position < bytes.length) {
int index = bytes[position] & 0xFF;
keywordBuffer.put(bytes[position]);
tempNode = tempNode.getSubNode(index);
if(tempNode == null){
position = position - rollback;
rollback = 0;
tempNode = rootNode;
keywordBuffer.clear();
}
else if(tempNode.isKeywordEnd()){
keywordBuffer.flip();
for (int i = 0; i <= rollback; i++) {
bytes[position-i] = 42;
oldBytes[position-i] = 42;
}
keywordBuffer.limit(keywordBuffer.capacity());
rollback = 1;
is_legal = "0";
}else{
rollback++;
}
position++;
}
}
try {
result = new String(oldBytes,charset); } catch (Exception e) {
e.printStackTrace();
} }
try
{
json.put("islegal", is_legal);
json.put("deststr", result);
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
} public void setCharset(String charset) {
this.charset = charset;
} public static void main(String[] args){
try{
List<String> keys = new ArrayList<String>();
keys.add("TMD");
keys.add("변형");
String text ="你真TMD是个변형啊!";
DFA.createKeywordTree(keys);
JSONObject a = DFA.searchKeyword(text);
System.out.println(a);
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
import java.util.ArrayList;
import java.util.List; public class TreeNode {
private static final int NODE_LEN = 256; /**
* true 关键词的终结 ; false 继续
*/
private boolean end = false; private List<TreeNode> subNodes = new ArrayList<TreeNode>(NODE_LEN); public TreeNode(){
for (int i = 0; i < NODE_LEN; i++) {
subNodes.add(i, null);
}
} /**
* 向指定位置添加节点树
* @param index
* @param node
*/
public void setSubNode(int index, TreeNode node){
subNodes.set(index, node);
} public TreeNode getSubNode(int index){
//System.out.println("index:"+index+" node:"+this+"--subNodes:"+subNodes.get(index));
return subNodes.get(index);
} public boolean isKeywordEnd() {
return end;
} public void setKeywordEnd(boolean end) {
this.end = end;
}
}

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

  1. DFA敏感词过滤实现

    package test.java.com.odianyun.util.sensi; import java.util.*; /** * 敏感词处理工具 - DFA算法实现 * * @author s ...

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

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

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

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

  4. Java实现敏感词过滤 - DFA算法

    Java实现DFA算法进行敏感词过滤 封装工具类如下: 使用前需对敏感词库进行初始化: SensitiveWordUtil.init(sensitiveWordSet); package cn.swf ...

  5. 敏感词过滤的算法原理之DFA算法

    参考文档 http://blog.csdn.net/chenssy/article/details/26961957 敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有 ...

  6. 基于DFA算法、RegExp对象和vee-validate实现前端敏感词过滤

    面临敏感词过滤的问题,最简单的方案就是对要检测的文本,遍历所有敏感词,逐个检测输入的文本是否包含指定的敏感词. 很明显上面这种实现方法的检测时间会随着敏感词库数量的增加而线性增加.系统会因此面临性能和 ...

  7. Java实现敏感词过滤

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

  8. java敏感词过滤

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

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

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

随机推荐

  1. LA 4255 UVa1423 拓扑排序

    题目给出的是Sij的正负号,Sij=ai+...+aj,所以令前缀和Bi=a0+a1+..+ai,a0=0,B0=0,则有Sij=Bj-B(i-1): 由此构造出Bi的拓扑序列,只要每个拓扑序列相邻的 ...

  2. win2008使用FireDac连接ORACLE数据库问题

    2008上装DELPHI XE7,无论用FireDac 还是Ado都连不上ORACLE数据库 --------------------------- Debugger Exception Notifi ...

  3. Application Pool

  4. iOS 里面 Swift与Objective-C混编,Swift与C++混编的一些比较

        即使你尽量用Swift编写iOS程序,难免会遇到部分算法是用C++语言编写的.那你只能去问问”度娘“或“狗哥”怎么用Swift调用C++算法.   一,C,C++, Objective-C,S ...

  5. IOS修改webView背景透明以及IOS调用前台js的方法

    工作上遇到IOS的webView中的H5页面需要透明以显示webView的背景颜色.用H5自身的透明度的css样式或者js控制背景颜色及透明度都打不到想要的效果,最后还是通过ios设置webView中 ...

  6. VS2013 添加已有文件夹

    1.将现有项目或文件夹拷贝到指定目录下 2.解决方案右上有个显示所有文件的按钮 然后选中所有要添加的文件,右击

  7. IOS UIImageView的contentMode属性

    红框表示imageView的frame,下面的图片是原图大小UIViewContentModeScaleToFill,       默认,对图片进行拉伸处理(不是按比例),是充满bouns UIVie ...

  8. PHP Problem with the SSL CA cert (path? access rights?)

    1.php使用curl模块报错问题 开发遇到问题,直接使用系统的curl命令正常,使用php的curl模块报错 错误:PHP Problem with the SSL CA cert (path? a ...

  9. laravel框架总结(十) -- 返回值

    以前用CI框架对于返回值没有过多关注,但是发现使用laravel框架的时候出现了一些小问题,特意实践总结了一些常用情形,希望对大家有所帮助   先理解几个概念: 1>StdClass 对象=&g ...

  10. 2016年12月28日 星期三 --出埃及记 Exodus 21:23

    2016年12月28日 星期三 --出埃及记 Exodus 21:23 But if there is serious injury, you are to take life for life,若有 ...