Codecademy Logo

Rule-Based Chatbots

Rule-Based Chatbots

Rule-based chatbots are structured as a dialog tree and often use regular expressions to match a user’s input to human-like responses. The aim is to simulate the back-and-forth of a real-life conversation, often in a specific context, like telling the user what the weather is like outside. In chatbot design, rule-based chatbots are closed-domain, also called dialog agents, because they are limited to conversations on a specific subject.

Chatbot Intents

In chatbots design, an intent is the purpose or category of the user query. The user’s utterance gets matched to a chatbot intent. In rule-based chatbots, you can use regular expressions to match a user’s statement to a chatbot intent.

import re
matching_intents = {'weather_intent': [r'weather.*on (\w+)']}
def match_reply(self, reply):
for key, values in matching_intents.items():
for regex_pattern in values:
found_match = re.match(regex_pattern, reply.lower())
if found_match and key == 'weather_intent':
return weather_intent(found_match.groups()[0])
return input("I did not understand you. Can you please ask your question again?")

Chatbot Utterances

In chatbot design, an utterance is a statement that the user makes to the chatbot. The chatbot attempts to match the utterance to an intent.

Chatbot Entities

In chatbot design, an entity is a value that is parsed from a user utterance and passed for use within the user response.

Learn More on Codecademy