首页 > 编程语言 >DFA算法实现查找敏感词功能

DFA算法实现查找敏感词功能

时间:2023-08-21 20:32:37浏览次数:45  
标签:DFAFilter int text xx 算法 查找 dfaFilter DFA public

public class DFAFilter {
    private Set<String> sensitiveWords;
    private int maxLength;

    public DFAFilter() {
        sensitiveWords = new HashSet<>();
        maxLength = 0;
    }

    public static void main(String[] args) {
        DFAFilter dfaFilter = new DFAFilter();
        // xx表示违禁词
        dfaFilter.addSensitiveWord("xx");
        dfaFilter.addSensitiveWord("xx");
        String text = "我是好人,从来不买卖xx";
        System.out.println(dfaFilter.containsSensitiveWord(text));

        System.out.println(dfaFilter.filterSensitiveWord(text, '*'));
    }

    public void addSensitiveWord(String word) {
        sensitiveWords.add(word);
        maxLength = Math.max(maxLength, word.length());
    }

    public boolean containsSensitiveWord(String text) {
        StringBuilder sb = new StringBuilder();
        int n = text.length();
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j <= i + maxLength && j <= n; j++) {
                String subStr = text.substring(i, j);
                if (sensitiveWords.contains(subStr)) {
                    sb.append(subStr);
                    System.out.println(sb);
                    return true;
                }
            }
        }

        return false;
    }

    public String filterSensitiveWord(String text, char replacement) {
        StringBuilder filteredText = new StringBuilder(text);
        int n = text.length();
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j <= i + maxLength && j <= n; j++) {
                String subStr = text.substring(i, j);
                if (sensitiveWords.contains(subStr)) {
                    for (int k = i; k < j; k++) {
                        filteredText.setCharAt(k, replacement);
                    }
                    i = j - 1;
                    break;
                }
            }
        }
        return filteredText.toString();
    }
}

标签:DFAFilter,int,text,xx,算法,查找,dfaFilter,DFA,public
From: https://blog.51cto.com/u_15497049/7179212

相关文章