動態規划分詞(結巴分詞算法)


看了好幾次結巴的算法, 總也記不住, 還是得自己寫一遍才能真正明白.
其實也不難, 就是動態規划算法, 先把所有的分詞路徑都找出來 ,然后分詞的路徑就是概率最大的路徑.
每個路徑的概率=該路徑所有詞的概率乘積, 也就是log之和; 每個詞的概率取log=log(freq/total), total是所有詞的總詞頻.

/**
     * example: 研究生命的起源
     */
    private List<String> seg(String str) {
        // get all paths like this:
//        0  [3, 1, 2]
//        1  [2]
//        2  [4, 3]
//        3  [4]
//        4  [5]
//        5  [7, 6]
//        6  [7]

        IntArray[] paths = new IntArray[str.length()];
        char[] chars = str.toCharArray();
        for (int i = 0; i < str.length(); i++) {
            IntArray path = new IntArray(1);
            int max = TRIE.maxMatch(chars, i, chars.length - i);
            path.add(i + max);

            for (int j = 1; j < max; j++) {
                if (TRIE.contains(chars, i, j)) {
                    path.add(i + j);
                }
            }
            paths[i] = path;
        }


        // 動態規划自下向上開始計算, 每個節點算出最大的分數, 同時記錄其下一個節點
        // 獲取的nexts路徑像這樣: [2, 2, 4, 4, 5, 7, 7]
        float[] maxScores = new float[str.length() + 1];
        maxScores[str.length()] = 0;
        int[] nexts = new int[str.length()];

        for (int i = str.length() - 1; i >= 0; i--) {
            float maxScore = Float.NEGATIVE_INFINITY;
            int next = 0;
            for (int j = 0; j < paths[i].size(); j++) {
                int possibleNext = paths[i].get(j);
                float score = TRIE.weight(chars, i, possibleNext - i) + maxScores[possibleNext];
                if (score > maxScore) {
                    maxScore = score;
                    next = possibleNext;
                }
            }
            maxScores[i] = maxScore;
            nexts[i] = next;
        }

        List<String> terms = new ArrayList<>(4);

        int current = 0;
        while (current != str.length()) {
            int next = nexts[current];
            String term = str.substring(current, next);
            terms.add(term);
            current = next;
        }
        return terms;
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM