给出 "Sorry I don't understand that" 来自聊天机器人的结果 如果聊天机器人不理解用户消息

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

我正在使用 chatterbot Python 库来创建 Flask 聊天机器人。聊天机器人实际上工作正常,但如果我发送一条消息,比如说一些随机的乱码,它要么忽略它,要么问我另一个问题,要么不给出答案。

如果用户发送它不理解的消息,我该如何让聊天机器人回复“抱歉,我不明白”?

我正在使用内置在 chatterbot 库中的 chatterbot 语料库,这是我目前的代码:

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()

如有任何帮助,我们将不胜感激。

我设法找到了解决方案。在查看了 chatterbot 库的源代码后,我发现 chatterbot 给出的每个回复都有自己的 confidence,即介于 0 和 1 之间的 Float 值。

如果 confidence 等于 0,则聊天机器人不理解它,因此响应应为“抱歉,我不明白”

并且如果 confidence 等于 1,则聊天机器人应该 return 响应。所以这就是我想出的。

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

而且它现在似乎工作得很好。