了解聊天机器人的主要用途很重要,每个行业都不能使用同一个聊天机器人,他们有不同的目的和不同的语料库。虽然消息传递组件可以很好地给予答复,但是可能需要时间作出回应。另一方面,考虑到时间问题,可以应用各种其他方法,甚至可以找到一些以规则为基础的系统,以获得适合回答所提问题的语句。
你曾多少次联系旅行社要求退票,得到一个恰当的答复是远远不够的。
现在让我们制作一个简单的聊天机器人,安装以下软件包:
pIP install nltk pIP install newspapeR3k
Package newspapeR3k有以下优点:
多线程文章下载框架 可识别新闻URL 可从HTML中提取文本 从HTML中提取顶层图像 可从HTML提取所有图像 可从文本中提取关键词 可从文本中提取摘要 可从文本中提取作者 谷歌趋势术语提取 使用10多种语言(英语、德语、阿拉伯语、中文等)
导入库,如下所示:
余弦相似度或余弦核将相似度计算为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)
# 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))
请参见下面聊天机器人的回复:

谢谢并不在我们的机器人感谢程序中,因此我们要传达这样的信息。随着时间的推移,你可以扩大这样的词汇表,或者使用正则表达式对其进行微调。
举个小例子,与聊天机器人开始聊天,应该是快速和简单的。你需要针对不同行业对聊天机器人进行微调,这些行业的语料库来自实时数据或云端的一些储存。
此外,需要注意的是,实时数据要面对挑战,聊天必须基于最新的数据作出回应,例如在旅行社订票。