From 9354c0fa320b6019de204c873215f9be2d5995d5 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Tue, 30 Oct 2018 13:43:00 -0700 Subject: [PATCH] more info on alternatives [(#1791)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1791) * more info on alternatives * add comment --- samples/analyze/beta_snippets.py | 36 +++++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/samples/analyze/beta_snippets.py b/samples/analyze/beta_snippets.py index d50f41d5..58bf6faf 100644 --- a/samples/analyze/beta_snippets.py +++ b/samples/analyze/beta_snippets.py @@ -55,21 +55,27 @@ def speech_transcription(input_uri): # There is only one annotation_result since only # one video is processed. annotation_results = result.annotation_results[0] - speech_transcription = annotation_results.speech_transcriptions[0] - alternative = speech_transcription.alternatives[0] - - print('Transcript: {}'.format(alternative.transcript)) - print('Confidence: {}\n'.format(alternative.confidence)) - - print('Word level information:') - for word_info in alternative.words: - word = word_info.word - start_time = word_info.start_time - end_time = word_info.end_time - print('\t{}s - {}s: {}'.format( - start_time.seconds + start_time.nanos * 1e-9, - end_time.seconds + end_time.nanos * 1e-9, - word)) + for speech_transcription in annotation_results.speech_transcriptions: + + # The number of alternatives for each transcription is limited by + # SpeechTranscriptionConfig.max_alternatives. + # Each alternative is a different possible transcription + # and has its own confidence score. + for alternative in speech_transcription.alternatives: + print('Alternative level information:') + + print('Transcript: {}'.format(alternative.transcript)) + print('Confidence: {}\n'.format(alternative.confidence)) + + print('Word level information:') + for word_info in alternative.words: + word = word_info.word + start_time = word_info.start_time + end_time = word_info.end_time + print('\t{}s - {}s: {}'.format( + start_time.seconds + start_time.nanos * 1e-9, + end_time.seconds + end_time.nanos * 1e-9, + word)) # [END video_speech_transcription_gcs_beta]