在 Watson Python SDK 中使用 json

Working with json in Watson Python SDK

我正在开展一个项目,希望能够将语音转文本的 Watson Python SDK 实现结合起来...Watson Conversation...和文本转语音。尽管使用 Python 2.7 json 数据,但我 运行 遇到了一些问题。我实际上正在尝试做两件事:

1) 我只想为转录值解析 json 数据,如果我能将这些值组合成易于阅读的字符串格式以供稍后在程序中使用,那就太棒了。

2) 我需要做的另一件事是以一种允许我将其用作对话或文本到语音部分的输入的方式操纵 json。基本上,我如何将 json 中提供的内容转换为其他 Watson 模块可接受的输入?

到目前为止我尝试过的: 我阅读了 Python 2.7 json 文档并尝试将其转换回 Python 字典,哪种方法有效?所有 key:value 对前面都有一个 "u",并且 none 的常规字典方法似乎对它们有效。此外,它们看起来不像标准的 Key:Value 组合。不过,我能够将所有 json 数据放在一个变量中。我将在下面 post 我的代码(忽略打印语句,因为我只是检查数据如何查看每个步骤),但它主要是您可以从 github 示例部分获得的内容。

** 还有一个快速的最后一个问题:与其他 SDK(Java、JScript 等)相比,Python SDK 是否有任何限制,因为它们的输出似乎很多更容易合作?

import json
from os.path import join, dirname
from watson_developer_cloud import SpeechToTextV1


speech_to_text = SpeechToTextV1(
    username='###########',
    password='###########,
    x_watson_learning_opt_out=True
)

with open(join(dirname(__file__), '/home/user/Desktop/output.wav'),'rb') as audio_file:

    json_str = (json.dumps(speech_to_text.recognize(audio_file, content_type='audio/wav', timestamps=False, word_confidence=False,
model='en-US_NarrowbandModel'), indent=2))

print json_str

json_dict = json.loads(json_str)

print json_dict



def main(args):
    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

在我看来,问题是您将 JSON 转储到字符串,然后尝试将其作为对象访问。

使用下面的示例代码,它可以工作。

from os.path import join, dirname
from watson_developer_cloud import SpeechToTextV1


speech_to_text = SpeechToTextV1(
    username='....',
    password='....',
    x_watson_learning_opt_out=True
)

with open('../blog/ihaveadream.wav','rb') as audio_file:
    response = speech_to_text.recognize(audio_file, content_type='audio/wav', timestamps=False, word_confidence=False, model='en-US_NarrowbandModel')

print json.dumps(response, indent=2)

这个returns以下:

{
  "results": [
    {
      "alternatives": [
        {
          "confidence": 1.0, 
          "transcript": "I still have a dream "
        }
      ], 
      "final": true
    }, 
    {
      "alternatives": [
        {
          "confidence": 0.999, 
          "transcript": "it is a dream deeply rooted in the American dream I have a dream "
        }
      ], 
      "final": true
    }, 
    {
      "alternatives": [
        {
          "confidence": 1.0, 
          "transcript": "that one day this nation will rise up and live out the true meaning of its creed we hold these truths to be self evident that all men are created equal "
        }
      ], 
      "final": true
    }
  ], 
  "result_index": 0, 
  "warnings": [
    "Unknown arguments: continuous."
  ]
}

因此,如果您想访问顶级回复,您可以执行以下操作。

print 'Confidence: {}'.format(response['results'][0]['alternatives'][0]['confidence'])
print 'Transcript: {}'.format(response['results'][0]['alternatives'][0]['transcript'])

输出结果为:

Confidence: 1.0
Transcript: I still have a dream