Logic Adapters

Logic adapters determine the logic for how ChatterBot selects a responses 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.

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.

chatbot = ChatBot(
    "My ChatterBot",
    logic_adapters=[
        "chatterbot.logic.BestMatch"
    ]
)

Best Match Adapter

chatterbot.logic.BestMatch(**kwargs)[source]

A logic adater that returns a response based on known responses to the closest matches to the input statement.

The BestMatch logic adapter selects a response based on the best know match to a given statement.

How it works

The best match adapter determines uses an 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.levenshtein_distance",
            "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

chatterbot.logic.TimeLogicAdapter(**kwargs)[source]

The TimeLogicAdapter returns the current time.

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

chatterbot.logic.MathematicalEvaluation(**kwargs)[source]

The MathematicalEvaluation logic adapter parses input to determine whether the user is asking a question that requires math to be done. If so, MathematicalEvaluation goes through a set of steps to parse the input and extract the equation that must be solved. The steps, in order, are:

  1. Normalize input: Remove punctuation and other irrelevant data
  2. Convert words to numbers
  3. Extract the equation
  4. Simplify the equation
  5. Solve the equation & return result

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

Low Confidence Response Adapter

This adapter returns a specified default response if a response can not be determined with a high amount of confidence.

chatterbot.logic.LowConfidenceAdapter(**kwargs)[source]

Returns a default response with a high confidence when a high confidence response is not known.

Low confidence response example

# -*- coding: utf-8 -*-
from chatterbot import ChatBot


# Create a new instance of a ChatBot
bot = ChatBot(
    'Default Response Example Bot',
    storage_adapter='chatterbot.storage.JsonFileStorageAdapter',
    logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch'
        },
        {
            'import_path': 'chatterbot.logic.LowConfidenceAdapter',
            'threshold': 0.65,
            'default_response': 'I am sorry, but I do not understand.'
        }
    ],
    trainer='chatterbot.trainers.ListTrainer'
)

# Train the chat bot with a few responses
bot.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)

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.

chatterbot.logic.SpecificResponseAdapter(**kwargs)[source]

Return a specific response to a specific input.

Specific response example

# -*- coding: utf-8 -*-
from chatterbot import ChatBot


# Create a new instance of a ChatBot
bot = ChatBot(
    'Exact Response Example Bot',
    storage_adapter='chatterbot.storage.JsonFileStorageAdapter',
    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/en/latest/quickstart.html'
        }
    ],
    trainer='chatterbot.trainers.ListTrainer'
)

# Get a response given the specific input
response = bot.get_response('Help me!')
print(response)