【问题标题】:Decoding Google Speech API response in python在 python 中解码 Google Speech API 响应
【发布时间】:2013-11-23 07:37:02
【问题描述】:

我正在尝试在 Python 中使用 Google Speech API。我像这样加载一个 .flac 文件:

url = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"
audio = open('temp_voice.flac','rb').read()
headers = {'Content-Type': 'audio/x-flac; rate=44100', 'User-Agent':'Mozilla/5.0'}
req = urllib2.Request(url, data=audio, headers=headers)
resp = urllib2.urlopen(req)
system("rm temp_voice.wav; rm temp_voice.flac")
print resp.read()

输出:

{"status":0,"id":"","hypotheses":[{"utterance":"今天是星期三","confidence":0.75135982}]}

谁能教我如何提取文本“今天是星期三”并将其保存为变量并打印?

【问题讨论】:

标签: python json google-api


【解决方案1】:

您可以使用json.loads 将 JSON 数据转换为字典,如下所示

data = '{"status":0,"id":"","hypotheses":[{"utterance":"Today is Wednesday","confidence":0.75135982}]}'
import json
data = json.loads(data)
print data["hypotheses"][0]["utterance"]

【讨论】:

  • 非常感谢您的回复。不幸的是,这似乎对我不起作用。我得到以下信息: raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded
  • 我刚刚将代码添加到我的原始帖子中。感谢您的帮助
  • @Stralo 但这没有使用json 的部分
  • 是的,我不知道该怎么做,这就是我寻求帮助的原因。我在代码末尾添加了您的建议,并得到了我之前提到的 ValueError。我做了 data=resp.read() text=json.loads(data) print text["hypotheses"][0]["utterance"]
【解决方案2】:

如果响应以字符串形式出现,那么您可以将其评估为字典,(为了安全起见,最好使用 ast 库中的 literal_eval 代替):

>>> d=eval('{"status":0,"id":"","hypotheses":[{"utterance":"Today is Wednesday","confidence":0.75135982}]}')
>>> d
{'status': 0, 'hypotheses': [{'confidence': 0.75135982, 'utterance': 'Today is Wednesday'}], 'id': ''}  

>>> h=d.get('hypotheses')                                                                            
>>> h                                                                                                 
[{'confidence': 0.75135982, 'utterance': 'Today is Wednesday'}]                                       
>>> for i in h:                                                                                       
...    print i.get('utterance')
... 
Today is Wednesday

当然,如果它已经是字典,那么您不需要进行评估,请尝试使用print type(response),其中response 是您得到的结果。

【讨论】:

  • ast.literal_eval() 是执行此操作的安全方法。 eval 绝对不能用于此目的。
  • 我试过 text=eval(str(response.read())) 打印文本并得到文件“”,第 0 行 ^ SyntaxError: unexpected EOF while parsing
  • 听起来像是缓冲区已满或连接在完整字符串结束之前丢失你得到了一些格式错误的字符串。
【解决方案3】:

检索输出的问题看起来有点复杂。首先resp是实例类型,但是如果您手动复制输出是dictionary->list->dictionary。如果您将 resp.read() 分配给新变量,您将获得长度为 0 的类型字符串。它会发生,因为一旦使用(打印),所有输出都会消失在空气中。因此,必须在授予来自 google api 的响应后立即进行 json 解码。如下:

resp = urllib2.urlopen(req)

text = json.loads(resp.read())["hypotheses"][0]["utterance"]

在我的情况下就像一个魅力;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 2018-09-21
    • 2019-07-09
    相关资源
    最近更新 更多