check if a word is a noun python 1

check if a word is a noun python

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

Here is what the above code is Doing:
1. For each word in the list of words, we get all the synsets for that word.
2. For each synset, we check if the first part of the synset name is the same as the word.
3. If it is, we add the part of speech of that synset to the set of parts of speech for that word.
4. We then add the set of parts of speech to the dictionary.

The output of the above code is:
{‘great’: set([‘a’, ‘s’]), ‘love’: set([‘n’, ‘v’]), ‘nice’: set([‘a’, ‘s’]), ‘amazing’: set([‘a’, ‘s’]), ‘interesting’: set([‘a’, ‘s’])}

The keys are the words, and the values are the sets of parts of speech.

The parts of speech are:
a – adjective
s – adjective satellite
n – noun
v – verb
r – adverb

So, for example, the word ‘great’ is both an adjective and an adjective satellite.

The word ‘love’ is both a noun and a verb.

The word ‘nice’ is both an adjective and an adjective satellite.

The word ‘amazing’ is both an adjective and an adjective satellite.

The word ‘interesting’ is both an adjective and an adjective satellite.

So, we can see that all of the words in the list are both adjectives and adjective satellites.

So, we can use the wordnet lemmatizer to lemmatize the words in the list.

Similar Posts