敏感词处理算法

public class Minganci {

    static  Map mgcMap = new HashMap<>();
    public static void  add(String mgcs) {
        //{"广":{"isEnd":"0,"告":{"isEnd":"1","词":{"isEnd":"1"}}},"告":{"isEnd":"0,"诉":{"isEnd":"1"}}}
        //广告,广告词,告诉,哈哈
        String[] split = mgcs.split(",");
        for (int i = 0; i < split.length; i++) {
            String mgc = split[i];
            Map currentMap = mgcMap;
            Map wordMap  = null;
            //广告
           // 广:{告:{isEnd=1}, isEnd=0}
            for (int j = 0; j < mgc.length(); j++) {
                wordMap = (Map)currentMap.get(mgc.charAt(j));
                if (wordMap == null) {
                    wordMap = new HashMap<>();
                    Map<String,String> isEnd = new HashMap<>();
                    if (j < mgc.length() - 1) {
                        isEnd.put("isEnd","0");
                    }else {
                        isEnd.put("isEnd","1");
                    }
                    wordMap.put(mgc.charAt(j),isEnd);
                    currentMap.putAll(wordMap);
                    currentMap = isEnd;
                }else {
                    //{isEnd=1}
                    currentMap = wordMap;
                }


            }
        }

    }

    /**
     *
     * @param string 查询的字符串
     * @param type  0-短匹配  1-长匹配
     * @return
     */
    public static boolean checkMgc(String string,String type) {
        for (int i = 0; i < string.length(); i++) {
            Map currentMap = (Map)mgcMap.get(string.charAt(i));
            if (currentMap != null) {
                int num = 1;
                //说明第i个字在敏感词map的一级目录中有,这个时候需要接着这个敏感词继续查下去
                for (int j = i+1; j < string.length()-1; j++) {
                    currentMap = (Map)currentMap.get(string.charAt(j));
                    if (currentMap != null) {
                        num++;
                        //判断是否到敏感词尾
                        String isEnd = (String)currentMap.get("isEnd");
                        if (isEnd.equals("1") && type.equals("0")) {
                            return true;
                        }
                    }else {
                        break;
                    }
                }
                //两个字以上才算是词
                if (num > 1) {
                    return true;
                }
            }
        }
        return false;
    }
    public static Set<String> findMgc(String string,String type) {
        Set<String> set = new HashSet<>();
        for (int i = 0; i < string.length(); i++) {
            Map currentMap = (Map)mgcMap.get(string.charAt(i));
            if (currentMap != null) {
                StringBuffer buffer = new StringBuffer();
                buffer.append(string.charAt(i));
                int num = 1;
                //说明第i个子在敏感词map的一级目录中有,这个时候需要接着这个敏感词继续查下去
                for (int j = i+1; j < string.length(); j++) {
                    currentMap = (Map)currentMap.get(string.charAt(j));
                    if (currentMap == null) {
                        break;
                    }
                    buffer.append(string.charAt(j));
                    num++;
                    //判断是否到敏感词尾
                    String isEnd = (String)currentMap.get("isEnd");
                    if (isEnd.equals("1") && type.equals("0")) {
                        break;
                    }
                }
                if (num > 1) {
                    set.add(buffer.toString());
                }
            }
        }
        return set;
    }

    public static void main(String[] args) {
        Minganci.add("广告,广告词,告诉,哈哈");
        System.out.println(JSON.toJSONString(mgcMap));
        boolean mgc = Minganci.checkMgc("这个广告的广告词写的真好", "1");
        Set<String> set = Minganci.findMgc("他告诉我说这个广告的广告词写的真好哈哈", "0");
        Set<String> set1 = Minganci.findMgc("他告诉我说这个广告的广告词写的真好哈哈", "1");
        System.out.println(mgc);
        System.out.println(set);
        System.out.println(set1);


    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值