Skip to content

Commit

Permalink
Print all results, not all alternatives (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerjou authored Sep 7, 2017
1 parent 92d6468 commit cfa07e8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
14 changes: 6 additions & 8 deletions speech/cloud-client/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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]


Expand Down
20 changes: 10 additions & 10 deletions speech/cloud-client/transcribe_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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]


Expand Down
4 changes: 3 additions & 1 deletion speech/cloud-client/transcribe_streaming_mic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions speech/cloud-client/transcribe_word_time_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit cfa07e8

Please sign in to comment.