如何通过nltk python中的标签获取树中的节点? [英] How to get a node in a tree by its label in nltk python?
本文介绍了如何通过nltk python中的标签获取树中的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一棵树:
(S
(WH-QUERY Which)
(FLIGHT-NP
(FLIGHT-CNP
(FLIGHT-CNP (FLIGHT-N flight))
(FLIGHT-DEST to (CITY-NP (CITY-NAME Hue) (CITY-N city)))))
(FLIGHT-VP
(FLIGHT-V arrives)
(FLIGHT-TIME (P-TIME at) (TIME-MOD 20:00HR))))
我想通过nltk中的标签获取特定的节点.例如,我具有标签"CITY-NAME",并且我想要获取节点(CITY-NAM Hue).我该如何实现?
I want to get a specific node by its label in nltk. For example, I have label "CITY-NAME", and I want to get the node (CITY-NAM Hue). How can I achieve this?
推荐答案
一种方法是遍历树搜索匹配的节点:
One way to do it is to walk the tree searching for nodes that match:
for subtree in tree.subtrees():
if subtree.label() == 'CITY-NAME':
print subtree.leaves()
这篇关于如何通过nltk python中的标签获取树中的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文