ChatterBot
是一个基于机器学习的对话对话引擎,使用 Python
构建,可以根据已知对话的集合生成响应。ChatterBot
的语言独立设计允许它被训练说任何语言。
典型输入的示例如下:
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