Putting it all together I
It's time to put everything you've learned in the course together by combining the coffee ordering bot with the ELIZA rules from chapter 1.
To begin, you'll define a function called chitchat_response()
, which calls the predefined function match_rule()
from back in chapter 1. This returns a response if the message matched an ELIZA template, and otherwise, None
.
The ELIZA rules are contained in a dictionary called eliza_rules
.
This exercise is part of the course
Building Chatbots in Python
Exercise instructions
- Define a
chitchat_response()
function which takes in amessage
argument. - Call the
match_rule()
function witheliza_rules
andmessage
as arguments. Unpack the output intoresponse
andphrase
. - If the response is
"default"
, returnNone
. - If
"{0}"
is in the response, replace the pronouns of thephrase
usingreplace_pronouns()
, and then include thephrase
in theresponse
by using.format()
onresponse
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define chitchat_response()
def chitchat_response(message):
# Call match_rule()
____, ____ = ____
# Return none if response is "default"
if response == "____":
return None
if '{0}' in response:
# Replace the pronouns of phrase
phrase = ____
# Calculate the response
response = ____
return response