Build A Simple NLP/NLTK Chatbot

Featured Image – Canva.

How Can Chatbots Help Improve Your Bottom-line? 

chatbot is a computer program or software that automates conversation with a user. They can be programmed with different responses based on what a user chooses or requests.

Chatbots can provide real-time customer support and are therefore a valuable asset in many industries. 


  1. Summary
  2. References
    1. ChatterBot: Build a Chatbot With Python
  3. Infographic

You can build an industry-specific chatbot by training it with relevant data.

  • Let’s create the example text file chatbot.txt containing (unstructured) public-domain wiki-style information about chatbots.
  • Let’s create the directory YOURPATH and copy the above file to this directory.

We are ready to implement the Python workflow within the Jupyter IDE:

  • Let’s set the working directory

import os
os. getcwd()

and import/install the key libraries

import io
import random
import string # to process standard python strings
import warnings
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import warnings

!pip install nltk

import nltk
from nltk.stem import WordNetLemmatizer‘popular’, quiet=True)‘punkt’)‘wordnet’)

  • Let’s read our text file and convert the content to the lowercase.

f=open(‘chatbot.txt’,’r’,errors = ‘ignore’)
raw = raw.lower()

  • Let’s tokenize the content by converting it to the lists of sentences and words

sent_tokens = nltk.sent_tokenize(raw)
word_tokens = nltk.word_tokenize(raw)

  • Let’ invoke WordNet (a semantically-oriented dictionary of English included in NLTK) to apply WordNetLemmatizer

lemmer = nltk.stem.WordNetLemmatizer()

def LemTokens(tokens):
return [lemmer.lemmatize(token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)

def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))

  • Let’s create GREETING lists

GREETING_INPUTS = (“hello”, “hi”, “greetings”, “sup”, “what’s up”,”hey”,)
GREETING_RESPONSES = [“hi”, “hey”, “hi there”, “hello”, “I am glad! You are talking to me”]
def greeting(sentence):

for word in sentence.split():
    if word.lower() in GREETING_INPUTS:
        return random.choice(GREETING_RESPONSES)
  • We need to invoke the user response function

def response(user_response):
TfidfVec = TfidfVectorizer(tokenizer=LemNormalize, stop_words=’english’)
tfidf = TfidfVec.fit_transform(sent_tokens)
vals = cosine_similarity(tfidf[-1], tfidf)
flat = vals.flatten()
req_tfidf = flat[-2]
robo_response=robo_response+”I am sorry! I don’t understand you”
return robo_response
robo_response = robo_response+sent_tokens[idx]
return robo_response

  • Let’s implement the simple dialogue

print(“Al VA: I am Al, your customer support VA. I will be happy to answer your questions about Chatbots. If you want to exit, please type Bye!”)
user_response = input()
if(user_response==’thanks’ or user_response==’thank you’ ):
print(“Al: You are welcome..”)
print(“Al: “+greeting(user_response))
print(“Al: “,end=””)
print(“Al: Bye! take care..”)

  • Here is the example demo
Al VA: I am Al, your customer support VA. I will be happy to answer your questions about Chatbots. If you want to exit, please type Bye!
Al: hey
What is the definition of chatbots
Al: in 2016, facebook messenger allowed developers to place chatbots on their platform.
WHich platform?
Al: chatbot development platforms
the process of building, testing and deploying chatbots can be done on cloud based chatbot development platforms offered by cloud platform as a service (paas) providers such as yekaliva, oracle cloud platform, snatchbot and ibm watson.these cloud platforms provide natural language processing, artificial intelligence and mobile backend as a service for chatbot development.
Al: You are welcome..


  • Bots are everywhere, especially in the world of digital marketing. 
  • Example: Chatbot marketing utilizes a chatbot to market the business. 

Other Relevant Business Applications:

  • Chatbots for Customer Service
  • Chatbots for Sales
  • Chatbots for FAQ
  • Chatbots for Shopping
  • Chatbots for Marketing


Building a Simple Chatbot from Scratch in Python (using NLTK)

ChatterBot: Build a Chatbot With Python

Intelligent AI Chatbot in Python

How to Build a MarTech Stack from Scratch



Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount


Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

3 responses to “Build A Simple NLP/NLTK Chatbot”

  1. I like the helpful info you provide to your articles. I抣l bookmark your blog and take a look at again right here regularly. I’m somewhat certain I抣l be told many new stuff proper here! Good luck for the following!

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: