How to add, view, and remove rules to pronunciation dictionaries with the Python SDK
nginx
could be pronounced incorrectly. Instead, we can add our version of pronunciation. Based on IPA, nginx
is pronounced as /ˈɛndʒɪnˈɛks/
. Finding IPA or CMU of words manually can be difficult. Instead, LLMs like ChatGPT can help you to make the search easier.
We’ll start by adding rules to the pronunciation dictionary from a file and comparing the text-to-speech results that use and do not use the dictionary. After that, we’ll discuss how to add and remove specific rules to existing dictionaries.
If you want to jump straight to the finished repo you can find it here
eleven_turbo_v2
and eleven_monolingual_v1
. If you use phoneme tags with
other models, they will silently skip the word.python-dotenv
to manage your environmental variables:
.env
file in your project directory and fill it with your credentials like so:
.pls
file for our rules.
This rule will use the “IPA” alphabet and update the pronunciation for tomato
and Tomato
with a different pronunciation. PLS files are case sensitive which is why we include it both with and without a capital “T”. Save it as dictionary.pls
.
remove_rules_from_the_pronunciation_dictionary
method in the pronunciation dictionary module. In the following snippet, we start by removing rules based on the rule string and get the updated result. Finally, we generate and play another text-to-speech audio to test the difference. In the example, we take pronunciation dictionary version id from remove_rules_from_the_pronunciation_dictionary
response because every changes to pronunciation dictionary will create a new version, so we need to use the latest version returned from the response. The old version also still available.
PronunciationDictionaryRule_Phoneme
class and call add_rules_to_the_pronunciation_dictionary
from the pronunciation dictionary. The snippet will demonstrate adding rules with the class and get the updated result. Finally, we generate and play another text-to-speech audio to test the difference. This example also use pronunciation dictionary version returned from add_rules_to_the_pronunciation_dictionary
to ensure we use the latest dictionary version.
env.example
: Template for your environment variables.main.py
: The complete code for snippets above.dictionary.pls
: Custom dictionary example with XML format.requirements.txt
: List of python package used for this example.