LoslegenKostenlos loslegen

Creating SQL from natural language

Now you'll write a respond() function that can handle messages like "I want an expensive hotel in the south of town" and respond appropriately according to the number of matching results in a database. This is an important functionality for any database-backed chatbot.

Your find_hotels() function from the previous exercises has already been defined for you, along with a Rasa NLU interpreter object, which can handle hotel queries, and a list of responses, which you can explore in the Shell.

Diese Übung ist Teil des Kurses

Building Chatbots in Python

Kurs anzeigen

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# 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)
Code bearbeiten und ausführen