How to get a node in a tree by its label in nltk python?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 07:29:50

问题


I have a tree:

(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))))  

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?


回答1:


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()



回答2:


Look at the _get_node method in the function.

http://www.nltk.org/_modules/nltk/tree.html



来源:https://stackoverflow.com/questions/37521009/how-to-get-a-node-in-a-tree-by-its-label-in-nltk-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!