Skip to content Skip to sidebar Skip to footer

Nltk: How Do I Traverse A Noun Phrase To Return List Of Strings?

In NLTK, how do I traverse a parsed sentence to return a list of noun phrase strings? I have two goals: (1) Create the list of Noun Phrases instead of printing them using the 'trav

Solution 1:

def extract_np(psent):
  for subtree in psent.subtrees():
    if subtree.label() == 'NP':
      yield ' '.join(word for word, tag in subtree.leaves())


cp = nltk.RegexpParser(grammar)
parsed_sent = cp.parse(tagged_sent)
for npstr in extract_np(parsed_sent):
    print (npstr)

Post a Comment for "Nltk: How Do I Traverse A Noun Phrase To Return List Of Strings?"