master
duhui 2022-08-27 21:22:07 +08:00
parent b01813bd73
commit 3d8ac071d3
1 changed files with 29 additions and 12 deletions

View File

@ -1,5 +1,8 @@
package com.ljsd;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
@ -10,6 +13,7 @@ import java.util.Set;
* @Description:
*/
public class SensitivewordFilter {
private static final Logger LOGGER = LoggerFactory.getLogger(SensitivewordFilter.class);
@SuppressWarnings("rawtypes")
private static Map sensitiveWordMap = null;
public static final int minMatchTYpe = 1; //最小匹配规则
@ -124,28 +128,41 @@ public class SensitivewordFilter {
*/
@SuppressWarnings({"rawtypes"})
public static int CheckSensitiveWord(String txt, int beginIndex, int matchType) {
boolean flag = false; //敏感词结束标识位用于敏感词只有1位的情况
int matchFlag = 0; //匹配标识数默认为0
boolean flag = false;//敏感词结束标识位用于敏感词只有1位的情况
int matchFlag = 0;//匹配标识数默认为0
char word = 0;
Map nowMap = sensitiveWordMap;
boolean logTest = txt.equals("色女") || txt.equals("av") || txt.equals("成人");
for (int i = beginIndex; i < txt.length(); i++) {
word = txt.charAt(i);
nowMap = (Map) nowMap.get(word); //获取指定key
if (nowMap != null) { //存在,则判断是否为最后一个
matchFlag++; //找到相应key匹配标识+1
if ("1".equals(nowMap.get("isEnd"))) { //如果为最后一个匹配规则,结束循环,返回匹配标识数
flag = true; //结束标志位为true
if (SensitivewordFilter.minMatchTYpe == matchType) { //最小规则,直接返回,最大规则还需继续查找
break;
}
}
} else { //不存在,直接返回
if (logTest){
LOGGER.info(nowMap.toString());
}
//不存在,直接返回
if (nowMap == null){
break;
}
//存在,则判断是否为最后一个
//找到相应key匹配标识+1
matchFlag++;
//如果为最后一个匹配规则,结束循环,返回匹配标识数
if ("1".equals(nowMap.get("isEnd"))) {
//结束标志位为true
flag = true;
//最小规则,直接返回,最大规则还需继续查找
if (SensitivewordFilter.minMatchTYpe == matchType) {
break;
}
}
}
if (matchFlag < 1 || !flag) { //长度大于等于1为词
//长度大于等于1为词
if (matchFlag < 1 || !flag) {
matchFlag = 0;
}
if (logTest){
LOGGER.info("=========================结果:{}",matchFlag);
}
return matchFlag;
}