The Chatbot
class is the core of the OrionBot library. It provides the functionality to train a bot with sample text data and to respond to user input with an appropriate response.
To use the Chatbot
class, you will need to import it from the orionbot
library:
from orionbot import Chatbot
Once you have imported the Chatbot
class, you can create a new instance of it like this:
bot = Chatbot(name='MyBot')
The Chatbot
class constructor takes the following parameters:
name
(optional): A name for the chatbot instance. This will be used to identify the chatbot in messages and logs. Default: 'Chatbot'
lemmatizer
(optional): An instance of the nltk.stem.WordNetLemmatizer
class to be used for word lemmatization during text preprocessing. Default: None
stopwords
(optional): A set of stop words to be removed during text preprocessing. If not provided, a default set of English stop words will be used from the nltk.corpus
package. Default: None
data_file
(optional): A path to a JSON file containing training data. If the file exists, its contents will be loaded into the training_data
attribute. Default: 'training_data_qbot.json'
similarity_threshold
(optional): A threshold for the cosine similarity between user input and training data for a response to be considered a match. Default: 0.5
no_response_message
(optional): The message to be returned when the chatbot is unable to generate a response. Default: "I'm sorry, I did not understand what you just said. What should I have said?"
The Chatbot
class has the following methods:
train(input_text: str, response_text: str)
Train the chatbot with the provided input text and response text.
input_text
: A string containing the input text.response_text
: A string containing the response text.get_response(input_text: str)
Get a response from the chatbot for the provided input text.
input_text
: A string containing the input text.Returns a string containing the response generated by the chatbot.
enable_training()
Enable training mode for the chatbot. It will prompt the user for feedback when the bot cannot find a suitable response for the message.
disable_training()
Disables training mode for the chatbot. It will send the “No Response” message, without prompting the user to provide an answer for the bot to remember.
_preprocess(text: str)
Preprocesses the provided text, cleans it up and makes it easier to understand for the bot
text
: A string containing the input text.Returns a string, cleaned up and optimized for the bot.
set_no_response_message(message: str)
Sets the message, sent by the bot when no suitable answer has been found for the user text.
message
A string containing the “No Response” message.train_from_file(file_path: str)
Traines the bot from an existing JSON dataset, provided by the user.
file_path
: A string containing the path for the JSON dataset the bot will be trained from.train_from_default_file()
Trains the bot from the default dataset, which can also be specified in the class instance.