如何在chatbot python-flask中返回空响应?

时间:2018-11-23 05:36:58

标签: python chatterbot

当我搜索一些不可用的问题时,如何得到空的答复?

如果有问题,应该返回正确的答案。

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

1 个答案:

答案 0 :(得分:1)

    如果不能以很高的置信度确定响应,则可以使用
  • LowConfidenceAdapter返回默认响应。
  • 设置一个threshold值作为响应的最低置信度得分。如果置信度小于此threshold值,它将返回默认响应。

使用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

输出:

chatterbot example