Identifying noun in a sentence using NLTK and getting its description using TextBlob Python (Simple Chat-bot script to get answer of question starting with "what" keyword)

 

About this Post

In this post I will be sharing a simple python script to identify noun in a sentence and getting its description.

Prerequisite

  • Python 2 / Python 3

Python Packages Required

  • NLTK (to identify noun in a sentence)
  • TextBlob (To get word description and spell check)

Python Code

import nltk
from textblob import Word, TextBlob


# function to return all nouns in a sentence
def is_noun(sentence):
noun_list = []
tokenized = nltk.word_tokenize(sentence)
print(nltk.pos_tag(tokenized))
for word, pos in nltk.pos_tag(tokenized):
if pos[:2] == 'NN':
noun_list.append(word)
return noun_list


# function to test if spelling is correct
def spell_check(sentence):
return sentence == TextBlob(sentence).correct()


# function to check question starts with what
def check_start_with_what(sentence):
return sentence.find('what ') == 0


question = 'what is a rabbit?'

if spell_check(question):
if check_start_with_what(question):
nouns = is_noun(question)
for val in nouns:
print(val + "--" + ' ,'.join(Word(val).definitions))
else:
print("Your question should start with what keyword")
else:
print("Enter the correct question")

Screenshots





Correct Question and Description


Incorrect Word: Rabit


Question starting with how






Comments

Popular posts from this blog

Highlight Text In PDF With Different Colors Using Python

Basic of Python - Variable and Memory Address

Face Detection And Blur Face Using Face-Recognition and Pillow Python Package