Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a word if it is adjective or verb using python nltk?

i have list of words like amazing, interesting, love, great, nice. And i want to check if word is adjective or verb , like "love" is verb and nice is adjective... How to do it using python, or nltk, any help ?

like image 532
nizam uddin Avatar asked Feb 17 '16 16:02

nizam uddin


People also ask

How do you check if a word is a verb?

Verbs always tell the time (also called the tense) of the sentence. The easiest way to find a verb in a sentence is to change the time of the sentence and find the word that changes.

How do you check if a word is a noun in Python?

if (val = = 'NN' or val = = 'NNS' or val = = 'NNPS' or val = = 'NNP' ): print (text, " is a noun." ) else : print (text, " is not a noun." )


2 Answers

The only way to guess what a word is without having any context is to use WordNet, but it won't be 100% reliable since for example "love" can have different roles in a sentence.

from nltk.corpus import wordnet as wn
words = ['amazing', 'interesting', 'love', 'great', 'nice']

for w in words:
    tmp = wn.synsets(w)[0].pos()
    print w, ":", tmp

Will output:

amazing : v
interesting : v
love : n
great : n
nice : n
like image 164
Alex Avatar answered Oct 18 '22 14:10

Alex


An update to @Alex solution:

  1. To only include synsets that belong to word w (not the first synset)
  2. To list all pos tags that the word w gets

Code:

from nltk.corpus import wordnet as wn
words = ['amazing', 'interesting', 'love', 'great', 'nice']
pos_all = dict()
for w in words:
    pos_l = set()
    for tmp in wn.synsets(w):
        if tmp.name().split('.')[0] == w:
            pos_l.add(tmp.pos())
    pos_all[w] = pos_l
print pos_all

Output:

{'interesting': set([u'a']), 
 'amazing': set([u's']), 
 'love': set([u'v', u'n']), 
 'great': set([u's', u'n']),
 'nice': set([u'a', u's', u'n'])}
like image 40
Saeid BK Avatar answered Oct 18 '22 13:10

Saeid BK