Implements the question-answering chatbot

class ChatBot[source]

ChatBot(context_data_file=None, faq_data_file=None, configs_faq=None, download_models=True) :: DialogSystem

Inherits the Dialog System class and presents it as a conversation in a text interface.

The chatbot can be initialized by calling the method chatbot of an instance of the class.

Once the bot is initialized, it asks to introduce a question and returns a set of answers, After this, asks if a better answer will be provided, if yes, asks for the type of answer (context or FAQ), if not, starts again.

It can be exited by pressing Crtl+C keys.

ChatBot.are_answers_ok[source]

ChatBot.are_answers_ok()

Asks if a better answer will be provided

ChatBot.chatbot[source]

ChatBot.chatbot()

Chatbots logic:

Asks for a question -> Gives responses -> Asks if a better answer will be provided

If not starts again.

If yes -> asks if it is a context or a FAQ -> asks for the response and updates the data -> starts again.

The chatbot stops at any moment with the keys Ctrl+C

ChatBot.is_equal[source]

ChatBot.is_equal(string, letter='y')

Open I/O input until response is 'yes' or 'no'

DialogSystem.new_context[source]

DialogSystem.new_context(topic, context)

Adds a new context.

INPUT:

  • topic (The title of the context)
  • context

The new context is stored in the path self.data['context']['path']

ChatBot.new_context_interface[source]

ChatBot.new_context_interface()

Asks for a new context

DialogSystem.new_question_answer[source]

DialogSystem.new_question_answer(question, answer)

Adds a new question-answer pair.

INPUT:

  • question

  • answer

The new question-answer pair is stored in the path self.data['faq']['path'] and the models in qa_models['faq'] get re-trained by calling the function deeppavlaov.train_model

ChatBot.new_question_answer_interface[source]

ChatBot.new_question_answer_interface()

Asks for a new question-answer pair

DialogSystem.question_answer[source]

DialogSystem.question_answer(question)

Gets answers to a question.

INPUT:

  • question parameter

The method creates the following attributes:

  • 'self.question' -> the input parameter

  • 'self.responses' -> a dict of possible responses

  • 'self.formatted_responses' -> a formatted string of the possible responses

This method calls the functions settings.get_response and settings.format_responses

ChatBot.question_answer_interface[source]

ChatBot.question_answer_interface()

Gets 'self.formatted_responses' in 'print' function

# Test Example
#test
import tempfile
from os import path
with tempfile.TemporaryDirectory() as tmpdirname:

    cb = ChatBot(
        faq_data_file=path.join(tmpdirname,'faq_example.csv'),
        context_data_file=path.join(tmpdirname,'context_example.csv'),
        download_models=False
    )
    cb.chatbot()
[nltk_data] Downloading package punkt to /home/jovyan/nltk_data...
[nltk_data]   Package punkt is already up-to-date!
[nltk_data] Downloading package stopwords to /home/jovyan/nltk_data...
[nltk_data]   Package stopwords is already up-to-date!
[nltk_data] Downloading package perluniprops to
[nltk_data]     /home/jovyan/nltk_data...
[nltk_data]   Package perluniprops is already up-to-date!
[nltk_data] Downloading package nonbreaking_prefixes to
[nltk_data]     /home/jovyan/nltk_data...
[nltk_data]   Package nonbreaking_prefixes is already up-to-date!
/opt/conda/lib/python3.7/site-packages/sklearn/linear_model/logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
 Welcome to the Chatbot. To exit press Ctrl+C.


 See you!