十分鐘,如何製作一個聊天機器人?
全文共
4088
字,預計學習時長
11
分鐘
在現實中,聊天機器人的響應時間非常重要。無論是旅遊業、銀行還是醫療,如果你真的想幫助客戶,機器人的響應時間應該更短,並且應與客戶服務代表交談時的真實情況類似。
除此之外,瞭解聊天機器人的主要用途也很重要,每個行業都不能使用同一個聊天機器人,他們有不同的目的和不同的語料庫。雖然訊息傳遞元件可以很好地給予答覆,但是可能需要時間作出迴應。另一方面,考慮到時間問題,可以應用各種其他方法,甚至可以找到一些以規則為基礎的系統,以獲得適合回答所提問題的語句。
你曾多少次聯絡旅行社要求退票,得到一個恰當的答覆是遠遠不夠的。
現在讓我們製作一個簡單的聊天機器人,安裝以下軟體包:
pip install nltk
pip install newspaper3k
Package newspaper3k有以下優點:
· 多執行緒文章下載框架
· 可識別新聞URL
· 可從HTML中提取文字
· 從HTML中提取頂層影象
· 可從HTML提取所有影象
· 可從文字中提取關鍵詞
· 可從文字中提取摘要
· 可從文字中提取作者
· 谷歌趨勢術語提取
· 使用10多種語言(英語、德語、阿拉伯語、中文等)
匯入庫,如下所示:
#import libraries
from newspaper import Article
import random
import nltk
import string
from sklearn。feature_extraction。text import CountVectorizer
from sklearn。metrics。pairwise import cosine_similarity
餘弦相似度或餘弦核將相似度計算為X和Y的標準化點積:
sklearn。metrics。pairwise。cosine_similarity(X, Y=None, dense_output=True)
引數
X{ndarray, sparse matrix} of shape (n_samples_X, n_features) 輸入資料。
Y{ndarray,sparse matrix} of shape (n_samples_Y, n_features), default=None 輸入資料。
如果沒有,輸出將是X。 dense_outputbool中所有樣本之間的成對相似性,default =True是否返回密集輸出,即使輸入是稀疏的。如果為False,則如果兩個輸入陣列都是稀疏的,則輸出是稀疏的。
返回
核矩陣:ndarray of shape(n_samples_X, n_samples_Y)
import numpy as np
import warnings
warnings。filterwarnings(‘ignore’)
這裡從一個醫療保健網站獲取資料:
article=Article(“https://www。mayoclinic。org/diseases-conditions/chronic-kidney-disease/symptoms-causes/syc-20354521”)
article。download()
article。parse()
article。nlp()
corpus=article。text
print(corpus)
#tokenization
text=corpus
sentence_list=nltk。sent_tokenize(text) #A list of sentences
#Print the list of sentences
print(sentence_list)
準備好了語料庫之後,你需要考慮使用者或客戶可能會問或說的問題,這與我們的內容無關。它可以是問候語、感謝語,也可以是拜拜之類的資訊。團隊需要就這些資訊和他們的反應進行考量。
問候機器人響應:
#Random response to greeting
def greeting_response(text):
text=text。lower()
#Bots greeting
bot_greetings=[“howdy”,“hi”,“hola”,“hey”,“hello”]
#User Greetings
user_greetings=[“wassup”,“howdy”,“hi”,“hola”,“hey”,“hello”]
for word in text。split():
if word in user_greetings:
return random。choice(bot_greetings)
#Random response to greeting
def gratitude_response(text):
text=text。lower()
感謝機器人響應:
#Bots gratitude
bot_gratitude=[“Glad tohelp”,“You are most welcome”, “Pleasure to be ofhelp”]
#User Gratitude
user_gratitude=[“Thankyou somuch”,“grateful”,“Thankyou”,“thankyou”,“thankyou”]
for word in text。split():
if word in user_gratitude:
return random。choice(bot_gratitude)
圖源:unsplash
種類列表:
# Default title text
def index_sort(list_var):
length=len(list_var)
list_index=list(range(0,length))
x=list_var
for i in range(length):
for j in range(length):
if x[list_index[i]]>x[list_index[j]]:
#swap
temp=list_index[i]
list_index[i]=list_index[j]
list_index[j]=temp
return list_index
聊天機器人響應功能來自於對預定義文字的餘弦相似性的響應。
#Creat Bots Response
def bot_response(user_input):
user_input=user_input。lower()
sentence_list。append(user_input)
bot_response=“”
cm=CountVectorizer()。fit_transform(sentence_list)
similarity_scores=cosine_similarity(cm[-1],cm)
similarity_scores_list=similarity_scores。flatten()
index=index_sort(similarity_scores_list)
index=index[1:]
response_flag=0
j=0
for i in range(len(index)):
ifsimilarity_scores_list[index[i]]>0。0:
bot_response=bot_response+‘’+sentence_list[index[i]]
response_flag=1
j=j+1
if j>2:
break
if response_flag==0:
bot_response=bot_response+“”+“I apologize, I dont understand”
sentence_list。remove(user_input)
return bot_response
對於退出聊天,退出列表中的單詞寫為“退出”,“再見”,“再見”,“退出”。
響應這些話,聊天機器人將退出聊天。
啟動聊天機器人,盡情享受吧!
#Start Chat
print(“Doc Bot: I am DOc bot and I will answer your queries about chronickidney disease, if you want to exit type, bye”)
exit_list=[‘exit’,‘bye’,‘see you later’,‘quit’]
while(True):
user_input=input()
if user_input。lower() in exit_list:
print(“Doc Bot: Bye Bye See youlater”)
break
elif greeting_response(user_input)!=None:
print(“Doc Bot: ”+greeting_response(user_input))
elif gratitude_response(user_input)!=None:
print(“Doc Bot: ”+gratitude_response(user_input))
else:
print(“Doc Bot: ”+bot_response(user_input))
請參見下面聊天機器人的回覆:
“謝謝”並不在我們的機器人感謝程式中,因此我們要傳達這樣的資訊。隨著時間的推移,你可以擴大這樣的詞彙表,或者使用正則表示式對其進行微調。
舉個小例子,與聊天機器人開始聊天,應該是快速和簡單的。你需要針對不同行業對聊天機器人進行微調,這些行業的語料庫來自實時資料或雲端的一些儲存。
此外,需要注意的是,實時資料要面對挑戰,聊天必須基於最新的資料作出迴應,例如在旅行社訂票。
留言點贊關注
我們一起分享AI學習與發展的乾貨
如轉載,請後臺留言,遵守轉載規範