简体   繁体   中英

How can I access the contents of a node in a NLTK tree?

I am trying to search for a POS tag based on a word using NLTK Tree.

I want to locate a word (here: different) in the tree (the word is definitely present in the tree), and check if any of the children of the node containing this word has a given label (here: NN).

from nltk.tree import Tree

input_string = '(ROOT (SBARQ (WHADVP (WRB How)) (SQ (VBZ is) (NP (PRP it)) (ADJP (JJ different) (PP (IN from) (NP (DT the) (JJ dishonest) (NNS businessmen))))) (. ?)))'
for t in Tree.fromstring(input_string, read_node=lambda s: '<%s>' % s, read_leaf=lambda s: '"%s"' % s):
    print (t)

I tried to go through the documentation, but I am unable to get any further than this.

What I am trying to do is :

if t.leaves() in ["different"]:
    if content_of_t (I don't know how to access that) in ["NN"]:
        return "yes"

You can walk through all subtrees of the tree.

tree = Tree.fromstring(
           input_string, 
           read_node=lambda s: '<%s>' % s,
           read_leaf=lambda s: '%s' % s)

for sub_tree in tree.subtrees(): 
    if sub_tree.label()  == '<JJ>' and 'different' in set(sub_tree.leaves()):
        print('yes')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM