Pronouncing API Reference

pronouncing.init_cmu(filehandle=None)[source]

Initialize the module’s pronunciation data.

This function is called automatically the first time you attempt to use another function in the library that requires loading the pronunciation data from disk. You can call this function manually to control when and how the pronunciation data is loaded (e.g., you’re using this module in a web application and want to load the data asynchronously).

Parameters:filehandle – a filehandle with CMUdict-formatted data
Returns:None
pronouncing.parse_cmu(cmufh)[source]

Parses an incoming file handle as a CMU pronouncing dictionary file.

(Most end-users of this module won’t need to call this function explicitly, as it’s called internally by the init_cmu() function.)

Parameters:cmufh – a filehandle with CMUdict-formatted data
Returns:a list of 2-tuples pairing a word with its phones (as a string)
pronouncing.phones_for_word(find)[source]

Get the CMUdict phones for a given word.

Because a given word might have more than one pronunciation in the dictionary, this function returns a list of all possible pronunciations.

>>> import pronouncing
>>> pronouncing.phones_for_word("permit")
['P ER0 M IH1 T', 'P ER1 M IH2 T']
Parameters:find – a word to find in CMUdict.
Returns:a list of phone strings that correspond to that word.
pronouncing.rhymes(word)[source]

Get words rhyming with a given word.

This function may return an empty list if no rhyming words are found in the dictionary, or if the word you pass to the function is itself not found in the dictionary.

>>> import pronouncing
>>> pronouncing.rhymes("conditioner")
['commissioner', 'parishioner', 'petitioner', 'practitioner']
Parameters:word – a word
Returns:a list of rhyming words
pronouncing.rhyming_part(phones)[source]

Get the “rhyming part” of a string with CMUdict phones.

“Rhyming part” here means everything from the vowel in the stressed syllable nearest the end of the word up to the end of the word.

>>> import pronouncing
>>> phones = pronouncing.phones_for_word("purple")
>>> pronouncing.rhyming_part(phones[0])
'ER1 P AH0 L'
Parameters:phones – a string containing space-separated CMUdict phones
Returns:a string with just the “rhyming part” of those phones
pronouncing.search(pattern)[source]

Get words whose pronunciation matches a regular expression.

This function Searches the CMU dictionary for pronunciations matching a given regular expression. (Word boundary anchors are automatically added before and after the pattern.)

>>> import pronouncing
>>> 'interpolate' in pronouncing.search('ER1 P AH0')
True
Parameters:pattern – a string containing a regular expression
Returns:a list of matching words
pronouncing.search_stresses(pattern)[source]

Get words whose stress pattern matches a regular expression.

This function is a special case of search() that searches only the stress patterns of each pronunciation in the dictionary. You can get stress patterns for a word using the stresses_for_word() function.

>>> import pronouncing
>>> pronouncing.search_stresses('020120')
['gubernatorial']
Parameters:pattern – a string containing a regular expression
Returns:a list of matching words
pronouncing.stresses(s)[source]

Get the vowel stresses for a given string of CMUdict phones.

Returns only the vowel stresses (i.e., digits) for a given phone string.

>>> import pronouncing
>>> pronouncing.stresses(pronouncing.phones_for_word('obsequious')[0])
'0100'
Parameters:s – a string of CMUdict phones
Returns:string of just the stresses
pronouncing.stresses_for_word(find)[source]

Get a list of possible stress patterns for a given word.

>>> import pronouncing
>>> pronouncing.stresses_for_word('permit')
['01', '12']
Parameters:find – a word to find
Returns:a list of possible stress patterns for the given word.
pronouncing.syllable_count(phones)[source]

Count the number of syllables in a string of phones.

To find the number of syllables in a word, call phones_for_word() first to get the CMUdict phones for that word.

>>> import pronouncing
>>> phones = pronouncing.phones_for_word("literally")
>>> pronouncing.syllable_count(phones[0])
4
Parameters:phones – a string containing space-separated CMUdict phones
Returns:integer count of syllables in list of phones