[ ChatterBot聊天机器人 ] ChatterBot使用SQL/Mongo数据库及配合Gitter,停止学习等实例

Song • 88 次浏览 • 0 个回复 • 2018年04月11日

以下示例可帮助您开始使用ChatterBot,在下面的例子中,我们将学习ChatterBot使用SQL数据库,ChatterBot使用Mongo数据库,Gitterpython中的安装,ChatterBot配合Gitter聊天根据,ChatterBot停止继续学习即ChatterBot只读模式。

简单的例子

# -*- coding: utf-8 -*-
from chatterbot import ChatBot

# 创建一个名为Charlie的机器人
chatbot = ChatBot(
    'Charlie',
    trainer='chatterbot.trainers.ListTrainer'
)

# 训练数据
chatbot.train([
    "Hi, can I help you?",
    "Sure, I'd like to book a flight to Iceland.",
    "Your flight has been booked."
])

# Get a response to the input text 'How are you?'
# 获取'How are you?'的响应
response = chatbot.get_response('I would like to book a flight.')

print(response)

Terminal例子

本示例程序显示了如何创建一个简单的Terminal客户端,通过在终端中输入内容,您可以与您的聊天机器人进行通信。

# -*- coding: utf-8 -*-
from chatterbot import ChatBot

# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)

# Create a new instance of a ChatBot
bot = ChatBot(
    "Terminal",
    storage_adapter="chatterbot.storage.SQLStorageAdapter",
    logic_adapters=[
        "chatterbot.logic.MathematicalEvaluation",
        "chatterbot.logic.TimeLogicAdapter",
        "chatterbot.logic.BestMatch"
    ],
    input_adapter="chatterbot.input.TerminalAdapter",
    output_adapter="chatterbot.output.TerminalAdapter",
    database="../database.db"
)

print("Type something to begin...")

# The following loop will execute each time the user enters input
while True:
    try:
        # We pass None to this method because the parameter
        # is not used by the TerminalAdapter
        bot_input = bot.get_response(None)

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

使用MongoDB数据库

在您使用ChatterBotMongoDB内置适配器之前,您需要安装MongoDB。在执行程序之前,确保MongoDB正在您的环境中运行。设置storage_adapter参数指明ChatterBot使用MongoDB适配器。

storage_adapter="chatterbot.storage.MongoDatabaseAdapter"
# -*- coding: utf-8 -*-
from chatterbot import ChatBot

# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)

# Create a new ChatBot instance
bot = ChatBot(
    'Terminal',
    storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
    logic_adapters=[
        'chatterbot.logic.BestMatch'
    ],
    filters=[
        'chatterbot.filters.RepetitiveResponseFilter'
    ],
    input_adapter='chatterbot.input.TerminalAdapter',
    output_adapter='chatterbot.output.TerminalAdapter',
    database='chatterbot-database'
)

print('Type something to begin...')

while True:
    try:
        bot_input = bot.get_response(None)

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

获取时间和与进行数学计算

ChatterBot具有自然语言评估功能,获取时间和与进行数学计算。

# -*- coding: utf-8 -*-
from chatterbot import ChatBot

bot = ChatBot(
    "Math & Time Bot",
    logic_adapters=[
        "chatterbot.logic.MathematicalEvaluation",
        "chatterbot.logic.TimeLogicAdapter"
    ],
    input_adapter="chatterbot.input.VariableInputTypeAdapter",
    output_adapter="chatterbot.output.OutputAdapter"
)

# Print an example of getting one math based response
response = bot.get_response("What is 4 + 9?")
print(response)

# Print an example of getting one time based response
response = bot.get_response("What time is it?")
print(response)

Gitter示例

ChatterBot与聊天室很好地协作。Gitter是一个开源的聊天软件。 如果你没有安装Gitter,可以使用pip安装:

pip install gitter
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
from settings import GITTER

# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)

chatbot = ChatBot(
    'GitterBot',
    gitter_room=GITTER['ROOM'],
    gitter_api_token=GITTER['API_TOKEN'],
    gitter_only_respond_to_mentions=False,
    input_adapter='chatterbot.input.Gitter',
    output_adapter='chatterbot.output.Gitter',
    trainer='chatterbot.trainers.ChatterBotCorpusTrainer'
)

chatbot.train('chatterbot.corpus.english')

# The following loop will execute each time the user enters input
while True:
    try:
        response = chatbot.get_response(None)

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

使用SQL适配器

ChatterBot数据可以保存并从SQL数据库中检索。

# -*- coding: utf-8 -*-
from chatterbot import ChatBot

# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)

# Create a new instance of a ChatBot
bot = ChatBot(
    "SQLMemoryTerminal",
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    logic_adapters=[
        "chatterbot.logic.MathematicalEvaluation",
        "chatterbot.logic.TimeLogicAdapter",
        "chatterbot.logic.BestMatch"
    ],
    input_adapter="chatterbot.input.TerminalAdapter",
    output_adapter="chatterbot.output.TerminalAdapter",
)

print("Type something to begin...")

# The following loop will execute each time the user enters input
while True:
    try:
        # We pass None to this method because the parameter
        # is not used by the TerminalAdapter
        bot_input = bot.get_response(None)

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

只读模式

你的聊天机器人将根据它收到的每个新输入语句来学习。如果要在训练完bot后禁用学习功能,则可以在初始化bot时设置read_only = True参数。

chatbot = ChatBot("Johnny Five", read_only=True)

更多示例

更多示例可以在GitHub上的examples目录中找到:https://github.com/gunthercox/ChatterBot/tree/master/examples


原创文章,转载请注明 :[ ChatterBot聊天机器人 ] ChatterBot使用SQL/Mongo数据库及配合Gitter,停止学习等实例 - pytorch中文网
原文出处: https://ptorch.com/news/153.html
问题交流群 :168117787
提交评论
要回复文章请先登录注册
用户评论
  • 没有评论
Pytorch是什么?关于Pytorch! [ ChatterBot聊天机器人 ] ChatterBot训练数据以及使用三方语料库训练数据