ITPub博客

首页 > 应用开发 > Java > Java敏感词过滤

Java敏感词过滤

原创 Java 作者:541732025 时间:2014-04-18 13:21:49 0 删除 编辑

点击(此处)折叠或打开

  1. package test.java.lang;

  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;

  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.      * @param keywords
  21.      */
  22.     public void addKeywords(List<String> keywords) {
  23.         for (int i = 0; i < keywords.size(); i++) {
  24.             String key = keywords.get(i).trim();
  25.             HashMap nowhash = keysMap;//初始从最外层遍历
  26.             for (int j = 0; j < key.length(); j++) {
  27.                 char word = key.charAt(j);
  28.                 Object wordMap = nowhash.get(word);
  29.                 if (wordMap != null) {
  30.                     nowhash = (HashMap) wordMap;
  31.                 } else {
  32.                     HashMap<String, String> newWordHash = new HashMap<String, String>();
  33.                     newWordHash.put(\"isEnd\", \"0\");
  34.                     nowhash.put(word, newWordHash);
  35.                     nowhash = newWordHash;
  36.                 }
  37.                 if (j == key.length() - 1) {
  38.                     nowhash.put(\"isEnd\", \"1\");
  39.                 }
  40.             }
  41.         }
  42.     }

  43.     /**
  44.      * 检查一个字符串从begin位置起开始是否有keyword符合,
  45.      * 如果没有,则返回0
  46.      * 如果有符合的keyword值,继续遍历,直至遇到isEnd = 1,返回匹配的keyword的长度,
  47.      */
  48.     private int checkKeyWords(String txt, int begin) {
  49.         HashMap nowhash = keysMap;
  50.         int res = 0;
  51.         for (int i = begin; i < txt.length(); i++) {
  52.             char word = txt.charAt(i);
  53.             Object wordMap = nowhash.get(word);//得到该字符对应的HashMap
  54.             if (wordMap == null) {
  55.                 return 0;//如果该字符没有对应的HashMap,return 0
  56.             }

  57.             res++;//如果该字符对应的HashMap不为null,说明匹配到了一个字符,+1
  58.             nowhash = (HashMap) wordMap;//将遍历的HashMap指向该字符对应的HashMap
  59.             if (((String) nowhash.get(\"isEnd\")).equals(\"1\")) {//如果该字符为敏感词的结束字符,直接返回
  60.                 return res;
  61.             } else {
  62.                 continue;
  63.             }
  64.         }
  65.         return res;
  66.     }

  67.     /**
  68.      * 判断txt中是否有关键字
  69.      */
  70.     public boolean isContentKeyWords(String txt) {
  71.         for (int i = 0; i < txt.length(); i++) {
  72.             int len = checkKeyWords(txt, i);
  73.             if (len > 0) {
  74.                 return true;
  75.             }
  76.         }
  77.         return false;
  78.     }

  79.     /**
  80.      * 返回txt中关键字的列表
  81.      */
  82.     public List<String> getTxtKeyWords(String txt) {
  83.         List<String> list = new ArrayList<String>();
  84.         int l = txt.length();
  85.         for (int i = 0; i < l;) {
  86.             int len = checkKeyWords(txt, i);
  87.             if (len > 0) {
  88.                 String tt = txt.substring(i, i + len);
  89.                 list.add(tt);
  90.                 i += len;
  91.             } else {
  92.                 i++;
  93.             }
  94.         }
  95.         return list;
  96.     }

  97.     /**
  98.      * 初始化敏感词列表
  99.      * */
  100.     public void initfiltercode() {
  101.         List<String> keywords = new ArrayList<String>();
  102.         keywords.add(\"中国人\");
  103.         keywords.add(\"中国男人\");
  104.         keywords.add(\"法轮\");
  105.         this.addKeywords(keywords);
  106.     }

  107.     public static void main(String[] args) throws IOException {
  108.         KeywordFilter filter = new KeywordFilter();
  109.         filter.initfiltercode();
  110.         String txt = \"哈哈,反倒是 法轮热舞功,中国人,\";
  111.         boolean boo = filter.isContentKeyWords(txt);
  112.         System.out.println(boo);
  113.         List<String> set = filter.getTxtKeyWords(txt);
  114.         System.out.println(\"包含的敏感词如下:\" + set);
  115.     }
  116. }


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28912557/viewspace-1144669/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-05-23

  • 博文量
    127
  • 访问量
    515716