Klasifikace záměru pomocí regexu II
Slovník patterns máš hotový – teď je čas definovat funkci, která zjistí záměr zprávy.
Toto cvičení je součástí kurzu
Building Chatbots in Python
Pokyny k cvičení
- Iteruj přes záměry (
intent) a vzory (pattern) ve slovníkupatternspomocí metody.items(). - Pomocí metody
.search()objektupatternhledej klíčová slova ve zprávěmessage. - Pokud je nalezena shoda, vrať odpovídající
intent. - Zavolej funkci
match_intent()uvnitřrespond()s argumentemmessagea klikni na Submit Answer, abys viděl/a, jak bot reaguje na zadané zprávy.
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 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!")