ComenzarEmpieza gratis

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.

Este ejercicio forma parte del curso

Building Chatbots in Python

Ver curso

Instrucciones del ejercicio

  • 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.

Ejercicio interactivo práctico

Prueba este ejercicio completando el código de muestra.

# 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
Editar y ejecutar código