python - 无法从 chatterbot 中的特定 mongo db 读取问题总是写入 chatterbot_database
问题描述
我用烧瓶写了一个聊天机器人集成代码,它运行良好!现在,每当我尝试训练聊天机器人时,它都不会将数据转储到指定的数据库中,如果我尝试提出问题,它会读取和写入 chatterbot_database ,甚至认为我在代码中指定了我希望他使用另一个数据库,我分享我的代码,请帮助解释为什么会这样?
#! /usr/bin/python3
from flask import Flask, render_template, request
from chatterbot import ChatBot
from chatterbot.response_selection import get_random_response
import random
import csv
import os
from botConfig import myBotName, chatBG, botAvatar, useGoogle, confidenceLevel
##Experimental Date Time
from dateTime import getTime, getDate
import logging
logging.basicConfig(level=logging.INFO)
application = Flask(__name__)
chatbotName = myBotName
print("Bot Name set to: " + chatbotName)
print("Background is " + chatBG)
print("Avatar is " + botAvatar)
print("Confidence level set to " + str(confidenceLevel))
#Create Log file
try:
file = open('BotLog.csv', 'r')
except IOError:
file = open('BotLog.csv', 'w')
bot = ChatBot(
"ChatBot",
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch'
},
{
'import_path': 'chatterbot.logic.LowConfidenceAdapter',
'threshold': confidenceLevel,
'default_response': 'IDKresponse'
}
],
response_selection_method=get_random_response,
#storage_adapter="chatterbot.storage.MongoDatabaseAdapter",
#database='ora_database',
#database_uri='127.0.0.1:27017'
database_uri='mongodb://localhost:27017/ora_database'
)
bot.read_only=True #Comment this out if you want the bot to learn based on experience
print("Bot Learn Read Only:" + str(bot.read_only))
#You can comment these out for production later since you won't be training everytime:
#bot.set_trainer(ChatterBotCorpusTrainer)
#bot.train("data/trainingdata.yml")
def tryGoogle(myQuery):
#print("<br>Try this from my friend Google: <a target='_blank' href='" + j + "'>" + query + "</a>")
return "<br><br>please use this link for more info <a target='_blank' href='http://www.google.com/'>" + myQuery + "</a>"
@application.route("/")
def home():
return render_template("index.html", botName = chatbotName, chatBG = chatBG, botAvatar = botAvatar)
@application.route("/get")
def get_bot_response():
userText = request.args.get('msg')
botReply = str(bot.get_response(userText))
if botReply is "IDKresponse":
botReply = str(bot.get_response('IDKnull')) ##Send the i don't know code back to the DB
if useGoogle == "yes":
botReply = botReply + tryGoogle(userText)
elif botReply == "getTIME":
botReply = getTime()
print(getTime())
elif botReply == "getDATE":
botReply = getDate()
print(getDate())
##Log to CSV file
print("Logging to CSV file now")
with open('BotLog.csv', 'a', newline='') as logFile:
newFileWriter = csv.writer(logFile)
newFileWriter.writerow([userText, botReply])
logFile.close()
return botReply
if __name__ == "__main__":
#application.run()
application.run(host='127.0.0.1', port=5000)
解决方案
推荐阅读
- amazon-web-services - 限制 cognito 用户组的用户访问 API 网关端点
- java - 警告 org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Bootstrap broker 127.0.0.1:9092 (id: -1 rack: null) 已断开连接
- java - 如何将与布尔变量匹配的 int 变量和字符串变量放入 .equals() 中?
- scala - 当类型类中有类型类时,使用类型类语法的正确方法是什么?
- java - 如何为具有时间戳值的实体类创建 Gson 对象?
- jenkins - 如何在 Jenkins 管道中使用动态数据构建并行和顺序阶段的组合
- java - 有效地将长格式 (yyyyMMddHHmmss) 的 DateTime 转换为另一个区域以进行比较
- zsh - iTerm2 在终端上显示一些数字
- html - 为什么带有供应商前缀的 flexbox、justify-content 在 Safari 中不起作用?
- reactjs - 优化应用程序的路由和 React 延迟加载,登陆页面上有 2 个部分