Reputation: 63
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 unanswered questions) in a text file or database so that we can put their answers later. here is the code of chatterbot constructor
self.chatbot = ChatBot(
"GUI Bot",
storage_adapter="chatterbot.storage.SQLStorageAdapter",
logic_adapters=[{
'import_path': 'chatterbot.logic.BestMatch',
'default_response': 'I am sorry, but I do not understand.',
'maximum_similarity_threshold': 0.75
} ]
)
here is full code of class
class TkinterGUIExample(tk.Tk):
def __init__(self, *args, **kwargs):
"""
Create & set window variables.
"""
tk.Tk.__init__(self, *args, **kwargs)
self.chatbot = ChatBot(
"GUI Bot",
storage_adapter="chatterbot.storage.SQLStorageAdapter",
logic_adapters=[{
'import_path': 'chatterbot.logic.BestMatch',
'default_response': 'I am sorry, but I do not understand.',
'maximum_similarity_threshold': 0.75
} ]
)
for files in os.listdir('C:/Users/HP/Desktop/FYP BOT/training_data/'):
con=open('C:/Users/HP/Desktop/FYP BOT/training_data/'+files,'r').readlines()
trainer = ListTrainer(self.chatbot)
trainer.train(con)
self.title("Chatterbot")
self.initialize()
Upvotes: 2
Views: 876
Reputation: 7
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()
Upvotes: 0
Reputation: 4258
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 inputs
with open("unanswered.txt", "a") as f:
## Loop and get user input
## Check to see if none of the logic adapters was used
if str(bot.get_response(userText)) == "-2E-":
f.write(userText)
return "Sorry, I do not understand."
Upvotes: 1