0

我正在使用wolframalphawit.ai,我試圖從wit.ai音頻中取出數據,而不是終端文本的wolframalphaPython中的StopIteration錯誤

我的代碼是:

#!/usr/bin/python 
import speech_recognition as sr 
import wolframalpha 
import sys 

r = sr.Recognizer() 
with sr.Microphone() as source: 
    print("Say something!") 
    audio = r.listen(source) 


WIT_AI_KEY = "NQYEITRO5GL2Q2MZFIJE4UHWVNQEUROW" 
try: 
    print("Wit.ai thinks you said " + r.recognize_wit(audio, key=WIT_AI_KEY)) 
except sr.UnknownValueError: 
    print("Wit.ai could not understand audio") 
except sr.RequestError as e: 
    print("Could not request results from Wit.ai service; {0}".format(e)) 


client = wolframalpha.Client('PR5756-H3EP749GGH') 
print(r.recognize_wit(audio, key=WIT_AI_KEY)) 
res = client.query(r.recognize_wit(audio, key=WIT_AI_KEY)) 
print(next(res.results).text) 

我面對這個錯誤:

MacBook-Air:Documents exepaul$ python ak.py 
2016-02-22 23:05:04.429 Python[3003:122880] 23:05:04.428 WARNING: 140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h. 
Say something! 
Wit.ai thinks you said seven 
seven 
Traceback (most recent call last): 
    File "ak.py", line 24, in <module> 
    print(next(res.results).text) 
StopIteration 

我怎麼可以給數據WolframAlpha的API?

+1

如果你不知道'next()'可能會在Python中引發'StopIteration';你應該學習Python的基礎知識:你可能還有其他明顯的差距。請參閱https://www.python.org/about/gettingstarted/ – jfs

+0

這樣做的工作嗎? '打印(res.results [0]的.text)' – agentp

回答

2

StopIteration當發電機耗盡且沒有更多的值時會引發,總得有個值。但你需要自己處理:

try: 
    print(next(res.results).text) 
except StopIteration: 
    print("No more suggesstions.")