一、前述
ChatterBot是一個基於機器學習的聊天機器人引擎,構建在python上,主要特點是可以自可以從已有的對話中進行學(jiyi)習(pipei)。
二、具體
1、安裝
是的,安裝超級簡單,用pip就可以啦
pip install chatterbot
2、流程
大家已經知道chatterbot的聊天邏輯和輸入輸出以及儲存,是由各種adapter來限定的,我們先看看流程圖,一會軟再一起看點例子,看看怎麼用。
3、每個部分都設計了不同的“介面卡”(Adapter)。
機器人應答邏輯 => Logic Adapters
Closest Match Adapter 字串模糊匹配(編輯距離)
Closest Meaning Adapter 藉助nltk的WordNet,近義詞評估
Time Logic Adapter 處理涉及時間的提問
Mathematical Evaluation Adapter 涉及數學運算
儲存器後端 => Storage Adapters
Read Only Mode 只讀模式,當有輸入資料到chatterbot的時候,數
據庫並不會發生改變
Json Database Adapter 用以儲存對話資料的介面,對話資料以Json格式
進行儲存。
Mongo Database Adapter 以MongoDB database方式來儲存對話資料
輸入形式 => Input Adapters
Variable input type adapter 允許chatter bot接收不同型別的輸入的,如strings,dictionaries和Statements
Terminal adapter 使得ChatterBot可以通過終端進行對話
HipChat Adapter 使得ChatterBot 可以從HipChat聊天室獲取輸入語句,通過HipChat 和 ChatterBot 進行對話
Speech recognition 語音識別輸入,詳見chatterbot-voice
輸出形式 => Output Adapters
Output format adapter支援text,json和object格式的輸出
Terminal adapter
HipChat Adapter
Mailgun adapter允許chat bot基於Mailgun API進行郵件的傳送
Speech synthesisTTS(Text to speech)部分,詳見chatterbot-voice
4、程式碼
基礎版本
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
# 構建ChatBot並指定Adapter
bot = ChatBot(
'Default Response Example Bot',
storage_adapter='chatterbot.storage.JsonFileStorageAdapter',#儲存的Adapter
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
# -*- 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"
)
# 進行數學計算
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檔案
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
'''
如果一個已經訓練好的chatbot,你想取出它的語料,用於別的chatbot構建,可以這麼做
'''
chatbot = ChatBot(
'Export Example Bot',
trainer='chatterbot.trainers.ChatterBotCorpusTrainer'
)
# 訓練一下咯
chatbot.train('chatterbot.corpus.english')
# 把語料匯出到json檔案中
chatbot.trainer.export_for_training('./my_export.json')
反饋式學習聊天機器人
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
import logging
"""
反饋式的聊天機器人,會根據你的反饋進行學習
"""
# 把下面這行前的註釋去掉,可以把一些資訊寫入日誌中
# logging.basicConfig(level=logging.INFO)
# 建立一個聊天機器人
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)
# 更新chatbot的歷史聊天資料
bot.conversation_sessions.update(
bot.default_session.id_string,
(statement, response, )
)
# 直到按ctrl-c 或者 ctrl-d 才會退出
except (KeyboardInterrupt, EOFError, SystemExit):
break
使用Ubuntu資料集構建聊天機器人
from chatterbot import ChatBot
import logging
'''
這是一個使用Ubuntu語料構建聊天機器人的例子
'''
# 允許打日誌
logging.basicConfig(level=logging.INFO)
chatbot = ChatBot(
'Example Bot',
trainer='chatterbot.trainers.UbuntuCorpusTrainer'
)
# 使用Ubuntu資料集開始訓練
chatbot.train()
# 我們來看看訓練後的機器人的應答
response = chatbot.get_response('How are you doing today?')
print(response)
藉助微軟的聊天機器人
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
from settings import Microsoft
'''
關於獲取微軟的user access token請參考以下的文件
https://docs.botframework.com/en-us/restapi/directline/
'''
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)
# 直到按ctrl-c 或者 ctrl-d 才會退出
except (KeyboardInterrupt, EOFError, SystemExit):
break
HipChat聊天室Adapter
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
from settings import HIPCHAT
'''
炫酷一點,你可以接到一個HipChat聊天室,你需要一個user token,下面文件會告訴你怎麼做
https://developer.atlassian.com/hipchat/guide/hipchat-rest-api/api-access-tokens
'''
chatbot = ChatBot(
'HipChatBot',
hipchat_host=HIPCHAT['HOST'],
hipchat_room=HIPCHAT['ROOM'],
hipchat_access_token=HIPCHAT['ACCESS_TOKEN'],
input_adapter='chatterbot.input.HipChat',
output_adapter='chatterbot.output.HipChat',
trainer='chatterbot.trainers.ChatterBotCorpusTrainer'
)
chatbot.train('chatterbot.corpus.english')
# 沒錯,while True,會一直聊下去!
while True:
try:
response = chatbot.get_response(None)
# 直到按ctrl-c 或者 ctrl-d 才會退出
except (KeyboardInterrupt, EOFError, SystemExit):
break
郵件回覆的聊天系統
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
from settings import MAILGUN
'''
這個功能需要你新建一個檔案settings.py,並在裡面寫入如下的配置:
MAILGUN = {
"CONSUMER_KEY": "my-mailgun-api-key",
"API_ENDPOINT": "https://api.mailgun.net/v3/my-domain.com/messages"
}
'''
# 下面這個部分可以改成你自己的郵箱
FROM_EMAIL = "mailgun@salvius.org"
RECIPIENTS = ["gunthercx@gmail.com"]
bot = ChatBot(
"Mailgun Example Bot",
mailgun_from_address=FROM_EMAIL,
mailgun_api_key=MAILGUN["CONSUMER_KEY"],
mailgun_api_endpoint=MAILGUN["API_ENDPOINT"],
mailgun_recipients=RECIPIENTS,
input_adapter="chatterbot.input.Mailgun",
output_adapter="chatterbot.output.Mailgun",
storage_adapter="chatterbot.storage.JsonFileStorageAdapter",
database="../database.db"
)
# 簡單的郵件回覆
response = bot.get_response("How are you?")
print("Check your inbox at ", RECIPIENTS)
一箇中文的例子
注意chatterbot,中文聊天機器人的場景下一定要用python3.X,用python2.7會有編碼問題。
#!/usr/bin/python
# -*- coding: utf-8 -*-
#手動設定一些語料
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
Chinese_bot = ChatBot("Training demo")
Chinese_bot.set_trainer(ListTrainer)
Chinese_bot.train([
'你好',
'你好',
'有什麼能幫你的?',
'想買資料科學的課程',
'具體是資料科學哪塊呢?'
'機器學習',
])
# 測試一下
question = '你好'
print(question)
response = Chinese_bot.get_response(question)
print(response)
print("\n")
question = '請問哪裡能買資料科學的課程'
print(question)
response = Chinese_bot.get_response(question)
print(response)
結果:
你好
你好
請問哪裡能買資料科學的課程
具體是資料科學哪塊呢?
利用已經提供好的小中文語料庫
#!/usr/bin/python
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
chatbot = ChatBot("ChineseChatBot")
chatbot.set_trainer(ChatterBotCorpusTrainer)
# 使用中文語料庫訓練它
chatbot.train("chatterbot.corpus.chinese")
# 開始對話
while True:
print(chatbot.get_response(input(">")))