Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/deepgram/helpers/text_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,17 @@ def replace_phoneme(match):
ssml_text = re.sub(phoneme_pattern, replace_phoneme, ssml_text)

# Handle <break> tags
break_pattern = r'<break\s+time=["\'](\d+(?:\.\d+)?)(ms|s)["\']\s*/>'
break_pattern = r"<break\s+([^<>]*?)\s*/>"

def replace_break(match):
value = float(match.group(1))
unit = match.group(2)
time_match = re.search(
r"(?:^|\s)time\s*=\s*([\"'])(\d+(?:\.\d+)?)(ms|s)\1(?=\s|$)",
match.group(1),
)
if time_match is None:
return match.group(0)
value = float(time_match.group(2))
unit = time_match.group(3)

duration_ms = value * 1000 if unit == "s" else value
if not duration_ms.is_integer():
Expand Down
16 changes: 16 additions & 0 deletions tests/custom/test_text_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,19 @@ def test_standalone_function_workflow(self):
assert '"pronounce": "ˌæzəˈθaɪəpriːn"' in text
assert '"word": "dupilumab"' in text
assert '"pronounce": "duːˈpɪljuːmæb"' in text


@pytest.mark.parametrize(
"markup",
[
'<break time = "500ms"/>',
"<break time='0.5s' strength='medium'/>",
'<break strength="medium" time="500ms"/>',
'<break\nstrength="medium"\ntime = "0.5s" />',
],
)
def test_ssml_break_time_accepts_attribute_spacing_and_order(markup):
ssml = f"<speak>Hello{markup}world</speak>"

assert ssml_to_deepgram(ssml) == r"Hello\{pause:500ms\}world"
assert TextBuilder().from_ssml(ssml).build() == r"Hello\{pause:500ms\}world"