Steps to make Chatbot using ML

How to make Chatbot using ML

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

 

[yasr_overall_rating] Over 388 people have rated [5/5]

 

 

A chatbot is nothing but an Artificial Intelligence powered software inside a device. (Alexa, Siri, Google Assistant, etc) application website or other networks that try to gauge the customer’s needs and assist them in order to perform a particular task like a commercial transaction, hotel booking, submission and so on. Every company nowadays has deployed a chatbot on its website to help users as well as to engage with them. Some ways in which companies are using chatbots are as follows:

  • Delivering Flight Information
  • Connecting with customers and their finances
  • Assist and provide customer support

 

Inner workings of a Chatbot:

There are broadly two main types of chatbot variants: Rule-Based and Self Learning

 

In the rule-based approach, a bot answers questions on some rules that it is trained on. The rules can be made to be very simple or very complex. The bot can handle simple queries but often fails to manage complex ones.

 

Self-learning bot is those that use Machine learning-based approaches and are definitely more efficient than the rule-based bots, these bots are further subdivided into two types. Retrieval based or Generative.

 

In the Retrieval based models, a chatbot has the option to use heuristics to select a response from a library of predefined response. The chatbot can use the message as well as the context of the conversation for selecting the best response from a predefined list of bot messages. The content can include a previous message from the dialogue tree, all previous messages in the conversation, previously saved variables. The Heuristics for selecting a response can be engineered in a variety of ways, from rule-based conditional if-else logic to machine learning classifiers.

 

Generative bots are capable of generating the answers and not always replies with one of the answers from a set of answers. This makes them more intelligent as they take the word by word query and generate the answers.

 

You will require the following packages scikit library and NLTK

 

Downloading and installing NLTK
Install NLTK: run pip install nltk
Test installation: run python then type import nltk

 

Installing NLTK Packages

 

Import NLTK and run nltk.download(). This will open the NLTK downloader from where you can choose the corpora and models to download. You can also download all packages at once.

 

Would like to know more: NLP for chatbot

 

Importing the necessary libraries

 

import nltk
import numpy as np
import random
import string # to process standard python strings

 

Reading in the data

 

We will read in the corpus.txt file and convert the entire corpus into a list of sentences and a list of words for further pre-processing.

 

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

raw=f.read()
raw=raw.lower()# converts to lowercase

nltk.download(‘punkt’) # first-time use only
nltk.download(‘wordnet’) # first-time use only

sent_tokens = nltk.sent_tokenize(raw)# converts to list of sentences
word_tokens = nltk.word_tokenize(raw)# converts to list of words

 

 

Let see an example of the sent_tokens and the word_tokens

 

ent_tokens[:2]

 

[‘a chatbot (also known as a talkbot, chatterbot, bot, im bot, interactive agent, or artificial conversational entity) is a computer program or an artificial intelligence which conducts a conversation via auditory or textual methods.’,
‘such programs are often designed to convincingly simulate how a human would behave as a conversational partner, thereby passing the Turing test.’]

 

word_tokens[:2]

 

[‘a’, ‘chatbot’, ‘(‘, ‘also’, ‘known’]

 

Pre-processing the raw text

 

We shall now define a function called LemTokens which will take the tokens as an input and return normalized tokens.

 

lemmer = nltk.stem.WordNetLemmatizer()
#WordNet is a semantically-oriented dictionary of English included in NLTK.

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)))

 

 

Keyword matching

 

Next, we shall use and create a function for a greeting by the bot i.e if a user’s input is a greeting, the created bot shall return a comforting greeting response. ELIZA uses a simple keyword matching for greetings. We will utilize the same concept here.

GREETING_INPUTS = (“hello”, “hi”, “greetings”, “sup”, “what’s up”,”hey”,)
GREETING_RESPONSES = [“hi”, “hey”, “*nods*”, “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)

 

 

Generating Response

 

To generate a response from our bot for input questions, the concept of document similarity will be used. So we begin by importing the necessary modules.

  • From scikit learn library, import the TFidf vectorizer to convert a collection of raw documents to a matrix of TF-IDF features.

from sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.metrics.pairwise import cosine_similarity

 

This will be used to find the similarity between words entered by the user and the words in the corpus. This is the simplest possible implementation of a chatbot.

 

We define a function response which searches the user’s utterance for one or more known keywords and returns one of several possible responses. If it doesn’t find the input matching any of the keywords, it returns a response:” I am sorry! I don’t understand you”

 

def response(user_response):
robo_response=”
sent_tokens.append(user_response)
TfidfVec = TfidfVectorizer(tokenizer=LemNormalize, stop_words=’english’)
tfidf = TfidfVec.fit_transform(sent_tokens)
vals = cosine_similarity(tfidf[-1], tfidf)
idx=vals.argsort()[0][-2]
flat = vals.flatten()
flat.sort()
req_tfidf = flat[-2]

if(req_tfidf==0):
robo_response=robo_response+”I am sorry! I don’t understand you”
return robo_response
else:
robo_response = robo_response+sent_tokens[idx]
return robo_response

 

Finally, we will provide the lines that we want our bot to speak while starting and ending a conversation that depends on the user’s input.

 

flag=True
print(“ROBO: My name is Robo. I will answer your queries about Chatbots. If you want to exit, type Bye!”)
while(flag==True):
user_response = input()
user_response=user_response.lower()
if(user_response!=’bye’):
if(user_response==’thanks’ or user_response==’thank you’ ):

flag=False
print(“ROBO: You are welcome..”)
else:
if(greeting(user_response)!=None):
print(“ROBO: “+greeting(user_response))
else:
print(“ROBO: “,end=””)
print(response(user_response))
sent_tokens.remove(user_response)
else:
flag=False
print(“ROBO: Bye! take care..”)

 

 

Read More: Creating Chatbot with Deep Learning

 

 

So that’s pretty much it. We have coded our first chatbot in NLTK. Now, let us see how it interacts with humans:

 

 

Chatbot

 

Conclusion

Though it is a very simple bot that does not have any cognitive skills, its a good way to get into NLP and get to know about chatbots.Though ‘ROBO’ responds to user input. It cannot fool your friends, and for a production system you’ll want to consider one of the existing bot platforms or frameworks, but this example should help you think through the design and challenge of creating a chatbot. The Internet has many resources and after reading this article we’re sure, you will want to create a chatbot of your own. So happy tinkering!! We hope with this article you would have some knowledge about AI and ML for creating a chatbot. If you liked this article please check out our other articles also.

 

 

If you want to learn more about artificial intelligence and machine learning, then do check out our website iphonedevelopmentguide.com. You might be looking to pursue an artificial intelligence and machine learning course in Mumbai, in that case, check out the various training programs that we offer. You can also pursue an artificial intelligence developer course by contacting us at enquiry@nimapinfotech.com