Reputation:
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"
Upvotes: 1
Views: 1073
Reputation: 38952
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')
Upvotes: 1