From dc70bcd684b41f4a1425a3330cf4213de4d1f085 Mon Sep 17 00:00:00 2001 From: yudasen <2436348937@qq.com> Date: Sat, 1 Feb 2025 11:34:13 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20=E4=BF=AE=E5=A4=8D=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=95=8F=E6=84=9F=E8=AF=8D=E4=B8=AD=E5=8F=AF=E8=83=BD=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E7=9A=84=E5=B9=B6=E5=8F=91=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../houbb/sensitive/word/api/IWordData.java | 4 +- .../sensitive/word/bs/SensitiveWordBs.java | 9 +- .../word/support/data/AbstractWordData.java | 11 +- .../word/support/data/WordDataHashMap.java | 2 +- .../word/support/data/WordDataTree.java | 102 ++++++++++-------- 5 files changed, 70 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/github/houbb/sensitive/word/api/IWordData.java b/src/main/java/com/github/houbb/sensitive/word/api/IWordData.java index 7aa55cd..ef1d8b7 100644 --- a/src/main/java/com/github/houbb/sensitive/word/api/IWordData.java +++ b/src/main/java/com/github/houbb/sensitive/word/api/IWordData.java @@ -22,10 +22,10 @@ public interface IWordData extends ISensitiveWordDestroy { /** * 删除敏感词 - * @param word 单词 + * @param collection 单词 * @since 0.19.0 */ - void removeWord(String word); + void removeWord(Collection collection); /** * 新增敏感词 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 31441f3..efbc36f 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 @@ -639,9 +639,7 @@ public class SensitiveWordBs implements ISensitiveWordDestroy { // 主要原因是二者没有保持一致,初始化的数据和插入的数据没有做相同的格式化 List formatList = InnerWordFormatUtils.formatWordList(collection, context); - for(String word : formatList) { - this.wordData.removeWord(word); - } + this.wordData.removeWord(formatList); } /** @@ -698,9 +696,8 @@ public class SensitiveWordBs implements ISensitiveWordDestroy { // 主要原因是二者没有保持一致,初始化的数据和插入的数据没有做相同的格式化 List formatList = InnerWordFormatUtils.formatWordList(collection, context); - for(String word : formatList) { - this.wordDataAllow.removeWord(word); - } + this.wordDataAllow.removeWord(formatList); + } /** * 新增敏感词白名单 diff --git a/src/main/java/com/github/houbb/sensitive/word/support/data/AbstractWordData.java b/src/main/java/com/github/houbb/sensitive/word/support/data/AbstractWordData.java index 06515d5..ae1874d 100644 --- a/src/main/java/com/github/houbb/sensitive/word/support/data/AbstractWordData.java +++ b/src/main/java/com/github/houbb/sensitive/word/support/data/AbstractWordData.java @@ -1,6 +1,5 @@ package com.github.houbb.sensitive.word.support.data; -import com.github.houbb.heaven.util.lang.StringUtil; import com.github.houbb.heaven.util.util.CollectionUtil; import com.github.houbb.sensitive.word.api.IWordData; import com.github.houbb.sensitive.word.api.context.InnerSensitiveWordContext; @@ -31,9 +30,9 @@ public abstract class AbstractWordData implements IWordData { /** * 删除敏感词 - * @param word 敏感词 + * @param collection */ - protected abstract void doRemoveWord(String word); + protected abstract void doRemoveWord(Collection collection); /** * 新增敏感词 @@ -49,12 +48,12 @@ public abstract class AbstractWordData implements IWordData { } @Override - public void removeWord(String word) { - if(StringUtil.isEmpty(word)) { + public void removeWord(Collection collection) { + if(CollectionUtil.isEmpty(collection)) { return; } - doRemoveWord(word); + doRemoveWord(collection); } @Override diff --git a/src/main/java/com/github/houbb/sensitive/word/support/data/WordDataHashMap.java b/src/main/java/com/github/houbb/sensitive/word/support/data/WordDataHashMap.java index 834d85f..bf189c6 100644 --- a/src/main/java/com/github/houbb/sensitive/word/support/data/WordDataHashMap.java +++ b/src/main/java/com/github/houbb/sensitive/word/support/data/WordDataHashMap.java @@ -89,7 +89,7 @@ public class WordDataHashMap extends AbstractWordData { } @Override - protected void doRemoveWord(String word) { + protected void doRemoveWord(Collection collection) { } diff --git a/src/main/java/com/github/houbb/sensitive/word/support/data/WordDataTree.java b/src/main/java/com/github/houbb/sensitive/word/support/data/WordDataTree.java index 453c841..5027f24 100644 --- a/src/main/java/com/github/houbb/sensitive/word/support/data/WordDataTree.java +++ b/src/main/java/com/github/houbb/sensitive/word/support/data/WordDataTree.java @@ -4,7 +4,6 @@ import com.github.houbb.heaven.annotation.ThreadSafe; import com.github.houbb.heaven.util.lang.ObjectUtil; import com.github.houbb.heaven.util.lang.StringUtil; import com.github.houbb.sensitive.word.api.IWordContext; -import com.github.houbb.sensitive.word.api.IWordData; import com.github.houbb.sensitive.word.api.context.InnerSensitiveWordContext; import com.github.houbb.sensitive.word.constant.enums.WordContainsTypeEnum; @@ -84,49 +83,7 @@ public class WordDataTree extends AbstractWordData { this.root = newRoot; } - @Override - protected void doRemoveWord(String word) { - WordDataTreeNode tempNode = root; - //需要删除的 - Map map = new HashMap<>(); - char[] chars = word.toCharArray(); - int length = chars.length; - for (int i = 0; i < length; i++) { - //不存在第一个词 - WordDataTreeNode subNode = tempNode.getSubNode(chars[i]); - if (subNode == null) { - return; - } - if (i == (length - 1)) { - //尾字符判断是否结束 - if (!subNode.end()) { - return; - } - if (subNode.getNodeSize() > 0) { - //尾字符下还存在字符,即标识即可 - subNode.end(false); - return; - } - } - if (subNode.end()) { - map.clear(); - } - map.put(chars[i], tempNode); - tempNode = subNode; - } - - for (Map.Entry entry : map.entrySet()) { - WordDataTreeNode value = entry.getValue(); - //节点只有一个就置空 - if (value.getNodeSize() == 1) { - value.clearNode(); - return; - } - //多个就删除 - value.removeNode(entry.getKey()); - } - } /** * 新增敏感词 @@ -143,6 +100,21 @@ public class WordDataTree extends AbstractWordData { } } + @Override + protected synchronized void doRemoveWord(Collection collection) { + for (String word : collection) { + if (StringUtil.isEmpty(word)) { + continue; + } + removeWord(this.root, word); + } + } + + + + + + /** * 获取当前的 Map * @param nowNode 当前节点 @@ -211,4 +183,48 @@ public class WordDataTree extends AbstractWordData { tempNode.end(true); } + + private void removeWord(WordDataTreeNode root, String word){ + WordDataTreeNode tempNode = root; + //需要删除的 + Map map = new HashMap<>(); + char[] chars = word.toCharArray(); + int length = chars.length; + for (int i = 0; i < length; i++) { + //不存在第一个词 + WordDataTreeNode subNode = tempNode.getSubNode(chars[i]); + if (subNode == null) { + return; + } + if (i == (length - 1)) { + //尾字符判断是否结束 + if (!subNode.end()) { + return; + } + if (subNode.getNodeSize() > 0) { + //尾字符下还存在字符,即标识即可 + subNode.end(false); + return; + } + } + if (subNode.end()) { + map.clear(); + } + map.put(chars[i], tempNode); + + tempNode = subNode; + } + + for (Map.Entry entry : map.entrySet()) { + WordDataTreeNode value = entry.getValue(); + //节点只有一个就置空 + if (value.getNodeSize() == 1) { + value.clearNode(); + return; + } + //多个就删除 + value.removeNode(entry.getKey()); + } + } + }