Python Basics: Creating a Very Simple Chat Bot using Python
Hello guys, in this tutorial I will walk you through my very first application written in Python. I am just starting to learn Python so this program will cover only the basics of the language such as conditions, lists and arrays, and simple functions.
There is a lot of very cool and intelligent chatbots available in the internet — some even use neural networks to train their chatbots to talk like a certain person — and almost all of these chatbot applications are written in Python.
Simpler chatbots follow decision trees by looking for specific keywords and the responses will depend on nested if-else statements. This application is a simple demonstration on how decision-tree-based chatbots work.
Well, let us cut to the chase and let’s start coding!
First, this is a simple command line application. We start by prompting the user to send a message.
message = input('Human: ')
Then we need to create a method or a function to send a reply. We will call this function getReply(). We will pass the message as a parameter for this function because the response of our application will depend on the user’s message.
def getReply(message):
print('Bot: ' + message)
This function just sends back whatever message the user entered. Don’t worry, we can do better. This is where the nested if-else statements will come in handy. The idea is, the application will look for some keywords and send a reply depending on our if-else statements. Let’s make our getReply function a little better.
def getReply(message):
if 'who' in message and 'you' in message:
reply = "Hi, I'm Chandler."
else:
reply = "Blah blah blah"
print('Bot: ' + reply)
Now our chatbot has some identity. My chatbot is based on my favorite Friends character Chandler Bing. He’s funny and so I want my chatbot to be funny. Back to our function, yes this is a little bit better than before. The application will look for the words who and you in the message. If both words appear in the message, let’s say the message is who are you?, the chatbot will introduce himself otherwise blah blah blah. Now, let’s make it more interesting.
I will create a list named jokes.
jokes = ["Alright! I took the quiz and it turns out I do put career over men.",
"Yes, on a scale of one to 10, 10 being the dumbest a person can look, you are definitely 19",
"I'm not great at advice. Can I interest you with a sarcastic comment?",
"Ok, you have to stop the Q-tip when there's resistance!",
"What do you know? You're a door. You only like knock knock jokes.",
"Until I was 25, I thought that the only response to 'I love you' was 'Oh, crap!'"]
(Well, Medium kinda’ messed up the formatting.)
I want ChanBot (Chandler Bot, I didn’t really have to explain that, right?) to randomly select from the jokes list whenever the word joke appears in the user’s message. To do this, we need to make our if-else statement a little bit more complicated. Let’s make an if-else-if statement.
def getReply(message):
if 'who' in message and 'you' in message:
reply = "Hello, I'm Chandler.
elif 'joke' in message:
reply = random.choice(jokes)
else:
reply = "Blah blah blah"
print('Bot: ' + reply)
random.choice(jokes) tells our application to randomly select an item from the list jokes.
Note: Do not forget to import random on top of our code.
import random
...
Well that’s pretty much it but there’s one more thing missing. We want to have a long conversation so let’s put our code around a while loop so we can keep talking to ChanBot as long as we don’t say bye.
while ('bye' not in message):
message = input('Human: ')
getReply(message)
Here’s the complete code:
import randomjokes = ["Alright! I took the quiz and it turns out I do put career over men.",
"Yes, on a scale of one to 10, 10 being the dumbest a person can look, you are definitely 19",
"I'm not great at advice. Can I interest you with a sarcastic comment?",
"Ok, you have to stop the Q-tip when there's resistance!",
"What do you know? You're a door. You only like knock knock jokes.",
"Until I was 25, I thought that the only response to 'I love you' was 'Oh, crap!'"]def getReply(message):
if 'who' in message and 'you' in message:
reply = "Hello, I'm Chandler."
elif 'joke' in message:
reply = random.choice(jokes)
else:
reply = "Blah blah blah"
print('Bot: ' + reply)while ('bye' not in message):
message = input('Human: ')
getReply(message)
That’s it! Our very simple chatbot application. You can make it more interesting by putting more if-else statements. The more complicated the decision tree the better!
Thanks for reading my very first tutorial on Python. If you like this post don’t forget to give it a clap and share it on your social media. See you on the next tutorial!