|
现有一个txt格式文档里边存了有3000个敏感词汇(例如:sentive.txt),做一个小的java程序,输入一段文字可以实现比较(这段文字长度大概50多个汉字),如果存在就提示出 改字(词)敏感,用contains的话 太慢了!可不可以多线程比较 或者有更好的算法! |
|
![]() |
这可以用AC多模匹配算法。
LookThis: https://hkn.eecs.berkeley.edu/~dyoo/java/index.html |
![]() |
新手,求示例代码……
|
![]() |
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());
}
|
![]() |
map reduce
|
![]() |
同意一楼,使用AC自动机
|
![]() 25分 |
这个在系统中应该是经常要用到的吧。
如果是,就应该把这些词全部读出来,用个静态数组保存在内存中 我用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
我写的,结合楼上这逼的线程,你可以结合起来修改.修改好了记得上传,把链接发我哦,亲. |
![]() 10分 |
去哥的资源里看一下,有一个关键词过滤的, 过滤了符号然后判断关键词的. |
![]() |
6楼的多线程效率可以
|
![]() 5分 |
发现两个bug |
![]() |
你不觉得已经实现了这个功能吗?你觉得过滤之后,还有脏字? |
![]() |
你不信自己试试 |
![]() |
纠正一下: |
![]() |
http://www.9958.pw/post/web_badword 试试这个
|

