IKAnalyzer 獨立使用 配置擴展詞典


public class IIKAnalyzerTest {

    public static void main(String[] args) throws IOException {
        String s = "2012年高級會計實務基礎班";
        Configuration cfg = DefualtConfig.getInstance();
        cfg.setUseSmart(true);
        Dictionary.initial(cfg);

        Dictionary dictionary = Dictionary.getSingleton();
        // List<String> words = new ArrayList<String>();
        // words.add("基礎班");
        // words.add("高級會計實務");
        // dictionary.addWords(words);

        System.out.println(cfg.getMainDictionary());
        System.out.println(cfg.getQuantifierDicionary());

        Hit hit = dictionary.matchInMainDict("基礎班".toCharArray());
        System.out.println(hit.isMatch());

        System.out.println(queryWords(s));

    }

    /**
     * IK 分詞
     *
     * @param query
     * @return
     * @throws IOException
     */
    public static List<String> queryWords(String query) throws IOException {
        List<String> list = new ArrayList<String>();
        StringReader input = new StringReader(query.trim());

        IKSegmenter ikSeg = new IKSegmenter(input, true);// true 用智能分詞 ,false細粒度
        for (Lexeme lexeme = ikSeg.next(); lexeme != null; lexeme = ikSeg.next()) {
            list.add(lexeme.getLexemeText());
        }

        return list;
    }
}


控制檯打印:

加載擴展詞典:dic/cdel.dic
加載擴展停止詞典:F:/workSpace/Test_zlb/dic/stopword.dic

[2012年, 高級會計實務, 基礎班]


如果得不到想要的結果,也就是dic沒有起到作用,請檢查一下你的dic目錄是否配置正確


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章