AskMe Spring項(xiàng)目提問(wèn)功能——前綴樹(shù)敏感詞過(guò)濾

問(wèn)題發(fā)布功能的重點(diǎn)在于如何實(shí)現(xiàn)敏感詞過(guò)濾,基本的算法是前綴樹(shù)算法,前綴樹(shù)也就是字典樹(shù),通過(guò)前綴樹(shù)匹配可以加快敏感詞匹配的速度
下面是具體的代碼

  • 前綴樹(shù)的結(jié)構(gòu)
    其中包括有是否為結(jié)尾的標(biāo)志符end,當(dāng)在樹(shù)中找到這個(gè)位置時(shí),說(shuō)明找到了敏感詞,可以將其打碼設(shè)為**,從后面的字符繼續(xù)匹配
   private class TrieNode {
        private boolean end = false;
        private Map<Character, TrieNode> map = new HashMap<Character, TrieNode>();
        private Character c;

        TrieNode() {
        }

        TrieNode(Character ch) {
            c = ch;
        }

        public Character getch() {
            return c;
        }

        public void setend(boolean a) {
            this.end = a;
        }

        public boolean getend() {
            return end;
        }

        public void addTrieNode(TrieNode tn) {
            map.put(tn.getch(), tn);
        }

        public TrieNode getTrieNode(Character key) {
            return map.get(key);
        }

    }

  • 前綴樹(shù)的生成(從txt文件中讀取,每讀到一個(gè)詞語(yǔ)(一個(gè)詞語(yǔ)一行),利用addword來(lái)添加)
    注意:這里該類implements InitializingBean其中的一個(gè)函數(shù)——public void afterPropertiesSet() throws Exception,這個(gè)函數(shù)表示在實(shí)例化bean之后,即Bean屬性注入之后,執(zhí)行的操作,我們通過(guò)這個(gè)函數(shù),在每次注入該服務(wù)時(shí),能夠初始化前綴樹(shù)

    @Override
    public void afterPropertiesSet() throws Exception {
        rootNode = new TrieNode();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Users\\erika\\Videos\\offer\\askme\\src\\main\\resources\\SensitiveWords.txt")),
                    "UTF-8"));
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                addword(lineTxt.trim());

                System.out.println(lineTxt);
            }
            br.close();
        } catch (Exception e) {
            logger.error("讀取文件失敗" + e);
            System.out.println("111");
        }

    }
  • 向前綴樹(shù)中添加詞語(yǔ)(每個(gè)字占一個(gè)節(jié)點(diǎn))
    public void addword(String word) {
        TrieNode tmp = rootNode;
        for (int i = 0; i < word.length(); i++) {
            if (isSymbol(word.charAt(i)))
                continue;
            if (tmp.getTrieNode(word.charAt(i)) != null) ;
            else {
                TrieNode tt = new TrieNode(word.charAt(i));
                tmp.addTrieNode(tt);
            }
            tmp = tmp.getTrieNode(word.charAt(i));
            if (i == word.length() - 1) {
                tmp.setend(true);
            }
        }
    }
  • filter匹配字符串

    public String filterword(String content) {
        TrieNode root = rootNode;
        TrieNode tmp = root;
        StringBuilder result = new StringBuilder();
        int head = 0;
        int current = 0;
        while (head < content.length()) {
            Character c = content.charAt(head);
            tmp = root;
            if (isSymbol(c)) {
                head++;
                current++;
                result.append(c);
                continue;
            }
            boolean flag = false;
            while (current < content.length() && tmp.getTrieNode(c) != null) {
                if (tmp.getTrieNode(c).getend()) {
                    result.append("**");
                    flag = true;
                    break;
                }
                current++;
                tmp = tmp.getTrieNode(c);
                while(current<content.length() && isSymbol(content.charAt(current))) {
                    current++;
                    continue;
                }
                c = content.charAt(current);

            }
            if (flag) {
                current++;
                head = current;
            } else {
                result.append(content.charAt(head));
                head++;
                current = head;
            }
        }
        return result.toString();
    }

  • 一個(gè)輔助函數(shù),用來(lái)判斷字符是否為空格,或者特殊字符
    private boolean isSymbol(char c) {
        int ic = (int) c;
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        //判斷是否為中文標(biāo)點(diǎn)
        if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS
                || ub == Character.UnicodeBlock.VERTICAL_FORMS)
            return true;
        // 0x2E80-0x9FFF 東亞文字范圍
        return Character.isSpaceChar(c)||(!CharUtils.isAsciiAlphanumeric(c) && (ic < 0x2E80 || ic > 0x9FFF));
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,527評(píng)論 19 139
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,478評(píng)論 2 7
  • 文章作者:Tyan博客:noahsnail.com 3.4 依賴 標(biāo)準(zhǔn)企業(yè)應(yīng)用不會(huì)由一個(gè)對(duì)象(或Spring用語(yǔ)中...
    SnailTyan閱讀 1,258評(píng)論 0 1
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,261評(píng)論 6 342
  • 標(biāo)簽: C++ 算法 LeetCode 字符串 遞歸 每日算法——leetcode系列 問(wèn)題 Count and...
    CarlBlack閱讀 529評(píng)論 0 0

友情鏈接更多精彩內(nèi)容