我有一句话John saw a flashy hat at the store
如何将其表示为如下所示的依赖树?
(S
(NP (NNP John))
(VP
(VBD saw)
(NP (DT a) (JJ flashy) (NN hat))
(PP (IN at) (NP (DT the) (NN store)))))
import spacy
from nltk import Tree
en_nlp = spacy.load('en')
doc = en_nlp("John saw a flashy hat at the store")
def to_nltk_tree(node):
if node.n_lefts + node.n_rights > 0:
return Tree(node.orth_, [to_nltk_tree(child) for child in node.children])
else:
return node.orth_
[to_nltk_tree(sent.root).pretty_print() for sent in doc.sents]
我得到以下信息,但我正在寻找一种树(NLTK)格式。
saw
____|_______________
| | at
| | |
| hat store
| ___|____ |
John a flashy the