Skip to content Skip to sidebar Skip to footer

How To Store Chatbot's Unanswered Questions In A Text File

i'am newbie to python and building a chatbot using chatterbot library and i want to store those questions which are asked by users which chatbot could not answers(i mean storing un

Solution 1:

There must a be way to know which logic adapter was used in chatterbot or, if none of them was used. The easiest way I can think around it is to use default_response.

Set default_response = '-2E-' or something else. Next, add an if else condition to see if the value of str(bot.get_response(userText)) is equal to -2E-. If they are a match that means none of the logic adapters was used and no match for user input was found.

No logic adapter used means it is an input for which there is no answer. You can now append the user input which is stored in userText to a text file.

Code:

## initialize chatter bot
bot = ChatBot(
    'robot',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    preprocessors=[
        'chatterbot.preprocessors.clean_whitespace',
    ],
    logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch',
            'default_response': '-2E-',
            'maximum_similarity_threshold': 0.90,
            'statement_comparison_function': chatterbot.comparisons.levenshtein_distance,
            'response_selection_method': chatterbot.response_selection.get_first_response
        },
        'chatterbot.logic.MathematicalEvaluation'
    ],
    database_uri='sqlite:///database.db',
    read_only=True
)

Below is sample logic for usage within code. You should modify this logic with you own requirements.

## Open a file to write unknown user inputswithopen("unanswered.txt", "a") as f:

    ## Loop and get user input## Check to see if none of the logic adapters was usedifstr(bot.get_response(userText)) == "-2E-":
        f.write(userText)
        return"Sorry, I do not understand."

Solution 2:

You can set a variable to open a txt file for writing and loop every line you want to add from a list with formatting like below:

file1 = open('your_txt_file.txt', 'w')
for questions in question_list:
    file1.write('{}\n'.format(questions))
file1.close()

Post a Comment for "How To Store Chatbot's Unanswered Questions In A Text File"