Skip to content

Commit ca29935

Browse files
jerjoubusunkim96
authored andcommitted
Print all results, not all alternatives [(#1098)](#1098)
1 parent d54e32e commit ca29935

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

speech/snippets/transcribe.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ def transcribe_file(speech_file):
5151
# [START migration_sync_response]
5252
response = client.recognize(config, audio)
5353
# [END migration_sync_request]
54-
alternatives = response.results[0].alternatives
55-
56-
for alternative in alternatives:
57-
print('Transcript: {}'.format(alternative.transcript))
54+
# Print the first alternative of all the consecutive results.
55+
for result in response.results:
56+
print('Transcript: {}'.format(result.alternatives[0].transcript))
5857
# [END migration_sync_response]
5958
# [END def_transcribe_file]
6059

@@ -76,10 +75,9 @@ def transcribe_gcs(gcs_uri):
7675
# [END migration_audio_config_gcs]
7776

7877
response = client.recognize(config, audio)
79-
alternatives = response.results[0].alternatives
80-
81-
for alternative in alternatives:
82-
print('Transcript: {}'.format(alternative.transcript))
78+
# Print the first alternative of all the consecutive results.
79+
for result in response.results:
80+
print('Transcript: {}'.format(result.alternatives[0].transcript))
8381
# [END def_transcribe_gcs]
8482

8583

speech/snippets/transcribe_async.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ def transcribe_file(speech_file):
4949
# [END migration_async_request]
5050

5151
print('Waiting for operation to complete...')
52-
result = operation.result(timeout=90)
52+
response = operation.result(timeout=90)
5353

54-
alternatives = result.results[0].alternatives
55-
for alternative in alternatives:
56-
print('Transcript: {}'.format(alternative.transcript))
57-
print('Confidence: {}'.format(alternative.confidence))
54+
# Print the first alternative of all the consecutive results.
55+
for result in response.results:
56+
print('Transcript: {}'.format(result.alternatives[0].transcript))
57+
print('Confidence: {}'.format(result.alternatives[0].confidence))
5858
# [END migration_async_response]
5959
# [END def_transcribe_file]
6060

@@ -76,12 +76,12 @@ def transcribe_gcs(gcs_uri):
7676
operation = client.long_running_recognize(config, audio)
7777

7878
print('Waiting for operation to complete...')
79-
result = operation.result(timeout=90)
79+
response = operation.result(timeout=90)
8080

81-
alternatives = result.results[0].alternatives
82-
for alternative in alternatives:
83-
print('Transcript: {}'.format(alternative.transcript))
84-
print('Confidence: {}'.format(alternative.confidence))
81+
# Print the first alternative of all the consecutive results.
82+
for result in response.results:
83+
print('Transcript: {}'.format(result.alternatives[0].transcript))
84+
print('Confidence: {}'.format(result.alternatives[0].confidence))
8585
# [END def_transcribe_gcs]
8686

8787

speech/snippets/transcribe_streaming_mic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ def listen_print_loop(responses):
129129
if not response.results:
130130
continue
131131

132-
# There could be multiple results in each response.
132+
# The `results` list is consecutive. For streaming, we only care about
133+
# the first result being considered, since once it's `is_final`, it
134+
# moves on to considering the next utterance.
133135
result = response.results[0]
134136
if not result.alternatives:
135137
continue

speech/snippets/transcribe_word_time_offsets.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ def transcribe_file_with_word_time_offsets(speech_file):
4646

4747
response = client.recognize(config, audio)
4848

49-
alternatives = response.results[0].alternatives
50-
51-
for alternative in alternatives:
49+
for result in response.results:
50+
alternative = result.alternatives[0]
5251
print('Transcript: {}'.format(alternative.transcript))
5352

5453
for word_info in alternative.words:
@@ -82,8 +81,8 @@ def transcribe_gcs_with_word_time_offsets(gcs_uri):
8281
print('Waiting for operation to complete...')
8382
result = operation.result(timeout=90)
8483

85-
alternatives = result.results[0].alternatives
86-
for alternative in alternatives:
84+
for result in result.results:
85+
alternative = result.alternatives[0]
8786
print('Transcript: {}'.format(alternative.transcript))
8887
print('Confidence: {}'.format(alternative.confidence))
8988

0 commit comments

Comments
 (0)