1、前述html
ChatterBot是一個基於機器學習的聊天機器人引擎,構建在python上,主要特色是能夠自能夠從已有的對話中進行學(jiyi)習(pipei)。python
2、具體json
一、安裝後端
是的,安裝超級簡單,用pip就能夠啦api
pip install chatterbot
二、流程session
你們已經知道chatterbot的聊天邏輯和輸入輸出以及存儲,是由各類adapter來限定的,咱們先看看流程圖,一會軟再一塊兒看點例子,看看怎麼用。dom
三、每一個部分都設計了不一樣的「適配器」(Adapter)。python2.7
機器人應答邏輯 => Logic Adapters
Closest Match Adapter 字符串模糊匹配(編輯距離)機器學習
Closest Meaning Adapter 藉助nltk的WordNet,近義詞評估
Time Logic Adapter 處理涉及時間的提問
Mathematical Evaluation Adapter 涉及數學運算ide
存儲器後端 => 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
四、代碼
# -*- 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?
# -*- 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
# -*- 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(">")))