Issue
I am using the chatterbot Python library to create a Flask chatbot. The chatbot is actually working fine but If I send a message like say for example some random jibberish It either ignores It or asks me another question or doesn't give an answer.
How do I make It so that the chatbot replies "Sorry I don't understand that" If the user sends a message that It does not understand?
I am using the chatterbot corpus which is built in inside the chatterbot library and Here's my code so far:
from flask import Flask, render_template, request
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
app = Flask(__name__)
chatbot = ChatBot("chatbot_name")
bot = ChatBot(
'chatbot_name',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///database.sqlite3'
)
bot = ChatBot(
'chatbot_name',
logic_adapters=[
'chatterbot.logic.BestMatch',
'chatterbot.logic.TimeLogicAdapter'],
)
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train(
"chatterbot.corpus.english"
)
@app.route('/')
def home():
return render_template('bot1.html')
@app.route('/get')
def get_bot_response():
userText = request.args.get('msg')
return str(chatbot.get_response(userText))
if __name__ == '__main__':
app.run()
Any help would be much appreciated.
Solution
I managed to find a solution. After looking at the source code of the chatterbot library I found out that each reply that the chatterbot gives has Its own confidence
that is a Float
value between 0 and 1.
If the confidence
equals 0, then the chatbot did not understand It so the response should be "Sorry I do not understand that"
and If the confidence
equals 1, then the chatbot should return the response. So this is what I came up with.
def get_bot_response():
userText = request.args.get('msg')
if chatbot.get_response(userText).confidence < 0.5:
return str("Sorry I do not understand that.")
else:
chatbot.get_response(userText).confidence = 1
return str(chatbot.get_response(userText))
and It seems to work pretty well now.
Answered By - Syed Mohammad Sannan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.