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.