Py学习  »  Python

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

kiran • 4 年前 • 334 次点击  

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

如果问题是可用的,它应该返回正确的回答。

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
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43013
 
334 次点击  
文章 [ 1 ]  |  最新文章 4 年前
arsho
Reply   •   1 楼
arsho    5 年前
  • LowConfidenceAdapter 如果无法以高置信度确定响应,则可用于返回默认响应。
  • 设置一个 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