谁有做敏感词过滤的经验,求高手进来 这可以用AC多模匹配算法。LookThis: https://hkn.eecs.berkeley.edu/~dyoo/java/index.html 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 AhoCorasick tree = new AhoCorasick(); tree.add("hello".getBytes(), "hello"); tree.add("world".getBytes(), "world"); tree.prepare(); Iterator searcher = tree.search("hello world".getBytes()); while (searcher.hasNext()) { SearchResult result = searcher.next(); System.out.println(result.getOutputs()); System.out.println("Found at index: " + result.getLastIndex()); } 这个在系统中应该是经常要用到的吧。如果是,就应该把这些词全部读出来,用个静态数组保存在内存中我用initSensitives()方法模拟创建了一些敏感词,你可以改一下把你的文件里面的敏感词都读出来,保存在sensitives数组里面。package com.zf.test; public class TestSearch { String str ; //要检验的字符串 String[] sensitives ; //系统设置的敏感词 final int threadSize = 10 ; //总共线程数量 Integer completeSize = 0 ; //已经完成的线程数量 boolean tmp = false; //记录字符串是否属于敏感词 Object completeLock = new Object(); //同步锁辅助对象 public TestSearch(String str){ this.str = str ; initSensitives(); } //创建敏感词 public String[] initSensitives(){ sensitives = new String[3000]; for (int i = 0; i < 3000 ; i++) { sensitives[i] = "敏感词" + (i + 1); } return sensitives; } //检查词是否铭感 public boolean checkStr() throws Exception{ int everyLength = sensitives.length / threadSize; //每条线程匹配的长度 for (int i = 0; i < threadSize ; i++) { int start = i * everyLength ; int end = start + everyLength ; if(end > sensitives.length) end = sensitives.length ; new CheckThread(start , end).start(); } synchronized (completeLock) { while(completeSize < threadSize){ //保证线程都执行完了 completeLock.wait(); } } return tmp; } //线程判断字符是否属于敏感词 class CheckThread extends Thread{ int startIndex ; int endIndex ; public CheckThread(int startIndex , int endIndex ){ this.startIndex = startIndex ; this.endIndex = endIndex; } public void run() { for (int i = startIndex; i < endIndex && !tmp; i++) { if(str.matches(".*"+ sensitives[i] +".*")){ tmp = true ; } } synchronized (completeLock) { completeSize++; completeLock.notifyAll(); System.out.println("线程" + Thread.currentThread().getName() + "执行完毕"); } } } public static void main(String[] args) throws Exception { TestSearch ts = new TestSearch("敏感词4"); boolean result = ts.checkStr(); System.out.println(result); } } http://download.csdn.net/detail/javaloverkehui/3849901 我写的,结合楼上这逼的线程,你可以结合起来修改.修改好了记得上传,把链接发我哦,亲. 发现两个bugbug1:如果是 "去去你ccc妈" 将得到 *去cc**bug2: 如果是 "连续重复的骂人话只过滤掉第一个" 你不信自己试试测试的时候输入:你大爷的!!!你大爷的 得到的结果是:****!!!!你大爷的在楼上多线程例子和你的代码基础上进行整合并修改了上面的两个bug 得到了很高效的敏感词过滤类 分享给大家 http://dl.vmall.com/c0o00zlvak三十万个敏感词过滤时间不到一秒 纠正一下:bug1:如果是 "去去你^^妈" 将得到 *去^^** 正确的结果应该是:去*^^* http://www.9958.pw/post/web_badword 试试这个 如何使用JSTL 急,,,,,,,,,,,???? 谁有好的xp系统的下载地址啊 各位英语基础好的同仁请帮帮忙翻译一下有于J2EE1.4、JSTL与xml的文档,小女子不胜感激! 求助,关于Hibernate一对多查询问题? SessionBean问题 何以<bean:message>显示的中文全是问号? 高手们,低手们ejb如何解决这样的问题??? 求问Eclispe的两个问题,谢谢啊 在部署应用的时候报Stream closed异常部署失败,求大神指点 webservice问题 请教一个关于多对多关系与数据库设计的问题
tree.add("hello".getBytes(), "hello");
tree.add("world".getBytes(), "world");
tree.prepare(); Iterator searcher = tree.search("hello world".getBytes());
while (searcher.hasNext()) {
SearchResult result = searcher.next();
System.out.println(result.getOutputs());
System.out.println("Found at index: " + result.getLastIndex());
}
如果是,就应该把这些词全部读出来,用个静态数组保存在内存中我用initSensitives()方法模拟创建了一些敏感词,你可以改一下把你的文件里面的敏感词都读出来,保存在sensitives数组里面。
package com.zf.test; public class TestSearch { String str ; //要检验的字符串 String[] sensitives ; //系统设置的敏感词 final int threadSize = 10 ; //总共线程数量 Integer completeSize = 0 ; //已经完成的线程数量 boolean tmp = false; //记录字符串是否属于敏感词 Object completeLock = new Object(); //同步锁辅助对象 public TestSearch(String str){
this.str = str ;
initSensitives();
} //创建敏感词
public String[] initSensitives(){
sensitives = new String[3000];
for (int i = 0; i < 3000 ; i++) {
sensitives[i] = "敏感词" + (i + 1);
}
return sensitives;
} //检查词是否铭感
public boolean checkStr() throws Exception{
int everyLength = sensitives.length / threadSize; //每条线程匹配的长度
for (int i = 0; i < threadSize ; i++) {
int start = i * everyLength ;
int end = start + everyLength ;
if(end > sensitives.length)
end = sensitives.length ;
new CheckThread(start , end).start();
} synchronized (completeLock) {
while(completeSize < threadSize){ //保证线程都执行完了
completeLock.wait();
}
}
return tmp;
} //线程判断字符是否属于敏感词
class CheckThread extends Thread{
int startIndex ;
int endIndex ;
public CheckThread(int startIndex , int endIndex ){
this.startIndex = startIndex ;
this.endIndex = endIndex;
} public void run() {
for (int i = startIndex; i < endIndex && !tmp; i++) {
if(str.matches(".*"+ sensitives[i] +".*")){
tmp = true ;
}
}
synchronized (completeLock) {
completeSize++;
completeLock.notifyAll();
System.out.println("线程" + Thread.currentThread().getName() + "执行完毕");
}
}
}
public static void main(String[] args) throws Exception {
TestSearch ts = new TestSearch("敏感词4");
boolean result = ts.checkStr();
System.out.println(result);
} }
我写的,结合楼上这逼的线程,你可以结合起来修改.修改好了记得上传,把链接发我哦,亲.
bug1:如果是 "去去你ccc妈" 将得到 *去cc**
bug2: 如果是 "连续重复的骂人话只过滤掉第一个"
测试的时候输入:你大爷的!!!你大爷的 得到的结果是:****!!!!你大爷的
在楼上多线程例子和你的代码基础上进行整合并修改了上面的两个bug 得到了很高效的敏感词过滤类 分享给大家 http://dl.vmall.com/c0o00zlvak
三十万个敏感词过滤时间不到一秒
bug1:如果是 "去去你^^妈" 将得到 *去^^** 正确的结果应该是:去*^^*