在eclipse中使用stanford构建中文依存关系树

前言

stanford是著名的斯坦福大学自然语言处理工具包,现在支持的语言不仅仅是英语,而可以支持多方国家语言,中文就是其中的一部分.python中也有对应的nltk库,但核心的源代码还是JAVA.所以,我分享一些关于在eclipse中怎么使用stanford的过程

stanford的安装使用

我将stanford的相关jar包放到了我的百度云,不过也可以去官网下载(有点慢)
https://pan.baidu.com/s/1slnbQu5
将这些jar包导入项目中就好啦,有时候jvm容量可能hold不住,可以使其扩容,具体可以参照这份博客:
http://blog.csdn.net/qq_30843221/article/details/52316444
安装好就这样啦:
这里写图片描述

stanford的代码样例

由于我在使用stndford的中文分词的过程中老报错,所以我改用了hanlp来作分词,stanford来对分词后的结果构建依存关系树.关于hanlp的使用可以参照:
http://blog.csdn.net/qq_30843221/article/details/52326254

import java.util.ArrayList;
import java.util.List;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;

import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.trees.GrammaticalStructure;
import edu.stanford.nlp.trees.GrammaticalStructureFactory;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreebankLanguagePack;
import edu.stanford.nlp.trees.TypedDependency;


public class DemoStandFord {
    public static void main(String[] agrs) throws Exception{
        LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz"); 
        String sentence = new String("大家好,我的名字叫作Quincy.");
        Segment segment = HanLP.newSegment();   //使用了HanLP来进行分词
        List<Term> termList = segment.seg(sentence);
        StringBuilder termsBuilder = new StringBuilder();
        for(Term term: termList){
            termsBuilder.append(term.word + " ");  //只保存词原,不保留词性
        }
        String[] terms = termsBuilder.toString().split(" ");    //分词后的结果:[大家 好 我 的 名字 叫 作 Quincy ]
        List<CoreLabel> rawWords = new ArrayList<CoreLabel>();  //将分词结果映射成为词条库
        for(String word: terms){
            CoreLabel l = new CoreLabel();
            l.setWord(word);
            rawWords.add(l);
        }
        Tree parse = lp.apply(rawWords);
        parse.pennPrint();  
        System.out.println();  
        TreebankLanguagePack tlp = lp.getOp().langpack();
        GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
        GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
        List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
        System.out.println(tdl);
    }
}

结果效果图:
这里写图片描述

stanford的依存关系资料可以参照:
https://github.com/Quincy1994/standfordParser/blob/master/Stanford%E4%BE%9D%E5%AD%98%E5%8F%A5%E6%B3%95%E5%85%B3%E7%B3%BB%E8%A7%A3%E9%87%8A.docx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值