提问人:mocrow13 提问时间:11/12/2023 最后编辑:mocrow13 更新时间:11/13/2023 访问量:39
AI语音助手代码中的功能不起作用[已关闭]
functions in code of ai voice assistant doesnt work [closed]
问:
其他函数期望 wikipedia 和 wolframalpha 功能工作 当我调用这些函数时,它只是输出我在终端中所说的内容,然后跳到“监听命令”,不做任何事情
def ListorDict(var):
if isinstance(var, list):
return var[0]['plaintext']
else:
return var['plaintext']
def search_wolframAlpha(query = ''): 响应 = wolframClient.query(query) 当我们提出问题 #after,我们得到的是一个响应 # @success = wolfarm Alpha 能够解析查询 # @numpads = 结果数 # pods 结果列表,这也可以包含 subpod
#query solved
if response['@success'] == 'false': #if the computer cant find answer
return 'Could not compute'
else:
result = '' #treasure text
#question
pod0 = response['pod'][0] #page 1 of the book
pod1 = response['pod'][1] #page 2 of the book
#may contain the answer, has the highest confidence value
#if is primary, or has the title or result or definitian , then it is the official
if (('result') in pod1['@title'].lower()) or (pod1.get('@primary', 'false') == 'true') or ('definition' in pod1['@title'].lower()): # goes the second page first and looks for words definition, title or result, we found the result
#get the result
result = ListorDict(pod1['subpod'])
# remove the brackedet section
return result.split('(')[0]
else:
question = ListorDict(pod0['subpod']) #if not we go to the first page and return this page as a answer
return question.split('(')[0]
#search wikipedia instead
speak("Computation failed. Querying universal databank")
return search_Wikipedia(question) #if cant find, goes to wikipedia
'''
def search_Wikipedia(query = ''): searchResults = wikipedia.search(query) #søker på nett 如果不是 searchResults: print(“没有维基百科结果”) 返回 'No results reveiced' 尝试: wikiPage = wikipedia.page(searchResults[0]) #find 顶部信息 维基百科除外。DisambiguationError as error: #anlam giderme belirsizlik erroru giderme wikiPage = wikipedia.page(error.options[0])#if 不清楚,我们要去第一个 打印(wikiPage.title) wikiSummary = str(wikiPage.summary) 返回wikiSummary
def parseCommand():#def 机器监听命令 侦听器 = sr。Recognizer() #create listener,它将接入麦克风并将您的声音解析为文本 print(“监听命令”)
with sr.Microphone() as source: # taps (baglanmak ) into microphone
listener.pause_threshold = 2 #how long can it be a gap between speech before it cancels or ends the listening
listener.energy_threshold= 600
input_speech = listener.listen(source) #google voice recognition (limit is 30 sekunds)
try:
print("Recognizing speech.. ") #input speech for whatever we will say
query = listener.recognize_google(input_speech, language='en_us') #pass the input_speech over here and language (turns the voice into text)
print(f"the input speech was {query}")
return query
elif query[0] == "wikipedia":
query = ' '.join(query[1:])
speak("Querying the universial databank ")
result = search_Wikipedia(query)
speak(result)
elif query[0] == 'compute' or query[0] == 'computer':
query = ' '.join(query[1:])
speak("Computing")
try:
result = search_wolframAlpha(query)
speak(result)
except:
speak('Unable to compute')
我希望这段代码了解我 100% Procent 使用的内容及其功能有效。(我认为 Wolframalpha 库有助于在互联网上搜索任何您想要的东西。
答: 暂无答案
评论