Tvorba SQL dotazů z přirozeného jazyka
Teď napíšeš funkci respond(), která zvládne zpracovat zprávy jako "I want an expensive hotel in the south of town" a odpovědět podle toho, kolik výsledků se v databázi najde. To je klíčová funkčnost každého chatbota napojeného na databázi.
Funkce find_hotels() z předchozích cvičení je už připravená, stejně jako objekt interpreter z Rasa NLU, který umí zpracovat dotazy na hotely, a seznam responses — ten si můžeš prozkoumat v shellu.
Toto cvičení je součástí kurzu
Building Chatbots in Python
Interaktivní cvičení na vyzkoušení si v praxi
Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.
# Define respond()
def respond(message):
# Extract the entities
entities = ____.____(____)["____"]
# Initialize an empty params dictionary
params = {}
# Fill the dictionary with entities
for ent in entities:
params[ent["entity"]] = str(ent["value"])
# Find hotels that match the dictionary
results = ____
# Get the names of the hotels and index of the response
names = [r[0] for r in results]
n = ____(len(results),3)
# Select the nth element of the responses array
return ____[n].____(*names)