想要对一个句子中的依存关系进行解析,需要用到CoreNLPDependencyParser包,具体操作有点点麻烦,纪录下:
1. 首先需要去Corenlp的官网下载对应的包,本文使用的是stanford-corenlp-full-2018-10-05。需要进入CoreNLP官网的下载页点击Download CoreNLP 3.9.2 (对应stanford-corenlp-full-2018-10-05)来下载CoreNLP软件。

2. 下载好之后,进入stanford-corenlp-full-2018-10-05文件夹下,运行命令:
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000
如果出现报错“[Thread-0] INFO CoreNLP - CoreNLP Server is shutting down.”, 可参考另一篇博客的解决办法:Click Here
3.成功运行第二步之后,就可以运行自己的程序。 e.g.,将自己的程序存放在test.py中,内容如下:
- from nltk.parse.corenlp import CoreNLPDependencyParser
- dep_parser = CoreNLPDependencyParser(url='http://localhost:9000')
- sentence = 'The quick brown fox jumps over the lazy dog.'
- parse, = dep_parser.raw_parse(sentence)
- print(parse.to_conll(4))
4. 成功运行之后,得出下面的结果。

参考博客:Click Here