Logic Adapters¶
Logic adapters determine the logic for how ChatterBot selects a response to a given input statement.
The logic adapter that your bot uses can be specified by setting the logic_adapters
parameter
to the import path of the logic adapter you want to use.
chatbot = ChatBot(
"My ChatterBot",
logic_adapters=[
"chatterbot.logic.BestMatch"
]
)
It is possible to enter any number of logic adapters for your bot to use. If multiple adapters are used, then the bot will return the response with the highest calculated confidence value. If multiple adapters return the same confidence, then the adapter that is entered into the list first will take priority.
Common logic adapter attributes¶
Each logic adapter inherits the following attributes and methods.
Best Match Adapter¶
The BestMatch
logic adapter selects a response based on the best known match to a given statement.
How it works¶
The best match adapter uses a function to compare the input statement to known statements. Once it finds the closest match to the input statement, it uses another function to select one of the known responses to that statement.
Setting parameters¶
chatbot = ChatBot(
"My ChatterBot",
logic_adapters=[
{
"import_path": "chatterbot.logic.BestMatch",
"statement_comparison_function": chatterbot.comparisons.LevenshteinDistance,
"response_selection_method": chatterbot.response_selection.get_first_response
}
]
)
Note
The values for response_selection_method
and statement_comparison_function
can be a string
of the path to the function, or a callable.
See the Statement comparison documentation for the list of functions included with ChatterBot.
See the Response selection methods documentation for the list of response selection methods included with ChatterBot.
Time Logic Adapter¶
The TimeLogicAdapter
identifies statements in which a question about the current time is asked.
If a matching question is detected, then a response containing the current time is returned.
User: What time is it?
Bot: The current time is 4:45PM.
Mathematical Evaluation Adapter¶
The MathematicalEvaluation
logic adapter checks a given statement to see if
it contains a mathematical expression that can be evaluated.
If one exists, then it returns a response containing the result.
This adapter is able to handle any combination of word and numeric operators.
User: What is four plus four?
Bot: (4 + 4) = 8
Specific Response Adapter¶
If the input that the chat bot receives, matches the input text specified for this adapter, the specified response will be returned.
Specific response example¶
from chatterbot import ChatBot
# Create a new instance of a ChatBot
bot = ChatBot(
'Exact Response Example Bot',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch'
},
{
'import_path': 'chatterbot.logic.SpecificResponseAdapter',
'input_text': 'Help me!',
'output_text': 'Ok, here is a link: http://chatterbot.rtfd.org'
}
]
)
# Get a response given the specific input
response = bot.get_response('Help me!')
print(response)
Low confidence response example¶
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
# Create a new instance of a ChatBot
bot = ChatBot(
'Example Bot',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch',
'default_response': 'I am sorry, but I do not understand.',
'maximum_similarity_threshold': 0.90
}
]
)
trainer = ListTrainer(bot)
# Train the chat bot with a few responses
trainer.train([
'How can I help you?',
'I want to create a chat bot',
'Have you read the documentation?',
'No, I have not',
'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html'
])
# Get a response for some unexpected input
response = bot.get_response('How do I make an omelette?')
print(response)