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
Instrucciones del ejercicio
- Recorre los
intentypatterndel diccionariopatternsusando su método.items(). - Usa el método
.search()depatternpara buscar palabras clave enmessage. - Si hay coincidencia, devuelve la
intentcorrespondiente. - Llama a tu función
match_intent()dentro derespond()conmessagecomo 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!")