Python program to check if a word is a noun
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 required modulesimport nltknltk.download('averaged_perceptron_tagger') # taking input text as Indiatext = "India"ans = nltk.pos_tag() # ans returns a list of tupleval = ans[0][1] # checking if it is a noun or notif(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 required modulesimport spacynlp = spacy.load("en_core_web_sm") # taking inputtext = "Writing" # returns a document of objectdoc = nlp(text) # checking if it is a noun or notif(doc[0].tag_ == 'NNP'): print(text, " is a noun.")else: print(text, " is not a noun.") |
Output:
Writing is not a noun.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

