Extracting Nouns from Noun Phase in NLP

Can someone tell me how to extract only nouns from the following output:

I signed and analyzed the “Give me a movie review” line based on this grammar using the following procedure: -

sent=nltk.word_tokenize(msg) parser=nltk.ChartParser(grammar) trees=parser.nbest_parse(sent) for tree in trees: print tree tokens=find_all_NP(tree) tokens1=nltk.word_tokenize(tokens[0]) print tokens1 

and got the following result:

 >>> (S (VP (V Give) (Det me)) (NP (Det the) (N review) (PP (P of) (N movie)))) (S (VP (V Give) (Det me)) (NP (Det the) (N review) (NP (PP (P of) (N movie))))) ['the', 'review', 'of', 'movie'] >>> 

Now I would like to get only nouns. How to do it?

+6
python django nlp
source share
1 answer

You do not need to use a full parser to get nouns. You can just use a tagger. One of the functions you can use is nltk.tag.pos_tag (). This will return a list of tuples with the word and part of the speech. You can iterate through tuples and find words labeled “NN” or “NNS” for a noun or plural noun.

NLTK has a way to document how to use their tags. You can find it here: https://nltk.googlecode.com/svn/trunk/doc/howto/tag.html and here is a link to the chapter of the NLTK book on the use of tags: https://nltk.googlecode.com/svn/trunk /doc/book/ch05.html

There are many code examples in each of these places.

+6
source share

All Articles