Realtime ChatBot App Development

Siddarth M
3 min readMay 12, 2021

Alexa,Cortona,Siri….and the list goes on and on. Welcome to the world of Chatting Bots. Let us create a simple bot that will give us the answers for some questions. We will use wikipedia page for corona as the input article and our bot will parse the article, search for the matching sentences from this article and reply.

Source Article for our bot to read through and reply : https://en.wikipedia.org/wiki/Coronavirus

Here are the steps:

User Input -> Tokenize -> Count Matrix -> Similarity Scores -> Sort -> Display

The idea is to transform the user input to a count matrix using CountVectorizer function which is nothing but a frequency mapping of each word appearing in the Source / Article.

Next we find the similarity scores of the user input with the text [ Source / Article to compare ]

Sort the similarity scores in descending order so that the top most element will be the most highly matched sentence for the user input and return that text.

Browse https://colab.research.google.com/ and start a new notebook.

Step 1: Install the libraries. nltk [ Natural Language Toolkit ] and newspaper3k.

pip intall nltk

pip install newspaper3k

Step 2: Import the below necessary libraries required for building the bot

import random

import string

import numpy as np

import nltk

from newspaper import Article

from sklearn.feature_extraction.text import CountVectorizer

from sklearn.metrics.pairwise import cosine_similarity

import warnings

warnings.filterwarnings(‘ignore’)

Step 3: Download the sentence tokenizer from nltk library

nltk.download(‘punkt’,quiet=True)

Step 4: Get the article downloaded to your application.

article = Article(‘https://en.wikipedia.org/wiki/COVID-19')

article.download()

article.parse()

article.nlp()

content = article.text

Step 5: Tokenize the downloaded content and create a sentence list.

sentence_list = nltk.sent_tokenize(content)

Step 6: Create the bot greetings function to greet the user.

def greeting_response(text):

text = text.lower()

user_greetings = [‘hi’,’hello’,’hola’,’greetings’,’wassup’,’hey’]

bot_greetings = [‘howdy’,’hey’,’hello’]

for word in text.split():

if word in user_greetings:

return random.choice(bot_greetings)

Step 7: Create bot response. I have limited the number of responses to 3.

def bot_response(user_input):

user_input = user_input.lower()

sentence_list.append(user_input)

[ We are appending the user input to the sentence list so that it can be compared with other sentences and a score can be assigned ]

bot_reply = ‘ ’

cm = CountVectorizer().fit_transform(sentence_list)

[entire sentence list along with user input is transformed to a vector]

similarity_scores = cosine_similarity(cm[-1],cm)

[similarity scores are calculated based on the user input which was mapped to the last element in the count matrix ]

similarity_scores_list = similarity_scores.flatten()

index = index_sort(similarity_scores_list)

[ Refer to the next step to see the implementation of this index_sort. Just a normal descending order sort.]

index = index[1:]

[We removed the first element from the indice because that will be the index of our input which will be the highly matched since it was appended to the sentence_list. So we pick the next highest match ignoring the first one ]

#now we have to make use of the similarity index and save reply

response_flag = 0

j = 0 #controls the number of responses

for i in range(len(index)):

if similarity_scores_list[index[i]] > 0.0:

bot_reply = bot_reply +’ ‘+ sentence_list[index[i]]

response_flag = 1

j = j + 1

if j > 3:

break

if response_flag == 0:

bot_reply = bot_reply+ ‘ ‘+ “I apologize. Couldn't Understand.”

sentence_list.remove(user_input)

return bot_reply

Step 8: Make sure to sort the similarity scores.

def index_sort(list_var):

length = len(list_var)

list_index = list(range(0,length))

x = list_var

for i in range(length):

for j in range(length):

if x[list_index[i]] > x[list_index[j]]:

temp = list_index[i]

list_index[i] = list_index[j]

list_index[j] = temp

return list_index

Step 9: Start the bot.

#starting the bot

print(‘Covid Bot: Ask me any query about COVID’)

exit_list = [‘exit’,’see you later’,’break’,’quit’,’bye’]

while True:

user_input = input()

user_input = user_input.lower()

if user_input in exit_list:

print(‘Covid Bot: Chat with you later!!!’)

break

else:

if greeting_response(user_input)!=None:

print(‘Covid Bot: ‘+ ‘ ‘+ greeting_response(user_input))

else:

print(‘Covid Bot: ‘+ ‘ ‘+ bot_response(user_input))

Output:

Just a few lines of code to see your bot talk to you. Enjoy reading and coding!!!

--

--

Siddarth M
0 Followers

Technology Enthusiast | Coder | Presenter | Youtuber