導(dǎo)讀:
demo內(nèi)容來(lái)源網(wǎng)課貪心科技AI學(xué)社的網(wǎng)課《人工智能Python編程特訓(xùn)營(yíng)》
本篇博客的主要內(nèi)容是做一個(gè)聊天機(jī)器人冰蘑。
課件
這里先附上課件厌秒,可以收藏一下
課堂代碼
制作屬于自己的聊天機(jī)器人
圖靈機(jī)器人http://www.turingapi.com/
API接口教程https://www.kancloud.cn/turing/www-tuling123-com/718227
第三方機(jī)器人簡(jiǎn)易接口調(diào)用
這里用的是flask框架,調(diào)用方法很簡(jiǎn)單汉形,只需要在裝飾器里面寫(xiě)路徑就可以了纸镊。
from flask import Flask
import requests
app = Flask(__name__)
# 裝飾器里面寫(xiě)的是訪問(wèn)路徑
@app.route("/")
def hello_world():
url = "http://openapi.tuling123.com/openapi/api/v2"
data_param = {
"reqType": 0,
"perception": {
"inputText": {
"text": "附近的酒店"
},
"inputImage": {
"url": "imageUrl"
},
"selfInfo": {
"location": {
"city": "北京",
"province": "北京",
"street": "信息路"
}
}
},
"userInfo": {
"apiKey": "xxxxxxxxxxxxxxxxxxxxxx",
"userId": "xxxxxxxxxxxxxxxxxx"
}
}
response = requests.post(url=url,json=data_param)
print(response.text)
return "貪心學(xué)院"
if __name__ == "__main__":
app.run()
會(huì)得到返回的信息
{"intent":{"code":4003},"results":[{"groupType":0,"resultType":"text","values":{"text":"在外住酒店倍阐,還是得小心點(diǎn)好哦~"}}]}
當(dāng)前需要填上自己的apiKey和userId,而且還得去申請(qǐng)權(quán)限逗威,否則會(huì)出現(xiàn)
{"intent":{"code":4003},"results":[{"groupType":0,"resultType":"text","values":{"text":"請(qǐng)求次數(shù)超限制!"}}]}
我們換一種玩法
from flask import Flask
app = Flask(__name__)
# 裝飾器里面寫(xiě)的是訪問(wèn)路徑
@app.route("/greedyai/<data>")
def hello_world(data):
return data
if __name__ == "__main__":
app.run()
這時(shí)候運(yùn)行后
http://127.0.0.1:5000/greedyai/
在這個(gè)路徑之后輸入你想要顯示的字符串峰搪,就會(huì)顯示出來(lái)
比如說(shuō)http://127.0.0.1:5000/greedyai/你是誰(shuí)
就會(huì)在屏幕上輸出你是誰(shuí)的字樣。
具體開(kāi)發(fā)方法凯旭,基于模板匹配
from flask import Flask
import requests
app = Flask(__name__)
# 裝飾器里面寫(xiě)的是訪問(wèn)路徑
@app.route("/greedyai/<data>")
def hello_world(data):
if "帥" in data:
return "大周老師"
elif "線上" in data:
return "線上課程為主"
elif "助教" in data or "服務(wù)" in data:
return "為提高服務(wù)質(zhì)量概耻,課程配有專業(yè)的助教老師"
elif "基礎(chǔ)" in data:
return "http://aijiaoai.greedyai.com/home"
elif "優(yōu)勢(shì)" in data:
return "這你都問(wèn),大周老師就是優(yōu)勢(shì)"
elif "有效期" in data:
return "只要你想學(xué)尽纽,我一直在你身邊"
else:
return "哎喲咐蚯,小主,我不懂你的意思"
if __name__ == "__main__":
app.run()
然后跟上面的示例一樣弄贿,在路徑之后輸入一句話春锋,如果這一句話里面包含了某個(gè)關(guān)鍵詞,那么就會(huì)輸出相應(yīng)的回復(fù)
這是部分輸入的回復(fù)
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [02/Oct/2019 14:34:18] "GET /greedyai/有啥服務(wù) HTTP/1.1" 200 -
127.0.0.1 - - [02/Oct/2019 14:40:17] "GET /greedyai/哈哈哈 HTTP/1.1" 200 -
127.0.0.1 - - [02/Oct/2019 14:40:31] "GET /greedyai/線上課程 HTTP/1.1" 200 -
基于搜索的方法
用戶輸入 -> 檢索模型 -> 輸入
如何計(jì)算相似度(文本的向量表示)
余弦相似度
課程demo
import jieba
# 詞典
word_vector_list = ["我們", "來(lái)", "貪心", "學(xué)院", "學(xué)習(xí)", "人工智能", "和", "python"]
s1 = "我來(lái)貪心學(xué)院學(xué)習(xí)python"
s2 = "我學(xué)習(xí)人工智能"
s3 = "Python課程的學(xué)習(xí)周期是多久"
question = "Python學(xué)習(xí)多久"
# 第一種切分模式:精確模式差凹,比較適合做文本分析
s1_result_list = list(jieba.cut(s1))
print(s1_result_list)
# 全模式
s1_result_list = list(jieba.cut(s1, cut_all=True))
print(s1_result_list)
# 搜索引擎模式
s1_result_list = list(jieba.cut_for_search(s1))
print(s1_result_list)
# 向量轉(zhuǎn)換
import numpy as np
def get_vector(data):
vector_list = []
for i in word_vector_list:
if i in list(jieba.cut(data)):
vector_list.append(1)
else:
vector_list.append(0)
print(data)
print(vector_list)
return np.array(vector_list).reshape(1, -1)
question_vector_list = get_vector(question)
s1_vector_list = get_vector(s1)
s2_vector_list = get_vector(s2)
s3_vector_list = get_vector(s3)
# 相似度計(jì)算
from sklearn.metrics.pairwise import cosine_similarity
# print(cosine_similarity([[1, 0, 1]], [[0, 0, 1]]))
print(cosine_similarity(question_vector_list, s1_vector_list))
print(cosine_similarity(question_vector_list, s2_vector_list))
print(cosine_similarity(question_vector_list, s3_vector_list))
作業(yè)
完成一個(gè)完整的項(xiàng)目期奔,能夠根據(jù)已有的語(yǔ)料庫(kù),自動(dòng)根據(jù)輸入去計(jì)算哪個(gè)答案最符合
import jieba
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from flask import Flask
# 建立語(yǔ)料庫(kù)
corpus = {
"Python課程是線上課程還是線下課程危尿?": "線上課程為主",
"Python課程有助教嗎呐萌?": "為提高服務(wù)效率和質(zhì)量,課程都配備專業(yè)的全職助教谊娇。",
"我沒(méi)有基礎(chǔ)應(yīng)該從哪個(gè)課開(kāi)始學(xué)肺孤?": "大周老師的Python基礎(chǔ)集訓(xùn)營(yíng)非常適合你哦,"
"可以在這里學(xué)習(xí):http://aijiaoai.greedyai.com/info/5",
"Python的學(xué)習(xí)周期是多久济欢?": "如果你沒(méi)有基礎(chǔ)的話兩個(gè)月可以搞定",
"Python課程的學(xué)習(xí)方式是什么呢赠堵?": "無(wú)需安裝環(huán)境,在線直接寫(xiě)代碼法褥、看視頻茫叭、看漫畫(huà),趣味性學(xué)習(xí)",
"Python課程的優(yōu)勢(shì)是什么呢半等?": "全網(wǎng)覆蓋最全的Python基礎(chǔ)知識(shí)體系練與學(xué)的深度結(jié)合揍愁,"
"每一個(gè)知識(shí)點(diǎn)都配有練習(xí)項(xiàng)目,生動(dòng)有趣、授課方式多樣杀饵,視頻莽囤、文字、圖片切距、在線代碼編輯烁登,"
"在聊天與娛樂(lè)中學(xué)習(xí)",
"Python課程的有效期是多久呢?": "我們把有效期確定為1年,1年內(nèi)可無(wú)限次學(xué)習(xí)",
"Python課程有優(yōu)惠嗎饵沧?": "我們定價(jià)為開(kāi)課后是599锨络,現(xiàn)在是針對(duì)老學(xué)員預(yù)售199,開(kāi)課后恢復(fù)原價(jià)"
}
# 建立詞典狼牺,對(duì)于所有語(yǔ)料庫(kù)中的問(wèn)題進(jìn)行jieba分詞
all_question = ""
for question in corpus:
all_question += question
dictionaries = list(set(jieba.cut(all_question))) # 加上set是為了方便去重
# 單個(gè)詞典轉(zhuǎn)換為向量
def transform_vector(date):
vector_list = []
for wd in dictionaries:
if wd in list(jieba.cut(date)):
vector_list.append(1)
else:
vector_list.append(0)
return np.array(vector_list).reshape(1, -1) # .reshape(1,-1)是為了后期余弦計(jì)算
# 單個(gè)余弦相似度計(jì)算
def get_cosine(user_question, corpus_question):
similar_list = cosine_similarity(transform_vector(user_question), transform_vector(corpus_question))
similar_num = similar_list[0][0] # 相似度
return similar_num
# 計(jì)算語(yǔ)料庫(kù)中的所有相似度
def get_corpus_consine(user_question):
ori_question_dict = {}
ori_answer_dict = {}
similar_list = [] # 保存所有余弦值
for key in corpus:
similar_num = get_cosine(user_question, key) # 獲得余弦值
similar_list.append(similar_num) # 保存所有余弦值
ori_question_dict[similar_num] = key # 獲取原問(wèn)題并儲(chǔ)存
ori_answer_dict[similar_num] = corpus[key] # 獲取原答案并儲(chǔ)存
return similar_list, ori_question_dict, ori_answer_dict
# 得到最佳回答
def get_best_answer(similar_list, ori_question_dict, ori_answer_dict):
max_similar = max(similar_list)
if max_similar == 0: # 沒(méi)有匹配項(xiàng)
best_similar = 0
best_question = "未檢索到相似的問(wèn)題"
best_answer = "對(duì)不起羡儿,我聽(tīng)不懂你在說(shuō)什么"
else: # 返回值
best_similar = max_similar
best_question = ori_question_dict[max_similar]
best_answer = ori_answer_dict[max_similar]
return [best_similar, best_question, best_answer]
# 使用faslk,以接口的形式完成問(wèn)答
app = Flask(__name__)
@app.route("/<user_question>")
def question_for_web(user_question):
similar_list, ori_question_dict, ori_answer_dict = get_corpus_consine(user_question) # 計(jì)算所有相似度
res = get_best_answer(similar_list, ori_question_dict, ori_answer_dict) # 得到最佳答案
html_ = '<html><body>' \
'<p>' \
'<h2>問(wèn)題:{}</h2>' \
'</p>' \
'<p>' \
'<li>最高相似度:{}</li>' \
'<li>最高相似度的問(wèn)題:{}</li>' \
'<h2>最佳回答為:{}</h2>' \
'</p>' \
'</body></html>'.format(user_question, res[0], res[1], res[2])
return html_ # 返回html并寫(xiě)入網(wǎng)頁(yè)
if __name__ == "__main__":
app.run()