Get startedGet started for free

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

View Course

Exercise instructions

  • Define a chitchat_response() function which takes in a message argument.
  • Call the match_rule() function with eliza_rules and message as arguments. Unpack the output into response and phrase.
  • If the response is "default", return None.
  • If "{0}" is in the response, replace the pronouns of the phrase using replace_pronouns(), and then include the phrase in the response by using .format() on response.

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
Edit and Run Code