首页 > 解决方案 > 如何分离 JSON 数据

问题描述

我正在尝试在 python 中制作一个简单的 SR 程序来识别语音。我为此使用了 IBM 云的 api,但遇到了一些问题。

import json;
from os.path import join, dirname
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

authenticator = IAMAuthenticator('KEY')
speech_to_text = SpeechToTextV1(
    authenticator=authenticator
)

speech_to_text.set_service_url('URL')

with open(join(dirname(__file__), './.', 'recording.mp3'),
               'rb') as audio_file:
    speech_recognition_results = speech_to_text.recognize(
        audio=audio_file,
        content_type='audio/mp3',
        word_alternatives_threshold=0.9,
        keywords=['um', 'uh', 'like'],
        keywords_threshold=0.5
    ).get_result()
print(json.dumps(speech_recognition_results, indent=2))

这段代码工作得很好,但它是我遇到问题的输出。

{
  "result_index": 0,
  "results": [
    {
      "final": true,
      "alternatives": [
        {
          "transcript": "hello Ryan how are you today ",
          "confidence": 0.73
        }
      ],
      "word_alternatives": [
        {
          "start_time": 0.19,
          "end_time": 0.45,
          "alternatives": [
            {
              "word": "hello",
              "confidence": 0.93
            }
          ]
        },
        {
          "start_time": 0.45,
          "end_time": 0.99,
          "alternatives": [
            {
              "word": "Ryan",
              "confidence": 0.92
            }
          ]
        },
        {
          "start_time": 1.12,
          "end_time": 1.31,
          "alternatives": [
            {
              "word": "how",
              "confidence": 0.98
            }
          ]
        },
        {
          "start_time": 1.31,
          "end_time": 1.41,
          "alternatives": [
            {
              "word": "are",
              "confidence": 0.98
            }
          ]
        },
        {
          "start_time": 1.41,
          "end_time": 1.61,
          "alternatives": [
            {
              "word": "you",
              "confidence": 0.98
            }
          ]
        },
        {
          "start_time": 1.61,
          "end_time": 2.04,
          "alternatives": [
            {
              "word": "today",
              "confidence": 0.99
            }
          ]
        }
      ],
      "keywords_result": {}
    }
  ]
}

我要做的就是将显示整个成绩单的部分隔离为变量之类的东西。我该怎么做?

标签: pythonjsonibm-cloudspeech-recognition

解决方案


推荐阅读