Skip to content

Commit 063bb82

Browse files
author
Elizabeth Crowdus
committed
adding ssml functionality
1 parent f5bfaa9 commit 063bb82

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import io
2424
import os
25+
import html
2526
# [END translate_hybrid_imports]
2627

2728
# [START translate_hybrid_project_id]
@@ -165,7 +166,8 @@ def translate_text(text, source_language_code, target_language_code,
165166

166167
# [START translate_hybrid_tts]
167168
def text_to_speech(text, outfile):
168-
"""Generates synthetic audio from plaintext
169+
"""Converts plaintext to SSML and
170+
generates synthetic audio from SSML
169171
170172
ARGS
171173
text: text to synthesize
@@ -175,11 +177,22 @@ def text_to_speech(text, outfile):
175177
nothing
176178
"""
177179

180+
# Replace special characters with HTML Ampersand Character Codes
181+
# These Codes prevent the API from confusing text with
182+
# SSML commands
183+
# For example, '<' --> '&lt;' and '&' --> '&amp;'
184+
escaped_lines = html.escape(text)
185+
186+
# Convert plaintext to SSML in order to wait two seconds
187+
# between each line in synthetic speech
188+
ssml = '<speak>{}</speak>'.format(
189+
escaped_lines.replace('\n', '\n<break time="2s"/>'))
190+
178191
# Instantiates a client
179192
client = texttospeech.TextToSpeechClient()
180193

181194
# Sets the text input to be synthesized
182-
synthesis_input = texttospeech.types.SynthesisInput(text=text)
195+
synthesis_input = texttospeech.types.SynthesisInput(ssml=ssml)
183196

184197
# Builds the voice request, selects the language code ("en-US") and
185198
# the SSML voice gender ("MALE")
Binary file not shown.

0 commit comments

Comments
 (0)