How logic adapters select a response

A typical logic adapter designed to return a response to an input statement will use two main steps to do this. The first step involves searching the database for a known statement that matches or closely matches the input statement. Once a match is selected, the second step involves selecting a known response to the selected match. Frequently, there will be a number of existing statements that are responses to the known match.

To help with the selection of the response, several methods are build in to ChatterBot for selecting a response from the available options.

Response selection methods

Response selection methods determines which response should be used in the event that multiple responses are generated within a logic adapter.

chatterbot.response_selection.get_first_response(input_statement, response_list)[source]
Parameters:
  • input_statement (Statement) – A statement, that closely matches an input to the chat bot.
  • response_list (list) – A list of statement options to choose a response from.
Returns:

Return the first statement in the response list.

Return type:

Statement

chatterbot.response_selection.get_most_frequent_response(input_statement, response_list)[source]
Parameters:
  • input_statement (Statement) – A statement, that closely matches an input to the chat bot.
  • response_list (list) – A list of statement options to choose a response from.
Returns:

The response statement with the greatest number of occurrences.

Return type:

Statement

chatterbot.response_selection.get_random_response(input_statement, response_list)[source]
Parameters:
  • input_statement (Statement) – A statement, that closely matches an input to the chat bot.
  • response_list (list) – A list of statement options to choose a response from.
Returns:

Choose a random response from the selection.

Return type:

Statement

Use your own response selection method

You can create your own response selection method and use it as long as the function takes two parameters (a statements and a list of statements). The method must return a statement.

def select_response(statement, statement_list):

    # Your selection logic

    return selected_statement

Setting the response selection method

To set the response selection method for your chat bot, you will need to pass the response_selection_method parameter to your chat bot when you initialize it. An example of this is shown bellow.

from chatterbot import ChatBot
from chatterbot.response_selection import get_most_frequent_response

chatbot = ChatBot(
    # ...
    response_selection_method=get_most_frequent_response
)

Response selection in logic adapters

When a logic adapter is initialized, the response selection method parameter that was passed to it can be called using self.select_response as shown bellow.

response = self.select_response(
    input_statement, list_of_response_options
)