[AI] 如何制作一个低配山寨版贾维斯?-口语人机交互 Spoken Human Robot Interaction
口語人機交互 Spoken Human Robot Interaction
- 一、交互結構總覽
- 二、展示
- 一、代碼背景
- 二、對話運行結果展示
- 對話一
- 對話二
- 對話三
- 對話四
- 三、實現代碼
關注!點贊!評論!收藏!謝謝!
如何制作一個低配山寨版賈維斯?
一、交互結構總覽
-
第一步:首先通過語音輸入設備,將語音信息輸入計算機。這里我使用speech_recognition.sr.Microphone() 函數調用計算機麥克風,然后 sr.Recognizer().listen() 將麥克風輸入的語音信息保留下來。
-
第二步:使用語言識別庫,將輸入語音信息轉為文本信息。sr.Recognizer().recognize_google() 使用谷歌的語音識別獲取文本信息。
-
第三步:使用 en_core_web_sm 庫對語言進行解析,并繪制依賴樹
-
第四步: 根據文本單詞的詞性或者特定詞(此處可以自由替換對話中的某些信息,比如時間,數量等,并讓計算機可以識別到這些信息),來讓計算機自動回復設定好的語音(用Espeak工具可以讓計算機用語音讀出文本)
二、展示
一、代碼背景
我設定的對話背景是,我的計算機是個低配版賈維斯 (是真的低配) ,他管理我的房子,然后有朋友要來家里做客,我讓賈維斯幫我提前做一些準備。
二、對話運行結果展示
為了方便,所用的包均用的英文,讀者可以自由替換語言包,比如中文包
對話一
賈維斯:What can I do for you sir? 我: today my friends will come to my house
下面的對話可以是任意人數,賈維斯會識別語言中的表示數字的單詞,并在后面的對話中復述出來。
對話二
這里“ isyncr” 其實是 “I think”,谷歌語音識別的不是特別準(當然不能是我發音不漂準!)
此處語音可選擇 三種飲料中的一種,賈維斯識別后會復述一遍你的選擇
賈維斯:and what drinks do I need to prepare?cola, tea or coffee? 我 : isyncr coffee is the best 賈維斯:got it sir, I will prepare six cups of coffe對話三
賈維斯:By the way, sir, when they will come? 我: if I remember correctly live well, either 5 p.m. 賈維斯:Sir, please confirm, your six friends will come at 5. pm and I will prepare six cups of coffe for them對話四
賈維斯:And Sir, your house looks not clean, can I clean it now? 我:yes of course please 賈維斯:As your wish, please wait a moment 賈維斯:Sir, your house is clean now! 賈維斯:Have a nice day sir!三、實現代碼
我還在開頭加了個下載好的賈維斯“歡迎回家”的語音包,代碼及語音包戳這里
import speech_recognition as sr import en_core_web_sm import os import spacy from spacy import displacy from spacy.symbols import NOUN, NUM, VERB from nltk import Tree import winsound# Built a nltk tree def to_nltk_tree(node):if node.n_lefts + node.n_rights > 0:return Tree(node.orth_, [to_nltk_tree(child) for child in node.children])else:return node.orth_def dependency_tree(text):nlp = en_core_web_sm.load()nlp_doc = nlp(text)print('---------------')print('Dependency Graph')[to_nltk_tree(sent.root).pretty_print() for sent in nlp_doc.sents]print('---------------')def clean():winsound.PlaySound('Jarvis/Jarvis-System/Garbage cleared.wav', winsound.SND_FILENAME)reply = 'Sir, your house is clean now!'print(reply)os.system('espeak "{}"'.format(reply))winsound.PlaySound('Jarvis/Jarvis-System/Welcome Home Sir(No Song).wav', winsound.SND_FILENAME)#today my friends will come to my house r = sr.Recognizer() with sr.Microphone() as source:l = "What can I do for you sir?"print(l)os.system('espeak "{}"'.format(l))audio = r.listen(source)try:textT = r.recognize_google(audio) except sr.UnknownValueError:print("Jarvis could not understand your audio") except sr.RequestError as e:print("Could not request results from Google Speech Recognition service; {0}".format(e))print(textT) dependency_tree(textT)nlp = en_core_web_sm.load() nlp_doc = nlp(textT)for word in nlp_doc:if word.pos == VERB and str(word) == 'come':l = 'Sir, How many friends will come?'print(l)os.system('espeak "{}"'.format(l))with sr.Microphone() as source:audio = r.listen(source)try:textT = r.recognize_google(audio)except sr.UnknownValueError:print("Jarvis could not understand your audio")except sr.RequestError as e:print("Could not request results from Google Speech Recognition service; {0}".format(e))print(textT)dependency_tree(textT)nlp = en_core_web_sm.load()nlp_doc = nlp(textT)for word in nlp_doc:if word.pos == NUM:friends_num = str(word) #I remember six people# I think tea is the best r = sr.Recognizer() with sr.Microphone() as source:l = "and what drinks do I need to prepare?cola, tea or coffee?"print(l)os.system('espeak "{}"'.format(l))audio = r.listen(source)try:textT = r.recognize_google(audio) except sr.UnknownValueError:print("Jarvis could not understand your audio") except sr.RequestError as e:print("Could not request results from Google Speech Recognition service; {0}".format(e))print(textT) dependency_tree(textT)if 'tea' in textT.lower():drink = 'tea'l = 'got it sir, I will prepare ' + friends_num+' cups of tea' elif 'cola' in textT.lower():drink = 'cola'l = 'got it sir, I will prepare ' + friends_num+' cups of cola' elif 'coffe' in textT.lower():drink = 'coffe'l = 'got it sir, I will prepare ' + friends_num+' cups of coffe' else:l0 = 'Sir, can you please say it again?'print(l0)os.system('espeak "{}"'.format(l0))print(l) os.system('espeak "{}"'.format(l))#If I remeber correctly, they will come at 5 pm r = sr.Recognizer() with sr.Microphone() as source:l = "By the way, sir, when they will come?"print(l)os.system('espeak "{}"'.format(l))audio = r.listen(source)try:textT = r.recognize_google(audio) except sr.UnknownValueError:print("Jarvis could not understand your audio") except sr.RequestError as e:print("Could not request results from Google Speech Recognition service; {0}".format(e))print(textT) dependency_tree(textT) nlp_doc = nlp(textT)time_num = ''for word in nlp_doc:if word.pos == NUM:time_num += str(word) + '.'if 'a.m.' in textT.lower():m = 'am'elif 'p.m.' in textT.lower():m = 'pm'else:m = 'pm'l = 'Sir, please confirm, your ' + friends_num + ' friends will come at '\+ time_num + ' ' + m + ' and I will prepare ' + friends_num + ' cups of ' + drink + ' for them' print(l) os.system('espeak "{}"'.format(l))r = sr.Recognizer() with sr.Microphone() as source:l = "And Sir, your house looks not clean, can I clean it now?"print(l)os.system('espeak "{}"'.format(l))audio = r.listen(source)try:textT = r.recognize_google(audio) except sr.UnknownValueError:print("Jarvis could not understand your audio") except sr.RequestError as e:print("Could not request results from Google Speech Recognition service; {0}".format(e))print(textT) dependency_tree(textT) if 'yes' or 'yeah' in textT.lower():l = 'As your wish, please wait a moment'print(l)os.system('espeak "{}"'.format(l))clean()l = 'Have a nice day sir!'print(l)os.system('espeak "{}"'.format(l))當然,讀者可以在此框架下,加入RNN來讓賈維斯的回復更加智能!
關注!點贊!評論!收藏!謝謝!
總結
以上是生活随笔為你收集整理的[AI] 如何制作一个低配山寨版贾维斯?-口语人机交互 Spoken Human Robot Interaction的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 时间序列分析(一) 如何判断序列是否平稳
- 下一篇: 最小二乘残差 C语言6,传感器原理和应用