index) {
+ this.index = index;
+ }
+
+ public boolean isWhiteWord() {
+ return isWhiteWord;
+ }
+
+ public void setWhiteWord(boolean whiteWord) {
+ isWhiteWord = whiteWord;
+ }
+}
diff --git a/bootx-services/service-baseapi/src/main/java/cn/bootx/platform/baseapi/core/chinaword/wordfilter/WordContext.java b/bootx-services/service-baseapi/src/main/java/cn/bootx/platform/baseapi/core/chinaword/wordfilter/WordContext.java
new file mode 100644
index 0000000000000000000000000000000000000000..fe4e49605f5a787b43240ec6f5589eae505d8ea5
--- /dev/null
+++ b/bootx-services/service-baseapi/src/main/java/cn/bootx/platform/baseapi/core/chinaword/wordfilter/WordContext.java
@@ -0,0 +1,194 @@
+package cn.bootx.platform.baseapi.core.chinaword.wordfilter;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.*;
+/**
+ * 词库上下文环境
+ *
+ * 初始化敏感词库,将敏感词加入到HashMap中,构建DFA算法模型
+ *
+ * @author minghu.zhang
+ */
+@SuppressWarnings({"rawtypes", "unchecked"})
+public class WordContext {
+
+ /**
+ * 敏感词字典
+ */
+ private final Map wordMap = new HashMap(1024);
+
+ /**
+ * 是否已初始化
+ */
+ private boolean init;
+ /**
+ * 黑名单列表
+ */
+ private final String blackList;
+ /**
+ * 白名单列表
+ */
+ private final String whiteList;
+
+ public WordContext() {
+ this.blackList = "/blacklist.txt";
+ this.whiteList = "/whitelist.txt";
+ initKeyWord();
+ }
+
+ public WordContext(String blackList, String whiteList) {
+ this.blackList = blackList;
+ this.whiteList = whiteList;
+ initKeyWord();
+ }
+
+ /**
+ * 获取初始化的敏感词列表
+ *
+ * @return 敏感词列表
+ */
+ public Map getWordMap() {
+ return wordMap;
+ }
+
+ /**
+ * 初始化
+ */
+ private synchronized void initKeyWord() {
+ try {
+ if (!init) {
+ // 将敏感词库加入到HashMap中
+ addWord(readWordFile(blackList), WordType.BLACK);
+ // 将非敏感词库也加入到HashMap中
+ addWord(readWordFile(whiteList), WordType.WHITE);
+ }
+ init = true;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * 读取敏感词库,将敏感词放入HashSet中,构建一个DFA算法模型:
+ * 中 = { isEnd = 0 国 = {
+ * isEnd = 1 人 = {isEnd = 0 民 = {isEnd = 1} } 男 = { isEnd = 0 人 = { isEnd = 1 }
+ * } } } 五 = { isEnd = 0 星 = { isEnd = 0 红 = { isEnd = 0 旗 = { isEnd = 1 } } } }
+ */
+ public void addWord(Iterable wordList, WordType wordType) {
+ Map nowMap;
+ Map newWorMap;
+ // 迭代keyWordSet
+ for (String key : wordList) {
+ nowMap = wordMap;
+ for (int i = 0; i < key.length(); i++) {
+ // 转换成char型
+ char keyChar = key.charAt(i);
+ // 获取
+ Object wordMap = nowMap.get(keyChar);
+ // 如果存在该key,直接赋值
+ if (wordMap != null) {
+ nowMap = (Map) wordMap;
+ } else {
+ // 不存在则构建一个map,同时将isEnd设置为0,因为他不是最后一个
+ newWorMap = new HashMap<>(4);
+ // 不是最后一个
+ newWorMap.put("isEnd", String.valueOf(EndType.HAS_NEXT.ordinal()));
+ nowMap.put(keyChar, newWorMap);
+ nowMap = newWorMap;
+ }
+
+ if (i == key.length() - 1) {
+ // 最后一个
+ nowMap.put("isEnd", String.valueOf(EndType.IS_END.ordinal()));
+ nowMap.put("isWhiteWord", String.valueOf(wordType.ordinal()));
+ }
+ }
+ }
+ }
+
+ /**
+ * 在线删除敏感词
+ *
+ * @param wordList 敏感词列表
+ * @param wordType 黑名单 BLACk,白名单WHITE
+ */
+ public void removeWord(Iterable wordList, WordType wordType) {
+ Map nowMap;
+ for (String key : wordList) {
+ List