天天看點

DFA算法實作的敏感詞過濾器

遇到一個過濾敏感詞的需求,根據網上查詢的資料整理了一下,代碼如下

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Component;

/**
 * 敏感詞過濾bean
 *
 * @author zhanchong
 * @email [email protected]
 * @date 2019-02-21 13:14:51
 */
@Component
public class SensitivewordFilter {
	@SuppressWarnings("rawtypes")
	private Map sensitiveWordMap = null;
	private static int minMatchTYpe = 1; // 最小比對規則
	// private static int maxMatchType = 2; // 最大比對規則
	private String ENCODING = "UTF-8"; // 字元編碼

	/**
	 * 構造函數,初始化敏感詞庫
	 */
	private SensitivewordFilter() {
		this.sensitiveWordMap = initKeyWord();
	}

	/**
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	private Map initKeyWord() {
		try {
			// 讀取敏感詞庫
			Set<String> keyWordSet = readSensitiveWordFile();
			// 将敏感詞庫加入到HashMap中
			addSensitiveWordToHashMap(keyWordSet);
			// spring擷取application,然後application.setAttribute("sensitiveWordMap",sensitiveWordMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sensitiveWordMap;
	}

	/**
	 * 讀取敏感詞庫,将敏感詞放入HashSet中,建構一個DFA算法模型:<br>
	 * 中 = { isEnd = 0 國 = {<br>
	 * isEnd = 1 人 = {isEnd = 0 民 = {isEnd = 1} } 男 = { isEnd = 0 人 = { isEnd =
	 * 1 } } } } 五 = { isEnd = 0 星 = { isEnd = 0 紅 = { isEnd = 0 旗 = { isEnd = 1
	 * } } } }
	 * 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void addSensitiveWordToHashMap(Set<String> keyWordSet) {
		sensitiveWordMap = new HashMap(keyWordSet.size()); // 初始化敏感詞容器,減少擴容操作
		String key = null;
		Map nowMap = null;
		Map<String, String> newWorMap = null;
		// 疊代keyWordSet
		Iterator<String> iterator = keyWordSet.iterator();
		while (iterator.hasNext()) {
			key = iterator.next(); // 關鍵字
			nowMap = sensitiveWordMap;
			for (int i = 0; i < key.length(); i++) {
				char keyChar = key.charAt(i); // 轉換成char型
				Object wordMap = nowMap.get(keyChar); // 擷取

				if (wordMap != null) { // 如果存在該key,直接指派
					nowMap = (Map) wordMap;
				} else { // 不存在則,則建構一個map,同時将isEnd設定為0,因為他不是最後一個
					newWorMap = new HashMap<String, String>();
					newWorMap.put("isEnd", "0"); // 不是最後一個
					nowMap.put(keyChar, newWorMap);
					nowMap = newWorMap;
				}

				if (i == key.length() - 1) {
					nowMap.put("isEnd", "1"); // 最後一個
				}
			}
		}
	}

	/**
	 * 讀取敏感詞庫中的内容,将内容添加到set集合中
	 * 
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("resource")
	private Set<String> readSensitiveWordFile() throws Exception {
		Set<String> set = null;
		String url = SensitivewordFilter.class.getResource("/key.txt").getFile();
		File file = new File(url); // 讀取檔案
		InputStreamReader read = new InputStreamReader(new FileInputStream(file), ENCODING);
		try {
			if (file.isFile() && file.exists()) { // 檔案流是否存在
				set = new HashSet<>();
				BufferedReader bufferedReader = new BufferedReader(read);
				String txt;
				while ((txt = bufferedReader.readLine()) != null) { // 讀取檔案,将檔案内容放入到set中
					set.add(txt);
				}
			} else { // 不存在抛出異常資訊
				throw new Exception("敏感詞庫檔案不存在");
			}
		} catch (Exception e) {
			throw e;
		} finally {
			read.close(); // 關閉檔案流
		}
		return set;
	}

	/**
	 * 判斷文字是否包含敏感字元
	 *
	 * @param txt
	 *            文字
	 * @param matchType
	 *            比對規則&nbsp;1:最小比對規則,2:最大比對規則
	 * @return 若包含傳回true,否則傳回false
	 */
	public boolean isContaintSensitiveWord(String txt, int matchType) {
		boolean flag = false;
		for (int i = 0; i < txt.length(); i++) {
			int matchFlag = this.CheckSensitiveWord(txt, i, matchType); // 判斷是否包含敏感字元
			if (matchFlag > 0) { // 大于0存在,傳回true
				flag = true;
			}
		}
		return flag;
	}

	/**
	 * 擷取文字中的敏感詞
	 *
	 * @param txt
	 *            文字
	 * @param matchType
	 *            比對規則&nbsp;1:最小比對規則,2:最大比對規則
	 * @return
	 */
	public Set<String> getSensitiveWord(String txt, int matchType) {
		Set<String> sensitiveWordList = new HashSet<>();

		for (int i = 0; i < txt.length(); i++) {
			int length = CheckSensitiveWord(txt, i, matchType); // 判斷是否包含敏感字元
			if (length > 0) { // 存在,加入list中
				sensitiveWordList.add(txt.substring(i, i + length));
				i = i + length - 1; // 減1的原因,是因為for會自增
			}
		}

		return sensitiveWordList;
	}

	/**
	 * 替換敏感字字元
	 *
	 * @param txt
	 * @param matchType
	 * @param replaceChar
	 *            替換字元,預設*
	 */
	public String replaceSensitiveWord(String txt, int matchType, String replaceChar) {
		String resultTxt = txt;
		Set<String> set = getSensitiveWord(txt, matchType); // 擷取所有的敏感詞
		Iterator<String> iterator = set.iterator();
		String word = null;
		String replaceString = null;
		while (iterator.hasNext()) {
			word = iterator.next();
			replaceString = getReplaceChars(replaceChar, word.length());
			resultTxt = resultTxt.replaceAll(word, replaceString);
		}

		return resultTxt;
	}

	/**
	 * 擷取替換字元串
	 *
	 * @param replaceChar
	 * @param length
	 * @return
	 */
	public String getReplaceChars(String replaceChar, int length) {
		String resultReplace = replaceChar;
		for (int i = 1; i < length; i++) {
			resultReplace += replaceChar;
		}

		return resultReplace;
	}

	/**
	 * 檢查文字中是否包含敏感字元,檢查規則如下:<br>
	 *
	 * @param txt
	 * @param beginIndex
	 * @param matchType
	 * @return,如果存在,則傳回敏感詞字元的長度,不存在傳回0
	 */
	@SuppressWarnings({ "rawtypes" })
	public int CheckSensitiveWord(String txt, int beginIndex, int matchType) {
		boolean flag = false; // 敏感詞結束辨別位:用于敏感詞隻有1位的情況
		int matchFlag = 0; // 比對辨別數預設為0
		char word = 0;
		Map nowMap = sensitiveWordMap;
		for (int i = beginIndex; i < txt.length(); i++) {
			word = txt.charAt(i);
			nowMap = (Map) nowMap.get(word); // 擷取指定key
			if (nowMap != null) { // 存在,則判斷是否為最後一個
				matchFlag++; // 找到相應key,比對辨別+1
				if ("1".equals(nowMap.get("isEnd"))) { // 如果為最後一個比對規則,結束循環,傳回比對辨別數
					flag = true; // 結束标志位為true
					if (SensitivewordFilter.minMatchTYpe == matchType) { // 最小規則,直接傳回,最大規則還需繼續查找
						break;
					}
				}
			} else { // 不存在,直接傳回
				break;
			}
		}
		if (matchFlag < 2 || !flag) { // 長度必須大于等于1,為詞
			matchFlag = 0;
		}
		return matchFlag;
	}

	/**
	 * 判斷是否含有敏感詞
	 *
	 * @param word
	 * @return
	 */
	public boolean isSensitive(String word) {
		Set<String> words = getSensitiveWord(word, 1);
		return words != null && words.size() > 0;
	}

}