package com.wt.miniapp.module.playstation.common.impl;
import com.wt.base.common.redis.RedisService;
import com.wt.miniapp.core.base.service.impl.BaseServiceImpl;
import com.wt.miniapp.core.util.CollectionKit;
import com.wt.miniapp.module.commentpaper.entity.SensitiveWord;
import com.wt.miniapp.module.playstation.common.SensitiveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
/**
* @Author: Heyi
* @Description: 敏感词过滤接口
* @Date 16:04 2018/8/10
*/
@Service
public class SensitiveWordServiceImpl extends BaseServiceImpl<SensitiveWord> implements SensitiveWordService {
@Autowired
private RedisService redisService;
private static final int MinMatchTYpe = 1; //最小匹配规则,如:敏感词库["中国","中国人"],语句:"我是中国人",匹配结果:我是[中国]人
private static final int MaxMatchType = 2; //最大匹配规则,如:敏感词库["中国","中国人"],语句:"我是中国人",匹配结果:我是[中国人]
private static final String SENSITIVE_WORD = "SensitiveWord"; // 敏感词的key
private static final Long SENSITIVE_WORD_TIME = 60 * 60 * 24 * 3L;
@Override
public String replaceSensitiveWordToNew(String text) {
HashMap hashMap = getSensitiveWords();
String newStr = replaceSensitiveWord(text, '*', hashMap);
return newStr;
}
@Override
public Boolean containsSensitiveWordToNew(String text) {
HashMap hashMap = getSensitiveWords();
return contains(text, 1, hashMap);
}
private HashMap getSensitiveWords(){
//redis中取敏感词
HashMap hashMap = (HashMap) redisService.getObject(SENSITIVE_WORD);
//没有就查数据库
if (CollectionKit.isEmpty(hashMap)) {
List<SensitiveWord> sensitiveWords = sqlManager.template(SensitiveWord.builder().isDeleted(0).build());
//去重
List<String> list = sensitiveWords.stream().map(SensitiveWord::getWord).distinct().collect(Collectors.toList());
//放入缓存中
hashMap = getSensitiveWordMap(list);
//有效期为三天
redisService.setObject(SENSITIVE_WORD, hashMap, SENSITIVE_WORD_TIME);
}
return hashMap;
}
private HashMap getSensitiveWordMap(List<String> sensitiveWordList) {
//初始化敏感词容器,减少扩容操作
HashMap sensitiveWordMap = new HashMap(sensitiveWordList.size());
String key;
Map nowMap;
Map<String, String> newWorMap;
//迭代sensitiveWordSet
Iterator<String> iterator = sensitiveWordList.iterator();
while (iterator.hasNext()) {
//关键字
key = iterator.next();
nowMap = sensitiveWordMap;
for (int i = 0; i < key.length(); i++) {
//转换成char型
char keyChar = key.charAt(i);
//库中获取关键字
Object wordMap = nowMap.get(keyChar);
//如果存在该key,直接赋值,用于下一个循环获取
if (wordMap != null) {
nowMap = (Map) wordMap;
} else {
//不存在则,则构建一个map,同时将isEnd设置为0,因为他不是最后一个
newWorMap = new HashMap<>();
//不是最后一个
newWorMap.put("isEnd", "0");
nowMap.put(keyChar, newWorMap);
nowMap = newWorMap;
}
if (i == key.length() - 1) {
//最后一个
nowMap.put("isEnd", "1");
}
}
}
return sensitiveWordMap;
}
/**
* 判断文字是否包含敏感字符
*
* @param txt 文字
* @param matchType 匹配规则 1:最小匹配规则,2:最大匹配规则
* @return 若包含返回true,否则返回false
*/
private boolean contains(String txt, int matchType, HashMap sensitiveWordMap) {
boolean flag = false;
for (int i = 0; i < txt.length(); i++) {
//判断是否包含敏感字符
int matchFlag = checkSensitiveWord(txt, i, matchType, sensitiveWordMap);
//大于0存在,返回true
if (matchFlag > 0) {
flag = true;
break;
}
}
return flag;
}
/**
* 判断文字是否包含敏感字符
*
* @param txt 文字
* @return 若包含返回true,否则返回false
*/
private boolean contains(String txt, HashMap sensitiveWordMap) {
return contains(txt, MaxMatchType, sensitiveWordMap);
}
/**
* 获取文字中的敏感词
*
* @param txt 文字
* @param matchType 匹配规则 1:最小匹配规则,2:最大匹配规则
* @return
*/
private Set<String> getSensitiveWord(String txt, int matchType, HashMap sensitiveWordMap) {
Set<String> sensitiveWordList = new HashSet<>();
for (int i = 0; i < txt.length(); i++) {
//判断是否包含敏感字符
int length = checkSensitiveWord(txt, i, matchType, sensitiveWordMap);
if (length > 0) {//存在,加入list中
sensitiveWordList.add(txt.substring(i, i + length));
i = i + length - 1;//减1的原因,是因为for会自增
}
}
return sensitiveWordList;
}
/**
* 获取文字中的敏感词
*
* @param txt 文字
* @return
*/
private Set<String> getSensitiveWord(String txt, HashMap sensitiveWordMap) {
return getSensitiveWord(txt, MaxMatchType, sensitiveWordMap);
}
/**
* 替换敏感字字符
*
* @param txt 文本
* @param replaceChar 替换的字符,匹配的敏感词以字符逐个替换,如 语句:我爱中国人 敏感词:中国人,替换字符:*, 替换结果:我爱***
* @param matchType 敏感词匹配规则
* @return
*/
private String replaceSensitiveWord(String txt, char replaceChar, int matchType, HashMap sensitiveWordMap) {
String resultTxt = txt;
//获取所有的敏感词
Set<String> set = getSensitiveWord(txt, matchType, sensitiveWordMap);
Iterator<String> iterator = set.iterator();
String word;
String replaceString;
while (iterator.hasNext()) {
word = iterator.next();
replaceString = getReplaceChars(replaceChar, word.length());
resultTxt = resultTxt.replaceAll(word, replaceString);
}
return resultTxt;
}
/**
* 替换敏感字字符
*
* @param txt 文本
* @param replaceChar 替换的字符,匹配的敏感词以字符逐个替换,如 语句:我爱中国人 敏感词:中国人,替换字符:*, 替换结果:我爱***
* @return
*/
private String replaceSensitiveWord(String txt, char replaceChar, HashMap sensitiveWordMap) {
return replaceSensitiveWord(txt, replaceChar, MaxMatchType, sensitiveWordMap);
}
/**
* 替换敏感字字符
*
* @param txt 文本
* @param replaceStr 替换的字符串,匹配的敏感词以字符逐个替换,如 语句:我爱中国人 敏感词:中国人,替换字符串:[屏蔽],替换结果:我爱[屏蔽]
* @param matchType 敏感词匹配规则
* @return
*/
private String replaceSensitiveWord(String txt, String replaceStr, int matchType, HashMap sensitiveWordMap) {
String resultTxt = txt;
//获取所有的敏感词
Set<String> set = getSensitiveWord(txt, matchType, sensitiveWordMap);
Iterator<String> iterator = set.iterator();
String word;
while (iterator.hasNext()) {
word = iterator.next();
resultTxt = resultTxt.replaceAll(word, replaceStr);
}
return resultTxt;
}
/**
* 替换敏感字字符
*
* @param txt 文本
* @param replaceStr 替换的字符串,匹配的敏感词以字符逐个替换,如 语句:我爱中国人 敏感词:中国人,替换字符串:[屏蔽],替换结果:我爱[屏蔽]
* @return
*/
private String replaceSensitiveWord(String txt, String replaceStr, HashMap sensitiveWordMap) {
return replaceSensitiveWord(txt, replaceStr, MaxMatchType, sensitiveWordMap);
}
/**
* 获取替换字符串
*
* @param replaceChar
* @param length
* @return
*/
private String getReplaceChars(char replaceChar, int length) {
String resultReplace = String.valueOf(replaceChar);
for (int i = 1; i < length; i++) {
resultReplace += replaceChar;
}
return resultReplace;
}
/**
* 检查文字中是否包含敏感字符,检查规则如下:<br>
*
* @param txt
* @param beginIndex
* @param matchType
* @return 如果存在,则返回敏感词字符的长度,不存在返回0
*/
private int checkSensitiveWord(String txt, int beginIndex, int matchType, HashMap sensitiveWordMap) {
//敏感词结束标识位:用于敏感词只有1位的情况
boolean flag = false;
//匹配标识数默认为0
int matchFlag = 0;
char word;
Map nowMap = sensitiveWordMap;
for (int i = beginIndex; i < txt.length(); i++) {
word = txt.charAt(i);
//获取指定key
nowMap = (Map) nowMap.get(word);
if (nowMap != null) {//存在,则判断是否为最后一个
//找到相应key,匹配标识+1
matchFlag++;
//如果为最后一个匹配规则,结束循环,返回匹配标识数
if ("1".equals(nowMap.get("isEnd"))) {
//结束标志位为true
flag = true;
//最小规则,直接返回,最大规则还需继续查找
if (MinMatchTYpe == matchType) {
break;
}
}
} else {//不存在,直接返回
break;
}
}
if (matchFlag < 2 || !flag) {//长度必须大于等于1,为词
matchFlag = 0;
}
return matchFlag;
}
}
Map的使用
最新推荐文章于 2023-06-16 19:36:45 发布