Github每日精选(第18期):聊天机器人ChatterBot

ChatterBot 是一个基于机器学习的对话对话引擎,使用 Python 构建,可以根据已知对话的集合生成响应。ChatterBot 的语言独立设计允许它被训练说任何语言。

在这里插入图片描述

github的地址在这里

典型输入的示例如下:

user: Good morning! How are you doing?
bot: I am doing very well, thank you for asking.
user: You're welcome.
bot: Do you like hats?
原理

一个未经训练的 ChatterBot 实例一开始并不知道如何沟通。每次用户输入语句时,库都会保存他们输入的文本和语句响应的文本。随着 ChatterBot 收到更多输入,它可以回复的响应数量以及与输入语句相关的每个响应的准确性都会增加。该程序通过搜索与输入匹配的最接近匹配的已知语句来选择最接近的匹配响应,然后根据与机器人通信的人发出每个响应的频率,返回对该语句的最可能响应。

安装
pip install chatterbot
用法

用法非常的简单,初始化以后,可以问机器不同的问题,或者跟他进行简单的聊天。

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('Ron Obvious')

# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)

# Train the chatbot based on the english corpus
trainer.train("chatterbot.corpus.english")

# Get a response to an input statement
chatbot.get_response("Hello, how are you today?")
训练数据

ChatterBot 带有一个数据实用程序模块,可用于训练聊天机器人。目前,该模块中有十几种语言的训练数据。非常感谢您提供其他培训数据或其他语言的培训数据。 如果您有兴趣贡献,请查看chatterbot-corpus包中的数据文件。

from chatterbot.trainers import ChatterBotCorpusTrainer

# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)

# Train based on the english corpus
trainer.train("chatterbot.corpus.english")

# Train based on english greetings corpus
trainer.train("chatterbot.corpus.english.greetings")

# Train based on the english conversations corpus
trainer.train("chatterbot.corpus.english.conversations")
逻辑适配器

逻辑适配器确定 ChatterBot 如何选择对给定输入语句的响应的逻辑。

您的机器人使用的逻辑适配器可以通过将logic_adapters参数设置为您要使用的逻辑适配器的导入路径来指定。

chatbot = ChatBot(
    "My ChatterBot",
    logic_adapters=[
        "chatterbot.logic.BestMatch"
    ]
)

可以输入任意数量的逻辑适配器供您的机器人使用。如果使用多个适配器,则机器人将返回具有最高计算置信度值的响应。如果多个适配器返回相同的置信度,则首先进入列表的适配器将具有优先权。

在这里插入图片描述

例子

对于聊天机器人也是需要一个功能一个功能的进行训练,慢慢的学会做各种事情,对于每一部分的功能可以看成独立的模块,但是这个模块又有相同的地方,这里的一个例子是让机器人学会数学运算。

from chatterbot import ChatBot


bot = ChatBot(
    'Math & Time Bot',
    logic_adapters=[
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter'
    ]
)

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

在聊天机器人中有一个比较有意思的是上下文的对话回答,机器人会根据上文的内容来作为提供下一个答案的前提背景或者是关键点的补充,这里是训练机器人,让他学会上下文的回答:

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer


'''
This is an example showing how to train a chat bot using the
ChatterBot ListTrainer.
'''

chatbot = ChatBot('Example Bot')

# Start by training our bot with the ChatterBot corpus data
trainer = ListTrainer(chatbot)

trainer.train([
    'Hello, how are you?',
    'I am doing well.',
    'That is good to hear.',
    'Thank you'
])

# You can train with a second list of data to add response variations

trainer.train([
    'Hello, how are you?',
    'I am great.',
    'That is awesome.',
    'Thanks'
])

# Now let's get a response to a greeting
response = chatbot.get_response('How are you doing today?')
print(response)

这个例子展示了如何创建一个聊天机器人,它能够将根据用户的反馈,进行学习并响应输出。

from chatterbot import ChatBot
from chatterbot.conversation import Statement

"""
This example shows how to create a chat bot that
will learn responses based on an additional feedback
element from the user.
"""

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

# Create a new instance of a ChatBot
bot = ChatBot(
    'Feedback Learning Bot',
    storage_adapter='chatterbot.storage.SQLStorageAdapter'
)


def get_feedback():

    text = input()

    if 'yes' in text.lower():
        return True
    elif 'no' in text.lower():
        return False
    else:
        print('Please type either "Yes" or "No"')
        return get_feedback()


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

# The following loop will execute each time the user enters input
while True:
    try:
        input_statement = Statement(text=input())
        response = bot.generate_response(
            input_statement
        )

        print('\n Is "{}" a coherent response to "{}"? \n'.format(
            response.text,
            input_statement.text
        ))
        if get_feedback() is False:
            print('please input the correct one')
            correct_response = Statement(text=input())
            bot.learn_response(correct_response, input_statement)
            print('Responses added to bot!')

    # Press ctrl-c or ctrl-d on the keyboard to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

go2coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值