Commit 14f006088a6d4c4d0cee5694d4def487948141d2
1 parent
630890d0
code review:敏感词
Showing
4 changed files
with
12 additions
and
17 deletions
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordService.java
| ... | ... | @@ -88,11 +88,11 @@ public interface SensitiveWordService { |
| 88 | 88 | List<String> validateText(String text, List<String> tags); |
| 89 | 89 | |
| 90 | 90 | /** |
| 91 | - * 判断文本是否合法 | |
| 91 | + * 判断文本是否包含敏感词 | |
| 92 | 92 | * |
| 93 | 93 | * @param text 文本 |
| 94 | 94 | * @param tags 标签数组 |
| 95 | - * @return 是否合法 true-合法 false-不合法 | |
| 95 | + * @return 是否包含敏感词 | |
| 96 | 96 | */ |
| 97 | 97 | boolean isTextValid(String text, List<String> tags); |
| 98 | 98 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordServiceImpl.java
| ... | ... | @@ -258,7 +258,10 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 258 | 258 | if (trie == null) { |
| 259 | 259 | continue; |
| 260 | 260 | } |
| 261 | - return trie.isValid(text); | |
| 261 | + // 如果有一个标签不合法,则返回 false 不合法 | |
| 262 | + if (!trie.isValid(text)) { | |
| 263 | + return false; | |
| 264 | + } | |
| 262 | 265 | } |
| 263 | 266 | return true; |
| 264 | 267 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/util/collection/SimpleTrie.java
| ... | ... | @@ -30,9 +30,10 @@ public class SimpleTrie { |
| 30 | 30 | * @param strs 字符串数组 |
| 31 | 31 | */ |
| 32 | 32 | public SimpleTrie(Collection<String> strs) { |
| 33 | - children = new HashMap<>(); | |
| 33 | + // 排序,优先使用较短的前缀 | |
| 34 | + strs = CollUtil.sort(strs, String::compareTo); | |
| 34 | 35 | // 构建树 |
| 35 | - strs = CollUtil.sort(strs, String::compareTo); // 排序,优先使用较短的前缀 | |
| 36 | + children = new HashMap<>(); | |
| 36 | 37 | for (String str : strs) { |
| 37 | 38 | Map<Character, Object> child = children; |
| 38 | 39 | // 遍历每个字符 |
| ... | ... | @@ -60,7 +61,7 @@ public class SimpleTrie { |
| 60 | 61 | */ |
| 61 | 62 | public boolean isValid(String text) { |
| 62 | 63 | // 遍历 text,使用每一个 [i, n) 段的字符串,使用 children 前缀树匹配,是否包含敏感词 |
| 63 | - for (int i = 0; i < text.length() ; i++) { | |
| 64 | + for (int i = 0; i < text.length(); i++) { | |
| 64 | 65 | Map<Character, Object> child = (Map<Character, Object>) children.get(text.charAt(i)); |
| 65 | 66 | if (child == null) { |
| 66 | 67 | continue; |
| ... | ... | @@ -126,7 +127,7 @@ public class SimpleTrie { |
| 126 | 127 | * @param index 开始未知 |
| 127 | 128 | * @param child 节点(当前遍历到的) |
| 128 | 129 | * @param result 返回敏感词 |
| 129 | - * @return 是否无敏感词 true-无 false-有 | |
| 130 | + * @return 是否有敏感词 | |
| 130 | 131 | */ |
| 131 | 132 | @SuppressWarnings("unchecked") |
| 132 | 133 | private static boolean recursionWithResult(String text, int index, Map<Character, Object> child, StringBuilder result) { | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordServiceImplTest.java
| ... | ... | @@ -66,7 +66,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 66 | 66 | // 调用 |
| 67 | 67 | sensitiveWordService.initLocalCache(); |
| 68 | 68 | // 断言 sensitiveWordTagsCache 缓存 |
| 69 | - assertEquals(SetUtils.asSet("论坛", "蔬菜","测试"), sensitiveWordService.getSensitiveWordTagSet()); | |
| 69 | + assertEquals(SetUtils.asSet("论坛", "蔬菜", "测试"), sensitiveWordService.getSensitiveWordTagSet()); | |
| 70 | 70 | // 断言 sensitiveWordCache |
| 71 | 71 | assertEquals(4, sensitiveWordService.getSensitiveWordCache().size()); |
| 72 | 72 | assertPojoEquals(wordDO1, sensitiveWordService.getSensitiveWordCache().get(0)); |
| ... | ... | @@ -239,7 +239,6 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 239 | 239 | testInitLocalCache(); |
| 240 | 240 | // 准备参数 |
| 241 | 241 | String text = "你是傻瓜,你是笨蛋"; |
| 242 | - | |
| 243 | 242 | // 调用 |
| 244 | 243 | List<String> result = sensitiveWordService.validateText(text, null); |
| 245 | 244 | // 断言 |
| ... | ... | @@ -247,7 +246,6 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 247 | 246 | |
| 248 | 247 | // 准备参数 |
| 249 | 248 | String text2 = "你是傻瓜,你是笨蛋,你是白"; |
| 250 | - | |
| 251 | 249 | // 调用 |
| 252 | 250 | List<String> result2 = sensitiveWordService.validateText(text2, null); |
| 253 | 251 | // 断言 |
| ... | ... | @@ -259,7 +257,6 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 259 | 257 | testInitLocalCache(); |
| 260 | 258 | // 准备参数 |
| 261 | 259 | String text = "你是傻瓜,你是笨蛋"; |
| 262 | - | |
| 263 | 260 | // 调用 |
| 264 | 261 | List<String> result = sensitiveWordService.validateText(text, singletonList("论坛")); |
| 265 | 262 | // 断言 |
| ... | ... | @@ -268,12 +265,10 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 268 | 265 | |
| 269 | 266 | // 准备参数 |
| 270 | 267 | String text2 = "你是白"; |
| 271 | - | |
| 272 | 268 | // 调用 |
| 273 | 269 | List<String> result2 = sensitiveWordService.validateText(text2, singletonList("测试")); |
| 274 | 270 | // 断言 |
| 275 | 271 | assertEquals(singletonList("白"), result2); |
| 276 | - | |
| 277 | 272 | } |
| 278 | 273 | |
| 279 | 274 | @Test |
| ... | ... | @@ -281,13 +276,11 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 281 | 276 | testInitLocalCache(); |
| 282 | 277 | // 准备参数 |
| 283 | 278 | String text = "你是傻瓜,你是笨蛋"; |
| 284 | - | |
| 285 | 279 | // 调用,断言 |
| 286 | 280 | assertFalse(sensitiveWordService.isTextValid(text, null)); |
| 287 | 281 | |
| 288 | 282 | // 准备参数 |
| 289 | 283 | String text2 = "你是白"; |
| 290 | - | |
| 291 | 284 | // 调用,断言 |
| 292 | 285 | assertFalse(sensitiveWordService.isTextValid(text2, null)); |
| 293 | 286 | } |
| ... | ... | @@ -297,13 +290,11 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 297 | 290 | testInitLocalCache(); |
| 298 | 291 | // 准备参数 |
| 299 | 292 | String text = "你是傻瓜,你是笨蛋"; |
| 300 | - | |
| 301 | 293 | // 调用,断言 |
| 302 | 294 | assertFalse(sensitiveWordService.isTextValid(text, singletonList("论坛"))); |
| 303 | 295 | |
| 304 | 296 | // 准备参数 |
| 305 | 297 | String text2 = "你是白"; |
| 306 | - | |
| 307 | 298 | // 调用,断言 |
| 308 | 299 | assertFalse(sensitiveWordService.isTextValid(text2, singletonList("测试"))); |
| 309 | 300 | } | ... | ... |