这可以用AC多模匹配算法。LookThis: https://hkn.eecs.berkeley.edu/~dyoo/java/index.html

解决方案 »

  1.   

           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());
           }
      

  2.   

    这个在系统中应该是经常要用到的吧。
    如果是,就应该把这些词全部读出来,用个静态数组保存在内存中我用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);
    }  }  
      

  3.   

    http://download.csdn.net/detail/javaloverkehui/3849901   
    我写的,结合楼上这逼的线程,你可以结合起来修改.修改好了记得上传,把链接发我哦,亲.
      

  4.   

    发现两个bug
    bug1:如果是 "去去你ccc妈" 将得到 *去cc**
    bug2: 如果是 "连续重复的骂人话只过滤掉第一个"
      

  5.   

    你不信自己试试
    测试的时候输入:你大爷的!!!你大爷的    得到的结果是:****!!!!你大爷的
    在楼上多线程例子和你的代码基础上进行整合并修改了上面的两个bug 得到了很高效的敏感词过滤类 分享给大家  http://dl.vmall.com/c0o00zlvak
    三十万个敏感词过滤时间不到一秒
      

  6.   

    纠正一下:
    bug1:如果是 "去去你^^妈" 将得到 *去^^** 正确的结果应该是:去*^^*
      

  7.   

    http://www.9958.pw/post/web_badword 试试这个