解析樹我有一個句子約翰看到在商店華而不實的帽子
如下圖所示如何表示這是一個依賴關係樹?依賴於Spacy
(S
(NP (NNP John))
(VP
(VBD saw)
(NP (DT a) (JJ flashy) (NN hat))
(PP (IN at) (NP (DT the) (NN store)))))
我從here
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