Building Chatbots using Python — Part 0
By: Shrusti Ghela
Tags: Data Science, Chatbots, Naturallanguageprocessing, Eliza

Creating a personality

Creating an engaging personality is a critical part of chatbot development. It’s one of the key differences as compared to any other kind of software.

Why creating a personality for the Chatbot is so essential? — Let us suppose, with our Chatbot, all we could do was type precise instructions to our bot. We would just have a command-line application and not a chatbot.

Most chatbots are rooted in a messaging app that people are comfortable using to talk to their friends. And we can expect that the users of our will want to make a bit of smalltalk before trying out any functionality that they came for. It’s not much effort to code up some responses to common questions and is worth it for the improved user experience.

Smalltalk and Including variables

# Define variables

name = "Shrusti"

weather = "sunny" # Define a dictionary with the predefined responses responses = { "what's your name?": "my name is {0}".format(name), "what's today's weather?": "the weather is {0}".format(weather), "default": "default message" } # Return the matching response if there is one, default otherwise def respond(message): # Check if the message is in the responses if message in responses: # Return the matching message bot_message = responses[message] else: # Return the "default" message bot_message = responses["default"] return bot_message

The simplest thing we can do is use a python dictionary, with user messages as the keys and responses as the values. For example, say we define a dictionary called ‘responses,’ with the messages “what’s your name?” and “what’s today’s weather” as keys, and suitable responses as values. Next, we define a function called ‘respond,’ which accepts a message as a single argument. This function tests if a message has a defined response by using the ‘in’ keyword, that is, “if message in responses.” This statement only returns ‘True’ if the message corresponds to one of the dictionary’s keys. Notice that this will only work if the user’s message exactly matches a key in the dictionary. In later parts of this blog, we will build much more robust solutions. Notice that if there isn’t a matching message, the ‘return’ keyword will never be reached so that the function will return None.

Since the world outside is always changing, our bot’s answers have to be able to as well. The first thing that we can do is add some placeholders to the responses. For example, instead of “The weather is sunny,” you can have a template string, like “it’s {} today.” Then later, you can insert a variable ‘weather_today’ by calling format weather today.

Choosing responses

# Import the random module

import random name = "Shrusti"

weather = "sunny" # Define a dictionary containing a list of responses for each message responses = { "what's your name?": [ "my name is {0}".format(name), "they call me {0}".format(name), "I go by {0}".format(name) ], "what's today's weather?": [ "the weather is {0}".format(weather), "it's {0} today".format(weather) ], "default": ["default message"] }

# Use random.choice() to choose a matching response def respond(message): if message in responses: bot_message = random.choice(responses[message]) else: bot_message = random.choice(responses["default"]) return bot_message

It gets dull hearing the same responses over and over again, so it’s an excellent idea to add a little variety! To return completely different responses, we can replace the values in the responses dictionary with lists. Then when we are choosing a response, we can randomly select an answer from the appropriate list. To do this, import random, and use the ‘random.choice’ function, passing the list of options as an argument. For now, we are still relying on the user message matching our predefined messages exactly, but we’ll soon move to a more robust approach.

Asking questions

import random responses = { "question": [ "I don't know :(", "you tell me!" ], "statement": [ "can you back that up?", "tell me more!", "oh wow!", ":)", "how long have you felt this way?", "I find that extremely interesting", "why do you think that?" ] }

def respond(message): # Check for a question mark if message.endswith("?"): # Return a random question return random.choice(responses["question"]) # Return a random statement return random.choice(responses["statement"]) # Send messages ending in a question mark send_message("what's today's weather?") send_message("what's today's weather?")

# Send messages which don't end with a question mark send_message("I love building chatbots") send_message("I love building chatbots")

output

A great way to keep users engaged is to ask them questions or invite them to go into more detail. This was actually one of the things which made the ELIZA bot, so fun to talk to. Instead of using a bland default message like “I’m sorry, I didn’t understand you,” you can use some phrases that invite further conversation. Questions are a great way to achieve this. “Why do you think that?”, “How long have you felt this way?”, and “Tell me more!” are appropriate responses to many different kinds of message, and even when they don’t quite match are more entertaining than a boring fallback