如何恢复JSON DUMP抄本并将其放入变量

时间:2019-06-19 09:49:35

标签: python json ibm-watson speech-to-text

我想从JSON DUMP中检索记录,以便可以将其放入变量(字符串)中,以便以后可以与IBM Watson NLU服务一起使用

我的代码:

class MyRecognizeCallback(RecognizeCallback):
def __init__(self):
    RecognizeCallback.__init__(self

def on_transcription(self, transcript):
    print(transcript)

def on_connected(self):
    print('Connection was successful')

def on_error(self, error):
    print('Error received: {}'.format(error))

def on_inactivity_timeout(self, error):
    print('Inactivity timeout: {}'.format(error))

def on_listening(self):
    print('Service is listening')

def on_hypothesis(self, hypothesis):
    print(hypothesis)

def on_data(self, data):
    print(json.dumps(data, indent=2))

def on_close(self):
    print("Connection closed")


myRecognizeCallback = MyRecognizeCallback()

with open(join(dirname(__file__), './.', 'output.wav'),
          'rb') as audio_file:
audio_source = AudioSource(audio_file)
speech_to_text.recognize_using_websocket(
    audio=audio_source,
    content_type='audio/wav',
    recognize_callback=myRecognizeCallback,
    model='fr-FR_BroadbandModel')

API WATSON返回此Json转储:

{
  "results": [
    {
      "alternatives": [
        {
          "confidence": 0.87
          "transcript": "several tornadoes touch down as a line of
severe thunderstorms swept through colorado on sunday "
        }
      ],
      "final": true
    }
  ],
  "result_index": 0
}

提前谢谢!

1 个答案:

答案 0 :(得分:0)

要加载json文件:

with open(file_name) as f:
    d = json.load(f)