|
@@ -36,6 +36,19 @@ public class SensitiveWordConfig {
|
|
|
return sensitiveWordMap;
|
|
|
}
|
|
|
|
|
|
+ public Map initKeyWord(File file){
|
|
|
+ try {
|
|
|
+ //读取敏感词库
|
|
|
+ Set<String> keyWordSet = readSensitiveWordFile(file);
|
|
|
+ //将敏感词库加入到HashMap中
|
|
|
+ addSensitiveWordToHashMap(keyWordSet);
|
|
|
+ //spring获取application,然后application.setAttribute("sensitiveWordMap",sensitiveWordMap);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return sensitiveWordMap;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 读取敏感词库,将敏感词放入HashSet中,构建一个DFA算法模型:<br>
|
|
|
* 中 = {
|
|
@@ -128,4 +141,29 @@ public class SensitiveWordConfig {
|
|
|
}
|
|
|
return set;
|
|
|
}
|
|
|
+
|
|
|
+ private Set<String> readSensitiveWordFile(File file) throws Exception{
|
|
|
+ Set<String> set = null;
|
|
|
+ //File file = new File("E:\\2017\\4Dweb\\bug汇总\\过滤敏感词\\敏感词库\\敏感词库\\2012年最新敏感词列表\\论坛需要过滤的不良词语大全.txt"); //读取文件
|
|
|
+ //File file = new File("D:\\SensitiveWord.txt"); //读取文件
|
|
|
+ InputStreamReader read = new InputStreamReader(new FileInputStream(file),ENCODING);
|
|
|
+ try {
|
|
|
+ if(file.isFile() && file.exists()){ //文件流是否存在
|
|
|
+ set = new HashSet<String>();
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(read);
|
|
|
+ String txt = null;
|
|
|
+ while((txt = bufferedReader.readLine()) != null){ //读取文件,将文件内容放入到set中
|
|
|
+ set.add(txt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else{ //不存在抛出异常信息
|
|
|
+ throw new Exception("敏感词库文件不存在");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw e;
|
|
|
+ }finally{
|
|
|
+ read.close(); //关闭文件流
|
|
|
+ }
|
|
|
+ return set;
|
|
|
+ }
|
|
|
}
|