Java实现敏感词过滤(敏感词替换DFA算法)

DFA:

        DFA即Deterministic Finite Automaton,也就是确定有穷自动机。在实现文字过滤的算法中,DFA是比较好的实现算法。

实现思路:

        1.将敏感词添加到List列表中

private static String s1 = "你妈的";
private static String s2 = "草泥马";
private static String s3 = "日本佬";
private static String s4 = "你大爷的";
private static String s5 = "泥马的";

List<String> list = new LinkedList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);

        2.遍历List列表将所有敏感词封装进Map中

/**
     * 封装敏感词汇
     *
     * @param list list列表
     * @return 返回根节点
     */
    private static Map<Character, Map> getSentiveWorld(List<String> list) {

        //root:根节点
        Map<Character, Map> root = new HashMap<>();
        //node:当前节点
        Map<Character, Map> node = new HashMap<>();
        //遍历敏感词列表,取出每一个敏感词
        for (String word : list) {
            //遍历每个敏感词前先把node指向root
            node = root;
            //遍历每一个敏感词的每一个字符
            for (int i = 0; i < word.length(); i++) {
                if (node.containsKey(word.charAt(i))) {
                    //如果node包含该字符,node指向该字符(key)所对应的value
                    node = node.get(word.charAt(i));
                } else {
                    //如果node不包含该字符,向node中添加以该字符为(key),next为(value)的map,并把node指向next
                    //next:下一个节点
                    Map<Character, Map> next = new HashMap<>();
                    node.put(word.charAt(i), next);
                    node = next;
                }
            }
            //遍历完每一个敏感词之后都在末尾添加一个结束标志
            node.put('\t', null);
        }
        return root;
    }

        3.替换敏感词

/**
     * 替换内容的方法
     *
     * @param str 字符串
     * @return 返回替换之后的字符串
     */
    private static String replaceContent(String str, Map<Character, Map> map) {
        //下标指针
        int index;
        //是否有敏感词汇统计
        int count;
        StringBuilder sb = new StringBuilder();
        //遍历字符串
        for (int i = 0; i < str.length(); i += index) {
            index = 0;
            count = 0;
            //将封装好敏感词汇的map赋值给map1
            Map<Character, Map> map1 = map;
            //判断map1中是否包含敏感词
            while (map1.containsKey(str.charAt(i + index))) {
                //包含敏感词就将str.charAt(i + index)(敏感词)(key)所对应的value赋值给map1
                map1 = map1.get(str.charAt(i + index));
                //下标指针加一
                index++;
                //判断map1中是否包含结束符
                if (map1.containsKey('\t')) {
                    //包含结束符代表含有敏感词汇
                    count++;
                    break;
                }
                //判断下标指针加一之后是否为特殊字符
                while (chars.contains(str.charAt(i + index))) {
                    //是特殊字符则下标指针加一
                    index++;
                }
            }

            if (count != 0) {
                //包含敏感词汇追加*
                sb.append('*');
                //i变成(i+下标指针)
            } else {
                //不包含敏感词汇追加该字符
                sb.append(str.charAt(i));
                //下标指针回到1
                index = 1;
                //i变成(i+下标指针)
            }
        }
        return sb.toString();
    }

        4.完整代码(含测试代码)

package practice;

import java.util.*;

public class SentiveWord {

    private static String s1 = "你妈的";
    private static String s2 = "草泥马";
    private static String s3 = "日本佬";
    private static String s4 = "你大爷的";
    private static String s5 = "泥马的";

    public static void main(String[] args) {
        List<String> list = new LinkedList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        Map<Character, Map> map = getSentiveWorld(list);
        String str = "今天遇见一个 日  本'佬,一万个草 泥'马 的在空中飘过!";
        String s = replaceContent(str, map);
        System.out.println(s);
    }

    /**
     * 封装敏感词汇
     *
     * @param list list列表
     * @return 返回根节点
     */
    private static Map<Character, Map> getSentiveWorld(List<String> list) {

        //root:根节点
        Map<Character, Map> root = new HashMap<>();
        //node:当前节点
        Map<Character, Map> node = new HashMap<>();
        //遍历敏感词列表,取出每一个敏感词
        for (String word : list) {
            //遍历每个敏感词前先把node指向root
            node = root;
            //遍历每一个敏感词的每一个字符
            for (int i = 0; i < word.length(); i++) {
                if (node.containsKey(word.charAt(i))) {
                    //如果node包含该字符,node指向该字符(key)所对应的value
                    node = node.get(word.charAt(i));
                } else {
                    //如果node不包含该字符,向node中添加以该字符为(key),next为(value)的map,并把node指向next
                    //next:下一个节点
                    Map<Character, Map> next = new HashMap<>();
                    node.put(word.charAt(i), next);
                    node = next;
                }
            }
            //遍历完每一个敏感词之后都在末尾添加一个结束标志
            node.put('\t', null);
        }
        return root;
    }

    //封装特殊字符
    public static HashSet<Character> chars = new HashSet<Character>() {{
        add(' ');
        add('·');
        add('ˉ');
        add('ˇ');
        add('¨');
        add('々');
        add('·');
        add('~');
        add('‖');
        add('∶');
        add('"');
        add('|');
        add('〃');
        add('〔');
        add('〕');
        add('〈');
        add('〉');
        add('「');
        add('」');
        add('『');
        add('』');
        add('.');
        add('[');
        add(']');
        add('{');
        add('}');
        add('\'');
        add('【');
        add('】');
    }};


//    private static char[] chars = {
//            ' ', '·', 'ˉ', 'ˇ', '¨', '々', '~', '‖', '∶', '"', '`',
//            '|', '〃', '〔', '〕', '〈', '〉', '「', '」', '『', '』',
//            '.', '〖', '〗', '【', '】', '[', ']', '{', '}', '\''
//    };

    /**
     * 替换内容的方法
     *
     * @param str 字符串
     * @return 返回替换之后的字符串
     */
    private static String replaceContent(String str, Map<Character, Map> map) {
        //下标指针
        int index;
        //是否有敏感词汇统计
        int count;
        StringBuilder sb = new StringBuilder();
        //遍历字符串
        for (int i = 0; i < str.length(); i += index) {
            index = 0;
            count = 0;
            //将封装好敏感词汇的map赋值给map1
            Map<Character, Map> map1 = map;
            //判断map1中是否包含敏感词
            while (map1.containsKey(str.charAt(i + index))) {
                //包含敏感词就将str.charAt(i + index)(敏感词)(key)所对应的value赋值给map1
                map1 = map1.get(str.charAt(i + index));
                //下标指针加一
                index++;
                //判断map1中是否包含结束符
                if (map1.containsKey('\t')) {
                    //包含结束符代表含有敏感词汇
                    count++;
                    break;
                }
                //判断下标指针加一之后是否为特殊字符
                while (chars.contains(str.charAt(i + index))) {
                    //是特殊字符则下标指针加一
                    index++;
                }
            }

            if (count != 0) {
                //包含敏感词汇追加*
                sb.append('*');
                //i变成(i+下标指针)
            } else {
                //不包含敏感词汇追加该字符
                sb.append(str.charAt(i));
                //下标指针回到1
                index = 1;
                //i变成(i+下标指针)
            }
        }
        return sb.toString();
    }

}

        5.测试结果

        有任何问题欢迎在评论区讨论! 


版权声明:本文为W211001原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。