ComenzarEmpieza gratis

Clasificación de intenciones con regex II

Con tu diccionario patterns creado, es hora de definir una función que encuentre la intención de un mensaje.

Este ejercicio forma parte del curso

Creación de chatbots en Python

Ver curso

Instrucciones del ejercicio

  • Recorre los intent y pattern del diccionario patterns usando su método .items().
  • Usa el método .search() de pattern para buscar palabras clave en message.
  • Si hay coincidencia, devuelve la intent correspondiente.
  • Llama a tu función match_intent() dentro de respond() con message como argumento y luego pulsa "Enviar respuesta" para ver cómo responde el bot a los mensajes proporcionados.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# Define a function to find the intent of a message
def match_intent(message):
    matched_intent = None
    for intent, pattern in ____:
        # Check if the pattern occurs in the message 
        if ____:
            matched_intent = ____
    return matched_intent

# Define a respond function
def respond(message):
    # Call the match_intent function
    intent = ____
    # Fall back to the default response
    key = "default"
    if intent in responses:
        key = intent
    return responses[key]

# Send messages
send_message("hello!")
send_message("bye byeee")
send_message("thanks very much!")
Editar y ejecutar código