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
6 changes: 4 additions & 2 deletions src/deepgram/helpers/text_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
and speed controls for Deepgram's Text-to-Speech API.
"""

import html
import json
import re
from typing import Tuple
Expand Down Expand Up @@ -301,12 +302,12 @@ def ssml_to_deepgram(ssml_text: str) -> str:

def replace_phoneme(match):
attributes = match.group(1)
word = match.group(2)
word = html.unescape(match.group(2))
alphabet_match = re.search(r'(?:^|\s)alphabet\s*=\s*(["\'])ipa\1(?=\s|$)', attributes)
ph_match = re.search(r'(?:^|\s)ph\s*=\s*(["\'])(.*?)\1(?=\s|$)', attributes)
if alphabet_match is None or ph_match is None:
return word
ipa = ph_match.group(2)
ipa = html.unescape(ph_match.group(2))
return _pronunciation_control(word, ipa)

ssml_text = re.sub(phoneme_pattern, replace_phoneme, ssml_text)
Expand Down Expand Up @@ -345,6 +346,7 @@ def replace_break(match):

# Remove any remaining XML tags
ssml_text = re.sub(r"<[^>]+>", "", ssml_text)
ssml_text = html.unescape(ssml_text)

ssml_text = ssml_text.strip()
pronunciation_count, pause_count = _control_counts(ssml_text)
Expand Down
10 changes: 10 additions & 0 deletions tests/custom/test_client_construction_and_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,13 @@ def test_add_pronunciation_and_ssml() -> None:
def test_text_builder_from_ssml_updates_counts() -> None:
builder = TextBuilder().from_ssml('Hi <break time="500ms"/> there')
assert r"\{pause:500ms\}" in builder.build()


def test_ssml_to_deepgram_decodes_xml_entities() -> None:
result = ssml_to_deepgram("<speak>Call AT&amp;T, it&apos;s open &quot;now&quot;.</speak>")
assert result == 'Call AT&T, it\'s open "now".'


def test_ssml_to_deepgram_decodes_phoneme_entities() -> None:
result = ssml_to_deepgram('<phoneme alphabet="ipa" ph="t&#601;">AT&amp;T</phoneme>')
assert result == r'\{"word": "AT&T", "pronounce": "tə"\}'