Skip to content
Related Articles

Related Articles

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 required modules
import nltk
nltk.download('averaged_perceptron_tagger')
  
# taking input text as India
text = "India"
ans = nltk.pos_tag()
  
# ans returns a list of tuple
val = ans[0][1]
  
# checking if it is a noun or not
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 required modules
import spacy
nlp = spacy.load("en_core_web_sm")
  
# taking input
text = "Writing"
  
# returns a document of object
doc = nlp(text)
  
# checking if it is a noun or not
if(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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :