Converting text to the International Phonetic Alphabet (IPA) is essential for text-to-speech engines, language learning applications, pronunciation guides, and linguistic analysis.
This guide covers Python libraries for phonetic conversion, handling edge cases, and working with pronunciation variants.
Using eng-to-ipa (Simple Approach)
The eng-to-ipa library provides straightforward conversion using the CMU Pronouncing Dictionary:
pip install eng-to-ipaimport eng_to_ipa as ipa
sentence = "The quick brown fox jumps over the lazy dog"
phonetic = ipa.convert(sentence)
print(phonetic)Word-by-Word Conversion
import eng_to_ipa as ipa
words = ["hello", "world", "python", "programming"]
for word in words:
phonetic = ipa.convert(word)
print(f"{word}: {phonetic}")Output:
hello: hɛˈloʊ
world: wərld
python: ˈpaɪθɑn
programming: ˈproʊˌgræmɪŋHandling Multiple Pronunciations
Many English words have variant pronunciations. Use ipa_list() to get all possibilities:
import eng_to_ipa as ipa
words_with_variants = ["tomato", "either", "route", "caramel"]
for word in words_with_variants:
variants = ipa.ipa_list(word)
print(f"{word}: {variants}")Output:
tomato: [['təˈmeɪˌtoʊ', 'təˈmɑˌtoʊ']]
either: [['ˈaɪðər', 'ˈiðər']]
route: [['raʊt', 'rut']]
caramel: [['ˈkɛrəməl']]Homographs (Same Spelling, Different Pronunciation)
import eng_to_ipa as ipa
homographs = ["lead", "read", "live", "wind"]
for word in homographs:
variants = ipa.ipa_list(word)
print(f"{word}: {variants}")Output:
lead: [['lid', 'lɛd']]
read: [['rid', 'rɛd']]
live: [['laɪv', 'lɪv']]
wind: [['waɪnd', 'wɪnd']]Finding Rhymes
The phonetic data enables programmatic rhyme detection:
import eng_to_ipa as ipa
def find_rhymes(word, limit=10):
"""Find words that rhyme with the given word."""
rhymes = ipa.get_rhymes(word)
return rhymes[:limit] if rhymes else []
print(f"Rhymes with 'code': {find_rhymes('code')}")
print(f"Rhymes with 'python': {find_rhymes('python')}")Output:
Rhymes with 'code': ['abode', 'annaud', 'bestowed', 'bestrode', 'blowed', 'bode', 'bowed', 'brode', 'busload', 'c-code']
Rhymes with 'python': []Handling Unknown Words
Words not in the CMU dictionary are marked with an asterisk:
import eng_to_ipa as ipa
test_words = ["OpenAI", "ChatGPT", "TikTok", "hello"]
for word in test_words:
result = ipa.convert(word)
if '*' in result:
print(f"{word}: NOT FOUND (returned: {result})")
else:
print(f"{word}: {result}")Output:
OpenAI: NOT FOUND (returned: openai*)
ChatGPT: NOT FOUND (returned: chatgpt*)
TikTok: NOT FOUND (returned: tiktok*)
hello: hɛˈloʊFiltering Unknown Words
import eng_to_ipa as ipa
def convert_with_fallback(text, fallback="[?]"):
"""Convert text, replacing unknown words with fallback."""
words = text.split()
results = []
for word in words:
converted = ipa.convert(word)
if '*' in converted:
results.append(fallback)
else:
results.append(converted)
return ' '.join(results)
text = "Hello OpenAI world"
print(convert_with_fallback(text))Output:
hɛˈloʊ [?] wərldUsing phonemizer (Neural TTS Quality)
For production TTS systems, phonemizer offers high-quality conversion:
pip install phonemizerfrom phonemizer import phonemize
text = "Hello, how are you today?"
phonetic = phonemize(
text,
language='en-us',
backend='espeak',
strip=True
)
print(phonetic)Building a Pronunciation Dictionary
import eng_to_ipa as ipa
import json
def build_pronunciation_dict(words):
"""Build a pronunciation dictionary for a word list."""
dictionary = {}
unknown = []
for word in words:
word_lower = word.lower()
converted = ipa.convert(word_lower)
if '*' in converted:
unknown.append(word)
else:
dictionary[word_lower] = converted
return dictionary, unknown
vocabulary = ["apple", "banana", "computer", "ChatGPT", "python"]
pron_dict, failures = build_pronunciation_dict(vocabulary)
print("Pronunciation Dictionary:")
print(json.dumps(pron_dict, indent=2, ensure_ascii=False))
print(f"\nUnknown words: {failures}")Output:
Pronunciation Dictionary:
{
"apple": "ˈæpəl",
"banana": "bəˈnænə",
"computer": "kəmˈpjutər",
"python": "ˈpaɪθɑn"
}
Unknown words: ['ChatGPT']Practical Example: Pronunciation Guide Generator
import eng_to_ipa as ipa
def generate_pronunciation_guide(text):
"""Generate a pronunciation guide for text."""
words = text.split()
guide = []
for word in words:
clean_word = ''.join(c for c in word if c.isalpha())
if not clean_word:
continue
phonetic = ipa.convert(clean_word.lower())
if '*' not in phonetic:
guide.append({
'word': clean_word,
'ipa': phonetic,
'syllables': phonetic.count('ˈ') + phonetic.count('ˌ') + 1
})
return guide
text = "The pronunciation of these words varies significantly"
guide = generate_pronunciation_guide(text)
for entry in guide:
print(f"{entry['word']}: /{entry['ipa']}/ ({entry['syllables']} syllables)")Output:
The: /ðə/ (1 syllables)
pronunciation: /prəˌnənsiˈeɪʃən/ (3 syllables)
of: /əv/ (1 syllables)
these: /ðiz/ (1 syllables)
words: /wərdz/ (1 syllables)
varies: /ˈvɛriz/ (2 syllables)
significantly: /sɪgˈnɪfɪkəntli/ (2 syllables)Summary
| Library | Best For | Handles Unknown |
|---|---|---|
| eng-to-ipa | Quick conversion, rhyme finding | No (marks with *) |
| epitran | Rule-based G2P, multiple languages | Yes (approximates) |
| phonemizer | TTS systems, high accuracy | Yes (with espeak) |