java 敏感词过滤 效率凑合

使用原理:
    先把资源文件中的敏感词加读出来拼接成正则表达式,然后放到Pattern中,用Matcher m = pattern.matcher(str);   str = m.replaceAll("XXX");替换敏感词 返回替换后的内容

缺点:不能准确的输出敏感词的位置,和具体是哪个敏感词
java 敏感词过滤 效率凑合

KeyWordFilter.java
package com.gjw.action.copy;

import java.io.IOException; 
import java.io.InputStream; 
import java.io.UnsupportedEncodingException;
import java.util.Date; 
import java.util.Enumeration; 
import java.util.Properties; 
import java.util.regex.Pattern; 
import java.util.regex.Matcher; 

public class KeyWordFilter 
{ 
private static Pattern pattern = null; 
// 从words.properties初始化正则表达式字符串 
private static void initPattern() 
{ 
  StringBuffer patternBuf = new StringBuffer(""); 
  try 
  { 
	  InputStream in = KeyWordFilter.class.getClassLoader().getResourceAsStream("words.properties");
    Properties pro = new Properties(); 
   pro.load(in); 
   in.close();
   Enumeration<String> enu = (Enumeration<String>) pro.propertyNames(); 
   patternBuf.append("("); 
   while(enu.hasMoreElements()) 
   { 
		try {
			String dd = (String) enu.nextElement();
			dd = new String(dd.getBytes("ISO8859-1"),"UTF-8");
			patternBuf.append(dd+"|"); 
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
   } 
   patternBuf.deleteCharAt(patternBuf.length()-1); 
   patternBuf.append(")"); 
   pattern = Pattern.compile(patternBuf.toString());
   } 
  catch(IOException ioEx) 
  { 
   ioEx.printStackTrace(); 
  } 
} 
private static String doFilter(String str) 
{ 
  Matcher m = pattern.matcher(str); 
  str = m.replaceAll("XXX"); //敏感词替换
  return str; 
} 

public static void main(String[] args) 
{ 
  String str = "需要过滤的内容"; 
  initPattern(); 
  Date d1 = new Date(); 
  System.out.println("共"+str.length()+"个字符,查到" + KeyWordFilter.doFilter(str)); 
  Date d2 = new Date(); 
  float cost = d2.getTime()-d1.getTime();
  System.out.println("总共花费:"+cost+"毫秒");
} 

} 


words.properties内容下游

你可能感兴趣的:(java敏感词过滤)