Explorar el Código

优化敏感词加载逻辑

tianboguang hace 3 años
padre
commit
d31be36e02

+ 1 - 2
4dkankan-utils-sensitive-word/src/main/java/com/fdkankan/sensitive/SensitiveWord.java

@@ -24,8 +24,7 @@ public class SensitiveWord {
 	 */
 	public SensitiveWord(){
 		if (sensitiveWordMap == null){
-			String txtPath = this.getClass().getResource("/static/txt/"+ConstantFileName.BBS_SENSITIVE).getPath();
-			sensitiveWordMap = new SensitiveWordConfig().initKeyWord(txtPath);
+			sensitiveWordMap = new SensitiveWordConfig().initKeyWord("/static/txt/"+ ConstantFileName.BBS_SENSITIVE);
 		}
 	}
 	

+ 12 - 36
4dkankan-utils-sensitive-word/src/main/java/com/fdkankan/sensitive/SensitiveWordConfig.java

@@ -1,9 +1,7 @@
 package com.fdkankan.sensitive;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStreamReader;
+import cn.hutool.core.io.IoUtil;
+
 import java.util.*;
 
 /**
@@ -14,7 +12,6 @@ import java.util.*;
  * @version 1.0
  */
 public class SensitiveWordConfig {
-	private String ENCODING = "utf-8";    //字符编码
 	@SuppressWarnings("rawtypes")
 	public HashMap sensitiveWordMap;
 	
@@ -83,24 +80,23 @@ public class SensitiveWordConfig {
 		Map<String, String> newWorMap = null;
 		//迭代keyWordSet
 		Iterator<String> iterator = keyWordSet.iterator();
-		while(iterator.hasNext()){
+		while (iterator.hasNext()) {
 			key = iterator.next();    //关键字
 			nowMap = sensitiveWordMap;
-			for(int i = 0 ; i < key.length() ; i++){
+			for (int i = 0; i < key.length(); i++) {
 				char keyChar = key.charAt(i);       //转换成char型
 				Object wordMap = nowMap.get(keyChar);       //获取
-				
-				if(wordMap != null){        //如果存在该key,直接赋值
+
+				if (wordMap != null) {        //如果存在该key,直接赋值
 					nowMap = (Map) wordMap;
-				}
-				else{     //不存在则,则构建一个map,同时将isEnd设置为0,因为他不是最后一个
-					newWorMap = new HashMap<String,String>();
+				} else {     //不存在则,则构建一个map,同时将isEnd设置为0,因为他不是最后一个
+					newWorMap = new HashMap<String, String>();
 					newWorMap.put("isEnd", "0");     //不是最后一个
 					nowMap.put(keyChar, newWorMap);
 					nowMap = newWorMap;
 				}
-				
-				if(i == key.length() - 1){
+
+				if (i == key.length() - 1) {
 					nowMap.put("isEnd", "1");    //最后一个
 				}
 			}
@@ -117,28 +113,8 @@ public class SensitiveWordConfig {
 	 */
 	@SuppressWarnings("resource")
 	private Set<String> readSensitiveWordFile(String path) throws Exception{
-		Set<String> set = null;
-		//File file = new File("E:\\2017\\4Dweb\\bug汇总\\过滤敏感词\\敏感词库\\敏感词库\\2012年最新敏感词列表\\论坛需要过滤的不良词语大全.txt");    //读取文件
-		//File file = new File("D:\\SensitiveWord.txt");    //读取文件
-		File file = new File(path);
-		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();     //关闭文件流
-		}
+		Set<String> set = new HashSet<>();
+		IoUtil.readUtf8Lines(SensitiveWordConfig.class.getResourceAsStream(path),set);
 		return set;
 	}
 }

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 1125 - 0
4dkankan-utils-sensitive-word/src/main/resources/static/txt/SensitiveWord.txt