Java——word分词·自定义词库

word:

https://github.com/ysc/word

word-1.3.1.jar  需要JDK8

word-1.2.jar  c语言给解析成了“语言”,自定义词库必须为UTF-8

        程序一旦运行,停不下来!百度上百的主要是这个word分词,除了作者的微示例,没有别的例子,感觉全是作者自吹自擂出来的,不好用。

import java.util.List;

import org.apdplat.word.WordSegmenter;
import org.apdplat.word.dictionary.DictionaryFactory;
import org.apdplat.word.segmentation.Word;
import org.apdplat.word.util.WordConfTools;

public class WordCut {

    public static void main(String[] args) {
        分词("这节课我们讲授c语言里的结构体");
    }

    public static void 分词(String strSentence) {
        String strPath = System.getProperty("user.dir");
        WordConfTools.set("dic.path", "classpath:key.txt, " + strPath); DictionaryFactory.reload();// 更改词典路径之后,重新加载词典
        List<Word> words = WordSegmenter.segWithStopWords(strSentence);
        for (Word w : words) {
            System.out.print(w + "_");
        }
    }

}

工程文件夹下新建文件

key.txt

c语言
C语言
结构体

结果:

这_节_课_我_们_讲_授_c语言_里_的_结构体_