Skip to content

Commit

Permalink
docs(samples): Add Spoken Punctuation and Emojis code samples (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
mquinde authored and dandhlee committed Feb 9, 2023
1 parent 8d548f1 commit badeec7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
37 changes: 37 additions & 0 deletions speech/snippets/beta_snippets.py
Original file line number Diff line number Diff line change
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(
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 speech/snippets/beta_snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
transcribe_file_with_metadata,
transcribe_file_with_multichannel,
transcribe_file_with_multilanguage,
transcribe_file_with_spoken_punctuation_end_emojis,
transcribe_file_with_word_level_confidence,
)

Expand Down Expand Up @@ -75,3 +76,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 test_transcribe_file_with_spoken_punctuation_end_emojis(capsys):
transcribe_file_with_spoken_punctuation_end_emojis()
out, err = capsys.readouterr()

assert "First alternative of result " in out

0 comments on commit badeec7

Please sign in to comment.