mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-26 09:11:52 +08:00
code review:敏感词
This commit is contained in:
parent
630890d0ac
commit
14f006088a
@ -88,11 +88,11 @@ public interface SensitiveWordService {
|
|||||||
List<String> validateText(String text, List<String> tags);
|
List<String> validateText(String text, List<String> tags);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断文本是否合法
|
* 判断文本是否包含敏感词
|
||||||
*
|
*
|
||||||
* @param text 文本
|
* @param text 文本
|
||||||
* @param tags 标签数组
|
* @param tags 标签数组
|
||||||
* @return 是否合法 true-合法 false-不合法
|
* @return 是否包含敏感词
|
||||||
*/
|
*/
|
||||||
boolean isTextValid(String text, List<String> tags);
|
boolean isTextValid(String text, List<String> tags);
|
||||||
|
|
||||||
|
@ -258,7 +258,10 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
|
|||||||
if (trie == null) {
|
if (trie == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
return trie.isValid(text);
|
// 如果有一个标签不合法,则返回 false 不合法
|
||||||
|
if (!trie.isValid(text)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -30,9 +30,10 @@ public class SimpleTrie {
|
|||||||
* @param strs 字符串数组
|
* @param strs 字符串数组
|
||||||
*/
|
*/
|
||||||
public SimpleTrie(Collection<String> strs) {
|
public SimpleTrie(Collection<String> strs) {
|
||||||
children = new HashMap<>();
|
// 排序,优先使用较短的前缀
|
||||||
|
strs = CollUtil.sort(strs, String::compareTo);
|
||||||
// 构建树
|
// 构建树
|
||||||
strs = CollUtil.sort(strs, String::compareTo); // 排序,优先使用较短的前缀
|
children = new HashMap<>();
|
||||||
for (String str : strs) {
|
for (String str : strs) {
|
||||||
Map<Character, Object> child = children;
|
Map<Character, Object> child = children;
|
||||||
// 遍历每个字符
|
// 遍历每个字符
|
||||||
@ -60,7 +61,7 @@ public class SimpleTrie {
|
|||||||
*/
|
*/
|
||||||
public boolean isValid(String text) {
|
public boolean isValid(String text) {
|
||||||
// 遍历 text,使用每一个 [i, n) 段的字符串,使用 children 前缀树匹配,是否包含敏感词
|
// 遍历 text,使用每一个 [i, n) 段的字符串,使用 children 前缀树匹配,是否包含敏感词
|
||||||
for (int i = 0; i < text.length() ; i++) {
|
for (int i = 0; i < text.length(); i++) {
|
||||||
Map<Character, Object> child = (Map<Character, Object>) children.get(text.charAt(i));
|
Map<Character, Object> child = (Map<Character, Object>) children.get(text.charAt(i));
|
||||||
if (child == null) {
|
if (child == null) {
|
||||||
continue;
|
continue;
|
||||||
@ -126,7 +127,7 @@ public class SimpleTrie {
|
|||||||
* @param index 开始未知
|
* @param index 开始未知
|
||||||
* @param child 节点(当前遍历到的)
|
* @param child 节点(当前遍历到的)
|
||||||
* @param result 返回敏感词
|
* @param result 返回敏感词
|
||||||
* @return 是否无敏感词 true-无 false-有
|
* @return 是否有敏感词
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private static boolean recursionWithResult(String text, int index, Map<Character, Object> child, StringBuilder result) {
|
private static boolean recursionWithResult(String text, int index, Map<Character, Object> child, StringBuilder result) {
|
||||||
|
@ -66,7 +66,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
|
|||||||
// 调用
|
// 调用
|
||||||
sensitiveWordService.initLocalCache();
|
sensitiveWordService.initLocalCache();
|
||||||
// 断言 sensitiveWordTagsCache 缓存
|
// 断言 sensitiveWordTagsCache 缓存
|
||||||
assertEquals(SetUtils.asSet("论坛", "蔬菜","测试"), sensitiveWordService.getSensitiveWordTagSet());
|
assertEquals(SetUtils.asSet("论坛", "蔬菜", "测试"), sensitiveWordService.getSensitiveWordTagSet());
|
||||||
// 断言 sensitiveWordCache
|
// 断言 sensitiveWordCache
|
||||||
assertEquals(4, sensitiveWordService.getSensitiveWordCache().size());
|
assertEquals(4, sensitiveWordService.getSensitiveWordCache().size());
|
||||||
assertPojoEquals(wordDO1, sensitiveWordService.getSensitiveWordCache().get(0));
|
assertPojoEquals(wordDO1, sensitiveWordService.getSensitiveWordCache().get(0));
|
||||||
@ -239,7 +239,6 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
|
|||||||
testInitLocalCache();
|
testInitLocalCache();
|
||||||
// 准备参数
|
// 准备参数
|
||||||
String text = "你是傻瓜,你是笨蛋";
|
String text = "你是傻瓜,你是笨蛋";
|
||||||
|
|
||||||
// 调用
|
// 调用
|
||||||
List<String> result = sensitiveWordService.validateText(text, null);
|
List<String> result = sensitiveWordService.validateText(text, null);
|
||||||
// 断言
|
// 断言
|
||||||
@ -247,7 +246,6 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
|
|||||||
|
|
||||||
// 准备参数
|
// 准备参数
|
||||||
String text2 = "你是傻瓜,你是笨蛋,你是白";
|
String text2 = "你是傻瓜,你是笨蛋,你是白";
|
||||||
|
|
||||||
// 调用
|
// 调用
|
||||||
List<String> result2 = sensitiveWordService.validateText(text2, null);
|
List<String> result2 = sensitiveWordService.validateText(text2, null);
|
||||||
// 断言
|
// 断言
|
||||||
@ -259,7 +257,6 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
|
|||||||
testInitLocalCache();
|
testInitLocalCache();
|
||||||
// 准备参数
|
// 准备参数
|
||||||
String text = "你是傻瓜,你是笨蛋";
|
String text = "你是傻瓜,你是笨蛋";
|
||||||
|
|
||||||
// 调用
|
// 调用
|
||||||
List<String> result = sensitiveWordService.validateText(text, singletonList("论坛"));
|
List<String> result = sensitiveWordService.validateText(text, singletonList("论坛"));
|
||||||
// 断言
|
// 断言
|
||||||
@ -268,12 +265,10 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
|
|||||||
|
|
||||||
// 准备参数
|
// 准备参数
|
||||||
String text2 = "你是白";
|
String text2 = "你是白";
|
||||||
|
|
||||||
// 调用
|
// 调用
|
||||||
List<String> result2 = sensitiveWordService.validateText(text2, singletonList("测试"));
|
List<String> result2 = sensitiveWordService.validateText(text2, singletonList("测试"));
|
||||||
// 断言
|
// 断言
|
||||||
assertEquals(singletonList("白"), result2);
|
assertEquals(singletonList("白"), result2);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -281,13 +276,11 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
|
|||||||
testInitLocalCache();
|
testInitLocalCache();
|
||||||
// 准备参数
|
// 准备参数
|
||||||
String text = "你是傻瓜,你是笨蛋";
|
String text = "你是傻瓜,你是笨蛋";
|
||||||
|
|
||||||
// 调用,断言
|
// 调用,断言
|
||||||
assertFalse(sensitiveWordService.isTextValid(text, null));
|
assertFalse(sensitiveWordService.isTextValid(text, null));
|
||||||
|
|
||||||
// 准备参数
|
// 准备参数
|
||||||
String text2 = "你是白";
|
String text2 = "你是白";
|
||||||
|
|
||||||
// 调用,断言
|
// 调用,断言
|
||||||
assertFalse(sensitiveWordService.isTextValid(text2, null));
|
assertFalse(sensitiveWordService.isTextValid(text2, null));
|
||||||
}
|
}
|
||||||
@ -297,13 +290,11 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
|
|||||||
testInitLocalCache();
|
testInitLocalCache();
|
||||||
// 准备参数
|
// 准备参数
|
||||||
String text = "你是傻瓜,你是笨蛋";
|
String text = "你是傻瓜,你是笨蛋";
|
||||||
|
|
||||||
// 调用,断言
|
// 调用,断言
|
||||||
assertFalse(sensitiveWordService.isTextValid(text, singletonList("论坛")));
|
assertFalse(sensitiveWordService.isTextValid(text, singletonList("论坛")));
|
||||||
|
|
||||||
// 准备参数
|
// 准备参数
|
||||||
String text2 = "你是白";
|
String text2 = "你是白";
|
||||||
|
|
||||||
// 调用,断言
|
// 调用,断言
|
||||||
assertFalse(sensitiveWordService.isTextValid(text2, singletonList("测试")));
|
assertFalse(sensitiveWordService.isTextValid(text2, singletonList("测试")));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user