[AI] 如何制作一个低配山寨版贾维斯?-口语人机交互 Spoken Human Robot Interaction
口語人機(jī)交互 Spoken Human Robot Interaction
- 一、交互結(jié)構(gòu)總覽
- 二、展示
- 一、代碼背景
- 二、對話運(yùn)行結(jié)果展示
- 對話一
- 對話二
- 對話三
- 對話四
- 三、實(shí)現(xiàn)代碼
關(guān)注!點(diǎn)贊!評論!收藏!謝謝!
如何制作一個(gè)低配山寨版賈維斯?
一、交互結(jié)構(gòu)總覽
-
第一步:首先通過語音輸入設(shè)備,將語音信息輸入計(jì)算機(jī)。這里我使用speech_recognition.sr.Microphone() 函數(shù)調(diào)用計(jì)算機(jī)麥克風(fēng),然后 sr.Recognizer().listen() 將麥克風(fēng)輸入的語音信息保留下來。
-
第二步:使用語言識別庫,將輸入語音信息轉(zhuǎn)為文本信息。sr.Recognizer().recognize_google() 使用谷歌的語音識別獲取文本信息。
-
第三步:使用 en_core_web_sm 庫對語言進(jìn)行解析,并繪制依賴樹
-
第四步: 根據(jù)文本單詞的詞性或者特定詞(此處可以自由替換對話中的某些信息,比如時(shí)間,數(shù)量等,并讓計(jì)算機(jī)可以識別到這些信息),來讓計(jì)算機(jī)自動(dòng)回復(fù)設(shè)定好的語音(用Espeak工具可以讓計(jì)算機(jī)用語音讀出文本)
二、展示
一、代碼背景
我設(shè)定的對話背景是,我的計(jì)算機(jī)是個(gè)低配版賈維斯 (是真的低配) ,他管理我的房子,然后有朋友要來家里做客,我讓賈維斯幫我提前做一些準(zhǔn)備。
二、對話運(yùn)行結(jié)果展示
為了方便,所用的包均用的英文,讀者可以自由替換語言包,比如中文包
對話一
賈維斯:What can I do for you sir? 我: today my friends will come to my house
下面的對話可以是任意人數(shù),賈維斯會(huì)識別語言中的表示數(shù)字的單詞,并在后面的對話中復(fù)述出來。
對話二
這里“ isyncr” 其實(shí)是 “I think”,谷歌語音識別的不是特別準(zhǔn)(當(dāng)然不能是我發(fā)音不漂準(zhǔn)!)
此處語音可選擇 三種飲料中的一種,賈維斯識別后會(huì)復(fù)述一遍你的選擇
賈維斯: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!三、實(shí)現(xiàn)代碼
我還在開頭加了個(gè)下載好的賈維斯“歡迎回家”的語音包,代碼及語音包戳這里
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))當(dāng)然,讀者可以在此框架下,加入RNN來讓賈維斯的回復(fù)更加智能!
關(guān)注!點(diǎn)贊!評論!收藏!謝謝!
總結(jié)
以上是生活随笔為你收集整理的[AI] 如何制作一个低配山寨版贾维斯?-口语人机交互 Spoken Human Robot Interaction的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 时间序列分析(一) 如何判断序列是否平稳
- 下一篇: AI 趋势