使用数据结构过滤敏感词算法

前缀树

名称:Tire、字典树、查找树

特点:查找效率高,消耗内存大

应用:字符串检索、词频统计、字符串排序等

敏感词过滤器

定义前缀树

package com.nowcoder.community.util;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class SensitiveFilter {
    //描述前缀树的每一个节点
    private class TrieNode{
        //关键词结束标识
        private boolean isKeywordEnd=false;
        //当前节点的子节点(key是下级节点的字符,value是下级)                                                          ·   999999
        private Map<Character,TrieNode> subModes=new HashMap<>();

        public boolean isKeywordEnd() {
            return isKeywordEnd;
        }

        public void setKeywordEnd(boolean keywordEnd) {
            isKeywordEnd = keywordEnd;
        }
        //添加子节点的方法
        public void addSubNode(Character c,TrieNode node){
            subModes.put(c,node);
        }
        //获取子节点
        public TrieNode getSubNode(Character c){
           return subModes.get(c);
        }
    }
}

根据敏感词,初始化前缀树

在sensitive.words.txt文件中一行定义一个敏感词

    private static final Logger logger = LoggerFactory.getLogger(SensitiveFilter.class);
   //替换符
    private static final String REPLACEMENT="***";
    //初始化根节点
    private TrieNode rootNode=new TrieNode();
    @PostConstruct//在服务启动的时候会初始化这个方法,解决了只需要初始化一次的问题
    public void init(){
        try(        InputStream is= this.getClass().getClassLoader().getResourceAsStream("sensitive-words.txt");
                    BufferedReader reader=new BufferedReader(new InputStreamReader(is));
        ){
            //读取每一个敏感词
            String keyword;
            while ((keyword=reader.readLine())!=null){
                 //添加到前缀树
                this.addKeyWord(keyword);
            }
        }catch (IOException e){
            logger.error("加载敏感词文件失败"+e.getMessage());
        }
    }
    //将一个敏感词添加到前缀树当中
    private void addKeyWord(String keyword){
        TrieNode tempNode=rootNode;
        for(int i=0;i<keyword.length();i++){
            char c=keyword.charAt(i);
          TrieNode subNode=tempNode.getSubNode(c);
          if(subNode==null){
              //初始化子节点
              subNode=new TrieNode();
              tempNode.addSubNode(c,subNode);
          }
          //让指针指向子节点,进入下一轮循环
            tempNode=subNode;
          //设置结束标识
            if(i==keyword.length()-1){
                tempNode.setKeywordEnd(true);
            }
        }
    }

编写过滤敏感词的方法

 /**
     * 过滤敏感词
     * @param text 待过滤的文本
     * @return 过滤后的文本
     */
    public String filter(String text){
        if(StringUtils.isBlank(text)){
            return null;
        }
        //指针1
        TrieNode tempNode=rootNode;
        //指针2
        int begin=0;
        //指针3
        int position=0;
        //结果
        StringBuilder sb=new StringBuilder();
        while (position<text.length()){
            char c=text.charAt(position);
            //跳过符号
            if(isSymbol(c)){
                //若指针1指向根节点,将此符号计入结果,让指针2向下走一步
                if(tempNode==rootNode){
                    sb.append(c);
                    begin++;
                }
                //无论符号在开头或中间,指针3都向下走一步
                position++;
                continue;
            }
            //检查下级节点
           tempNode= tempNode.getSubNode(c);
            if(tempNode==null){
                //以begin开头的字符串不是敏感词
                sb.append(text.charAt(begin));
                //进入下一个位置
                position=++begin;
                //重新指向根节点
                tempNode=rootNode;
            }else if(tempNode.isKeywordEnd()){
                //发现敏感词,将begin-position字符串替换掉
                sb.append(REPLACEMENT);
                //进入下一个位置
                begin=++position;
                //重新指向根节点
                tempNode=rootNode;
            }else {
                //检查下一个字符
                position++;
            }
        }
        //将最后一批字符计入结果
        sb.append(text.substring(begin));
        return sb.toString();
    }
    //判断是否为符号
    private boolean isSymbol(Character c){
        //0x2E80-0x9FFF是东亚文字范围
        return !CharUtils.isAsciiAlphanumeric(c)&&(c<0x2E80||c>0x9FFF);
    }

测试

package com.nowcoder.community;

import com.nowcoder.community.util.SensitiveFilter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class SensitiveTest {
    @Autowired
    private SensitiveFilter sensitiveFilter;
    @Test
    public void testSensitiveFilter(){
        String text="这里可以赌※博,可以嫖娼,哈哈哈";
        System.out.println(sensitiveFilter.filter(text));
    }
}

原理

 

检测目标以字符为单位

 根据敏感词初始化前缀树,根节点为root,根据每个敏感词的字符来添加节点,将末节点进行标记。给定一个字符串“xwabfabcff”来过滤敏感词abc、bf、be,需要三个指针,指针1指向前缀树,指针2、指针3指向字符串,默认初始指针1指向root,指针2、3指向字符串的起始端。当过滤开始,指针2首先会停在x,然后指针1判断前缀树是否有x的起点,判断不是不需要过滤x,指针2会向后移动一位到w,随之指针3也会向后移动一位,类似判断没有w开始的敏感词不需要过滤w,指针2和3向后移动一位到a,指针1进行判断有a为起始端的树,然后指针2会停在a,指针3往后移动一位到b,指针1也会向下查看a下节点是否有b,判断有,但是没有到达末节点,指针3会继续向后移动,到f,指针1会在b在查看是否有f,判断没有不需要过滤a,指针2会移动到b,指针3也会回到指针2的位置,类似的判断bf是敏感词,会将其过滤为***代替,最终的字符串后过滤为"xwa*****ff"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大磊程序员(“hello world”)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值