[英]How to properly navigate an NLTK parse tree?
NLTK再次让我发疯。
如何正确浏览NLTK树(或ParentedTree)? 我想用父节点“ VBZ”标识某个叶子,然后我想从那里进一步向上移动到树的左侧,以标识NP节点。
我该怎么做呢? NLTK树类似乎没有被考虑...或者我太愚蠢了...
谢谢你的帮助!
根据您要执行的操作,这应该可行。 它将首先为您提供最接近的左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.