[英]StopIteration error in python
I am using wolframalpha
and wit.ai
together and i am trying to build that wolframalpha
fetch data from wit.ai
audio instead of terminal text. 我正在一起使用
wolframalpha
和wit.ai
,并且我试图构建该wolframalpha
从wit.ai
音频而不是终端文本中获取数据。
My code is: 我的代码是:
#!/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)
I am facing this error: 我遇到此错误:
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
How can I give data to wolframalpha api? 如何向Wolframalpha API提供数据?
StopIteration
is raised when generator is exhausted and has no more values, it's totaly ok to get one. 当生成器用尽并且没有更多值时,将引发
StopIteration
,完全可以获取一个值。 But you need to handle it yourself: 但是您需要自己处理:
try:
print(next(res.results).text)
except StopIteration:
print("No more suggesstions.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.