DFA实现步骤

package com.leo.common.util;


import java.util.*;

public class SensitiveWordUtilCopy {

    public static Map<String, Object> dictionaryMap = new HashMap<String, Object>();

    /**
     * @Description: 生成关键词字典库
     * @Param:
     * @Return:
     * @Author leo
     * @Date 2022/9/26 21:34
     */
    public static void initMap(Collection<String> words) {
        if (words == null) {
            System.out.println("列表不能为空");
            return;
        }

        Map<String, Object> map = new HashMap<>(words.size());

        Map<String, Object> curMap = null;

        Iterator<String> iterator = words.iterator();

        while (iterator.hasNext()) {
            String word = iterator.next();
            // 地址值传递,同增同改
            curMap = map;
            int len = word.length();

            for (int i = 0; i < len; i++) {
                String key = String.valueOf(word.charAt(i)).toUpperCase();
                Map<String, Object> wordMap = (Map<String, Object>) curMap.get(key);

                if (wordMap == null) {
                    wordMap = new HashMap<>(2);
                    wordMap.put("isEnd", 0);
                    curMap.put(key, wordMap);
                }
                curMap = wordMap;
                if (i == len - 1) {
                    curMap.put("isEnd", "1");
                }
            }

        }
        dictionaryMap = map;
    }

    /**
     * @Description: 搜索文本中某个文字是否匹配关键词
     * @Param:
     * @Return:
     * @Author leo
     * @Date 2022/9/26 21:50
     */
    public static int checkWord(String text, int beginIndex) {

        if (dictionaryMap == null) {
            throw new RuntimeException("字典不能为空");
        }
        boolean isEnd = false;
        int wordLength = 0;
        Map<String, Object> curMap = dictionaryMap;
        int len = text.length();
        for (int i = beginIndex; i < len; i++) {
            String key = String.valueOf(text.charAt(i)).toUpperCase();
            curMap = (Map<String, Object>) curMap.get(key);
            if (curMap == null) {
                break;
            } else {
                wordLength++;
                if ("1".equals(curMap.get("isEnd"))) {
                    isEnd = true;
                }
            }
        }
        if (!isEnd) {
            wordLength = 0;
        }
        return wordLength;
    }


    /**
    * @Description: 获取匹配的关键词和命中次数
    * @Param:
    * @Return:
    * @Author leo
    * @Date 2022/9/26 21:59
    */
    public static Map<String,Integer> matchWords(String text){
        Map<String,Integer> wordMap = new HashMap<>();
        text = text.toUpperCase();
        int len = text.length();

        for (int i = 0; i < len; i++) {

            int wordLength = checkWord(text, i);
            if (wordLength > 0) {
                String word = text.substring(i, i + wordLength);

                if (wordMap.containsKey(word)) {
                    wordMap.put(word,wordMap.get(word)+1);
                }else {
                    wordMap.put(word,1);
                }
                i += wordLength -1;
            }
        }
        return wordMap;
    }

    public static String getBestResult(Map<String,Integer> resultMap){
        String bestRes = "";
        Integer num = 0;
        for (String k : resultMap.keySet()) {
            if (k.length() > bestRes.length()) {
                bestRes = k;
            }else if (k.length() == bestRes.length()){
                if (resultMap.get(k) > num) {
                    num= resultMap.get(k);
                    bestRes = k;
                }
            }
        }
        return bestRes;
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("冰");
        list.add("a");
        list.add("b");
        System.out.println(list);
        initMap(list);
        String content = "冰的,冰,ABA,ABAA";
        System.out.println(content);
        Map<String,Integer> map = matchWords(content);
        System.out.println(map);
        System.out.println(getBestResult(map));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值