Tutorial: Fix How Names Are Pronounced
In this tutorial you build pronounce.py, a short Python script that reads a message aloud and says every name in it the way you want.
The message is: “Hi Saoirse, your Hermes bag has shipped. Niamh from our team will call you tomorrow.” Saoirse is said “Seersha”, Niamh is said “Neev”, and the brand Hermes is said “Air mez”. The voice can’t know that from the spelling.
This is the message before the script fixes it:
And this is what your script makes by the end:
You get there in five steps. You run the script at the end of every step, so you always have something that works:
- Set up a project folder.
- Hear the names said wrong.
- Create a pronunciation dictionary.
- Use the dictionary in your request.
- Add another word to the dictionary.
It takes about 10 minutes. The script uses only the Python standard library, so there is nothing to install.
Before you start
You need:
- Python 3.9 or later. Check with
python3 --version. - A Smallest AI API key. If you don’t have one yet, follow Step 1 of the Quickstart, then come back.
- About 1,000 characters of TTS credit. That’s all the text the tutorial sends, from start to finish.
Step 1: Set up a project folder
Make a folder for the tutorial and move into it:
Export your API key in the same terminal. The script reads it from there:
You are in an empty pronounce-tutorial folder, and SMALLEST_API_KEY is exported in this terminal.
Step 2: Hear the names said wrong
Start with the problem. Turn the message into audio the normal way, with no dictionary.
Create a file called pronounce.py in the folder and paste in this code:
Run it:
You should see one line:
Open before.wav and listen. The voice meher reads the names the way they are spelled, so Saoirse doesn’t sound like “Seersha” and Niamh doesn’t sound like “Neev”.
before.wav plays the message, and the two names sound wrong.
api_request sends one request to the API and returns what comes back. synthesize uses it to turn text into a WAV file. You’ll use both in every step from here on.
Got an error instead?
Step 3: Create a pronunciation dictionary
A pronunciation dictionary is a list of words, each with how it should sound. You create it once, and the API gives you back an ID. You use that ID in your requests.
Write each pronunciation the way you’d spell it for a friend, in plain letters. Don’t use phonetic symbols.
Replace everything in pronounce.py with this code. The highlighted lines are new:
What changed:
WORDSlists the two names and how to say them.- A new
create_dictionaryfunction sendsWORDSto the API and returns the new dictionary’s ID. - The bottom of the file saves the ID in
dictionary_id.txt. On the next run, the script reads the ID from that file instead of creating a second dictionary.
Run it:
You should see two lines. Your dictionary ID will be different:
Run it once more:
This time the script finds dictionary_id.txt and uses the same dictionary:
Your folder has a dictionary_id.txt file with one ID in it, and the second run said Using dictionary.
Step 4: Use the dictionary in your request
Creating a dictionary doesn’t change anything by itself. You turn it on for a request by sending its ID in the pronunciation_dicts field.
Replace everything in pronounce.py with this code. The highlighted lines are new:
What changed:
synthesizetakes a new, optionaldictionary_id. When you pass one, it goes into the request aspronunciation_dicts.- The last line makes a second file,
after.wav, with the dictionary turned on.
Run it:
You should see three lines:
Play before.wav, then after.wav. In after.wav, Saoirse sounds like “Seersha” and Niamh sounds like “Neev”. Hermes still sounds like the English word, because it isn’t in the dictionary yet.
In after.wav, both names sound right. Hermes still sounds wrong.
A word only matches when the capitals match, too. Saoirse in the dictionary doesn’t fix saoirse or SAOIRSE in the text. If your text can use different capitals, add each form as its own word.
Step 5: Add another word to the dictionary
Now fix Hermes. You change the dictionary you already have, instead of creating a new one.
When you update a dictionary, the API replaces all its words with the list you send. A word you leave out of the update is removed. That’s why the script keeps every word in WORDS and always sends the whole list.
Replace everything in pronounce.py with this code. The highlighted lines are new:
What changed:
- Hermes is now in
WORDS. - A new
update_dictionaryfunction sends the fullWORDSlist to the existing dictionary. - When
dictionary_id.txtexists, the script now updates the dictionary instead of only reading the ID.
Run it:
You should see four lines:
Play after.wav. All three words now sound the way you want. It should sound like the second sample at the top of this page.
after.wav says “Seersha”, “Air mez” and “Neev”. The update line says the dictionary now has 3 words.
Got an error in Step 3, 4 or 5?
Your folder now looks like this:
Clean up
The dictionary stays in your account until you delete it. If you don’t need it any more, delete it with the ID from dictionary_id.txt:
The response ends with "deleted":true. Then delete dictionary_id.txt.
If you send the ID of a deleted dictionary in pronunciation_dicts, the request still works but the dictionary does nothing. The words go back to the default way of saying them, with no error.
What you built
You have a script that says names and brands the way you want. Along the way you:
- Heard how the voice says words it doesn’t know.
- Created a pronunciation dictionary and kept its ID to use again.
- Turned the dictionary on for a request with
pronunciation_dicts. - Updated the dictionary by sending its full list of words.
Try it with your own words: put the names your customers have in WORDS, change MESSAGE, and run python3 pronounce.py again.