chatterbot是一款python接口的,基于一系列规则和机器学习算法完成的聊天机器人。具有结构清晰,可扩展性好,简单实用的特点。本文通过chatterbot 的不同adapter来介绍如何构建自己的聊天机器人,关与chatterbot详细资料请请阅读源码,纯Python写的,阅读性比较强。好啦,我就直接上代码了。PS:现在正在收集语料库,过段时间更新基于深度循环网络LSTM的带有记忆的ChatBot。
安装
是的,安装超级简单(Ubuntu),用pip就可以啦~
sudo pip install chatterbot
各式各样的Adapter
大家已经知道chatterbot的聊天逻辑和输入输出以及存储,是由各种adapter来限定的,我们先看看流程图,一会软再一起看点例子,看看怎么用。
基础版本
-
- from chatterbot import ChatBot
-
- Adapterbot = ChatBot(
- 'Default Response Example Bot',
- storage_adapter='chatterbot.storage.JsonFileStorageAdapter',
- logic_adapters=[
- {
- 'import_path': 'chatterbot.logic.BestMatch'
- },
- {
- 'import_path': 'chatterbot.logic.LowConfidenceAdapter',
- 'threshold': 0.65,
- 'default_response': 'I am sorry, but I do not understand.'
- }
- ],
- trainer='chatterbot.trainers.ListTrainer')
-
- bot.train([
- 'How can I help you?',
- 'I want to create a chat bot',
- 'Have you read the documentation?',
- 'No, I have not',
- 'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html'])
-
- question = 'How do I make an omelette?'
- print(question)
- response = bot.get_response(question)
- print(response)
- print("\n")
- question = 'how to make a chat bot?'
- print(question)
- response = bot.get_response(question)
- print(response)
对话内容如下:
How do I make an omelette?
I am sorry, but I do not understand.
how to make a chat bot?
Have you read the documentation?
处理时间和数学计算的Adapter
-
- 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")
-
- question = "What is 4 + 9?"
- print(question)response = bot.get_response(question)
- print(response)
- print("\n")
-
- question = "What time is it?"
- print(question)
- response = bot.get_response(question)
- print(response)
对话如下:
What is 4 + 9?
( 4 + 9 ) = 13
What time is it?
The current time is 05:08 PM
导出语料到json文件
-
- from chatterbot import ChatBot
- ''
- chatbot = ChatBot(
- 'Export Example Bot',
- trainer='chatterbot.trainers.ChatterBotCorpusTrainer')
-
- chatbot.train('chatterbot.corpus.english')
-
反馈式学习聊天机器人
-
- from chatterbot import ChatBot
- import logging
-
-
-
-
- bot = ChatBot(
- 'Feedback Learning Bot',
- storage_adapter='chatterbot.storage.JsonFileStorageAdapter',
- logic_adapters=[
- 'chatterbot.logic.BestMatch'
- ],
- input_adapter='chatterbot.input.TerminalAdapter',
- output_adapter='chatterbot.output.TerminalAdapter')
- DEFAULT_SESSION_ID = bot.default_session.id
- def get_feedback():
- from chatterbot.utils import input_function
- text = input_function()
- if 'Yes' in text:
- return True
- elif 'No' in text:
- return False
- else:
- print('Please type either "Yes" or "No"')
- return get_feedback()
- print('Type something to begin...')
-
- while True:
- try:
- input_statement = bot.input.process_input_statement()
- statement, response = bot.generate_response(input_statement, DEFAULT_SESSION_ID)
- print('\n Is "{}" this a coherent response to "{}"? \n'.format(response, input_statement))
- if get_feedback():
- bot.learn_response(response,input_statement)
- bot.output.process_response(response)
-
- bot.conversation_sessions.update(
- bot.default_session.id_string,
- (statement, response, )
- )
-
- except (KeyboardInterrupt, EOFError, SystemExit):
- break
- 反馈式学习聊天机器人
-
- 使用Ubuntu数据集构建聊天机器人
-
- from chatterbot import ChatBot
- import logging
- ''
-
- chatbot = ChatBot(
- 'Example Bot', trainer='chatterbot.trainers.UbuntuCorpusTrainer')
-
- chatbot.train()
-
- response = chatbot.get_response('How are you doing today?')
- print(response)
借助微软的聊天机器人
-
- from chatterbot import ChatBot
- from settings import Microsoft
- ''
- chatbot = ChatBot(
- 'MicrosoftBot',
- directline_host = Microsoft['directline_host'],
- direct_line_token_or_secret = Microsoft['direct_line_token_or_secret'],
- conversation_id = Microsoft['conversation_id'],
- input_adapter='chatterbot.input.Microsoft',
- output_adapter='chatterbot.output.Microsoft',
- trainer='chatterbot.trainers.ChatterBotCorpusTrainer')chatbot.train('chatterbot.corpus.english')
-
- while True:
- try:
- response = chatbot.get_response(None)
-
-
- except (KeyboardInterrupt, EOFError, SystemExit):
- break