2016-08-31 6 views
2

私はcorenlpサーバーでpycorenlpを使用しています。私は文字列形式で解析木を得ることができます。しかし、NLTKライブラリのようなツリーとしても使えますか?PythonでCoreNLPサーバーの返された文字列から解析ツリーを取得する方法は?

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']] 

出力:NLTKから

[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 (. .)))'] 

答えて

1

インポートツリー:

from nltk.tree import * 

次に、

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() 

そして、そのためには、すべてです。

result

関連する問題