简体   繁体   English

如何从Python中的CoreNLP服务器返回的字符串获取解析树?

[英]How to get parse tree from CoreNLP server's returned string in python?

I'm using pycorenlp with the corenlp server. 我在corenlp服务器上使用pycorenlp。 I can get the parse tree in the string format. 我可以以字符串格式获取解析树。 But can I get it as a tree like the NLTK library? 但是我可以像NLTK库这样的树来获取它吗?

from pycorenlp import StanfordCoreNLP
import pprint
import nltk

nlp = StanfordCoreNLP('http://localhost:9000')

text = ('Purgrug Vobter and Juklog Qligjar vruled into the Battlefield. Vobter was about to Hellfire. Juklog Qligjar started kiblaring.')

output = nlp.annotate(text, properties={
'annotators': 'tokenize,ssplit,pos,depparse,parse',
'outputFormat': 'json'
})


print [s['parse'] for s in output['sentences']]

Output: 输出:

[u'(ROOT\r\n  (S\r\n    (NP (NNP Purgrug) (NNP Vobter)\r\n      (CC and)\r\n      (NNP Juklog) (NNP Qligjar))\r\n    (VP (VBD vruled)\r\n      (PP (IN into)\r\n        (NP (DT the) (NN Battlefield))))\r\n    (. .)))', u'(ROOT\r\n  (S\r\n    (NP (NNP Vobter))\r\n    (VP (VBD was)\r\n      (ADJP (IN about)\r\n        (PP (TO to)\r\n          (NP (NNP Hellfire)))))\r\n    (. .)))', u'(ROOT\r\n  (S\r\n    (NP (NNP Juklog) (NNP Qligjar))\r\n    (VP (VBD started)\r\n      (S\r\n        (VP (VBG kiblaring))))\r\n    (. .)))']

Import tree from nltk : 从nltk导入树:

from nltk.tree import *

Next, for 接下来,

a = [u'(ROOT\r\n  (S\r\n    (NP (NNP Purgrug) (NNP Vobter)\r\n      (CC and)\r\n      (NNP Juklog) (NNP Qligjar))\r\n    (VP (VBD vruled)\r\n      (PP (IN into)\r\n        (NP (DT the) (NN Battlefield))))\r\n    (. .)))', u'(ROOT\r\n  (S\r\n    (NP (NNP Vobter))\r\n    (VP (VBD was)\r\n      (ADJP (IN about)\r\n        (PP (TO to)\r\n          (NP (NNP Hellfire)))))\r\n    (. .)))', u'(ROOT\r\n  (S\r\n    (NP (NNP Juklog) (NNP Qligjar))\r\n    (VP (VBD started)\r\n      (S\r\n        (VP (VBG kiblaring))))\r\n    (. .)))']

Tree.fromstring(a[0]).pretty_print()

And that's all. 就这样。

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM