敏感词的简单实现

学习了Java实现敏感词过滤后,一个微调整版。

public class SensitiveWordContainer {
    private final static String END_MARK = "isEnd";
    private final static String END_STATE = "1";
    private final static String CHILD_STATE = "0";


    private Map container;

    private Set<String> words;

    public SensitiveWordContainer() {
        this.container=new ConcurrentHashMap();
    }

    public SensitiveWordContainer(Set<String> words) {
        this.words = words;
        init();
    }



    private void init() {
        container = new ConcurrentHashMap(words.size());
        for (String word : words) {
            addSensitiveWord(word);
        }
    }


    public void addSensitiveWord(String word){
        Map nowMap = container;
        for (int i = 0; i < word.length(); i++) {
            char keyChar = word.charAt(i);
            Object wordMap = nowMap.get(keyChar);
            if (wordMap != null) {
                nowMap = (Map) wordMap;
            } else {
                Map<String, String> newWorMap = new ConcurrentHashMap<>();
                newWorMap.put(END_MARK, CHILD_STATE);
                nowMap.put(keyChar, newWorMap);
                nowMap = newWorMap;
            }
            if (i == word.length() - 1) {
                nowMap.put(END_MARK, END_STATE);
            }
        }
    }

    public void delSensitiveWord(String word){
        Map nowMap = container;
        for (int i = 0; i < word.length(); i++) {
            char keyChar = word.charAt(i);
            Object wordMap = nowMap.get(keyChar);
            if (wordMap != null) {
                nowMap = (Map) wordMap;
                if (i == word.length() - 1) {
                    if(END_STATE.equals(nowMap.get(END_MARK))){
                        nowMap.remove(END_MARK);
                    }
                }
            }
        }
    }

    public boolean checkSensitiveWord(String txt) {
        boolean flag = false;
        for (int i = 0; i < txt.length(); i++) {
            //判断是否包含敏感字符
            int matchFlag = this.doSensitiveWord(txt, i);
            //大于0存在,返回true
            if (matchFlag > 0) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    private int doSensitiveWord(String txt, int beginIndex) {
        boolean flag = false;
        int matchFlag = 0;
        Map nowMap = container;
        for (int i = beginIndex; i < txt.length(); i++) {
            char word = txt.charAt(i);
            nowMap = (Map) nowMap.get(word);
            if (nowMap != null) {
                matchFlag++;
                //如果为最后一个匹配规则,结束循环,返回匹配标识数
                if (END_STATE.equals(nowMap.get(END_MARK))) {
                    flag = true;
                    break;
                }
            } else {
                break;
            }
        }
        //长度必须大于等于1,为词
        if (matchFlag < 2 || !flag) {
            matchFlag = 0;
        }
        return matchFlag;
    }


    public Map getContainer() {
        return container;
    }
} 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值