From 95d61b5b12f2808e153258fa366ab33a49f59007 Mon Sep 17 00:00:00 2001 From: Jerjou Cheng Date: Thu, 31 Aug 2017 10:50:19 -0700 Subject: [PATCH] Print all results, not all alternatives --- speech/cloud-client/transcribe.py | 14 ++++++------- speech/cloud-client/transcribe_async.py | 20 +++++++++---------- .../cloud-client/transcribe_streaming_mic.py | 4 +++- .../transcribe_word_time_offsets.py | 9 ++++----- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/speech/cloud-client/transcribe.py b/speech/cloud-client/transcribe.py index b808e5813c00..69f83a1ec8da 100644 --- a/speech/cloud-client/transcribe.py +++ b/speech/cloud-client/transcribe.py @@ -51,10 +51,9 @@ def transcribe_file(speech_file): # [START migration_sync_response] response = client.recognize(config, audio) # [END migration_sync_request] - alternatives = response.results[0].alternatives - - for alternative in alternatives: - print('Transcript: {}'.format(alternative.transcript)) + # Print the first alternative of all the consecutive results. + for result in response.results: + print('Transcript: {}'.format(result.alternatives[0].transcript)) # [END migration_sync_response] # [END def_transcribe_file] @@ -76,10 +75,9 @@ def transcribe_gcs(gcs_uri): # [END migration_audio_config_gcs] response = client.recognize(config, audio) - alternatives = response.results[0].alternatives - - for alternative in alternatives: - print('Transcript: {}'.format(alternative.transcript)) + # Print the first alternative of all the consecutive results. + for result in response.results: + print('Transcript: {}'.format(result.alternatives[0].transcript)) # [END def_transcribe_gcs] diff --git a/speech/cloud-client/transcribe_async.py b/speech/cloud-client/transcribe_async.py index 9ac77918cf5a..cb33821e345c 100644 --- a/speech/cloud-client/transcribe_async.py +++ b/speech/cloud-client/transcribe_async.py @@ -49,12 +49,12 @@ def transcribe_file(speech_file): # [END migration_async_request] print('Waiting for operation to complete...') - result = operation.result(timeout=90) + response = operation.result(timeout=90) - alternatives = result.results[0].alternatives - for alternative in alternatives: - print('Transcript: {}'.format(alternative.transcript)) - print('Confidence: {}'.format(alternative.confidence)) + # Print the first alternative of all the consecutive results. + for result in response.results: + print('Transcript: {}'.format(result.alternatives[0].transcript)) + print('Confidence: {}'.format(result.alternatives[0].confidence)) # [END migration_async_response] # [END def_transcribe_file] @@ -76,12 +76,12 @@ def transcribe_gcs(gcs_uri): operation = client.long_running_recognize(config, audio) print('Waiting for operation to complete...') - result = operation.result(timeout=90) + response = operation.result(timeout=90) - alternatives = result.results[0].alternatives - for alternative in alternatives: - print('Transcript: {}'.format(alternative.transcript)) - print('Confidence: {}'.format(alternative.confidence)) + # Print the first alternative of all the consecutive results. + for result in response.results: + print('Transcript: {}'.format(result.alternatives[0].transcript)) + print('Confidence: {}'.format(result.alternatives[0].confidence)) # [END def_transcribe_gcs] diff --git a/speech/cloud-client/transcribe_streaming_mic.py b/speech/cloud-client/transcribe_streaming_mic.py index bde8b30f1687..eb6f6ded3dd8 100644 --- a/speech/cloud-client/transcribe_streaming_mic.py +++ b/speech/cloud-client/transcribe_streaming_mic.py @@ -129,7 +129,9 @@ def listen_print_loop(responses): if not response.results: continue - # There could be multiple results in each response. + # The `results` list is consecutive. For streaming, we only care about + # the first result being considered, since once it's `is_final`, it + # moves on to considering the next utterance. result = response.results[0] if not result.alternatives: continue diff --git a/speech/cloud-client/transcribe_word_time_offsets.py b/speech/cloud-client/transcribe_word_time_offsets.py index 1c98feaf1cc3..f576e58aed7a 100644 --- a/speech/cloud-client/transcribe_word_time_offsets.py +++ b/speech/cloud-client/transcribe_word_time_offsets.py @@ -46,9 +46,8 @@ def transcribe_file_with_word_time_offsets(speech_file): response = client.recognize(config, audio) - alternatives = response.results[0].alternatives - - for alternative in alternatives: + for result in response.results: + alternative = result.alternatives[0] print('Transcript: {}'.format(alternative.transcript)) for word_info in alternative.words: @@ -82,8 +81,8 @@ def transcribe_gcs_with_word_time_offsets(gcs_uri): print('Waiting for operation to complete...') result = operation.result(timeout=90) - alternatives = result.results[0].alternatives - for alternative in alternatives: + for result in result.results: + alternative = result.alternatives[0] print('Transcript: {}'.format(alternative.transcript)) print('Confidence: {}'.format(alternative.confidence))