Creating a chatting robot (Chat bot)
Learn how to create a basic chat bot using this free python web tutorial.
Like this tutorial? Share it with your followers by clicking on the image below.
This tutorial 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)
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:
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
Chat bots
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. To explore the reasons why it is so difficult to make a thinking robot, we are going to create our own chatting robot (chat bot) in Python.
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. To explore the reasons why it is so difficult to make a thinking robot, we are going to create our own chatting robot (chat bot) in Python.
Use the window (above) to test your code.
STEP 1 - Creating a chat bot
In order for the chat bot to work, you will need to import the time and random libraries.
In order for the chat bot to work, you will need to import the time and random libraries.
import time
import random |
Next, we need to ask the user a question and store the response in a variable. Add 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()`. 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. 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'. 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". 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, add an input() asking the user to enter their favourite colour (remembering to add a natural pause):
First, 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, create a list of possible responses:
colours = ["Red","Green","Blue"]
|
Finally, add a response by choosing a colour from the list at random:
time.sleep(2)
print("My favourite colour is " + random.choice(colours)) |
Your finished code should look like this:
STEP 2 - Improving your chat bot
- 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.
- Modify your 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?" 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!") |
STEP 3
- 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))
|
Resources:
Example solution
|
|
You may also like:
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