diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ACFilter.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ACFilter.java index ee576ad..7db4f7a 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ACFilter.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ACFilter.java @@ -1,7 +1,7 @@ package com.abin.mallchat.common.common.algorithm.sensitiveWord; -import com.abin.mallchat.common.common.algorithm.ac.ACTrie; -import com.abin.mallchat.common.common.algorithm.ac.MatchResult; +import com.abin.mallchat.common.common.algorithm.sensitiveWord.ac.ACTrie; +import com.abin.mallchat.common.common.algorithm.sensitiveWord.ac.MatchResult; import org.HdrHistogram.ConcurrentHistogram; import org.apache.commons.lang3.StringUtils; diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ACProFilter.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ACProFilter.java index 8791ed1..b471fba 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ACProFilter.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ACProFilter.java @@ -1,7 +1,6 @@ package com.abin.mallchat.common.common.algorithm.sensitiveWord; -import com.abin.mallchat.common.common.algorithm.ac.ACTrie; -import com.abin.mallchat.common.common.algorithm.acpro.ACProTrie; +import com.abin.mallchat.common.common.algorithm.sensitiveWord.acpro.ACProTrie; import io.micrometer.core.instrument.util.StringUtils; import java.util.List; @@ -13,6 +12,7 @@ import java.util.Objects; *@description: 基于ACFilter的优化增强版本 */ public class ACProFilter implements SensitiveWordFilter{ + private ACProTrie acProTrie; @Override diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/ac/ACTrie.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ac/ACTrie.java similarity index 98% rename from mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/ac/ACTrie.java rename to mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ac/ACTrie.java index cdace14..7b7cfdd 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/ac/ACTrie.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ac/ACTrie.java @@ -1,4 +1,4 @@ -package com.abin.mallchat.common.common.algorithm.ac; +package com.abin.mallchat.common.common.algorithm.sensitiveWord.ac; import com.google.common.collect.Lists; diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/ac/ACTrieNode.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ac/ACTrieNode.java similarity index 93% rename from mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/ac/ACTrieNode.java rename to mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ac/ACTrieNode.java index 1068524..cc3cddb 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/ac/ACTrieNode.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ac/ACTrieNode.java @@ -1,4 +1,4 @@ -package com.abin.mallchat.common.common.algorithm.ac; +package com.abin.mallchat.common.common.algorithm.sensitiveWord.ac; import com.google.common.collect.Maps; import lombok.Getter; diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/ac/MatchResult.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ac/MatchResult.java similarity index 86% rename from mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/ac/MatchResult.java rename to mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ac/MatchResult.java index f3b7da4..56d9aa8 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/ac/MatchResult.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/ac/MatchResult.java @@ -1,4 +1,4 @@ -package com.abin.mallchat.common.common.algorithm.ac; +package com.abin.mallchat.common.common.algorithm.sensitiveWord.ac; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/acpro/ACProTrie.java b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/acpro/ACProTrie.java similarity index 69% rename from mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/acpro/ACProTrie.java rename to mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/acpro/ACProTrie.java index de85a3d..1d3bd23 100644 --- a/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/acpro/ACProTrie.java +++ b/mallchat-chat-server/src/main/java/com/abin/mallchat/common/common/algorithm/sensitiveWord/acpro/ACProTrie.java @@ -1,4 +1,4 @@ -package com.abin.mallchat.common.common.algorithm.acpro; +package com.abin.mallchat.common.common.algorithm.sensitiveWord.acpro; import java.util.*; @@ -87,52 +87,42 @@ public class ACProTrie { // 匹配 public String match(String matchWord) { - Word walkNode=root; - char[] wordArray=matchWord.toCharArray(); - for(int i=0;i0) - { - length--; - wordArray[i+length]=MASK; - } - // 直接跳到最后的end节点failOver - i=i+length; - walkNode = sentinelB.failOver; - }else{ - int length=walkNode.depth; - while (length>0){ - length--; - wordArray[i-length]=MASK; - } - walkNode = walkNode.failOver; + // 计算替换长度 + int len = flag ? sentinelB.depth : walkNode.depth; + while (len > 0) { + len--; + int index = flag ? i - walkNode.depth + 1 + len : i - len; + wordArray[index] = MASK; } + // 更新i + i += flag ? sentinelB.depth : 0; + // 更新node + walkNode = flag ? sentinelB.failOver : walkNode.failOver; } } } diff --git a/mallchat-chat-server/src/test/java/com/abin/mallchat/common/SensitiveTest.java b/mallchat-chat-server/src/test/java/com/abin/mallchat/common/SensitiveTest.java index 01818b3..65cf45f 100644 --- a/mallchat-chat-server/src/test/java/com/abin/mallchat/common/SensitiveTest.java +++ b/mallchat-chat-server/src/test/java/com/abin/mallchat/common/SensitiveTest.java @@ -4,9 +4,6 @@ import com.abin.mallchat.common.common.algorithm.sensitiveWord.ACFilter; import com.abin.mallchat.common.common.algorithm.sensitiveWord.ACProFilter; import com.abin.mallchat.common.common.algorithm.sensitiveWord.DFAFilter; import org.junit.Test; - -import java.io.BufferedReader; -import java.io.FileReader; import java.util.*; /** @@ -50,7 +47,7 @@ public class SensitiveTest { @Test public void ACMulti() { - List sensitiveList = Arrays.asList("白痴", "你是白痴", "白痴吗"); + List sensitiveList = Arrays.asList("你是白痴","你是"); ACFilter instance = new ACFilter(); instance.loadWord(sensitiveList); System.out.println(instance.filter("你是白痴吗"));