使用Stanford Parser做句法分析

这两天在用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>

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值