Skip to content

Commit

Permalink
add word time offsets to async sample [(#1042)](GoogleCloudPlatform/p…
Browse files Browse the repository at this point in the history
  • Loading branch information
dizcology authored and busunkim96 committed Sep 3, 2020
1 parent 93f100a commit 3e504dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion google-cloud-speech/samples/snippets/transcribe_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def transcribe_gcs(gcs_uri):
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
sample_rate_hertz=16000,
language_code='en-US')
language_code='en-US',
enable_word_time_offsets=True)

operation = client.long_running_recognize(config, audio)

Expand All @@ -96,6 +97,15 @@ def transcribe_gcs(gcs_uri):
for alternative in alternatives:
print('Transcript: {}'.format(alternative.transcript))
print('Confidence: {}'.format(alternative.confidence))

for word_info in alternative.words:
word = word_info.word
start_time = word_info.start_time
end_time = word_info.end_time
print('Word: {}, start_time: {}, end_time: {}'.format(
word,
start_time.seconds + start_time.nanos * 1e-9,
end_time.seconds + end_time.nanos * 1e-9))
# [END def_transcribe_gcs]


Expand Down
11 changes: 11 additions & 0 deletions google-cloud-speech/samples/snippets/transcribe_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ def test_transcribe_gcs(capsys):
out, err = capsys.readouterr()

assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)


def test_transcribe_gcs_word_time_offsets(capsys):
transcribe_async.transcribe_gcs(
'gs://python-docs-samples-tests/speech/audio.flac')
out, err = capsys.readouterr()

match = re.search(r'Bridge, start_time: ([0-9.]+)', out, re.DOTALL | re.I)
time = float(match.group(1))

assert time > 0

0 comments on commit 3e504dc

Please sign in to comment.