Search

Friday, April 24, 2020

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

  • There is a problem with the code. The code in Python is case sensitive therefore, if we were to type in "Good" (Uppercase G) instead of "good" in response to the question "How are you feeling?" Python will not find a match and will therefore reply with "I'm sorry to hear that!". In order to get around this problem, we can convert the users reply to lower case using the `lower()` command. Instruct students to modify their code as follows:
if "good" in feeling.lower():
    print("I'm feeling good too!")
  • At the moment, we only have two responses to the question "How are you feeling?" Challenge students to add some more responses to make the chat bot appear more realistic. For example:
feeling = input("How are you today? ")
if "good" in feeling:
    print("I'm feeling good too!")
elif "awesome" in feeling:
    print("I'm feeling awesome too!")
else:
    print("I'm sorry to hear that!")
Challenge students to:​
  • Add some more questions and responses
  • Comment their code explaining how it works.
  • Add a random pause between each question and answer:
​time.sleep(random.randrange(5))
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!

Follow on Facebook

ManishaTech . 2017 Copyright. All rights reserved. Designed by Manisha Gupta | Manisha Gupta