必须有一种方法可以知道聊天机器人中使用了哪个逻辑适配器,或者是否没有使用它们。我能想到的最简单的方法是使用default_response
.
Set default_response = '-2E-'
或者是其他东西。接下来,添加 if else 条件以查看值是否str(bot.get_response(userText))
等于-2E-
。如果它们匹配,则意味着未使用任何逻辑适配器,并且未找到与用户输入的匹配项。
没有使用逻辑适配器意味着它是一个没有答案的输入。您现在可以附加存储在的用户输入userText
到一个文本文件。
Code:
## initialize chatter bot
bot = ChatBot(
'robot',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
preprocessors=[
'chatterbot.preprocessors.clean_whitespace',
],
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch',
'default_response': '-2E-',
'maximum_similarity_threshold': 0.90,
'statement_comparison_function': chatterbot.comparisons.levenshtein_distance,
'response_selection_method': chatterbot.response_selection.get_first_response
},
'chatterbot.logic.MathematicalEvaluation'
],
database_uri='sqlite:///database.db',
read_only=True
)
以下是在代码中使用的示例逻辑。您应该根据自己的要求修改此逻辑。
## Open a file to write unknown user inputs
with open("unanswered.txt", "a") as f:
## Loop and get user input
## Check to see if none of the logic adapters was used
if str(bot.get_response(userText)) == "-2E-":
f.write(userText)
return "Sorry, I do not understand."