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.
This exercise is part of the course
Building Chatbots in Python
Exercise instructions
- Define a
respond()
function that accepts two arguments - amessage
and a dictionary ofparams
- and returns two results - the message to send to the user and the updatedparams
dictionary. - Extract
"entities"
from themessage
using the.parse()
method of theinterpreter
, exactly like you did in the previous exercise. - Find the hotels that match
params
using yourfind_hotels()
function. - Initialize the
params
dictionary outside therespond()
function and hit 'Submit Answer' to pass the messages to the bot.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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))