Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit cc6b234

Browse files
authored
docs(samples): Add Spoken Punctuation and Emojis code samples (#155)
@lucaswadedavis
1 parent e7efa67 commit cc6b234

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

samples/snippets/beta_snippets.py

+37
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
python beta_snippets.py multi-channel
2626
python beta_snippets.py multi-language
2727
python beta_snippets.py word-level-conf
28+
python beta_snippets.py spoken-punctuation-emojis
2829
"""
2930

3031
import argparse
@@ -291,6 +292,40 @@ def transcribe_file_with_word_level_confidence():
291292
# [END speech_transcribe_word_level_confidence_beta]
292293

293294

295+
def transcribe_file_with_spoken_punctuation_end_emojis():
296+
"""Transcribe the given audio file with spoken punctuation and emojis enabled."""
297+
# [START speech_transcribe_spoken_punctuation_emojis_beta]
298+
from google.cloud import speech_v1p1beta1 as speech
299+
from google.protobuf import wrappers_pb2
300+
301+
client = speech.SpeechClient()
302+
303+
speech_file = "resources/commercial_mono.wav"
304+
305+
with io.open(speech_file, "rb") as audio_file:
306+
content = audio_file.read()
307+
308+
audio = speech.RecognitionAudio(content=content)
309+
config = speech.RecognitionConfig(
310+
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
311+
sample_rate_hertz=8000,
312+
language_code="en-US",
313+
# Enable spoken punctuation
314+
enable_spoken_punctuation=wrappers_pb2.BoolValue(value=True),
315+
# Enable spoken emojis
316+
enable_spoken_emojis=wrappers_pb2.BoolValue(value=True),
317+
)
318+
319+
response = client.recognize(config=config, audio=audio)
320+
321+
for i, result in enumerate(response.results):
322+
alternative = result.alternatives[0]
323+
print("-" * 20)
324+
print(u"First alternative of result {}".format(i))
325+
print(u"Transcript: {}".format(alternative.transcript))
326+
# [END speech_transcribe_spoken_punctuation_emojis_beta]
327+
328+
294329
if __name__ == "__main__":
295330
parser = argparse.ArgumentParser(
296331
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
@@ -313,3 +348,5 @@ def transcribe_file_with_word_level_confidence():
313348
transcribe_file_with_multilanguage()
314349
elif args.command == "word-level-conf":
315350
transcribe_file_with_word_level_confidence()
351+
elif args.command == "spoken-punctuation-emojis":
352+
transcribe_file_with_spoken_punctuation_end_emojis()

samples/snippets/beta_snippets_test.py

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
transcribe_file_with_metadata,
2121
transcribe_file_with_multichannel,
2222
transcribe_file_with_multilanguage,
23+
transcribe_file_with_spoken_punctuation_end_emojis,
2324
transcribe_file_with_word_level_confidence,
2425
)
2526

@@ -75,3 +76,10 @@ def test_transcribe_word_level_confidence(capsys):
7576
out, err = capsys.readouterr()
7677

7778
assert "OK Google stream stranger things from Netflix to my TV" in out
79+
80+
81+
def test_transcribe_file_with_spoken_punctuation_end_emojis(capsys):
82+
transcribe_file_with_spoken_punctuation_end_emojis()
83+
out, err = capsys.readouterr()
84+
85+
assert "First alternative of result " in out

0 commit comments

Comments
 (0)