Creating a chatting robot (Chat bot) Using Python
Creating a chatting robot (Chat bot) using Python
By Manisha Gupta
STEP 1 - Creating the chat bot
In order for the chat bot to work, the students will need to import the time and random libraries. Instruct students to type in the following code:
In order for the chat bot to work, the students will need to import the time and random libraries. Instruct students to type in the following code:
import time
import random |
Next, we need to ask the user a question and store the response in a variable. Instruct students to input the following:
name = input("Hello, what is your name? ")
|
Next, we want to give the user the impression that they are chatting to a real person, therefore, we need to place a pause in-between each new question / response. To do this, we will use the time `time.sleep()`. Instruct students to enter the following:
time.sleep(2)
|
Next, we need to create a response from the computer. We want the reply to sound personalised so we are going to add the users name to the end of the response. To do this, we will use the concatenation `+` command. Instruct students to add the following code to their script:
print("Hello " + name)
|
Next we are going to ask the user how they are feeling. We will store this in a variable called 'feeling'. Instruct students to add the following:
feeling = input("How are you today? ")
time.sleep(2) |
Next, we are need to create a response. In order for the computer to appear human, we need to have a set of different responses. To do this, we will use an `if` statement. We will start with the response for 'if' the user is feeling "good". Instruct students to add the following:
if "good" in feeling:
print("I'm feeling good too!") else: print("I'm sorry to hear that!") |
So far, there is very little variation in our answers. In order to make our chat bot appear more human, we are going to add a list of random possible answers.
First, ask students to add an input() asking the user to enter their favourite colour (remembering to add a natural pause):
First, ask students to add an input() asking the user to enter their favourite colour (remembering to add a natural pause):
time.sleep(2)
favcolour = input("What is your favourite colour? ") |
Next, instruct students to create a list of possible responses:
colours = ["Red","Green","Blue"]
|
Finally, instruct students to add a response by choosing a colour from the list at random:
time.sleep(2)
print("My favourite colour is " + random.choice(colours))
Extension
Challenge students to:
Plenary:
Direct students to swap places with a partner or person next to them and try out their chat bot. Instruct the students to leave a comment in their partner’s code suggesting at least one improvement. After a few minutes, ask the students to return to their seats and make any suggested improvements. As an extra incentive, you could offer a prize for the most convincing chat bot (almost like a mini Loebner Prize - http://en.wikipedia.org/wiki/Loebner_Prize) However, rather than rewarding the students with $6,000 for creating the most convincing chat bot, I suggest some chocolate instead! |