python微信集成_Python微信公众号后台开发005:集成智能聊天机器人
?給公眾號(hào)集成一個(gè)智能聊天機(jī)器人
一、前述
ChatterBot是一個(gè)基于機(jī)器學(xué)習(xí)的聊天機(jī)器人引擎,構(gòu)建在python上,主要特點(diǎn)是可以自可以從已有的對(duì)話中進(jìn)行學(xué)(jiyi)習(xí)(pipei)。
二、具體
1、安裝
是的,安裝超級(jí)簡(jiǎn)單,用pip就可以啦
pip install chatterbot2、流程
大家已經(jīng)知道chatterbot的聊天邏輯和輸入輸出以及存儲(chǔ),是由各種adapter來(lái)限定的,我們先看看流程圖,一會(huì)再一起看點(diǎn)例子,看看怎么用。
image
3、每個(gè)部分都設(shè)計(jì)了不同的“適配器”(Adapter)。
機(jī)器人應(yīng)答邏輯 => Logic Adapters
Closest Match Adapter 字符串模糊匹配(編輯距離)
Closest Meaning Adapter ?借助nltk的WordNet,近義詞評(píng)估
Time Logic Adapter 處理涉及時(shí)間的提問(wèn)
Mathematical Evaluation Adapter?涉及數(shù)學(xué)運(yùn)算
存儲(chǔ)器后端 => Storage Adapters
Read Only Mode 只讀模式,當(dāng)有輸入數(shù)據(jù)到chatterbot的時(shí)候,數(shù)
據(jù)庫(kù)并不會(huì)發(fā)生改變
Json Database Adapter 用以存儲(chǔ)對(duì)話數(shù)據(jù)的接口,對(duì)話數(shù)據(jù)以Json格式
進(jìn)行存儲(chǔ)。
Mongo Database Adapter ?以MongoDB database方式來(lái)存儲(chǔ)對(duì)話數(shù)據(jù)
輸入形式 => Input Adapters
Variable input type adapter 允許chatter bot接收不同類(lèi)型的輸入的,如strings,dictionaries和Statements
Terminal adapter 使得ChatterBot可以通過(guò)終端進(jìn)行對(duì)話
HipChat Adapter 使得ChatterBot 可以從HipChat聊天室獲取輸入語(yǔ)句,通過(guò)HipChat 和 ChatterBot 進(jìn)行對(duì)話
Speech recognition 語(yǔ)音識(shí)別輸入,詳見(jiàn)chatterbot-voice
輸出形式 => Output Adapters
Output format adapter支持text,json和object格式的輸出
Terminal adapter
HipChat Adapter
Mailgun adapter允許chat bot基于Mailgun API進(jìn)行郵件的發(fā)送
Speech synthesisTTS(Text to speech)部分,詳見(jiàn)chatterbot-voice
4、代碼
計(jì)算模式
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"
)
# 進(jìn)行數(shù)學(xué)計(jì)算
question = "What is 4 + 9?"
print(question)
response = bot.get_response(question)
print(response)
print("\n")
# 回答和時(shí)間相關(guān)的問(wèn)題
question = "What time is it?"
print(question)
response = bot.get_response(question)
print(response)
image
利用已經(jīng)提供好的小中文語(yǔ)料庫(kù)
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
chatbot = ChatBot("ChineseChatBot")
trainer = ChatterBotCorpusTrainer(chatbot)
# 使用中文語(yǔ)料庫(kù)訓(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__':
# 開(kāi)始對(duì)話
while True:
print(chatbot.get_response(input(">")))
# print(response_text("你是誰(shuí)"))
image
小黃雞語(yǔ)料更智能(推薦)
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)
# 開(kāi)始對(duì)話
while True:
print(response_text(input(">")))
image
小黃雞語(yǔ)料數(shù)據(jù)庫(kù):
Snip20191204_47.png
總結(jié)
以上是生活随笔為你收集整理的python微信集成_Python微信公众号后台开发005:集成智能聊天机器人的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java钱币换算_Java编写钱币转换为
- 下一篇: python内置模块重要程度排名_pyt