_images/banner.png

About ChatterBot

ChatterBot is a Python library that makes it easy to generate automated responses to a user’s input. ChatterBot uses a selection of machine learning algorithms to produce different types of responses. This makes it easy for developers to create chat bots and automate conversations with users. For more details about the ideas and concepts behind ChatterBot see the process flow diagram.

An example of typical input would be something like this:

user: Good morning! How are you doing?
bot:  I am doing very well, thank you for asking.
user: You're welcome.
bot:  Do you like hats?

Language Independence

The language independent design of ChatterBot allows it to be trained to speak any language. Additionally, the machine-learning nature of ChatterBot allows an agent instance to improve it’s own knowledge of possible responses as it interacts with humans and other sources of informative data.

How ChatterBot Works

ChatterBot is a Python library designed to make it easy to create software that can engage in conversation.

An untrained instance of ChatterBot starts off with no knowledge of how to communicate. Each time a user enters a statement, the library saves the text that they entered and the text that the statement was in response to. As ChatterBot receives more input the number of responses that it can reply and the accuracy of each response in relation to the input statement increase.

The program selects the closest matching response by searching for the closest matching known statement that matches the input, it then chooses a response from the selection of known responses to that statement.

Process flow diagram

ChatterBot process flow diagram

Contents:

Installation

The recommended method for installing ChatterBot is by using pip.

Installing from PyPi

If you are just getting started with ChatterBot, it is recommended that you start by installing the latest version from the Python Package Index (PyPi). To install ChatterBot from PyPi using pip run the following command in your terminal.

pip install chatterbot

Installing from GitHub

You can install the latest development version of ChatterBot directly from GitHub using pip.

pip install git+git://github.com/gunthercox/ChatterBot.git@master

Installing from source

  1. Download a copy of the code from GitHub. You may need to install git.
git clone https://github.com/gunthercox/ChatterBot.git
  1. Install the code you have just downloaded using pip
pip install ./ChatterBot

Checking the version of ChatterBot that you have installed

If you already have ChatterBot installed and you want to check what version you have installed you can run the following command.

python -m chatterbot --version

Upgrading ChatterBot to the latest version

Upgrading to Newer Releases

Like any software, changes will be made to ChatterBot over time. Most of these changes are improvements. Frequently, you don’t have to change anything in your code to benefit from a new release.

Occasionally there are changes that will require modifications in your code or there will be changes that make it possible for you to improve your code by taking advantage of new features.

To view a record of ChatterBot’s history of changes, visit the releases tab on ChatterBot’s GitHub page.

Use the pip command to upgrade your existing ChatterBot installation by providing the –upgrade parameter:

pip install chatterbot --upgrade

Also see Versioning for information about ChatterBot’s versioning policy.

Quick Start Guide

The first thing you’ll need to do to get started is install ChatterBot.

pip install chatterbot

See Installation for options for alternative installation methods.

Create a new chat bot

from chatterbot import ChatBot
chatbot = ChatBot("Ron Obvious")

Note

The only required parameter for the ChatBot is a name. This can be anything you want.

Training your ChatBot

After creating a new ChatterBot instance it is also possible to train the bot. Training is a good way to ensure that the bot starts off with knowledge about specific responses. The current training method takes a list of statements that represent a conversation. Additional notes on training can be found in the Training documentation.

Note

Training is not required but it is recommended.

from chatterbot.trainers import ListTrainer

conversation = [
    "Hello",
    "Hi there!",
    "How are you doing?",
    "I'm doing great.",
    "That is good to hear",
    "Thank you.",
    "You're welcome."
]

trainer = ListTrainer(chatbot)

trainer.train(conversation)

Get a response

response = chatbot.get_response("Good morning!")
print(response)

ChatterBot Tutorial

This tutorial will guide you through the process of creating a simple command-line chat bot using ChatterBot.

Getting help

If you’re having trouble with this tutorial, you can post a message on Gitter to chat with other ChatterBot users who might be able to help.

You can also ask questions on Stack Overflow under the chatterbot tag.

If you believe that you have encountered an error in ChatterBot, please open a ticket on GitHub: https://github.com/gunthercox/ChatterBot/issues/new

Installing ChatterBot

You can install ChatterBot on your system using Python’s pip command.

pip install chatterbot

See Installation for alternative installation options.

Creating your first chat bot

Create a new file named chatbot.py. Then open chatbot.py in your editor of choice.

Before we do anything else, ChatterBot needs to be imported. The import for ChatterBot should look like the following line.

from chatterbot import ChatBot

Create a new instance of the ChatBot class.

bot = ChatBot('Norman')

This line of code has created a new chat bot named Norman. There is a few more parameters that we will want to specify before we run our program for the first time.

Setting the storage adapter

ChatterBot comes with built in adapter classes that allow it to connect to different types of databases. In this tutorial, we will be using the SQLStorageAdapter which allows the chat bot to connect to SQL databases. By default, this adapter will create a SQLite database.

The database parameter is used to specify the path to the database that the chat bot will use. For this example we will call the database sqlite:///database.sqlite3. this file will be created automatically if it doesn’t already exist.

bot = ChatBot(
    'Norman',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    database_uri='sqlite:///database.sqlite3'
)

Note

The SQLStorageAdapter is ChatterBot’s default adapter. If you do not specify an adapter in your constructor, the SQLStorageAdapter adapter will be used automatically.

Specifying logic adapters

The logic_adapters parameter is a list of logic adapters. In ChatterBot, a logic adapter is a class that takes an input statement and returns a response to that statement.

You can choose to use as many logic adapters as you would like. In this example we will use two logic adapters. The TimeLogicAdapter returns the current time when the input statement asks for it. The MathematicalEvaluation adapter solves math problems that use basic operations.

bot = ChatBot(
    'Norman',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    logic_adapters=[
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter'
    ],
    database_uri='sqlite:///database.sqlite3'
)

Getting a response from your chat bot

Next, you will want to create a while loop for your chat bot to run in. By breaking out of the loop when specific exceptions are triggered, we can exit the loop and stop the program when a user enters ctrl+c.

while True:
    try:
        bot_input = bot.get_response(input())
        print(bot_input)

    except(KeyboardInterrupt, EOFError, SystemExit):
        break

Training your chat bot

At this point your chat bot, Norman will learn to communicate as you talk to him. You can speed up this process by training him with examples of existing conversations.

from chatterbot.trainers import ListTrainer

trainer = ListTrainer(bot)

trainer.train([
    'How are you?',
    'I am good.',
    'That is good to hear.',
    'Thank you',
    'You are welcome.',
])

You can run the training process multiple times to reinforce preferred responses to particular input statements. You can also run the train command on a number of different example dialogs to increase the breadth of inputs that your chat bot can respond to.


This concludes this ChatterBot tutorial. Please see other sections of the documentation for more details and examples.

Up next: Examples

Examples

The following examples are available to help you get started with ChatterBot.

Note

Before you run any example, you will need to install ChatterBot on your system. See the Setup guide for instructions.

Simple Example

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

# Create a new chat bot named Charlie
chatbot = ChatBot('Charlie')

trainer = ListTrainer(chatbot)

trainer.train([
    "Hi, can I help you?",
    "Sure, I'd like to book a flight to Iceland.",
    "Your flight has been booked."
])

# Get a response to the input text 'I would like to book a flight.'
response = chatbot.get_response('I would like to book a flight.')

print(response)

Terminal Example

This example program shows how to create a simple terminal client that allows you to communicate with your chat bot by typing into your terminal.

from chatterbot import ChatBot


# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)

# Create a new instance of a ChatBot
bot = ChatBot(
    'Terminal',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    logic_adapters=[
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter',
        'chatterbot.logic.BestMatch'
    ],
    database_uri='sqlite:///database.db'
)

print('Type something to begin...')

# The following loop will execute each time the user enters input
while True:
    try:
        user_input = input()

        bot_response = bot.get_response(user_input)

        print(bot_response)

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

Using MongoDB

Before you can use ChatterBot’s built in adapter for MongoDB, you will need to install MongoDB. Make sure MongoDB is running in your environment before you execute your program. To tell ChatterBot to use this adapter, you will need to set the storage_adapter parameter.

storage_adapter="chatterbot.storage.MongoDatabaseAdapter"
from chatterbot import ChatBot

# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)

# Create a new ChatBot instance
bot = ChatBot(
    'Terminal',
    storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
    logic_adapters=[
        'chatterbot.logic.BestMatch'
    ],
    database_uri='mongodb://localhost:27017/chatterbot-database'
)

print('Type something to begin...')

while True:
    try:
        user_input = input()

        bot_response = bot.get_response(user_input)

        print(bot_response)

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

Time and Mathematics Example

ChatterBot has natural language evaluation capabilities that allow it to process and evaluate mathematical and time-based inputs.

from chatterbot import ChatBot


bot = ChatBot(
    'Math & Time Bot',
    logic_adapters=[
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter'
    ]
)

# Print an example of getting one math based response
response = bot.get_response('What is 4 + 9?')
print(response)

# Print an example of getting one time based response
response = bot.get_response('What time is it?')
print(response)

Using SQL Adapter

ChatterBot data can be saved and retrieved from SQL databases.

from chatterbot import ChatBot

# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)

# Create a new instance of a ChatBot
bot = ChatBot(
    'SQLMemoryTerminal',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    database_uri=None,
    logic_adapters=[
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter',
        'chatterbot.logic.BestMatch'
    ]
)

# Get a few responses from the bot

bot.get_response('What time is it?')

bot.get_response('What is 7 plus 7?')

Read only mode

Your chat bot will learn based on each new input statement it receives. If you want to disable this learning feature after your bot has been trained, you can set read_only=True as a parameter when initializing the bot.

chatbot = ChatBot("Johnny Five", read_only=True)

More Examples

Even more examples can be found in the examples directory on GitHub: https://github.com/gunthercox/ChatterBot/tree/master/examples

Training

ChatterBot includes tools that help simplify the process of training a chat bot instance. ChatterBot’s training process involves loading example dialog into the chat bot’s database. This either creates or builds upon the graph data structure that represents the sets of known statements and responses. When a chat bot trainer is provided with a data set, it creates the necessary entries in the chat bot’s knowledge graph so that the statement inputs and responses are correctly represented.

ChatterBot training statement graph

Several training classes come built-in with ChatterBot. These utilities range from allowing you to update the chat bot’s database knowledge graph based on a list of statements representing a conversation, to tools that allow you to train your bot based on a corpus of pre-loaded training data.

You can also create your own training class. This is recommended if you wish to train your bot with data you have stored in a format that is not already supported by one of the pre-built classes listed below.

Setting the training class

ChatterBot comes with training classes built in, or you can create your own if needed. To use a training class you call train() on an instance that has been initialized with your chat bot.

Training classes

Training via list data

For the training process, you will need to pass in a list of statements where the order of each statement is based on its placement in a given conversation.

For example, if you were to run bot of the following training calls, then the resulting chatterbot would respond to both statements of “Hi there!” and “Greetings!” by saying “Hello”.

chatbot.py
 chatbot = ChatBot('Training Example')
train.py
from chatbot import chatbot
from chatterbot.trainers import ListTrainer

trainer = ListTrainer(chatbot)

trainer.train([
    "Hi there!",
    "Hello",
])

trainer.train([
    "Greetings!",
    "Hello",
])

You can also provide longer lists of training conversations. This will establish each item in the list as a possible response to it’s predecessor in the list.

train.py
trainer.train([
    "How are you?",
    "I am good.",
    "That is good to hear.",
    "Thank you",
    "You are welcome.",
])

Training with corpus data

ChatterBot comes with a corpus data and utility module that makes it easy to quickly train your bot to communicate. To do so, simply specify the corpus data modules you want to use.

chatbot.py
 chatbot = ChatBot('Training Example')
train.py
from chatbot import chatbot
from chatterbot.trainers import ChatterBotCorpusTrainer

trainer = ChatterBotCorpusTrainer(chatbot)

trainer.train(
    "chatterbot.corpus.english"
)
Specifying corpus scope

It is also possible to import individual subsets of ChatterBot’s corpus at once. For example, if you only wish to train based on the english greetings and conversations corpora then you would simply specify them.

train.py
trainer.train(
    "chatterbot.corpus.english.greetings",
    "chatterbot.corpus.english.conversations"
)

You can also specify file paths to corpus files or directories of corpus files when calling the train method.

train.py
trainer.train(
    "./data/greetings_corpus/custom.corpus.json",
    "./data/my_corpus/"
)

Training with the Ubuntu dialog corpus

Warning

The Ubuntu dialog corpus is a massive data set. Developers will currently experience significantly decreased performance in the form of delayed training and response times from the chat bot when using this corpus.

This training class makes it possible to train your chat bot using the Ubuntu dialog corpus. Because of the file size of the Ubuntu dialog corpus, the download and training process may take a considerable amount of time.

This training class will handle the process of downloading the compressed corpus file and extracting it. If the file has already been downloaded, it will not be downloaded again. If the file is already extracted, it will not be extracted again.

Creating a new training class

You can create a new trainer to train your chat bot from your own data files. You may choose to do this if you want to train your chat bot from a data source in a format that is not directly supported by ChatterBot.

Your custom trainer should inherit chatterbot.trainers.Trainer class. Your trainer will need to have a method named train, that can take any parameters you choose.

Take a look at the existing trainer classes on GitHub for examples.

Preprocessors

ChatterBot’s preprocessors are simple functions that modify the input statement that a chat bot receives before the statement gets processed by the logic adaper.

Here is an example of how to set preprocessors. The preprocessors parameter should be a list of strings of the import paths to your preprocessors.

chatbot = ChatBot(
    'Bob the Bot',
    preprocessors=[
        'chatterbot.preprocessors.clean_whitespace'
    ]
)

Preprocessor functions

ChatterBot comes with several built-in preprocessors.

Creating new preprocessors

It is simple to create your own preprocessors. A preprocessor is just a function with a few requirements.

  1. It must take one parameter, a Statement instance.
  2. It must return a statement instance.

Logic Adapters

Logic adapters determine the logic for how ChatterBot selects a response to a given input statement.

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 built into ChatterBot for selecting a response from the available options.

Response selection methods

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, storage=None):

    # 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 below.

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 below.

response = self.select_response(
    input_statement,
    list_of_response_options,
    self.chatbot.storage
)

Selecting a response from multiple logic adapters

The generate_response method is used to select a single response from the responses returned by all of the logic adapters that the chat bot has been configured to use. Each response returned by the logic adapters includes a confidence score that indicates the likeliness that the returned statement is a valid response to the input.

Response selection

The generate_response will return the response statement that has the greatest confidence score. The only exception to this is a case where multiple logic adapters return the same statement and therefore agree on that response.

For this example, consider a scenario where multiple logic adapters are being used. Assume the following results were returned by a chat bot’s logic adapters.

Confidence Statement
0.2 Good morning
0.5 Good morning
0.7 Good night

In this case, two of the logic adapters have generated the same result. When multiple logic adapters come to the same conclusion, that statement is given priority over another response with a possibly higher confidence score. The fact that the multiple adapters agreed on a response is a significant indicator that a particular statement has a greater probability of being a more accurate response to the input.

When multiple adapters agree on a response, the greatest confidence score that was generated for that response will be returned with it.

Creating a new logic adapter

You can write your own logic adapters by creating a new class that inherits from LogicAdapter and overrides the necessary methods established in the LogicAdapter base class.

Example logic adapter

from chatterbot.logic import LogicAdapter


class MyLogicAdapter(LogicAdapter):
    def __init__(self, chatbot, **kwargs):
        super().__init__(chatbot, **kwargs)

    def can_process(self, statement):
        return True

    def process(self, input_statement, additional_response_selection_parameters):
        import random

        # Randomly select a confidence between 0 and 1
        confidence = random.uniform(0, 1)

        # For this example, we will just return the input as output
        selected_statement = input_statement
        selected_statement.confidence = confidence

        return selected_statement

Directory structure

If you create your own logic adapter you will need to have it in a separate file from your chat bot. Your directory setup should look something like the following:

project_directory
├── cool_chatbot.py
└── cool_adapter.py

Then assuming that you have a class named MyLogicAdapter in your cool_adapter.py file, you should be able to specify the following when you initialize your chat bot.

ChatBot(
    # ...
    logic_adapters=[
        {
            'import_path': 'cool_adapter.MyLogicAdapter'
        }
    ]
)

Responding to specific input

If you want a particular logic adapter to only respond to a unique type of input, the best way to do this is by overriding the can_process method on your own logic adapter.

Here is a simple example. Let’s say that we only want this logic adapter to generate a response when the input statement starts with “Hey Mike”. This way, statements such as “Hey Mike, what time is it?” will be processed, but statements such as “Do you know what time it is?” will not be processed.

def can_process(self, statement):
    if statement.text.startswith('Hey Mike')
        return True
    else:
        return False

Interacting with services

In some cases, it is useful to have a logic adapter that can interact with an external service or api in order to complete its task. Here is an example that demonstrates how this could be done. For this example we will use a fictitious API endpoint that returns the current temperature.

def can_process(self, statement):
    """
    Return true if the input statement contains
    'what' and 'is' and 'temperature'.
    """
    words = ['what', 'is', 'temperature']
    if all(x in statement.text.split() for x in words)
        return True
    else:
        return False

def process(self, input_statement, additional_response_selection_parameters):
    from chatterbot.conversation import Statement
    import requests

    # Make a request to the temperature API
    response = requests.get('https://api.temperature.com/current?units=celsius')
    data = response.json()

    # Let's base the confidence value on if the request was successful
    if response.status_code == 200:
        confidence = 1
    else:
        confidence = 0

    temperature = data.get('temperature', 'unavailable')

    response_statement = Statement(text='The current temperature is {}'.format(temperature))

    return confidence, response_statement

Providing extra arguments

All key word arguments that have been set in your ChatBot class’s constructor will also be passed to the __init__ method of each logic adapter. This allows you to access these variables if you need to use them in your logic adapter. (An API key might be an example of a parameter you would want to access here.)

You can override the __init__ method on your logic adapter to store additional information passed to it by the ChatBot class.

class MyLogicAdapter(LogicAdapter):
    def __init__(self, chatbot, **kwargs):
        super().__init__(chatbot, **kwargs)

        self.api_key = kwargs.get('secret_key')

The secret_key variable would then be passed to the ChatBot class as shown below.

chatbot = ChatBot(
    # ...
    secret_key='************************'
 )

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.

ChatterBot dialog processing flow

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)

Storage Adapters

Storage adapters provide an interface that allows ChatterBot to connect to different storage technologies.

Creating a new storage adapter

You can write your own storage adapters by creating a new class that inherits from StorageAdapter and overrides necessary methods established in the base StorageAdapter class.

You will then need to implement the interface established by the StorageAdapter class.

import logging
from chatterbot import languages
from chatterbot.tagging import PosLemmaTagger


class StorageAdapter(object):
    """
    This is an abstract class that represents the interface
    that all storage adapters should implement.
    """

    def __init__(self, *args, **kwargs):
        """
        Initialize common attributes shared by all storage adapters.

        :param str tagger_language: The language that the tagger uses to remove stopwords.
        """
        self.logger = kwargs.get('logger', logging.getLogger(__name__))

        self.tagger = PosLemmaTagger(language=kwargs.get(
            'tagger_language', languages.ENG
        ))

    def get_model(self, model_name):
        """
        Return the model class for a given model name.

        model_name is case insensitive.
        """
        get_model_method = getattr(self, 'get_%s_model' % (
            model_name.lower(),
        ))

        return get_model_method()

    def get_object(self, object_name):
        """
        Return the class for a given object name.

        object_name is case insensitive.
        """
        get_model_method = getattr(self, 'get_%s_object' % (
            object_name.lower(),
        ))

        return get_model_method()

    def get_statement_object(self):
        from chatterbot.conversation import Statement

        StatementModel = self.get_model('statement')

        Statement.statement_field_names.extend(
            StatementModel.extra_statement_field_names
        )

        return Statement

    def count(self):
        """
        Return the number of entries in the database.
        """
        raise self.AdapterMethodNotImplementedError(
            'The `count` method is not implemented by this adapter.'
        )

    def remove(self, statement_text):
        """
        Removes the statement that matches the input text.
        Removes any responses from statements where the response text matches
        the input text.
        """
        raise self.AdapterMethodNotImplementedError(
            'The `remove` method is not implemented by this adapter.'
        )

    def filter(self, **kwargs):
        """
        Returns a list of objects from the database.
        The kwargs parameter can contain any number
        of attributes. Only objects which contain
        all listed attributes and in which all values
        match for all listed attributes will be returned.

        :param page_size: The maximum number of records to load into
            memory at once when returning results.
            Defaults to 1000

        :param order_by: The field name that should be used to determine
            the order that results are returned in.
            Defaults to None

        :param tags: A list of tags. When specified, the results will only
            include statements that have a tag in the provided list.
            Defaults to [] (empty list)

        :param exclude_text: If the ``text`` of a statement is an exact match
            for the value of this parameter the statement will not be
            included in the result set.
            Defaults to None

        :param exclude_text_words: If the ``text`` of a statement contains a
            word from this list then the statement will not be included in
            the result set.
            Defaults to [] (empty list)

        :param persona_not_startswith: If the ``persona`` field of a
            statement starts with the value specified by this parameter,
            then the statement will not be returned in the result set.
            Defaults to None

        :param search_text_contains: If the ``search_text`` field of a
            statement contains a word that is in the string provided to
            this parameter, then the statement will be included in the
            result set.
            Defaults to None
        """
        raise self.AdapterMethodNotImplementedError(
            'The `filter` method is not implemented by this adapter.'
        )

    def create(self, **kwargs):
        """
        Creates a new statement matching the keyword arguments specified.
        Returns the created statement.
        """
        raise self.AdapterMethodNotImplementedError(
            'The `create` method is not implemented by this adapter.'
        )

    def create_many(self, statements):
        """
        Creates multiple statement entries.
        """
        raise self.AdapterMethodNotImplementedError(
            'The `create_many` method is not implemented by this adapter.'
        )

    def update(self, statement):
        """
        Modifies an entry in the database.
        Creates an entry if one does not exist.
        """
        raise self.AdapterMethodNotImplementedError(
            'The `update` method is not implemented by this adapter.'
        )

    def get_random(self):
        """
        Returns a random statement from the database.
        """
        raise self.AdapterMethodNotImplementedError(
            'The `get_random` method is not implemented by this adapter.'
        )

    def drop(self):
        """
        Drop the database attached to a given adapter.
        """
        raise self.AdapterMethodNotImplementedError(
            'The `drop` method is not implemented by this adapter.'
        )

    class EmptyDatabaseException(Exception):

        def __init__(self, message=None):
            default = 'The database currently contains no entries. At least one entry is expected. You may need to train your chat bot to populate your database.'
            super().__init__(message or default)

    class AdapterMethodNotImplementedError(NotImplementedError):
        """
        An exception to be raised when a storage adapter method has not been implemented.
        Typically this indicates that the method should be implement in a subclass.
        """
        pass

The storage adapter that your bot uses can be specified by setting the storage_adapter parameter to the import path of the storage adapter you want to use.

chatbot = ChatBot(
    "My ChatterBot",
    storage_adapter="chatterbot.storage.SQLStorageAdapter"
)

Common storage adapter attributes

Each storage adapter inherits the following attributes and methods.

SQL Storage Adapter

MongoDB Storage Adapter

Database Migrations

Various frameworks such as Django and SQL Alchemy support functionality that allows revisions to be made to databases programmatically. This makes it possible for updates and revisions to structures in the database to be be applied in consecutive version releases.

The following explains the included migration process for each of the databases that ChatterBot comes with support for.

  • Django: Full schema migrations and data migrations will be included with each release.
  • SQL Alchemy: No migrations are currently provided in releases. If you require migrations between versions Alembic is the recommended solution for generating them.
  • MongoDB: No migrations are provided.

Filters

Filters are an efficient way to create queries that can be passed to ChatterBot’s storage adapters. Filters will reduce the number of statements that a chat bot has to process when it is selecting a response.

Setting filters

chatbot = ChatBot(
    "My ChatterBot",
    filters=[filters.get_recent_repeated_responses]
)

ChatterBot

The main class ChatBot is a connecting point between each of ChatterBot’s adapters. In this class, an input statement is processed and stored by the logic adapter and storage adapter. A response to the input is then generated and returned.

Example chat bot parameters

ChatBot(
    'Northumberland',
    storage_adapter='my.storage.AdapterClass',
    logic_adapters=[
        'my.logic.AdapterClass1',
        'my.logic.AdapterClass2'
    ],
    logger=custom_logger
)

Example expanded chat bot parameters

It is also possible to pass parameters directly to individual adapters. To do this, you must use a dictionary that contains a key called import_path which specifies the import path to the adapter class.

ChatBot(
    'Leander Jenkins',
    storage_adapter={
        'import_path': 'my.storage.AdapterClass',
        'database_uri': 'protocol://my-database'
    },
    logic_adapters=[
        {
            'import_path': 'my.logic.AdapterClass1',
            'statement_comparison_function': chatterbot.comparisons.LevenshteinDistance
            'response_selection_method': chatterbot.response_selection.get_first_response
        },
        {
            'import_path': 'my.logic.AdapterClass2',
            'statement_comparison_function': my_custom_comparison_function
            'response_selection_method': my_custom_selection_method
        }
    ]
)

Enable logging

ChatterBot has built in logging. You can enable ChatterBot’s logging by setting the logging level in your code.

import logging

logging.basicConfig(level=logging.INFO)

ChatBot(
    # ...
)

The logging levels available are CRITICAL, ERROR, WARNING, INFO, DEBUG, and NOTSET. See the Python logging documentation for more information.

Using a custom logger

You can choose to use your own custom logging class with your chat bot. This can be useful when testing and debugging your code.

import logging

custom_logger = logging.getLogger(__name__)

ChatBot(
    # ...
    logger=custom_logger
)

Adapters

ChatterBot uses adapter modules to control the behavior of specific types of tasks. There are four distinct types of adapters that ChatterBot uses, these are storage adapters and logic adapters.

Adapters types

  1. Storage adapters - Provide an interface for ChatterBot to connect to various storage systems such as MongoDB or local file storage.
  2. Logic adapters - Define the logic that ChatterBot uses to respond to input it receives.

Accessing the ChatBot instance

When ChatterBot initializes each adapter, it sets an attribute named chatbot. The chatbot variable makes it possible for each adapter to have access to all of the other adapters being used. Suppose logic adapters need to share some information or perhaps you want to give your logic adapter direct access to the storage adapter. These are just a few cases where this functionality is useful.

Each adapter can be accessed on the chatbot object from within an adapter by referencing self.chatbot. Then, self.chatbot.storage refers to the storage adapter, and self.chatbot.logic refers to the logic adapters.

Conversations

ChatterBot supports the ability to have multiple concurrent conversations. A conversations is where the chat bot interacts with a person, and supporting multiple concurrent conversations means that the chat bot can have multiple different conversations with different people at the same time.

Conversation scope

If two ChatBot instances are created, each will have conversations separate from each other.

An adapter can access any conversation as long as the unique identifier for the conversation is provided.

Conversation example

The following example is taken from the Django ChatterBotApiView built into ChatterBot. In this method, the unique identifiers for each chat session are being stored in Django’s session objects. This allows different users who interact with the bot through different web browsers to have separate conversations with the chat bot.

def post(self, request, *args, **kwargs):
    """
    Return a response to the statement in the posted data.

    * The JSON data should contain a 'text' attribute.
    """
    input_data = json.loads(request.body.decode('utf-8'))

    if 'text' not in input_data:
        return JsonResponse({
            'text': [
                'The attribute "text" is required.'
            ]
        }, status=400)

    response = self.chatterbot.get_response(input_data)

    response_data = response.serialize()

    return JsonResponse(response_data, status=200)

Statements

ChatterBot’s statement objects represent either an input statement that the chat bot has received from a user, or an output statement that the chat bot has returned based on some input.

Statement-response relationship

ChatterBot stores knowledge of conversations as statements. Each statement can have any number of possible responses.

ChatterBot statement-response relationship

Each Statement object has an in_response_to reference which links the statement to a number of other statements that it has been learned to be in response to. The in_response_to attribute is essentially a reference to all parent statements of the current statement.

ChatterBot statement relationship

The count of recorded statements with matching, or similar text indicates the number of times that the statement has been given as a response. This makes it possible for the chat bot to determine if a particular response is more commonly used than another.

Comparisons

Statement comparison

ChatterBot uses Statement objects to hold information about things that can be said. An important part of how a chat bot selects a response is based on its ability to compare two statements to each other. There are a number of ways to do this, and ChatterBot comes with a handful of methods built in for you to use.

Use your own comparison function

You can create your own comparison function and use it as long as the function takes two statements as parameters and returns a numeric value between 0 and 1. A 0 should represent the lowest possible similarity and a 1 should represent the highest possible similarity.

def comparison_function(statement, other_statement):

    # Your comparison logic

    # Return your calculated value here
    return 0.0
Setting the comparison method

To set the statement comparison method for your chat bot, you will need to pass the statement_comparison_function parameter to your chat bot when you initialize it. An example of this is shown below.

from chatterbot import ChatBot
from chatterbot.comparisons import LevenshteinDistance

chatbot = ChatBot(
    # ...
    statement_comparison_function=LevenshteinDistance
)

Utility Methods

ChatterBot has a utility module that contains a collection of miscellaneous but useful functions.

Module imports

Class initialization

ChatBot response time

Parsing datetime information

ChatterBot Corpus

This is a corpus of dialog data that is included in the chatterbot module.

Additional information about the chatterbot-corpus module can be found in the ChatterBot Corpus Documentation.

Corpus language availability

Corpus data is user contributed, but it is also not difficult to create one if you are familiar with the language. This is because each corpus is just a sample of various input statements and their responses for the bot to train itself with.

To explore what languages and collections of corpora are available, check out the chatterbot_corpus/data directory in the separate chatterbot-corpus repository.

Note

If you are interested in contributing content to the corpus, please feel free to submit a pull request on ChatterBot’s corpus GitHub page. Contributions are welcomed!

https://github.com/gunthercox/chatterbot-corpus

The chatterbot-corpus is distributed in its own Python package so that it can be released and upgraded independently from the chatterbot package.

Exporting your chat bot’s database as a training corpus

Now that you have created your chat bot and sent it out into the world, perhaps you are looking for a way to share what it has learned with other chat bots? ChatterBot’s training module provides methods that allow you to export the content of your chat bot’s database as a training corpus that can be used to train other chat bots.

chatbot = ChatBot('Export Example Bot')
chatbot.trainer.export_for_training('./export.yml')

Here is an example:

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

'''
This is an example showing how to create an export file from
an existing chat bot that can then be used to train other bots.
'''

chatbot = ChatBot('Export Example Bot')

# First, lets train our bot with some data
trainer = ChatterBotCorpusTrainer(chatbot)

trainer.train('chatterbot.corpus.english')

# Now we can export the data to a file
trainer.export_for_training('./my_export.json')

Django Integration

ChatterBot has direct support for integration with Django’s ORM. It is relatively easy to use ChatterBot within your Django application to create conversational pages and endpoints.

Chatterbot Django Settings

You can edit the ChatterBot configuration through your Django settings.py file.

CHATTERBOT = {
    'name': 'Tech Support Bot',
    'logic_adapters': [
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter',
        'chatterbot.logic.BestMatch'
    ]
}

Any setting that gets set in the CHATTERBOT dictionary will be passed to the chat bot that powers your django app.

Additional Django settings

  • django_app_name [default: ‘django_chatterbot’] The Django app name to look up the models from.

ChatterBot Django Views

Exmple API Views

ChatterBot’s Django example comes with an API view that demonstrates one way to use ChatterBot to create an conversational API endpoint for your application.

The endpoint expects a JSON request in the following format:

{"text": "My input statement"}
class ChatterBotApiView(View):
    """
    Provide an API endpoint to interact with ChatterBot.
    """

    chatterbot = ChatBot(**settings.CHATTERBOT)

    def post(self, request, *args, **kwargs):
        """
        Return a response to the statement in the posted data.

        * The JSON data should contain a 'text' attribute.
        """
        input_data = json.loads(request.body.decode('utf-8'))

        if 'text' not in input_data:
            return JsonResponse({
                'text': [
                    'The attribute "text" is required.'
                ]
            }, status=400)

        response = self.chatterbot.get_response(input_data)

        response_data = response.serialize()

        return JsonResponse(response_data, status=200)

    def get(self, request, *args, **kwargs):
        """
        Return data corresponding to the current conversation.
        """
        return JsonResponse({
            'name': self.chatterbot.name
        })

Note

Looking for the full example? Check it out on GitHub: https://github.com/gunthercox/ChatterBot/tree/master/examples/django_app

Webservices

If you want to host your Django app, you need to choose a method through which it will be hosted. There are a few free services that you can use to do this such as Heroku and PythonAnyWhere.

WSGI

A common method for serving Python web applications involves using a Web Server Gateway Interface (WSGI) package.

Gunicorn is a great choice for a WSGI server. They have detailed documentation and installation instructions on their website.

Hosting static files

There are numerous ways to host static files for your Django application. One extreemly easy way to do this is by using WhiteNoise, a python package designed to make it possible to serve static files from just about any web application.

Install packages

Begin by making sure that you have installed both django and chatterbot.

pip install django chatterbot

For more details on installing Django, see the Django documentation.

Installed Apps

Add chatterbot.ext.django_chatterbot to your INSTALLED_APPS in the settings.py file of your Django project.

INSTALLED_APPS = (
    # ...
    'chatterbot.ext.django_chatterbot',
)

Migrations

You can run the Django database migrations for your chat bot with the following command.

python manage.py migrate django_chatterbot

MongoDB and Django

ChatterBot has a storage adapter for MongoDB but it does not work with Django. If you want to use MongoDB as your database for Django and your chat bot then you will need to install a Django storage backend such as Django MongoDB Engine.

The reason this is required is because Django’s storage backends are different and completely separate from ChatterBot’s storage adapters.

Frequently Asked Questions

This document is comprised of questions that are frequently asked about ChatterBot and chat bots in general.

Python String Encoding

The Python developer community has published a great article that covers the details of unicode character processing.

The following notes are intended to help answer some common questions and issues that developers frequently encounter while learning to properly work with different character encodings in Python.

Does ChatterBot handle non-ascii characters?

ChatterBot is able to handle unicode values correctly. You can pass to it non-encoded data and it should be able to process it properly (you will need to make sure that you decode the output that is returned).

Below is one of ChatterBot’s tests from tests/test_chatbot.py, this is just a simple check that a unicode response can be processed.

def test_get_response_unicode(self):
    """
    Test the case that a unicode string is passed in.
    """
    response = self.chatbot.get_response(u'سلام')
    self.assertGreater(len(response.text), 0)

This test passes Python 3. It also verifies that ChatterBot can take unicode input without issue.

How do I fix Python encoding errors?

When working with string type data in Python, it is possible to encounter errors such as the following.

UnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in position 48: invalid start byte

Depending on what your code looks like, there are a few things that you can do to prevent errors like this.

Unicode header
# -*- coding: utf-8 -*-
When to use the unicode header

If your strings use escaped unicode characters (they look like u'\u00b0C') then you do not need to add the header. If you use strings like 'ØÆÅ' then you are required to use the header.

If you are using this header it must be the first line in your Python file.

Unicode escape characters
>>> print u'\u0420\u043e\u0441\u0441\u0438\u044f'
Россия
When to use escape characters

Prefix your strings with the unicode escape character u'...' when you are using escaped unicode characters.

Import unicode literals from future
from __future__ import unicode_literals
When to import unicode literals

Use this when you need to make sure that Python 3 code also works in Python 2.

A good article on this can be found here: http://python-future.org/unicode_literals.html

How do I deploy my chat bot to the web?

There are a number of excellent web frameworks for creating Python projects out there. Django and Flask are two excellent examples of these. ChatterBot is designed to be agnostic to the platform it is deployed on and it is very easy to get set up.

To run ChatterBot inside of a web application you just need a way for your application to receive incoming data and to return data. You can do this any way you want, HTTP requests, web sockets, etc.

There are a number of existing examples that show how to do this.

  1. An example using Django: https://github.com/gunthercox/ChatterBot/tree/master/examples/django_app
  2. An example using Flask: https://github.com/chamkank/flask-chatterbot/blob/master/app.py

Additional details and recommendations for configuring Django can be found in the Webservices section of ChatterBot’s Django documentation.

What kinds of machine learning does ChatterBot use?

In brief, ChatterBot uses a number of different machine learning techniques to generate its responses. The specific algorithms depend on how the chat bot is used and the settings that it is configured with.

Here is a general overview of some of the various machine learning techniques that are employed throughout ChatterBot’s codebase.

1. Search algorithms

Searching is the most rudimentary form of artificial intelligence. To be fair, there are differences between machine learning and artificial intelligence but lets avoid those for now and instead focus on the topic of algorithms that make the chat bot talk intelligently.

Search is a crucial part of how a chat bot quickly and efficiently retrieves the possible candidate statements that it can respond with.

Some examples of attributes that help the chat bot select a response include

  • the similarity of an input statement to known statements
  • the frequency in which similar known responses occur
  • the likeliness of an input statement to fit into a category that known statements are a part of

2. Classification algorithms

Several logic adapters in ChatterBot use naive Bayesian classification algorithms to determine if an input statement meets a particular set of criteria that warrant a response to be generated from that logic adapter.

Command line tools

ChatterBot comes with a few command line tools that can help

Get the installed ChatterBot version

If have ChatterBot installed and you want to check what version you have then you can run the following command.

python -m chatterbot --version

Development

As the code for ChatterBot is written, the developers attempt to describe the logic and reasoning for the various decisions that go into creating the internal structure of the software. This internal documentation is intended for future developers and maintainers of the project. A majority of this information is unnecessary for the typical developer using ChatterBot.

It is not always possible for every idea to be documented. As a result, the need may arise to question the developers and maintainers of this project in order to pull concepts from their minds and place them in these documents. Please pull gently.

Contributing to ChatterBot

There are numerous ways to contriubte to ChatterBot. All of which are highly encouraged.

  • Contributing bug reports and feature requests
  • Contributing documentation
  • Contributing code for new features
  • Contributing fixes for bugs

Every bit of help received on this project is highly appreciated.

Setting Up a Development Environment

To contribute to ChatterBot’s development, you simply need:

  • Python
  • pip
  • A few python packages:
pip install requirements.txt
pip install dev-requirements.txt
  • A text editor

Reporting a Bug

If you discover a bug in ChatterBot and wish to report it, please be sure that you adhere to the following when you report it on GitHub.

  1. Before creating a new bug report, please search to see if an open or closed report matching yours already exists.
  2. Please include a description that will allow others to recreate the problem you encountered.

Requesting New Features

When requesting a new feature in ChatterBot, please make sure to include the following details in your request.

  1. Your use case. Describe what you are doing that requires this new functionality.

Contributing Documentation

ChatterBot’s documentation is written in reStructuredText and is compiled by Sphinx. The reStructuredText source of the documentation is located in docs/.

To build the documentation yourself, run:

sphinx-build ./docs/ ./build/

You can then open the index.html file that will be created in the build directory.

Contributing Code

The development of ChatterBot happens on GitHub. Code contributions should be submitted there in the form of pull requests.

Pull requests should meet the following criteria.

  1. Fix one issue and fix it well.
  2. Do not include extraneous changes that do not relate to the issue being fixed.
  3. Include a descriptive title and description for the pull request.
  4. Have descriptive commit messages.

Releasing ChatterBot

ChatterBot follows the following rules when it comes to new versions and updates.

Versioning

ChatterBot follows semantic versioning as a set of guidelines for release versions.

  • Major releases (2.0.0, 3.0.0, etc.) are used for large, almost entirely backwards incompatible changes.
  • Minor releases (2.1.0, 2.2.0, 3.1.0, 3.2.0, etc.) are used for releases that contain small, backwards incompatible changes. Known backwards incompatibilities will be described in the release notes.
  • Patch releases (e.g., 2.1.1, 2.1.2, 3.0.1, 3.0.10, etc.) are used for releases that contain bug fixes, features and dependency changes.

Release Process

The following procedure is used to finalize a new version of ChatterBot.

  1. We make sure that all CI tests on the master branch are passing.
  2. We tag the release on GitHub.
  3. A new package is generated from the latest version of the master branch.
python setup.py sdist bdist_wheel
  1. The Python package files are uploaded to PyPi.
twine upload dist/*

Unit Testing

“A true professional does not waste the time and money of other people by handing over software that is not reasonably free of obvious bugs; that has not undergone minimal unit testing; that does not meet the specifications and requirements; that is gold-plated with unnecessary features; or that looks like junk.” – Daniel Read

ChatterBot tests

ChatterBot’s built in tests can be run using nose. See the nose documentation for more information.

nosetests

Note that nose also allows you to specify individual test cases to run. For example, the following command will run all tests in the test-module tests/logic_adapter_tests

nosetests tests/logic_adapter_tests

Django integration tests

Tests for Django integration have been included in the tests_django directory and can be run with:

python runtests.py

Django example app tests

Tests for the example Django app can be run with the following command from within the examples/django_app directory.

python manage.py test

Benchmark tests

You can run a series of benchmark tests that test a variety of different chat bot configurations for performance by running the following command.

python tests/benchmarks.py

Running all the tests

You can run all of ChatterBot’s tests with a single command: tox.

Tox is a tool for managing virtual environments and running tests.

Installing tox

You can install tox with pip.

pip install tox
Using tox

When you run the tox command from within the root directory of the ChatterBot repository it will run the following tests:

  1. Tests for ChatterBot’s core files.
  2. Tests for ChatterBot’s integration with multiple versions of Django.
  3. Tests for each of ChatterBot’s example files.
  4. Tests to make sure ChatterBot’s documentation builds.
  5. Code style and validation checks (linting).
  6. Benchmarking tests for performance.

You can run specific tox environments using the -e flag. A few examples include:

# Run the documentation tests
tox -e docs
# Run the tests with Django 2.0
tox -e django20
# Run the code linting scripts
tox -e lint

To see the list of all available environments that you can run tests for:

tox -l

To run tests for all environments:

tox

Packaging your code for ChatterBot

There are cases where developers may want to contribute code to ChatterBot but for various reasons it doesn’t make sense or isn’t possible to add the code to the main ChatterBot repository on GitHub.

Common reasons that code can’t be contributed include:

  • Licencing: It may not be possible to contribute code to ChatterBot due to a licencing restriction or a copyright.
  • Demand: There needs to be a general demand from the open source community for a particular feature so that there are developers who will want to fix and improve the feature if it requires maintenance.

In addition, all code should be well documented and thoroughly tested.

Package directory structure

Suppose we want to create a new logic adapter for ChatterBot and add it the Python Package Index (PyPI) so that other developers can install it and use it. We would begin doing this by setting up a directory file the following structure.

Python Module Structure
IronyAdapter/
|-- README
|-- setup.py
|-- irony_adapter
|   |-- __init__.py
|   |-- logic.py
|-- tests
|-- |-- __init__.py
|-- |-- test_logic.py

More information on creating Python packages can be found here: https://packaging.python.org/tutorials/distributing-packages/

Register on PyPI

Create an account: https://pypi.python.org/pypi?%3Aaction=register_form

Create a .pypirc configuration file.

.pypirc file contents
[distutils]
index-servers =
pypi

[pypi]
username=my_username
password=my_password
Generate packages
python setup.py sdist bdist_wheel
Upload packages

The official tool for uploading Python packages is called twine. You can install twine with pip if you don’t already have it installed.

pip install twine
twine upload dist/*
Install your package locally
cd IronyAdapter
pip install . --upgrade
Using your package

If you are creating a module that ChatterBot imports from a dotted module path then you can set the following in your chat bot.

chatbot = ChatBot(
    "My ChatBot",
    logic_adapters=[
        "irony_adapter.logic.IronyAdapter"
    ]
)
Testing your code
from unittest import TestCase


class IronyAdapterTestCase(TestCase):
    """
    Test that the irony adapter allows
    the chat bot to understand irony.
    """

    def test_irony(self):
       # TODO: Implement test logic
       self.assertTrue(response.irony)

Suggested Development Tools

To help developers work with ChatterBot and projects built using it, the following tools are suggested. Keep in mind that none of these are required, but this list has been assembled because it is often useful to have a tool or technology recommended by people who have experience using it.

Text Editors

Visual Studio Code

Website: https://code.visualstudio.com/

I find Visual Studio Code to be an optimally light-weight
and versatile editor.

~ Gunther Cox

Database Clients

Sqlectron
Sadly this cross-platform database client is no longer being maintained
by it’s creator. Regardless, I still use it frequently and I find its
interface to be highly convenient and user friendly.

~ Gunther Cox

Website: https://sqlectron.github.io/

Nosqlclient
This is a very versatile client for Mongo DB, and other non-relational databases.

~ Gunther Cox

Website: https://www.nosqlclient.com/

Glossary

adapters
A pluggable class that allows a ChatBot instance to execute some kind of functionality.
logic adapter
An adapter class that allows a ChatBot instance to select a response to
storage adapter
A class that allows a chat bot to store information somewhere, such as a database.
corpus
In linguistics, a corpus (plural corpora) or text corpus is a large and structured set of texts. They are used to do statistical analysis and hypothesis testing, checking occurrences or validating linguistic rules within a specific language territory [1].
preprocessors
A member of a list of functions that can be used to modify text input that the chat bot receives before the text is passed to the logic adapter for processing.
statement
A single string of text representing something that can be said.
search word
A word that is not a stop word and has been trimmed in some way ( for example through stemming).
stemming
A process through which a word is reduced into a derivative form.
stop word
A common word that is often filtered out during the process of analyzing text.
response
A single string of text that is uttered as an answer, a reply or an acknowledgement to a statement.
untrained instance
An untrained instance of the chat bot has an empty database.
[1]https://en.wikipedia.org/wiki/Text_corpus

Report an Issue

Please direct all bug reports and feature requests to the project’s issue tracker on GitHub.

Indices and tables