diff --git a/src/deepgram/helpers/text_builder.py b/src/deepgram/helpers/text_builder.py index b859b217..3d766414 100644 --- a/src/deepgram/helpers/text_builder.py +++ b/src/deepgram/helpers/text_builder.py @@ -239,12 +239,18 @@ def ssml_to_deepgram(ssml_text: str) -> str: # Parse XML fragments manually to handle mixed content # Use regex to find and replace SSML elements - # Handle tags - phoneme_pattern = r'(.*?)' + # Handle tags. Attribute order is not significant in SSML, so match + # the attributes as a group and pull `ph` out of it rather than requiring + # `alphabet` before `ph` (which silently dropped the pronunciation otherwise). + phoneme_pattern = r"]*?)\s*>(.*?)" def replace_phoneme(match): - ipa = match.group(1) + attributes = match.group(1) word = match.group(2) + ph_match = re.search(r'ph=["\'](.*?)["\']', attributes) + if ph_match is None: + return word + ipa = ph_match.group(1) return json.dumps({"word": word, "pronounce": ipa}, ensure_ascii=False) ssml_text = re.sub(phoneme_pattern, replace_phoneme, ssml_text) diff --git a/tests/custom/test_text_builder.py b/tests/custom/test_text_builder.py index 77a7ed1b..45ca7009 100644 --- a/tests/custom/test_text_builder.py +++ b/tests/custom/test_text_builder.py @@ -234,10 +234,18 @@ def test_basic_phoneme(self): """Test converting basic phoneme tag""" ssml = 'azathioprine' result = ssml_to_deepgram(ssml) - + assert '"word": "azathioprine"' in result assert '"pronounce": "ˌæzəˈθaɪəpriːn"' in result - + + def test_phoneme_attribute_order_independent(self): + """ph before alphabet must work too (SSML attribute order is not significant)""" + ssml = 'azathioprine' + result = ssml_to_deepgram(ssml) + + assert '"word": "azathioprine"' in result + assert '"pronounce": "ˌæzəˈθaɪəpriːn"' in result + def test_basic_break(self): """Test converting break tag (milliseconds)""" ssml = ''