自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

十分鐘,如何制作一個(gè)聊天機(jī)器人?

人工智能 機(jī)器人
在現(xiàn)實(shí)中,聊天機(jī)器人的響應(yīng)時(shí)間非常重要。無(wú)論是旅游業(yè)、銀行還是醫(yī)療,如果你真的想幫助客戶,機(jī)器人的響應(yīng)時(shí)間應(yīng)該更短,并且應(yīng)與客戶服務(wù)代表交談時(shí)的真實(shí)情況類(lèi)似。

 本文轉(zhuǎn)載自公眾號(hào)“讀芯術(shù)”(ID:AI_Discovery)

[[390546]]

了解聊天機(jī)器人的主要用途很重要,每個(gè)行業(yè)都不能使用同一個(gè)聊天機(jī)器人,他們有不同的目的和不同的語(yǔ)料庫(kù)。雖然消息傳遞組件可以很好地給予答復(fù),但是可能需要時(shí)間作出回應(yīng)。另一方面,考慮到時(shí)間問(wèn)題,可以應(yīng)用各種其他方法,甚至可以找到一些以規(guī)則為基礎(chǔ)的系統(tǒng),以獲得適合回答所提問(wèn)題的語(yǔ)句。

你曾多少次聯(lián)系旅行社要求退票,得到一個(gè)恰當(dāng)?shù)拇饛?fù)是遠(yuǎn)遠(yuǎn)不夠的。

現(xiàn)在讓我們制作一個(gè)簡(jiǎn)單的聊天機(jī)器人,安裝以下軟件包:

  1. pip install nltk  
  2. pip install newspaper3k 

Package newspaper3k有以下優(yōu)點(diǎn):

  • 多線程文章下載框架
  • 可識(shí)別新聞URL
  • 可從HTML中提取文本
  • 從HTML中提取頂層圖像
  • 可從HTML提取所有圖像
  • 可從文本中提取關(guān)鍵詞
  • 可從文本中提取摘要
  • 可從文本中提取作者
  • 谷歌趨勢(shì)術(shù)語(yǔ)提取
  • 使用10多種語(yǔ)言(英語(yǔ)、德語(yǔ)、阿拉伯語(yǔ)、中文等)

導(dǎo)入庫(kù),如下所示:

  1. #import libraries 
  2. from newspaper import Article 
  3. import random 
  4. import nltk 
  5. import string 
  6. from sklearn.feature_extraction.text import CountVectorizer 
  7. from sklearn.metrics.pairwise import cosine_similarity 

余弦相似度或余弦核將相似度計(jì)算為X和Y的標(biāo)準(zhǔn)化點(diǎn)積:

  1. sklearn.metrics.pairwise.cosine_similarity(X, Y=None, dense_output=True

參數(shù)

X{ndarray, sparse matrix} of shape (n_samples_X, n_features) 輸入數(shù)據(jù)。

Y{ndarray,sparse matrix} of shape (n_samples_Y, n_features), default=None 輸入數(shù)據(jù)。

如果沒(méi)有,輸出將是X. dense_outputbool中所有樣本之間的成對(duì)相似性,default =True是否返回密集輸出,即使輸入是稀疏的。如果為False,則如果兩個(gè)輸入數(shù)組都是稀疏的,則輸出是稀疏的。

返回

核矩陣:ndarray of shape(n_samples_X, n_samples_Y)

  1. import numpy as np 
  2. import warnings 
  3. warnings.filterwarnings('ignore'

這里從一個(gè)醫(yī)療保健網(wǎng)站獲取數(shù)據(jù):

  1. article=Article("https://www.mayoclinic.org/diseases-conditions/chronic-kidney-disease/symptoms-causes/syc-20354521"
  2. article.download() 
  3. article.parse() 
  4. article.nlp() 
  5. corpus=article.text 
  6. print(corpus) 
  7.  
  8.  
  9. #tokenization 
  10. text=corpus 
  11. sentence_list=nltk.sent_tokenize(text) #A list of sentences 
  12.  
  13.  
  14. #Print the list of sentences 
  15. print(sentence_list) 

準(zhǔn)備好了語(yǔ)料庫(kù)之后,你需要考慮用戶或客戶可能會(huì)問(wèn)或說(shuō)的問(wèn)題,這與我們的內(nèi)容無(wú)關(guān)。它可以是問(wèn)候語(yǔ)、感謝語(yǔ),也可以是拜拜之類(lèi)的信息。團(tuán)隊(duì)需要就這些信息和他們的反應(yīng)進(jìn)行考量。

問(wèn)候機(jī)器人響應(yīng):

  1. #Random response to greeting 
  2. def greeting_response(text): 
  3.  text=text.lower() 
  4.  
  5.  
  6.  #Bots greeting 
  7.  bot_greetings=["howdy","hi","hola","hey","hello"
  8.  
  9.  
  10.   #User Greetings 
  11.  user_greetings=["wassup","howdy","hi","hola","hey","hello"
  12.  for word in text.split(): 
  13.  if word in user_greetings: 
  14.  return random.choice(bot_greetings) 
  15. #Random response to greeting 
  16. def gratitude_response(text): 
  17.  text=text.lower() 

感謝機(jī)器人響應(yīng):

  1. #Bots gratitude 
  2.  bot_gratitude=["Glad tohelp","You are most welcome""Pleasure to be ofhelp"
  3.  
  4.  
  5.  #User Gratitude 
  6.  user_gratitude=["Thankyou somuch","grateful","Thankyou","thankyou","thankyou"
  7.  
  8.  
  9.  for word in text.split(): 
  10.  if word in user_gratitude: 
  11.  return random.choice(bot_gratitude)

種類(lèi)列表:

  1. Default title text 
  2. def index_sort(list_var): 
  3.  length=len(list_var) 
  4.  list_index=list(range(0,length)) 
  5.  x=list_var 
  6.  for i in range(length): 
  7.  for j in range(length): 
  8.  if x[list_index[i]]>x[list_index[j]]: 
  9.  #swap 
  10.  temp=list_index[i] 
  11.  list_index[i]=list_index[j] 
  12.  list_index[j]=temp 
  13.  
  14.  
  15.  return list_index 

聊天機(jī)器人響應(yīng)功能來(lái)自于對(duì)預(yù)定義文本的余弦相似性的響應(yīng)。

  1. #Creat Bots Response 
  2. def bot_response(user_input): 
  3.  user_input=user_input.lower() 
  4.  sentence_list.append(user_input) 
  5.  bot_response="" 
  6.  cm=CountVectorizer().fit_transform(sentence_list) 
  7.  similarity_scores=cosine_similarity(cm[-1],cm) 
  8.  similarity_scores_list=similarity_scores.flatten() 
  9.  index=index_sort(similarity_scores_list) 
  10.  index=index[1:] 
  11.  response_flag=0 
  12.  j=0 
  13.  for i in range(len(index)): 
  14.  ifsimilarity_scores_list[index[i]]>0.0: 
  15.   bot_response=bot_response+''+sentence_list[index[i]] 
  16.  response_flag=1 
  17.  j=j+1 
  18.  if j>2: 
  19.  break 
  20.  
  21.  
  22.  if response_flag==0: 
  23.  bot_response=bot_response+""+"I apologize, I dont understand" 
  24.  
  25.  
  26.  sentence_list.remove(user_input)  
  27.  
  28.  
  29.  return bot_response 

對(duì)于退出聊天,退出列表中的單詞寫(xiě)為“退出”,“再見(jiàn)”,“再見(jiàn)”,“退出”。

響應(yīng)這些話,聊天機(jī)器人將退出聊天。

啟動(dòng)聊天機(jī)器人,盡情享受吧!

  1. #Start Chat 
  2. print("Doc Bot: I am DOc bot and I will answer your queries about chronickidney disease, if you want to exit type, bye"
  3.  
  4.  
  5. exit_list=['exit','bye','see you later','quit'
  6.  
  7.  
  8. while(True): 
  9.  user_input=input() 
  10.  if user_input.lower() in exit_list: 
  11.  print("Doc Bot: Bye Bye See youlater"
  12.  break 
  13.  elif greeting_response(user_input)!=None: 
  14.  print("Doc Bot: "+greeting_response(user_input)) 
  15.  elif gratitude_response(user_input)!=None: 
  16.  print("Doc Bot: "+gratitude_response(user_input))  
  17.  else
  18.  print("Doc Bot: "+bot_response(user_input)) 

請(qǐng)參見(jiàn)下面聊天機(jī)器人的回復(fù):

 

“謝謝”并不在我們的機(jī)器人感謝程序中,因此我們要傳達(dá)這樣的信息。隨著時(shí)間的推移,你可以擴(kuò)大這樣的詞匯表,或者使用正則表達(dá)式對(duì)其進(jìn)行微調(diào)。

舉個(gè)小例子,與聊天機(jī)器人開(kāi)始聊天,應(yīng)該是快速和簡(jiǎn)單的。你需要針對(duì)不同行業(yè)對(duì)聊天機(jī)器人進(jìn)行微調(diào),這些行業(yè)的語(yǔ)料庫(kù)來(lái)自實(shí)時(shí)數(shù)據(jù)或云端的一些儲(chǔ)存。

此外,需要注意的是,實(shí)時(shí)數(shù)據(jù)要面對(duì)挑戰(zhàn),聊天必須基于最新的數(shù)據(jù)作出回應(yīng),例如在旅行社訂票。

 

 

責(zé)任編輯:華軒 來(lái)源: 讀芯術(shù)
相關(guān)推薦

2021-04-21 09:00:00

機(jī)器人語(yǔ)言工具

2021-11-30 10:56:43

ChatterBot機(jī)器人人工智能

2020-08-07 14:40:09

Python聊天機(jī)器人編程語(yǔ)言

2020-07-30 08:06:34

Python開(kāi)發(fā)工具

2020-12-17 06:48:21

SQLkafkaMySQL

2019-04-01 14:59:56

負(fù)載均衡服務(wù)器網(wǎng)絡(luò)

2022-10-12 23:02:49

Calcite異構(gòu)數(shù)據(jù)框架

2009-10-09 14:45:29

VB程序

2021-07-29 08:57:23

ViteReact模塊

2021-09-07 09:40:20

Spark大數(shù)據(jù)引擎

2022-06-16 07:31:41

Web組件封裝HTML 標(biāo)簽

2024-06-19 09:58:29

2023-04-12 11:18:51

甘特圖前端

2024-01-29 00:20:00

GolangGo代碼

2015-12-11 14:35:40

內(nèi)存監(jiān)控運(yùn)維

2012-07-10 01:22:32

PythonPython教程

2015-09-06 09:22:24

框架搭建快速高效app

2024-05-13 09:28:43

Flink SQL大數(shù)據(jù)

2023-11-30 10:21:48

虛擬列表虛擬列表工具庫(kù)

2025-03-18 12:20:00

編程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)