?給公眾號集成一個智能聊天機器人
一州袒、前述
ChatterBot是一個基于機器學(xué)習(xí)的聊天機器人引擎,構(gòu)建在python上迷雪,主要特點是可以自可以從已有的對話中進行學(xué)(jiyi)習(xí)(pipei)。
二、具體
1剿干、安裝
是的,安裝超級簡單穆刻,用pip就可以啦
<pre style="margin: 0px; padding: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; white-space: pre-wrap; overflow-wrap: break-word; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-size: 14px; text-align: left; text-size-adjust: auto;">pip install chatterbot
</pre>
2置尔、流程
大家已經(jīng)知道chatterbot的聊天邏輯和輸入輸出以及存儲,是由各種adapter來限定的氢伟,我們先看看流程圖榜轿,一會再一起看點例子,看看怎么用朵锣。
3谬盐、每個部分都設(shè)計了不同的“適配器”(Adapter)。
機器人應(yīng)答邏輯 => Logic Adapters
Closest Match Adapter 字符串模糊匹配(編輯距離)
Closest Meaning Adapter ?借助nltk的WordNet诚些,近義詞評估
Time Logic Adapter 處理涉及時間的提問
Mathematical Evaluation Adapter?涉及數(shù)學(xué)運算
存儲器后端 => Storage Adapters
?Read Only Mode 只讀模式设褐,當(dāng)有輸入數(shù)據(jù)到chatterbot的時候,數(shù)
據(jù)庫并不會發(fā)生改變
?Json Database Adapter 用以存儲對話數(shù)據(jù)的接口,對話數(shù)據(jù)以Json格式
進行存儲助析。
Mongo Database Adapter ?以MongoDB database方式來存儲對話數(shù)據(jù)
輸入形式 => 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進行郵件的發(fā)送
Speech synthesisTTS(Text to speech)部分,詳見chatterbot-voice
4雪隧、代碼
計算模式
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"
)
# 進行數(shù)學(xué)計算
question = "What is 4 + 9?"
print(question)
response = bot.get_response(question)
print(response)
print("\n")
# 回答和時間相關(guān)的問題
question = "What time is it?"
print(question)
response = bot.get_response(question)
print(response)
利用已經(jīng)提供好的小中文語料庫
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
chatbot = ChatBot("ChineseChatBot")
trainer = ChatterBotCorpusTrainer(chatbot)
# 使用中文語料庫訓(xùn)練它
trainer.train("chatterbot.corpus.chinese")
def response_text(sentence):
res_text = chatbot.get_response(sentence)
print(sentence , "----", res_text)
return res_text
#
if __name__ == '__main__':
# 開始對話
while True:
print(chatbot.get_response(input(">")))
# print(response_text("你是誰"))
小黃雞語料更智能(推薦)
from chatterbot import ChatBot
bot = ChatBot('my-chat', database_uri='sqlite:///db.sqlite3')
def response_text(sentence):
temp = bot.get_response(sentence)
return temp.text
if __name__ == '__main__':
# bot_response = response_text("你幾歲了")
# print(bot_response)
# 開始對話
while True:
print(response_text(input(">")))
小黃雞語料數(shù)據(jù)庫:
鏈接:https://pan.baidu.com/s/1bgGlyH4RwiB1UDud1P1DQw 密碼:wzny