首页 > 解决方案 > 无法从 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)

标签: pythonmongodbchatterbot

解决方案


推荐阅读