ELIZA IV: Putting it all together
Now you're going to put everything from the previous exercises together and experience the magic!
The match_rule(), send_message(), and replace_pronouns() functions have already been defined, and the rules dictionary is available in your workspace.
Your job here is to write a function called respond() with a single argument message which creates an appropriate response to be handled by send_message().
This exercise is part of the course
Building Chatbots in Python
Exercise instructions
- Get a
responseandphraseby callingmatch_rule()with therulesdictionary andmessage. - Check if the
responseis a template by seeing if it includes the string'{0}'. If it does:- Use the
replace_pronouns()function onphrase. - Include the
phraseby using.format()onresponseand overriding the value ofresponse.
- Use the
- Hit 'Submit Answer' to see how the bot responds to the provided messages!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define respond()
def respond(message):
# Call match_rule
____, ____ = ____
if '{0}' in response:
# Replace the pronouns in the phrase
phrase = ____
# Include the phrase in the response
response = ____
return response
# Send the messages
send_message("do you remember your last birthday")
send_message("do you think humans should be worried about AI")
send_message("I want a robot friend")
send_message("what if you could be anything you wanted")