user2201130
user2201130

Reputation: 3

Checking against Nltk POS tags

I am just learning nltk using Python. I am using POS tagging. What I want to know is how do I use the tags. For example, this is the pseudocode:

words = []
teststr = "George did well in the test."
tokens = nltk.word_tokenize(teststr)
words = nltk.pos_tag(tokens)

I want to do something like this:

if words[i] == "proper noun":
    #do something

How do I check whether a word is a noun or a verb or any other part of speech. Can someone please help me out here? Thanks.

Upvotes: 0

Views: 731

Answers (1)

Ryan O'Neill
Ryan O'Neill

Reputation: 3757

If you look at the results of your pos_tag function call you are returned the following list:

[('George', 'NNP'), ('did', 'VBD'), ('well', 'RB'), ('in', 'IN'), ('the', 'DT'), ('test', 'NN'), ('.', '.')]

If you iterate through the list to do something based on the value being a proper noun you would need the following code:

if words[i][1] == 'NNP':
    # do something

NNP is a singular proper noun. Each entry in that list is a tuple which the first value being the word and the second value being the pos.

Upvotes: 2

Related Questions