Python Turing Test - Creating a chatting robot (Chat bot)
Like this lesson? Share it with your followers by clicking on the image below.
This lesson is based on the work of Carrie Anne Philbin for the Raspberry Pi Foundation and is licenced under the Creative Commons Attribution 4.0 International License. (See credits below)
Suggested time: 60 mins
Learning Objectives:
Curriculum Mapping:
KS2:
KS3:
Introduction:
Can computers think? This was a question posed by computer pioneer and artificial intelligence (AI) theorist, Alan Turing. Turing proposed that, given time, a computer with sufficient computational power would acquire the abilities to rival human intelligence. In order to test his theory, Turing devised a test.
The Turing Test was based on a Victorian parlour game in which a judge (or interrogator) asks a series of questions to a man and a woman in a separate room. By reading a series of typed answers, the judge must determine which replies were from the man and which were from the woman.
Turing adapted the test by replacing the woman with a computer - the aim being to decide whether the answers were from a man or computer thus determining if a computer was able to think for itself.
To find out more about Alan Turing and the Turing Test, click on the links below:
Suggested time: 60 mins
Learning Objectives:
- Understand and use sequence in an algorithm
- Understand and use iteration in an algorithm (FOR and WHILE loops)
- Understand and use selection in an algorithm (IF, Else and Else if)
- Understand why it is so difficult to create a computer that can seemingly think for itself
Curriculum Mapping:
KS2:
- Design, write and debug programs that accomplish specific goals; solve problems by breaking them into smaller parts. Select, use and combine a variety of software on a range of digital devices to design and create a range of programs.
- Use sequence, selection and repetition in programs; work with variables and various forms of input and output
- Use logical reasoning to explain how some simple algorithms work; detect and correct errors in algorithms and programs
KS3:
- Use two or more programming languages, at least one of which is textual, to solve a variety of computational problems.
Introduction:
Can computers think? This was a question posed by computer pioneer and artificial intelligence (AI) theorist, Alan Turing. Turing proposed that, given time, a computer with sufficient computational power would acquire the abilities to rival human intelligence. In order to test his theory, Turing devised a test.
The Turing Test was based on a Victorian parlour game in which a judge (or interrogator) asks a series of questions to a man and a woman in a separate room. By reading a series of typed answers, the judge must determine which replies were from the man and which were from the woman.
Turing adapted the test by replacing the woman with a computer - the aim being to decide whether the answers were from a man or computer thus determining if a computer was able to think for itself.
To find out more about Alan Turing and the Turing Test, click on the links below:
- http://www.turing.org.uk - Online biography about Alan Turing
- http://en.wikipedia.org/wiki/Alan_Turing - Alan Turing wikipedia page
- http://www.bbc.co.uk/history/people/alan_turing - BBC History: Alan Turing
- http://en.wikipedia.org/wiki/Turing_test - The Turing Test wikipedia page
- http://www.turing.org.uk/scrapbook/test.html - Alan Turing Scrapbook: The Turing Test
Lesson Outline:
In this brief introduction to programming using Python, students are going to create a chatting robot (chat bot) designed to confuse the user into thinking that they are talking to a real person.
Starter:
Show the video “Project Natal – Milo demo”. At the end of the video explain to students that for years, science fiction writers and filmmakers have dreamed about robots that can think for themselves however, despite giant leaps in technology, this dream is still far from reality. Explain to students that to explore the reasons why it is so difficult to make a thinking robot, they are going to create their very own chatting robot (chat bot).
In this brief introduction to programming using Python, students are going to create a chatting robot (chat bot) designed to confuse the user into thinking that they are talking to a real person.
Starter:
Show the video “Project Natal – Milo demo”. At the end of the video explain to students that for years, science fiction writers and filmmakers have dreamed about robots that can think for themselves however, despite giant leaps in technology, this dream is still far from reality. Explain to students that to explore the reasons why it is so difficult to make a thinking robot, they are going to create their very own chatting robot (chat bot).
Task 1:
Direct students to one of the online chat bots listed below. Instruct students to ask the chat bots a series questions and note down any unusual or unexpected answers.
Note: Some online chat-bots learn from previous conversations. As a result, there may be some cases where what the chat bot says may be deemed as inappropriate. Use these sites at your own risk. TeachwithICT.weebly.com is not responsible for the content of external Internet sites.
Online chat bots:
After a few minutes, stop the class and ask them to feedback their findings to the rest of the class.
Note down some of the students’ findings and ask the students why they think the chat bot could not answer all of their questions.
Ask the students why you think it is so hard to make a computer appear as if it able to think like a human. Try to draw out answers such as “Computers can only follow a given sequence of instructions” or “Computers can only respond to questions they have been programmed to answer” – give students prompts where necessary.
Explain to students about the Turing Test (insert link to wikipedia) and discuss the concept of Artificial Intelligence (AI). Finally, inform students that they are going to create a chatting robot (chat bot) and that there will be a prize for the most convincing robot (the prize is optional).
Direct students to one of the online chat bots listed below. Instruct students to ask the chat bots a series questions and note down any unusual or unexpected answers.
Note: Some online chat-bots learn from previous conversations. As a result, there may be some cases where what the chat bot says may be deemed as inappropriate. Use these sites at your own risk. TeachwithICT.weebly.com is not responsible for the content of external Internet sites.
Online chat bots:
- Evie (recommended!) - http://www.existor.com
- Jabberwacky - http://www.jabberwacky.com
- Brain Bot - http://www.botlibre.com/chat.jsp
- SkyNet Ai - http://home.comcast.net/~chatterbot/bots/AI/SkynetV5/
- Clever Bot - http://www.cleverbot.com
After a few minutes, stop the class and ask them to feedback their findings to the rest of the class.
Note down some of the students’ findings and ask the students why they think the chat bot could not answer all of their questions.
Ask the students why you think it is so hard to make a computer appear as if it able to think like a human. Try to draw out answers such as “Computers can only follow a given sequence of instructions” or “Computers can only respond to questions they have been programmed to answer” – give students prompts where necessary.
Explain to students about the Turing Test (insert link to wikipedia) and discuss the concept of Artificial Intelligence (AI). Finally, inform students that they are going to create a chatting robot (chat bot) and that there will be a prize for the most convincing robot (the prize is optional).
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)) |
The students finished code should look like this:
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!
Homework:
Students can continue to work on their chat bot and publish their script when they have finished.
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!
Homework:
Students can continue to work on their chat bot and publish their script when they have finished.
Resources:
Example solution
|
|
You may also be interested in:
Licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
Python Turing Test is licenced under a Creative Commons Attribution 4.0 International License.
Based on a work at https://github.com/raspberrypilearning/turing-test-lessons
Based on a work at https://github.com/raspberrypilearning/turing-test-lessons