Login With Github

ChatterBot Filters

Filters are an efficient way to create base 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.

How to create a new filter for ChatterBot

This is the basic outline of the code that your filter will need to follow. Each filter should inherit from ChatterBot's Filter class and implement a method called filter_selection. Everything else that your filter does is up to you.

from chatterbot.filters import Filter

class MyFilter(Filter):

    def filter_selection(self, chatterbot):
        # ...
        return query

Filter Queries

Filters use a storage adapter's query object to build a query that the adapter can evaluate. The available query methods currently are:

statement_text_equals(statement_text)

This query method returns the current query with the added constraint that the text attribute of any statement returned must be equal to the text specified in the parameter.

statement_text_not_in(statements)

This query method takes a list of statement text values and returns the current query with the added constraint that any statements returned cannot exist in the list specified.

statement_response_list_contains(statement_text)

This query method takes a single statement text value and returns the current query with the added constraint that any statements returned must contain the specified text as a response.

statement_response_list_equals(response_list)

This query method takes a list of statement text values and returns the current query with the added constraint that any statements returned must have an exactly matching list of response values.

Filter Support

Not all storage adapters support filters. If a storage adapter does not support filters, then queries generated by filters will be ignored when using that storage adapter.

A storage adapter only supports filters if it supports querying. You can tell if a storage adapter supports querying by checking if its adapter_supports_queries property is set to true.

Setting filters

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

Filter classes

A filter that eliminates possibly repetitive responses to prevent a chat bot from repeating statements that it has recently said.

"""
Filters set the base query that gets passed to the storage adapter.
"""


class Filter(object):
    """
    A base filter object from which all other
    filters should be subclassed.
    """

    def filter_selection(self, chatterbot, conversation_id):
        """
        Because this is the base filter class, this method just
        returns the storage adapter's base query. Other filters
        are expected to override this method.
        """
        return chatterbot.storage.base_query


class RepetitiveResponseFilter(Filter):
    """
    A filter that eliminates possibly repetitive responses to prevent
    a chat bot from repeating statements that it has recently said.
    """

    def filter_selection(self, chatterbot, conversation_id):

        text_of_recent_responses = []

        # TODO: Add a larger quantity of response history
        latest_response = chatterbot.storage.get_latest_response(conversation_id)
        if latest_response:
            text_of_recent_responses.append(latest_response.text)

        # Return the query with no changes if there are no statements to exclude
        if not text_of_recent_responses:
            return super(RepetitiveResponseFilter, self).filter_selection(
                chatterbot,
                conversation_id
            )

        query = chatterbot.storage.base_query.statement_text_not_in(
            text_of_recent_responses
        )

        return query

filter_selection(chatterbotconversation_id)

Because this is the base filter class, this method just returns the storage adapter's base query. Other filters are expected to override this method.

0 Comment

temp