I have some questions about NLTK's tree functions. I am trying to extract a certain word from the tree structure like the one given below.
test = Tree.parse('(ROOT(SBARQ(WHADVP(WRB How))(SQ(VBP do)(NP (PRP you))(VP(VB ask)(NP(DT a)(JJ total)(NN stranger))(PRT (RP out))(PP (IN on)(NP (DT a)(NN date)))))))')
print "Input tree: ", test
print test.leaves()
(SBARQ
(WHADVP (WRB How))
(SQ
(VBP do)
(NP (PRP you))
(VP
(VB ask)
(NP (DT a) (JJ total) (NN stranger))
(PRT (RP out))
(PP (IN on) (NP (DT a) (NN date)))))))
['How', 'do', 'you', 'ask', 'a', 'total', 'stranger', 'out', 'on', 'a', 'date']
I can find a list of all the words using the leaves() function. Is there a way to get a specific leaf only? For example: I would like to get the first/last noun from the NP phrase only? The answer would be 'stranger' for the first noun and 'date' as the last noun.