Python program to check if a word is a noun
Last Updated :
11 Dec, 2020
Given a word, the task is to write a Python program to find if the word is a noun or not using Python.
Examples:
Input: India
Output: India is noun.
Input: Writing
Output: Writing is not a noun.
There are various libraries that can be used to solve this problem.
Approach 1: PoS tagging using NLTK
Python3
import nltk
nltk.download( 'averaged_perceptron_tagger' )
text = "India"
ans = nltk.pos_tag()
val = ans[ 0 ][ 1 ]
if (val = = 'NN' or val = = 'NNS' or val = = 'NNPS' or val = = 'NNP' ):
print (text, " is a noun." )
else :
print (text, " is not a noun." )
|
Output:
India is a noun.
Approach 2: PoS tagging using Spacy
Python3
import spacy
nlp = spacy.load( "en_core_web_sm" )
text = "Writing"
doc = nlp(text)
if (doc[ 0 ].tag_ = = 'NNP' ):
print (text, " is a noun." )
else :
print (text, " is not a noun." )
|
Output:
Writing is not a noun.
Share your thoughts in the comments
Please Login to comment...