I have a Python function that traverses a sentence parsed as a tree looking for adjective/noun pairs (such as "good cat"), creates a list of such pairs, and returns it. Here it is:
def traverse(t):
thelist = list()
try:
t.label()
except AttributeError:
return
else:
if t.label() == 'NP':
for leaf in t.leaves():
thelist.append(str(leaf[0]))
print("The list is ",thelist)
return thelist
else:
for child in t:
traverse(child)
I call this function like so:
print("traversing the tree returned ",traverse(pos_parser))
What I get is this:
The list is ['good', 'cat']
traversing the tree returned None
So it creates and prints out variable "thelist" in traverse but doesn't return it (returns None instead). Why??
Can someone please help?