java敏感词过滤

敏感词过滤在网站开发必不可少。一般用DFA,这种比较好的算法实现的.

参考链接:http://cmsblogs.com/?p=1031

一个比较好的代码实现:

  

  1 import java.io.IOException;
  2 import java.util.ArrayList;
  3 import java.util.HashMap;
  4 import java.util.List;
  5 
  6 /**
  7  * @title KeywordFilter
  8  * @description TODO 
  9  * @author 
 10  * @date 2014-4-17
 11  * @version 1.0
 12  */
 13 public class KeywordFilter {
 14     /** 敏感词集合
 15      * {法={isEnd=0, 轮={isEnd=1}}, 中={isEnd=0, 国={isEnd=0, 人={isEnd=1}, 男={isEnd=0, 人={isEnd=1}}}}}
 16      * */
 17     private HashMap keysMap = new HashMap();
 18 
 19     /**
 20      * 添加敏感词
 21      * @param keywords
 22      */
 23     public void addKeywords(List<String> keywords) {
 24         for (int i = 0; i < keywords.size(); i++) {
 25             String key = keywords.get(i).trim();
 26             HashMap nowhash = keysMap;//初始从最外层遍历
 27             for (int j = 0; j < key.length(); j++) {
 28                 char word = key.charAt(j);
 29                 Object wordMap = nowhash.get(word);
 30                 if (wordMap != null) {
 31                     nowhash = (HashMap) wordMap;
 32                 } else {
 33                     HashMap<String, String> newWordHash = new HashMap<String, String>();
 34                     newWordHash.put("isEnd", "0");
 35                     nowhash.put(word, newWordHash);
 36                     nowhash = newWordHash;
 37                 }
 38                 if (j == key.length() - 1) {
 39                     nowhash.put("isEnd", "1");
 40                 }
 41             }
 42         }
 43     }
 44 
 45     /** 
 46      * 检查一个字符串从begin位置起开始是否有keyword符合,
 47      * 如果没有,则返回0
 48      * 如果有符合的keyword值,继续遍历,直至遇到isEnd = 1,返回匹配的keyword的长度, 
 49      */
 50     private int checkKeyWords(String txt, int begin) {
 51         HashMap nowhash = keysMap;
 52         int res = 0;
 53         for (int i = begin; i < txt.length(); i++) {
 54             char word = txt.charAt(i);
 55             Object wordMap = nowhash.get(word);//得到该字符对应的HashMap
 56             if (wordMap == null) {
 57                 return 0;//如果该字符没有对应的HashMap,return 0
 58             }
 59 
 60             res++;//如果该字符对应的HashMap不为null,说明匹配到了一个字符,+1
 61             nowhash = (HashMap) wordMap;//将遍历的HashMap指向该字符对应的HashMap
 62             if (((String) nowhash.get("isEnd")).equals("1")) {//如果该字符为敏感词的结束字符,直接返回
 63                 return res;
 64             } else {
 65                 continue;
 66             }
 67         }
 68         return res;
 69     }
 70 
 71     /** 
 72      * 判断txt中是否有关键字 
 73      */
 74     public boolean isContentKeyWords(String txt) {
 75         for (int i = 0; i < txt.length(); i++) {
 76             int len = checkKeyWords(txt, i);
 77             if (len > 0) {
 78                 return true;
 79             }
 80         }
 81         return false;
 82     }
 83 
 84     /** 
 85      * 返回txt中关键字的列表 
 86      */
 87     public List<String> getTxtKeyWords(String txt) {
 88         List<String> list = new ArrayList<String>();
 89         int l = txt.length();
 90         for (int i = 0; i < l;) {
 91             int len = checkKeyWords(txt, i);
 92             if (len > 0) {
 93                 String tt = txt.substring(i, i + len);
 94                 list.add(tt);
 95                 i += len;
 96             } else {
 97                 i++;
 98             }
 99         }
100         return list;
101     }
102 
103     /** 
104      * 初始化敏感词列表 
105      * */
106     public void initfiltercode() {
107         List<String> keywords = new ArrayList<String>();
108         keywords.add("中国人");
109         keywords.add("中国男人");
110         keywords.add("*");
111         this.addKeywords(keywords);
112     }
113 
114     public static void main(String[] args) throws IOException {
115         KeywordFilter filter = new KeywordFilter();
116         filter.initfiltercode();
117         String txt = "哈哈,反倒是 *热舞功,中国人,";
118         boolean boo = filter.isContentKeyWords(txt);
119         System.out.println(boo);
120         List<String> set = filter.getTxtKeyWords(txt);
121         System.out.println("包含的敏感词如下:" + set);
122     }
123     
124     
125     
126     
127 }