Skip to content

Commit

Permalink
add Speech API auto punctuation sample [(#1446)](GoogleCloudPlatform/…
Browse files Browse the repository at this point in the history
…python-docs-samples#1446)

* add auto punctuation sample

* correct docstring
  • Loading branch information
dizcology authored and busunkim96 committed Sep 3, 2020
1 parent 99a6b8d commit a0d9c55
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Example usage:
python beta_snippets.py enhanced-model resources/commercial_mono.wav
python beta_snippets.py metadata resources/commercial_mono.wav
python beta_snippets.py punctuation resources/commercial_mono.wav
"""

import argparse
Expand Down Expand Up @@ -99,6 +100,32 @@ def transcribe_file_with_metadata(path):
# [END speech_transcribe_file_with_metadata]


# [START speech_transcribe_file_with_auto_punctuation]
def transcribe_file_with_auto_punctuation(path):
"""Transcribe the given audio file with auto punctuation enabled."""
client = speech.SpeechClient()

with io.open(path, 'rb') as audio_file:
content = audio_file.read()

audio = speech.types.RecognitionAudio(content=content)
config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=8000,
language_code='en-US',
# Enable automatic punctuation
enable_automatic_punctuation=True)

response = client.recognize(config, audio)

for i, result in enumerate(response.results):
alternative = result.alternatives[0]
print('-' * 20)
print('First alternative of result {}'.format(i))
print('Transcript: {}'.format(alternative.transcript))
# [END speech_transcribe_file_with_auto_punctuation]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
Expand All @@ -113,3 +140,5 @@ def transcribe_file_with_metadata(path):
transcribe_file_with_enhanced_model(args.path)
elif args.command == 'metadata':
transcribe_file_with_metadata(args.path)
elif args.command == 'punctuation':
transcribe_file_with_auto_punctuation(args.path)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import os

from beta_snippets import (
transcribe_file_with_enhanced_model, transcribe_file_with_metadata)
transcribe_file_with_auto_punctuation, transcribe_file_with_enhanced_model,
transcribe_file_with_metadata)

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')

Expand All @@ -33,3 +34,11 @@ def test_transcribe_file_with_metadata(capsys):
out, _ = capsys.readouterr()

assert 'Chrome' in out


def test_transcribe_file_with_auto_punctuation(capsys):
transcribe_file_with_auto_punctuation(
os.path.join(RESOURCES, 'commercial_mono.wav'))
out, _ = capsys.readouterr()

assert 'Okay. Sure.' in out

0 comments on commit a0d9c55

Please sign in to comment.