ComenzarEmpieza gratis

Refining your search

Now you'll write a bot that allows users to add filters incrementally, just in case they don't specify all of their preferences in one message.

To do this, initialize an empty dictionary params outside of your respond() function (as opposed to inside the function, like in the previous exercise). Your respond() function will take in this dictionary as an argument.

Este ejercicio forma parte del curso

Building Chatbots in Python

Ver curso

Instrucciones del ejercicio

  • Define a respond() function that accepts two arguments - a message and a dictionary of params - and returns two results - the message to send to the user and the updated params dictionary.
  • Extract "entities" from the message using the .parse() method of the interpreter, exactly like you did in the previous exercise.
  • Find the hotels that match params using your find_hotels() function.
  • Initialize the params dictionary outside the respond() function and hit 'Submit Answer' to pass the messages to the bot.

Ejercicio interactivo práctico

Prueba este ejercicio completando el código de muestra.

# Define a respond function, taking the message and existing params as input
def ____(____, ____):
    # Extract the entities
    entities = ____
    # Fill the dictionary with entities
    for ent in entities:
        params[ent["entity"]] = str(ent["value"])

    # Find the hotels
    results = ____
    names = [r[0] for r in results]
    n = min(len(results), 3)
    # Return the appropriate response
    return responses[n].format(*names), params

# Initialize params dictionary
params = ____

# Pass the messages to the bot
for message in ["I want an expensive hotel", "in the north of town"]:
    print("USER: {}".format(message))
    response, params = respond(message, params)
    print("BOT: {}".format(response))
Editar y ejecutar código