1.打开services项目,在pom文件中引入依赖如下
<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); }
标签:documents,配置,语义,System,docVectorModel,搜索,printNearestDocument,println,wordVector From: https://www.cnblogs.com/zwbsoft/p/18147201