Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print all results, not all alternatives #1098

Merged
merged 1 commit into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that there is always only one result in each response of streaming_recognize?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a) What're you doing doing a code review on a holiday?? tsk tsk
b) Mm... if interim_results is False, then yes - there will only be one result. But if interim_results is True, then there can be 0 or one is_final result, followed by 0 to many interim (is_final=False) results. I believe different results correspond to consecutive parts of the audio stream. See here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see - thanks for the clarification.

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