Utility Methods

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

Module imports

chatterbot.utils.import_module(dotted_path)[source]

Imports the specified module based on the dot notated import path for the module.

Class initialization

chatterbot.utils.initialize_class(data, **kwargs)[source]
Parameters:data – A string or dictionary containing a import_path attribute.

Terminal input

chatterbot.utils.input_function()[source]

Normalizes reading input between python 2 and 3. The function ‘raw_input’ becomes ‘input’ in Python 3.

NLTK corpus downloader

chatterbot.utils.nltk_download_corpus(resource_path)[source]

Download the specified NLTK corpus file unless it has already been downloaded.

Returns True if the corpus needed to be downloaded.

Stopword removal

chatterbot.utils.remove_stopwords(tokens, language)[source]

Takes a language (i.e. ‘english’), and a set of word tokens. Returns the tokenized text with any stopwords removed. Stop words are words like “is, the, a, ...”

Be sure to download the required NLTK corpus before calling this function: - from chatterbot.utils import nltk_download_corpus - nltk_download_corpus(‘corpora/stopwords’)

ChatBot response time

chatterbot.utils.get_response_time(chatbot)[source]

Returns the amount of time taken for a given chat bot to return a response.

Parameters:chatbot (ChatBot) – A chat bot instance.
Returns:The response time in seconds.
Return type:float

Random string generation

chatterbot.utils.generate_strings(total_strings, string_length=20)[source]

Generate a list of random strings.

Parameters:
  • total_strings (int) – The number of strings to generate.
  • string_length (int) – The length of each string to generate.
Returns:

The generated list of random strings.

Return type:

list

Parsing datetime information

chatterbot.parsing.datetime_parsing(text, base_date=datetime.datetime(2017, 9, 19, 7, 57, 22, 291469))[source]

Extract datetime objects from a string of text.