分享

Java 敏感字符处理类,功能非常强大

 昵称9552892 2015-11-26

敏感字符的处理,性能非常好,采用文件的方式,可通过代码增加敏感词等强大的功能

在开源中国的基础上增加部分方法

依赖apache的io 和lang包

  1. package com.wiker;  
  2.   
  3.   
  4. import java.io.File;  
  5. import java.io.IOException;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. import org.apache.commons.io.FileUtils;  
  10. import org.apache.commons.io.LineIterator;  
  11. import org.apache.commons.lang.StringUtils;  
  12.   
  13. /** 
  14.  * 敏感字词处理类 
  15.  * @author Wiker 
  16.  * @date 2010-1-11 下午10:51:30 
  17.  */  
  18. public class BadWord {  
  19.   
  20.     private final static File wordfilter = new File("C:/wordfilter.txt");  
  21.   
  22.     private static long lastModified = 0L;  
  23.     private static List<String> words = new ArrayList<String>();  
  24.       
  25.     private static void checkReload(){  
  26.         if(wordfilter.lastModified() > lastModified){  
  27.             synchronized(BadWord.class){  
  28.                 try{  
  29.                     lastModified = wordfilter.lastModified();  
  30.                     LineIterator lines = FileUtils.lineIterator(wordfilter, "utf-8");  
  31.                     while(lines.hasNext()){  
  32.                         String line = lines.nextLine();  
  33.                         if(StringUtils.isNotBlank(line))  
  34.                             words.add(StringUtils.trim(line).toLowerCase());  
  35.                     }  
  36.                 }catch(IOException e){  
  37.                     e.printStackTrace();  
  38.                 }  
  39.             }  
  40.         }  
  41.     }  
  42.       
  43.     /** 
  44.      * 检查敏感字内容 
  45.      * @param contents 
  46.      */  
  47.     public static String check(String ...contents) {  
  48.         if(!wordfilter.exists())  
  49.             return null;  
  50.         checkReload();  
  51.         for(String word : words){  
  52.             for(String content : contents)  
  53.                 if(content!=null && content.indexOf(word) >= 0)  
  54.                     return word;  
  55.         }  
  56.         return null;  
  57.     }  
  58.       
  59.     /** 
  60.      * 检查字符串是否包含敏感词 
  61.      * 
  62.      * @param content 
  63.      * @return 
  64.      */  
  65.     public static boolean isContain(String content) {  
  66.         if(!wordfilter.exists())  
  67.             return false;  
  68.         checkReload();  
  69.         for(String word : words){  
  70.             if(content!=null && content.indexOf(word) >= 0)  
  71.                 return true;  
  72.         }  
  73.         return false;  
  74.     }  
  75.       
  76.     /** 
  77.      * 替换掉字符串中的敏感词 
  78.      * 
  79.      * @param str 等待替换的字符串 
  80.      * @param replaceChar 替换字符 
  81.      * @return 
  82.      */  
  83.     public static String replace(String str,String replaceChar){  
  84.         checkReload();  
  85.         for(String word : words){  
  86.             if(str.indexOf(word)>=0){  
  87.                 String reChar = "";  
  88.                 for(int i=0;i<word.length();i++){  
  89.                     reChar += replaceChar;  
  90.                 }  
  91.                 str = str.replaceAll(word, reChar);  
  92.             }  
  93.         }  
  94.         return str;  
  95.     }  
  96.       
  97.     public static List<String> lists() {  
  98.         checkReload();  
  99.         return words;  
  100.     }  
  101.       
  102.     /** 
  103.      * 添加敏感词 
  104.      * 
  105.      * @param word 
  106.      * @throws IOException 
  107.      */  
  108.     public static void add(String word) throws IOException {  
  109.         word = word.toLowerCase();  
  110.         if(!words.contains(word)){  
  111.             words.add(word);  
  112.             FileUtils.writeLines(wordfilter, "UTF-8", words);  
  113.             lastModified = wordfilter.lastModified();  
  114.         }  
  115.     }  
  116.   
  117.     /** 
  118.      * 删除敏感词 
  119.      * 
  120.      * @param word 
  121.      * @throws IOException 
  122.      */  
  123.     public static void delete(String word) throws IOException {  
  124.         word = word.toLowerCase();  
  125.         words.remove(word);  
  126.         FileUtils.writeLines(wordfilter, "UTF-8", words);  
  127.         lastModified = wordfilter.lastModified();  
  128.     }  
  129.       
  130.     public static void main(String[] args) throws Exception{  
  131.         System.out.println(BadWord.replace("中国共产党钓鱼岛","*"));  
  132.         System.out.println(BadWord.isContain("岛"));  
  133.         BadWord.add("傻逼");  
  134.     }  
  135.       
  136. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多