这两天在用Stanford corenlp做句法分析,因水平有限,搭建使用过程中出了很多问题,现在简单记录一下。
测试中使用的model是xinhuaFactoredSegmenting.ser.gz,该模型是根据大陆《新华日报》语料训练的,可以对未分词的句子进行分析。
jar包下载地址https://nlp.stanford.edu/software/lex-parser.shtml
因为是maven托管项目,所以在pom文件中添加如下依赖:
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.2</version>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-parser</artifactId>
<version>3.9.2</version>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.2</version>
<classifier>models</classifier>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-parser</artifactId>
<version>3.9.2</version>
<classifier>models</classifier>
</dependency>
测试内容如下:
public void LexicalizedParser() throws IOException {
LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/xinhuaFactoredSegmenting.ser.gz");
List<String> lines = Arrays.asList("小明喜欢吃香蕉");
lines.stream().forEach(sentence -> {
Tree tree = lp.parse(sentence);
ChineseGrammaticalStructure gs = new ChineseGrammaticalStructure(tree);
Collection<TypedDependency> tdl = gs.typedDependenciesCollapsed();
System.out.println("sentence:"+sentence);
tdl.stream().forEach(typedDependency -> {
System.out.println("Governor Word: [" + typedDependency.gov() + "] Relation: [" + typedDependency.reln().getLongName() + "] Dependent Word: [" + typedDependency.dep() + "]");
});
});
}
运行成功,结果如下:
容易出的问题:
stanford-parser-3.9.2-models.jar包不需要单独安装,只需要在pom文件的parser依赖下添加<classifier>models</classifier>