How to Convert English Text to IPA Phonetics in Python
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-ipa
import eng_to_ipa as ipa
sentence = "The quick brown fox jumps over the lazy dog"
# Basic conversion
phonetic = ipa.convert(sentence)
print(phonetic)
# Output: ðə kwɪk braʊn fɑks ʤəmps ˈoʊvər ðə ˈleɪzi dɔg
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 multiple pronunciations
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
# "lead" as metal vs. verb
# "read" present vs. past tense
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']]
eng-to-ipa cannot determine which pronunciation is correct based on context. For context-aware conversion, you'll need more advanced NLP tools or manual selection.
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 []
# Examples
print(f"Rhymes with 'code': {find_rhymes('code')}")
# ['abode', 'bestrode', 'commode', 'corrode', 'decode', ...]
print(f"Rhymes with 'python': {find_rhymes('python')}")
# Results vary based on dictionary coverage
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
# Brand names and neologisms often fail
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ərld
Using phonemizer (Neural TTS Quality)
For production TTS systems, phonemizer offers high-quality conversion:
pip install phonemizer
from phonemizer import phonemize
text = "Hello, how are you today?"
# Using espeak backend
phonetic = phonemize(
text,
language='en-us',
backend='espeak',
strip=True
)
print(phonetic)
phonemizer requires espeak-ng installed on your system:
- Ubuntu:
sudo apt-get install espeak-ng - macOS:
brew install espeak-ng - Windows: Download from espeak-ng releases
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
# Example usage
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 punctuation
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) |
Use eng-to-ipa for simple applications with standard vocabulary. For production TTS or handling proper nouns and neologisms, combine it with phonemizer or a custom grapheme-to-phoneme model for unknown words.