Give a "Sorry I don't understand that" result from chatbot If the chatbot doesn't understand a user message


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(

bot = ChatBot(

trainer = ChatterBotCorpusTrainer(chatbot)



def home():
    return render_template('bot1.html')

def get_bot_response():
    userText = request.args.get('msg')
    return str(chatbot.get_response(userText))

if __name__ == '__main__':

Any help would be much appreciated.


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.")
        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

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published