package com.iris.nsonline.nsoweb.utils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
/**
* 过滤敏感词
*
* @author yxs
*
* @since 2016年4月18日 下午2:47:39
*/
public class FilterSensitiveWord {
public static final String PATH = "/config/filterWord.txt";
public static Set<String> FilterSenWord() {
return readTxtFile(PATH);
}
public static Set<String> readTxtFile(String file) {
Set<String> setWordsSet = new HashSet<String>();
try {
String encoding = "UTF-8";
InputStream in = FilterSensitiveWord.class.getResourceAsStream(file);
InputStreamReader read = new InputStreamReader(in, encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
setWordsSet.add(lineTxt);
}
read.close();
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return setWordsSet;
}
public static String getHarmony(String word) {
String result = "";
Set<String> sets = FilterSensitiveWord.FilterSenWord();
if (word != null) {
for (String string : sets) {
if (word.contains(string)) {
result = word.replace(string, "**");
}
}
}
return result;
}
public static void main(String[] args) {
String word = getHarmony("小虫真邪恶");
System.out.println(word);
}
}