import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 算法思路:把敏感詞的第一個字符取出來,做爲比較對象。
* 遍歷整個字符串,若是發現字符跟敏感詞第一個字符相同,
* 就從字符串取出跟關鍵詞相同長度的子串比較,若是相同就替換
*
* 本算法比較適合敏感詞都不長的場合
* @author Administrator
*
*/
public class WordFilter {
private static Map<Character,List<String>> wordListToMap(List<String> sensitiveWordList){
Map<Character,List<String>> result=new HashMap<Character,List<String>>();
for (String s:sensitiveWordList){
char c=s.charAt(0);
List<String> strs=result.get(c);
if (strs==null){
strs=new ArrayList<String>();
result.put(c,strs);
}
strs.add(s);
}
return result;
}
public static String filter(String src,List<String> sensitiveWordList){
Map<Character,List<String>> wordMap=wordListToMap(sensitiveWordList);
StringBuilder strb=new StringBuilder();
for (int i=0;i<src.length();i++){
char c=src.charAt(i);
String find=null;
if (wordMap.containsKey(c)){
List<String> words=wordMap.get(c);
for (String s:words){
String temp=src.substring(i,(s.length()<=(src.length()-i))?i+s.length():i);
if (s.equals(temp)){
find=s;
break;
}
}
}
if (find!=null){
strb.append("***");
i+=(find.length()-1);
} else {
strb.append(c);
}
}
return strb.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
List<String> wordList=new ArrayList<String>();
wordList.add("TMD");
wordList.add("變態");
System.out.println(filter("你TMD,也太缺德了吧",wordList));
System.out.println(filter("你TMD,也太缺德了TMD吧",wordList));
System.out.println(filter("你TMD,也太缺德了吧TM",wordList));
System.out.println(filter("你TMD,也太缺德了,太變態了吧TM",wordList));
wordList.add("TM");
System.out.println(filter("你TMD,也太缺德了,太變態了吧TM",wordList));
}
}
一下實現對敏感詞,禁忌詞的過濾.
兩個個文件words.properties和KeyWordFilter.java;
一、words.properties文件是個文本文件;內容以下:
敏感詞一
敏感詞二
敏感詞三
二、KeyWordFilter.java是個java文件內容以下:
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
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);
Enumeration enu = pro.propertyNames();
patternBuf.append("(");
while(enu.hasMoreElements())
{
patternBuf.append((String)enu.nextElement()+"|");
}
patternBuf.deleteCharAt(patternBuf.length()-1);
patternBuf.append(")");
//unix換成UTF-8
//pattern = Pattern.compile(new String(patternBuf.toString().getBytes("ISO-8859-1"), "UTF-8"));
//win下換成gb2312
pattern = Pattern.compile(new String(patternBuf.toString().getBytes("ISO-8859-1"), "gb2312"));
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
}
private static String doFilter(String str)
{
Matcher m = pattern.matcher(str);
str = m.replaceAll("");
return str;
}
public static void main(String[] args)
{
String str = "國敏感詞一院學位辦就敏感詞三的報道表示敏感詞二";
System.out.println("str:"+str);
initPattern();
Date d1 = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss:SSS Z");
System.out.println("start:"+formatter.format(d1));
System.out.println("共"+str.length()+"個字符,查到" + KeyWordFilter.doFilter(str));
Date d2 = new Date();
System.out.println("end:"+formatter.format(d2));
}
}
輸出爲:
__________________________________
str:國敏感詞一院學位辦就敏感詞三的報道表示敏感詞二
start:星期二, 24 三月 2009 14:50:17:171 +0800
共23個字符,查到國院學位辦就的報道表示
end:星期二, 24 三月 2009 14:50:17:531 +0800
另外兩種實現算法 基於DFA的