Intent classification with regex II
Intent classification with regex II
Intent classification with regex II
With your patterns dictionary created, it's now time to define a function to find the intent of a message.
- Iterate over the
intents andpatterns in thepatternsdictionary using its.items()method. - Use the
.search()method ofpatternto look for keywords in themessage. - If there is a match, return the corresponding
intent. - Call your
match_intent()function insiderespond()withmessageas the argument and then hit 'Submit Answer' to see how the bot responds to the provided messages.
___________________________________________________________________
def match_intent(message):
for intent, pattern in patterns.items():
if pattern.search(message):
return intent
def respond(message):
intent = match_intent(message)
if intent == 'greet':
return 'Hello, how can I help you today?'
elif intent == 'goodbye':
return 'Goodbye, have a great day!'
else:
return 'I am sorry, I do not understand what you mean.'
def send_message(message):
# Print user_template including the user_message
print(user_template.format(message))
# Get the bot's response to the message
response = respond(message)
# Print the bot template including the bot's response.
print(bot_template.format(response))
send_message("hello!")
send_message("bye byeee")
send_message("thanks very much!")
Last modified: Monday, 30 January 2023, 9:59 AM