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

docs(samples): Add Spoken Punctuation and Emojis code samples #155

Merged
merged 6 commits into from Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions samples/snippets/beta_snippets.py
Expand Up @@ -25,6 +25,7 @@
python beta_snippets.py multi-channel
python beta_snippets.py multi-language
python beta_snippets.py word-level-conf
python beta_snippets.py spoken-punctuation-emojis
"""

import argparse
Expand Down Expand Up @@ -291,6 +292,40 @@ def transcribe_file_with_word_level_confidence():
# [END speech_transcribe_word_level_confidence_beta]


def transcribe_file_with_spoken_punctuation_end_emojis():
"""Transcribe the given audio file with spoken punctuation and emojis enabled."""
# [START speech_transcribe_spoken_punctuation_emojis_beta]
from google.cloud import speech_v1p1beta1 as speech
from google.protobuf import wrappers_pb2

client = speech.SpeechClient()

speech_file = "resources/commercial_mono.wav"

with io.open(speech_file, "rb") as audio_file:
content = audio_file.read()

audio = speech.RecognitionAudio(content=content)
config = speech.RecognitionConfig(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we pass these kinds of configuration details into the function as parameters (with sane defaults) like this, but it's totally not a big deal.

I'm very impressed.

encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=8000,
language_code="en-US",
# Enable spoken punctuation
enable_spoken_punctuation=wrappers_pb2.BoolValue(value=True),
# Enable spoken emojis
enable_spoken_emojis=wrappers_pb2.BoolValue(value=True),
)

response = client.recognize(config=config, audio=audio)

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


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
Expand All @@ -313,3 +348,5 @@ def transcribe_file_with_word_level_confidence():
transcribe_file_with_multilanguage()
elif args.command == "word-level-conf":
transcribe_file_with_word_level_confidence()
elif args.command == "spoken-punctuation-emojis":
transcribe_file_with_spoken_punctuation_end_emojis()
8 changes: 8 additions & 0 deletions samples/snippets/beta_snippets_test.py
Expand Up @@ -21,6 +21,7 @@
transcribe_file_with_multichannel,
transcribe_file_with_multilanguage,
transcribe_file_with_word_level_confidence,
transcribe_file_with_spoken_punctuation_end_emojis,
)

RESOURCES = os.path.join(os.path.dirname(__file__), "resources")
Expand Down Expand Up @@ -74,3 +75,10 @@ def test_transcribe_word_level_confidence(capsys):
out, err = capsys.readouterr()

assert "OK Google stream stranger things from Netflix to my TV" in out


def transcribe_file_with_spoken_punctuation_end_emojis(capsys):
parthea marked this conversation as resolved.
Show resolved Hide resolved
transcribe_file_with_spoken_punctuation_end_emojis()
out, err = capsys.readouterr()

assert "First alternative of result " in out