mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 12:18:42 +08:00 
			
		
		
		
	code review:敏感词
This commit is contained in:
		@@ -88,11 +88,11 @@ public interface SensitiveWordService {
 | 
			
		||||
    List<String> validateText(String text, List<String> tags);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 判断文本是否合法
 | 
			
		||||
     * 判断文本是否包含敏感词
 | 
			
		||||
     *
 | 
			
		||||
     * @param text 文本
 | 
			
		||||
     * @param tags 标签数组
 | 
			
		||||
     * @return 是否合法 true-合法 false-不合法
 | 
			
		||||
     * @return 是否包含敏感词
 | 
			
		||||
     */
 | 
			
		||||
    boolean isTextValid(String text, List<String> tags);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -258,7 +258,10 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
 | 
			
		||||
            if (trie == null) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            return trie.isValid(text);
 | 
			
		||||
            // 如果有一个标签不合法,则返回 false 不合法
 | 
			
		||||
            if (!trie.isValid(text)) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -30,9 +30,10 @@ public class SimpleTrie {
 | 
			
		||||
     * @param 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) {
 | 
			
		||||
            Map<Character, Object> child = children;
 | 
			
		||||
            // 遍历每个字符
 | 
			
		||||
@@ -60,7 +61,7 @@ public class SimpleTrie {
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isValid(String text) {
 | 
			
		||||
        // 遍历 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));
 | 
			
		||||
            if (child == null) {
 | 
			
		||||
                continue;
 | 
			
		||||
@@ -126,7 +127,7 @@ public class SimpleTrie {
 | 
			
		||||
     * @param index  开始未知
 | 
			
		||||
     * @param child  节点(当前遍历到的)
 | 
			
		||||
     * @param result 返回敏感词
 | 
			
		||||
     * @return 是否无敏感词 true-无 false-有
 | 
			
		||||
     * @return 是否有敏感词
 | 
			
		||||
     */
 | 
			
		||||
    @SuppressWarnings("unchecked")
 | 
			
		||||
    private static boolean recursionWithResult(String text, int index, Map<Character, Object> child, StringBuilder result) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user