This commit is contained in:
Looly
2022-02-07 19:04:37 +08:00
parent bbb12fa22d
commit d5916b9998
6 changed files with 60 additions and 36 deletions

View File

@@ -195,11 +195,21 @@ public final class SensitiveUtil {
*/
public static <T> T sensitiveFilter(T bean, boolean isGreedMatch, SensitiveProcessor sensitiveProcessor) {
String jsonText = JSONUtil.toJsonStr(bean);
@SuppressWarnings("unchecked")
final Class<T> c = (Class<T>) bean.getClass();
@SuppressWarnings("unchecked") final Class<T> c = (Class<T>) bean.getClass();
return JSONUtil.toBean(sensitiveFilter(jsonText, isGreedMatch, sensitiveProcessor), c);
}
/**
* 处理过滤文本中的敏感词,默认替换成*
*
* @param text 文本
* @return 敏感词过滤处理后的文本
* @since 5.7.21
*/
public static String sensitiveFilter(String text) {
return sensitiveFilter(text, true, null);
}
/**
* 处理过滤文本中的敏感词,默认替换成*
*
@@ -214,13 +224,14 @@ public final class SensitiveUtil {
}
//敏感词过滤场景下,不需要密集匹配
List<FoundWord> foundWordList = getFoundAllSensitive(text, false, isGreedMatch);
List<FoundWord> foundWordList = getFoundAllSensitive(text, true, isGreedMatch);
if (CollUtil.isEmpty(foundWordList)) {
return text;
}
sensitiveProcessor = sensitiveProcessor == null ? new SensitiveProcessor() {
} : sensitiveProcessor;
Map<Integer, FoundWord> foundWordMap = new HashMap<>(foundWordList.size());
final Map<Integer, FoundWord> foundWordMap = new HashMap<>(foundWordList.size(), 1);
foundWordList.forEach(foundWord -> foundWordMap.put(foundWord.getStartIndex(), foundWord));
int length = text.length();
StringBuilder textStringBuilder = new StringBuilder();

View File

@@ -3,7 +3,6 @@ package cn.hutool.dfa;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import java.util.ArrayList;
@@ -247,15 +246,15 @@ public class WordTree extends HashMap<Character, WordTree> {
List<FoundWord> foundWords = new ArrayList<>();
WordTree current = this;
int length = text.length();
final int length = text.length();
final Filter<Character> charFilter = this.charFilter;
//存放查找到的字符缓存。完整出现一个词时加到findedWords中否则清空
final StrBuilder wordBuffer = StrUtil.strBuilder();
final StrBuilder keyBuffer = StrUtil.strBuilder();
final StringBuilder wordBuffer = StrUtil.builder();
final StringBuilder keyBuffer = StrUtil.builder();
char currentChar;
for (int i = 0; i < length; i++) {
wordBuffer.reset();
keyBuffer.reset();
wordBuffer.setLength(0);
keyBuffer.setLength(0);
for (int j = i; j < length; j++) {
currentChar = text.charAt(j);
// Console.log("i: {}, j: {}, currentChar: {}", i, j, currentChar);
@@ -284,6 +283,7 @@ public class WordTree extends HashMap<Character, WordTree> {
if (false == isDensityMatch) {
//如果非密度匹配,跳过匹配到的词
i = j;
break;
}
if (false == isGreedMatch) {
//如果懒惰匹配(非贪婪匹配)。当遇到第一个结尾标记就结束本轮匹配