Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 41e8754

Browse filesBrowse files
dizcologychenyumic
authored andcommitted
add Speech API auto punctuation sample (GoogleCloudPlatform#1446)
* add auto punctuation sample * correct docstring
1 parent 2efa244 commit 41e8754
Copy full SHA for 41e8754

File tree

Expand file treeCollapse file tree

2 files changed

+39
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+39
-1
lines changed

‎speech/cloud-client/beta_snippets.py

Copy file name to clipboardExpand all lines: speech/cloud-client/beta_snippets.py
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Example usage:
2121
python beta_snippets.py enhanced-model resources/commercial_mono.wav
2222
python beta_snippets.py metadata resources/commercial_mono.wav
23+
python beta_snippets.py punctuation resources/commercial_mono.wav
2324
"""
2425

2526
import argparse
@@ -99,6 +100,32 @@ def transcribe_file_with_metadata(path):
99100
# [END speech_transcribe_file_with_metadata]
100101

101102

103+
# [START speech_transcribe_file_with_auto_punctuation]
104+
def transcribe_file_with_auto_punctuation(path):
105+
"""Transcribe the given audio file with auto punctuation enabled."""
106+
client = speech.SpeechClient()
107+
108+
with io.open(path, 'rb') as audio_file:
109+
content = audio_file.read()
110+
111+
audio = speech.types.RecognitionAudio(content=content)
112+
config = speech.types.RecognitionConfig(
113+
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
114+
sample_rate_hertz=8000,
115+
language_code='en-US',
116+
# Enable automatic punctuation
117+
enable_automatic_punctuation=True)
118+
119+
response = client.recognize(config, audio)
120+
121+
for i, result in enumerate(response.results):
122+
alternative = result.alternatives[0]
123+
print('-' * 20)
124+
print('First alternative of result {}'.format(i))
125+
print('Transcript: {}'.format(alternative.transcript))
126+
# [END speech_transcribe_file_with_auto_punctuation]
127+
128+
102129
if __name__ == '__main__':
103130
parser = argparse.ArgumentParser(
104131
description=__doc__,
@@ -113,3 +140,5 @@ def transcribe_file_with_metadata(path):
113140
transcribe_file_with_enhanced_model(args.path)
114141
elif args.command == 'metadata':
115142
transcribe_file_with_metadata(args.path)
143+
elif args.command == 'punctuation':
144+
transcribe_file_with_auto_punctuation(args.path)

‎speech/cloud-client/beta_snippets_test.py

Copy file name to clipboardExpand all lines: speech/cloud-client/beta_snippets_test.py
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import os
1515

1616
from beta_snippets import (
17-
transcribe_file_with_enhanced_model, transcribe_file_with_metadata)
17+
transcribe_file_with_auto_punctuation, transcribe_file_with_enhanced_model,
18+
transcribe_file_with_metadata)
1819

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

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

3536
assert 'Chrome' in out
37+
38+
39+
def test_transcribe_file_with_auto_punctuation(capsys):
40+
transcribe_file_with_auto_punctuation(
41+
os.path.join(RESOURCES, 'commercial_mono.wav'))
42+
out, _ = capsys.readouterr()
43+
44+
assert 'Okay. Sure.' in out

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.