Skip to content
Open
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: 10 additions & 2 deletions src/deepgram/helpers/text_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import json
import html
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 @@ -346,6 +347,13 @@ def replace_break(match):
# Remove any remaining XML tags
ssml_text = re.sub(r"<[^>]+>", "", ssml_text)

# Decode XML entities in plain text segments without altering inline controls
control_split = re.compile(r'(\\\{(?:\s*"word"\s*:.*?|pause:\d+ms)\\\})')
parts = control_split.split(ssml_text)
ssml_text = "".join(
part if i % 2 == 1 else html.unescape(part) for i, part in enumerate(parts)
)

ssml_text = ssml_text.strip()
pronunciation_count, pause_count = _control_counts(ssml_text)
if pronunciation_count > 500:
Expand Down