Skip to content
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
29 changes: 29 additions & 0 deletions speech/cloud-client/beta_snippets.py
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)
11 changes: 10 additions & 1 deletion speech/cloud-client/beta_snippets_test.py
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