In NLTK, how do I go through a sentence to return a list of lines of phrase nouns?
I have two goals:
(1) Create a list of name phrases instead of printing them using the "traverse ()" method. I am currently using StringIO to write the output of an existing traverse () method. This is not an acceptable solution.
(2) Expand the line for Noun as follows: '(NP Michael / NNP Jackson / NNP)' becomes 'Michael Jackson'. Is there a method in NLTK for de-analysis?
The NLTK documentation recommends using traverse () to look up Noun Phrase, but how can I take 't' in this recursive method to generate a list of Noun Phrases strings?
from nltk.tag import pos_tag def traverse(t): try: t.label() except AttributeError: return else: if t.label() == 'NP': print(t)
Currently prints:
(NP Michael / NNP Jackson / NNP)
(NP McDonalds / NNP)
and saves "None" to NP
source share