<dependency> <groupId>com.hankcs</groupId> <artifactId>hanlp</artifactId> <version>portable-1.8.3</version> </dependency>
2.在hanlp github下載語言模型,放入專案根目錄下
3.在services專案中,找到hanlpconfig,配置需要載入的模型路徑
4.呼叫相關介面返回相關結果
public static void main(String[] args) throws IOException { WordVectorModel wordVectorModel = trainOrLoadModel(); printNearest("上海", wordVectorModel); printNearest("美麗", wordVectorModel); printNearest("購買", wordVectorModel); System.out.println(wordVectorModel.similarity("上海", "廣州")); System.out.println(wordVectorModel.analogy("日本", "自民黨", "共和黨")); // 文件向量 DocVectorModel docVectorModel = new DocVectorModel(wordVectorModel); String[] documents = new String[]{ "山東蘋果豐收", "農民在江蘇種水稻", "奧運會女排奪冠", "世界錦標賽勝出", "中國足球失敗", }; System.out.println(docVectorModel.similarity(documents[0], documents[1])); System.out.println(docVectorModel.similarity(documents[0], documents[4])); for (int i = 0; i < documents.length; i++) { docVectorModel.addDocument(i, documents[i]); } printNearestDocument("體育", documents, docVectorModel); printNearestDocument("農業", documents, docVectorModel); printNearestDocument("我要看比賽", documents, docVectorModel); printNearestDocument("要不做飯吧", documents, docVectorModel); }