getTag(String word);
+
+}
diff --git a/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordBs.java b/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordBs.java
index 29bd564..cf20c9c 100644
--- a/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordBs.java
+++ b/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordBs.java
@@ -2,6 +2,7 @@ package com.github.houbb.sensitive.word.bs;
import com.github.houbb.heaven.support.handler.IHandler;
import com.github.houbb.heaven.util.common.ArgUtil;
+import com.github.houbb.heaven.util.lang.StringUtil;
import com.github.houbb.heaven.util.util.CollectionUtil;
import com.github.houbb.sensitive.word.api.*;
import com.github.houbb.sensitive.word.api.combine.IWordAllowDenyCombine;
@@ -16,9 +17,12 @@ import com.github.houbb.sensitive.word.support.data.WordDatas;
import com.github.houbb.sensitive.word.support.deny.WordDenys;
import com.github.houbb.sensitive.word.support.replace.WordReplaces;
import com.github.houbb.sensitive.word.support.result.WordResultHandlers;
+import com.github.houbb.sensitive.word.support.tag.WordTags;
import java.util.Collection;
+import java.util.Collections;
import java.util.List;
+import java.util.Set;
/**
* 敏感词引导类
@@ -146,6 +150,12 @@ public class SensitiveWordBs {
*/
private IWordAllowDenyCombine wordAllowDenyCombine = WordAllowDenyCombines.defaults();
+ /**
+ * 单词标签
+ * @since 0.10.0
+ */
+ private IWordTag wordTag = WordTags.none();
+
/**
* 新建验证实例
*
@@ -214,10 +224,18 @@ public class SensitiveWordBs {
context.sensitiveCheckNumLen(numCheckLen);
context.wordReplace(wordReplace);
context.wordData(wordData);
+ context.wordTag(wordTag);
return context;
}
+ public SensitiveWordBs wordTag(IWordTag wordTag) {
+ ArgUtil.notNull(wordTag, "wordTag");
+
+ this.wordTag = wordTag;
+ return this;
+ }
+
public SensitiveWordBs wordCheckCombine(IWordCheckCombine wordCheckCombine) {
ArgUtil.notNull(wordCheckCombine, "wordCheckCombine");
@@ -509,6 +527,22 @@ public class SensitiveWordBs {
return sensitiveWord.replace(target, context);
}
+ /**
+ * 获取敏感词的标签
+ *
+ * @param word 敏感词
+ * @return 结果
+ * @since 0.10.0
+ */
+ public Set tags(final String word) {
+ if(StringUtil.isEmpty(word)) {
+ return Collections.emptySet();
+ }
+
+ // 是否需要格式化?
+ return wordTag.getTag(word);
+ }
+
//------------------------------------------------------------------------------------ 公开方法 END
}
diff --git a/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordContext.java b/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordContext.java
index 59bc673..a42730c 100644
--- a/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordContext.java
+++ b/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordContext.java
@@ -100,6 +100,13 @@ public class SensitiveWordContext implements IWordContext {
*/
private IWordData wordData;
+ /**
+ * 单词标签
+ *
+ * @since 0.10.0
+ */
+ private IWordTag wordTag;
+
public IWordData wordData() {
return wordData;
}
@@ -273,4 +280,14 @@ public class SensitiveWordContext implements IWordContext {
this.wordFormat = wordFormat;
return this;
}
+
+ public IWordTag wordTag() {
+ return wordTag;
+ }
+
+ public SensitiveWordContext wordTag(IWordTag wordTag) {
+ this.wordTag = wordTag;
+ return this;
+ }
+
}
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/tag/AbstractWordTag.java b/src/main/java/com/github/houbb/sensitive/word/support/tag/AbstractWordTag.java
new file mode 100644
index 0000000..abc3f51
--- /dev/null
+++ b/src/main/java/com/github/houbb/sensitive/word/support/tag/AbstractWordTag.java
@@ -0,0 +1,33 @@
+package com.github.houbb.sensitive.word.support.tag;
+
+import com.github.houbb.heaven.util.lang.StringUtil;
+import com.github.houbb.sensitive.word.api.IWordTag;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * 抽象的单词标签
+ *
+ * @since 0.10.0
+ */
+public abstract class AbstractWordTag implements IWordTag {
+
+
+ /**
+ * 获取标签
+ * @param word 单词
+ * @return 结果
+ */
+ protected abstract Set doGetTag(String word);
+
+ @Override
+ public Set getTag(String word) {
+ if(StringUtil.isEmpty(word)) {
+ return Collections.emptySet();
+ }
+
+ return doGetTag(word);
+ }
+
+}
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/tag/FileWordTag.java b/src/main/java/com/github/houbb/sensitive/word/support/tag/FileWordTag.java
new file mode 100644
index 0000000..50fae6e
--- /dev/null
+++ b/src/main/java/com/github/houbb/sensitive/word/support/tag/FileWordTag.java
@@ -0,0 +1,89 @@
+package com.github.houbb.sensitive.word.support.tag;
+
+import com.github.houbb.heaven.util.common.ArgUtil;
+import com.github.houbb.heaven.util.io.FileUtil;
+import com.github.houbb.heaven.util.lang.StringUtil;
+import com.github.houbb.heaven.util.util.CollectionUtil;
+
+import java.util.*;
+
+/**
+ * 基于文件的标签
+ *
+ * word tag1,tag2
+ * @since 0.10.0
+ */
+public class FileWordTag extends AbstractWordTag {
+
+ /**
+ * 文件路径
+ */
+ protected final String filePath;
+ /**
+ * 词和标签的分隔符
+ */
+ protected final String wordSplit;
+ /**
+ * 标签的分隔符
+ */
+ protected final String tagSplit;
+
+ protected Map> wordTagMap = new HashMap<>();
+
+ public FileWordTag(String filePath) {
+ this(filePath, " ", ",");
+ }
+
+ public FileWordTag(String filePath, String wordSplit, String tagSplit) {
+ ArgUtil.notEmpty(filePath, "filePath");
+ ArgUtil.notEmpty(wordSplit, "wordSplit");
+ ArgUtil.notEmpty(tagSplit, "tagSplit");
+
+ this.wordSplit = wordSplit;
+ this.tagSplit = tagSplit;
+ this.filePath = filePath;
+
+ this.initWordTagMap();
+ }
+
+
+ /**
+ * 初始化
+ */
+ protected synchronized void initWordTagMap() {
+ List lines = FileUtil.readAllLines(filePath);
+ if(CollectionUtil.isEmpty(lines)) {
+ return;
+ }
+
+ for(String line : lines) {
+ if(StringUtil.isEmpty(line)) {
+ continue;
+ }
+
+ // 处理每一行
+ handleInitLine(line);
+ }
+ }
+
+ protected synchronized void handleInitLine(String line) {
+ String[] strings = line.split(wordSplit);
+ if(strings.length < 2) {
+ return;
+ }
+
+ String word = strings[0];
+ String tagText = strings[1];
+
+
+ String[] tags = tagText.split(tagSplit);
+ Set tagSet = new HashSet<>(Arrays.asList(tags));
+ wordTagMap.put(word, tagSet);
+ }
+
+ @Override
+ protected Set doGetTag(String word) {
+ return wordTagMap.get(word);
+ }
+
+}
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/tag/NoneWordTag.java b/src/main/java/com/github/houbb/sensitive/word/support/tag/NoneWordTag.java
new file mode 100644
index 0000000..af6083a
--- /dev/null
+++ b/src/main/java/com/github/houbb/sensitive/word/support/tag/NoneWordTag.java
@@ -0,0 +1,19 @@
+package com.github.houbb.sensitive.word.support.tag;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * 空标签
+ *
+ * word tag1,tag2
+ * @since 0.10.0
+ */
+public class NoneWordTag extends AbstractWordTag {
+
+ @Override
+ protected Set doGetTag(String word) {
+ return Collections.emptySet();
+ }
+
+}
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/tag/WordTags.java b/src/main/java/com/github/houbb/sensitive/word/support/tag/WordTags.java
new file mode 100644
index 0000000..0279196
--- /dev/null
+++ b/src/main/java/com/github/houbb/sensitive/word/support/tag/WordTags.java
@@ -0,0 +1,20 @@
+package com.github.houbb.sensitive.word.support.tag;
+
+import com.github.houbb.sensitive.word.api.IWordTag;
+
+/**
+ * 单词标签
+ *
+ * @since 0.10.0
+ */
+public class WordTags {
+
+ public static IWordTag none() {
+ return new NoneWordTag();
+ }
+
+ public static IWordTag file(String filePath) {
+ return new FileWordTag(filePath);
+ }
+
+}
diff --git a/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsTagTest.java b/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsTagTest.java
new file mode 100644
index 0000000..dfef147
--- /dev/null
+++ b/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsTagTest.java
@@ -0,0 +1,29 @@
+package com.github.houbb.sensitive.word.bs;
+
+import com.github.houbb.sensitive.word.api.IWordTag;
+import com.github.houbb.sensitive.word.support.tag.WordTags;
+import org.junit.Assert;
+
+/**
+ * project: sensitive-word-SensitiveWordBsTest
+ * create on 2020/1/7 23:43
+ *
+ * @author Administrator
+ * @since 0.10.0
+ */
+public class SensitiveWordBsTagTest {
+
+ public static void main(String[] args) {
+ String filePath = "D:\\code\\github\\sensitive-word\\src\\test\\resources\\dict_tag_test.txt";
+
+ IWordTag wordTag = WordTags.file(filePath);
+
+ SensitiveWordBs sensitiveWordBs = SensitiveWordBs.newInstance()
+ .wordTag(wordTag)
+ .init()
+ ;
+
+ Assert.assertEquals("[政治, 国家]", sensitiveWordBs.tags("五星红旗").toString());;
+ }
+
+}
diff --git a/src/test/resources/dict_tag_test.txt b/src/test/resources/dict_tag_test.txt
new file mode 100644
index 0000000..51d66ca
--- /dev/null
+++ b/src/test/resources/dict_tag_test.txt
@@ -0,0 +1 @@
+五星红旗 政治,国家
\ No newline at end of file