package cn.edu.njtech.community.util;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
@Component
public class SensitiveFilter {
private static final Logger logger = LoggerFactory.getLogger(SensitiveFilter.class);
/**
* 过滤敏感词
* @param待过滤的文本
* @return过滤后的文本
*/
public String filter(String text){
if (StringUtils.isBlank(text)){
return null;
}
// 指针1
TrieNode tmpNode = root;
// 指针2
int begin = 0;
// 指针3
int pos = 0;
// 结果
StringBuilder stringBuilder = new StringBuilder();
while (pos < text.length()){
char c = text.charAt(pos);
// 跳过符号
if(isSymbol(c)){
// 若指针1指向根节点,将此符号计入结果,让指针2向下走一步
if(tmpNode == root){
stringBuilder.append(c);
begin++;
}
// 指针3肯定走
pos++;
continue;
}
// 检查下级节点
tmpNode = tmpNode.getSubNode(c);
if (tmpNode == null){
// 以begin开头的词不是敏感词
stringBuilder.append(text.charAt(begin));
// 进入下一个位置
pos = ++begin;
// 重新指向根节点
tmpNode = root;
}else if (tmpNode.isKeyWordEnd()){
// 发现敏感词 将begin~pos字符串替换掉
stringBuilder.append(REPLACEMENT);
begin = ++pos;
tmpNode = root;
}else{
// 继续检查下一个字符
pos++;
}
}
// 将最后一批字符计入结果
stringBuilder.append(text.substring(begin));
return stringBuilder.toString();
}
private boolean isSymbol(Character s){
// 0x2E80~0x9FFF 是东亚文字范围
return !CharUtils.isAsciiAlphanumeric(s) && (s < 0x2E80 || s > 0x9FFF);
}
// 替换符
private static final String REPLACEMENT = "***";
// 根节点初始化
private TrieNode root = new TrieNode();
@PostConstruct
public void init(){
try(
InputStream is = this.getClass().getClassLoader().getResourceAsStream("sensitive-word.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
){
String keyword;
while ((keyword = bufferedReader.readLine()) != null){
// 添加到前缀树
this.addKeyword(keyword);
}
}catch (IOException e){
logger.error("加载敏感词失败:"+e.getMessage());
}
}
// 将一个敏感词添加到前缀树中
private void addKeyword(String keyword){
TrieNode tmpNode = root;
for(int i= 0;i< keyword.length();i++){
char c = keyword.charAt(i);
TrieNode subNode = tmpNode.getSubNode(c);
if(subNode == null){
// 初始化子节点
subNode = new TrieNode();
tmpNode.addSubNode(c,subNode);
}
// 指向子节点,进行下一轮循环
tmpNode = subNode;
// 设置结束的标识
if (i == keyword.length() - 1){
tmpNode.setKeyWordEnd(true);
}
}
}
// 前缀树
private class TrieNode{
// 描述关键词结束的标识
private boolean isKeyWordEnd = false;
// 当前节点的子节点(key是下级字符,value是下级节点)
private Map<Character,TrieNode> subNodes = new HashMap<>();
public boolean isKeyWordEnd() {
return isKeyWordEnd;
}
public void setKeyWordEnd(boolean keyWordEnd) {
isKeyWordEnd = keyWordEnd;
}
// 添加子节点的方法
public void addSubNode(Character c,TrieNode node){
subNodes.put(c,node);
}
// 获取子节点的办法
public TrieNode getSubNode(Character c){
return subNodes.get(c);
}
}
}