[英]How to properly navigate an NLTK parse tree?
NLTK is driving me nuts again. NLTK再次让我发疯。
How do I properly navigate through an NLTK tree (or ParentedTree)? 如何正确浏览NLTK树(或ParentedTree)? I would like to identify a certain leaf with the parent node "VBZ", then I would like to move from there further up the tree and to the left to identify the NP node.
我想用父节点“ VBZ”标识某个叶子,然后我想从那里进一步向上移动到树的左侧,以标识NP节点。
How do I do this? 我该怎么做呢? The NLTK tree class does not seem to be thought through... Or I am too stupid...
NLTK树类似乎没有被考虑...或者我太愚蠢了...
Thanks for your help! 谢谢你的帮助!
Based on what you want to do, this should work. 根据您要执行的操作,这应该可行。 It will give you the closest left NP node first, then the second closest, etc. So, if you had a tree of
(S (NP1) (VP (NP2) (VBZ)))
, your np_trees
list would have [ParentedTree(NP2), ParentedTree(NP1)]
. 它将首先为您提供最接近的左NP节点,然后为您提供第二个最接近的NP节点,
np_trees
。因此,如果您有一棵(S (NP1) (VP (NP2) (VBZ)))
的np_trees
,则np_trees
列表将具有[ParentedTree(NP2), ParentedTree(NP1)]
。
from nltk.tree import *
np_trees = []
def traverse(t):
try:
t.label()
except AttributeError:
return
if t.label() == "VBZ":
current = t
while current.parent() is not None:
while current.left_sibling() is not None:
if current.left_sibling().label() == "NP":
np_trees.append(current.left_sibling())
current = current.left_sibling()
current = current.parent()
for child in t:
traverse(child)
tree = ParentedTree.fromstring("(S (NP (NNP)) (VP (VBZ) (NP (NNP))))")
traverse(tree)
print np_trees # [ParentedTree('NP', [ParentedTree('NNP', [])])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.