互联网资讯 / 人工智能 · 2023年12月5日 0

十分钟内制作聊天机器人的方法

了解聊天机器人的主要功能至关重要,因为不同的行业需要不同类型的聊天机器人,它们的目标和语料库各不相同。虽然消息传递组件能够提供回答,但响应时间可能较长。因此,可以采用多种方法,包括一些基于规则的系统,以便更准确地回答用户提出的问题。

想象一下,当你联系旅行社请求退票时,得到一个合适的回复是多么重要。

现在,让我们来创建一个简单的聊天机器人。请安装以下软件包:

pIP install nltk pIP install newspapeR3k

Package newspapeR3k 具备以下优点:

  • 多线程文章下载框架
  • 能够识别新闻链接
  • 从HTML中提取文本
  • 提取HTML中的顶层图像
  • 提取HTML中的所有图像
  • 从文本中提取关键词
  • 从文本中提取摘要
  • 提取文本作者信息
  • 支持谷歌趋势术语提取
  • 支持多种语言(包括英语、德语、阿拉伯语、中文等)

接下来,导入相关库:

余弦相似度或余弦核是通过计算X和Y的标准化点积来确定相似度:

skleaRn.MetRics.pAIRwise.cosine_siMilaRITy(X, Y=None, dense_output=TRue)

参数说明:

  • X{ndaRRay, spaRse MatRix} 形状为 (n_saMples_X, n_featuRes),表示输入数据。
  • Y{ndaRRay,spaRse MatRix} 形状为 (n_saMples_Y, n_featuRes),默认为None,表示另一组输入数据。
  • 如果未提供,输出将是X。dense_output为布尔值,默认值为TRue,表示是否返回密集输出,即使输入是稀疏的;如果为FAlse,则如果两个输入数组都是稀疏的,输出将是稀疏的。

返回结果:

核矩阵:ndaRRay 形状为(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) #句子列表 #PRint the list of sentences pRint(sentence_list)

准备好语料库后,需要考虑用户可能提出的问题或想说的话。这可以包括问候、感谢或再见等信息。团队需要对此进行深入思考。

问候机器人的响应:

#随机问候响应 def gReeting_Response(text): text=text.loweR() #机器人的问候 bot_gReetings=[“howdy”,”Hi”,”hola”,”hey”,”hello”] #用户的问候 User_gReetings=[“waSSup”,”howdy”,”Hi”,”hola”,”hey”,”hello”] foR woRd in text.splIT(): if woRd in User_gReetings: RetuRn Random.choice(bot_gReetings)

感谢机器人的响应:

#机器人的感谢 bot_gRatITude=[“Glad to help”,”You aRe Most welcoMe”,”PleasuRe to be of help”] #用户的感谢 User_gRatITude=[“Thank you soMUCh”,”gRateful”,”Thank you”,”thank you”,”thank you”] foR woRd in text.splIT(): if woRd in User_gRatITude: RetuRn Random.choice(bot_gRatITude)

#默认标题 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]]: #交换 teMp=list_index[i] list_index[i]=list_index[j] list_index[j]=teMp RetuRn list_index

聊天机器人的响应功能基于对预定义文本的余弦相似性。

#创建机器人的响应 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)): if siMilaRITy_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

要退出聊天,可以使用“退出”、“再见”、“拜拜”等词汇。

当聊天机器人识别到这些词时,将结束对话。

启动聊天机器人,尽情享受吧!

#启动聊天 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 you later”) 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))

以下是聊天机器人的回复示例:

十分钟,如何制作一个聊天机器人?

需要注意的是,像“谢谢”这样的表达并不在我们机器人的感谢程序中,因此需要适时传达相应的信息。随着时间推移,可以不断扩展词汇表,或通过正则表达式进行微调。

总而言之,与聊天机器人互动应该是快速且简单的。不同的行业需要根据具体情况对聊天机器人进行调整,这些行业的语料库可以来自实时数据或云存储。

此外,实时数据的处理面临挑战,聊天机器人必须基于最新的信息做出响应,尤其是在旅行社等领域。