简体   繁体   中英

How to return Empty Response in chatbot python-flask?

How can I get empty response when I search for some questions which are not available?

If the question is available it should return correct response.

from chatterbot import ChatBot

from chatterbot.trainers import ListTrainer
chatterbot = ChatBot("Training Example")
chatterbot.set_trainer(ListTrainer)

chatterbot.train([
    "Hi there!",
    "Hello",
])

chatterbot.train([
    "Greetings!",
    "Hello",
])

chatbot.train(["Greetings!","Hello",])

chatbot.train(["How are you?","I am good.","That is good to 
hear.","Thank you","You are welcome.",])

while True:

try:

  a = input("question please..? ")

  response = chatterbot.get_response(a)

  print(response)

except (KeyboardInterrupt,SystemExit):

  print("your loop has been closed: ")

  break
  • LowConfidenceAdapter can be used to return a default response if a response can not be determined with a high amount of confidence.
  • Set a threshold value as minimum confidence score of the response. If the confidence is less than this threshold value, it will return the default response.

Updated code using LowConfidenceAdapter :

from chatterbot import ChatBot

from chatterbot.trainers import ListTrainer
chatbot = ChatBot("Training Example",
                  logic_adapters=[
                    {
                        'import_path': 'chatterbot.logic.BestMatch'
                    },
                    {
                        'import_path': 'chatterbot.logic.LowConfidenceAdapter',
                        'threshold': 0.65,
                        'default_response': 'I am sorry, but I do not understand.'
                    }
                ])

chatbot.set_trainer(ListTrainer)

chatbot.train([
    "Hi there!",
    "Hello"])

chatbot.train([
    "Hello",
    "Hey!"])

chatbot.train([
    "How are you?",
    "I am good."])
chatbot.train([    
    "That is good to hear.",
    "Thank you",
    "You are welcome."])
chatbot.train([
    "Sure, I'd like to book a flight to Iceland.",
    "Your flight has been booked."])

while True:
    try:
        a = input("Question please..? ")
        response = chatbot.get_response(a)
        print(response)
    except (KeyboardInterrupt,SystemExit):
        print("\nYour loop has been closed.")
        break

Output:

chatterbot示例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM