natural language toolkit carnegie mellon pronouncing dictionary corpus reader c 20012023 nltk project steven bird stevenbird1gmail com url https www nltk org for license information see license txt the carnegie mellon pronouncing dictionary cmudict 0 6 ftp ftp cs cmu eduprojectspeechdict 1998 carnegie mellon university file format each line consists of an uppercased word a counter for alternative pronunciations and a transcription vowels are marked for stress 1primary 2secondary 0no stress e g natural 1 n ae1 ch er0 ah0 l the dictionary contains 127069 entries of these 119400 words are assigned a unique pronunciation 6830 words have two pronunciations and 839 words have three or more pronunciations many of these are fastspeech variants phonemes there are 39 phonemes as shown below phoneme example translation phoneme example translation aa odd aa d ae at ae t ah hut hh ah t ao ought ao t aw cow k aw ay hide hh ay d b be b iy ch cheese ch iy z d dee d iy dh thee dh iy eh ed eh d er hurt hh er t ey ate ey t f fee f iy g green g r iy n hh he hh iy ih it ih t iy eat iy t jh gee jh iy k key k iy l lee l iy m me m iy n knee n iy ng ping p ih ng ow oat ow t oy toy t oy p pee p iy r read r iy d s sea s iy sh she sh iy t tea t iy th theta th ey t ah uh hood hh uh d uw two t uw v vee v iy w we w iy y yield y iy l d z zee z iy zh seizure s iy zh er return the cmudict lexicon as a list of entries containing word transcriptions tuples return a list of all words defined in the cmudict lexicon return the cmudict lexicon as a dictionary whose keys are lowercase words and whose values are lists of pronunciations natural language toolkit carnegie mellon pronouncing dictionary corpus reader c 2001 2023 nltk project steven bird stevenbird1 gmail com url https www nltk org for license information see license txt the carnegie mellon pronouncing dictionary cmudict 0 6 ftp ftp cs cmu edu project speech dict 1998 carnegie mellon university file format each line consists of an uppercased word a counter for alternative pronunciations and a transcription vowels are marked for stress 1 primary 2 secondary 0 no stress e g natural 1 n ae1 ch er0 ah0 l the dictionary contains 127069 entries of these 119400 words are assigned a unique pronunciation 6830 words have two pronunciations and 839 words have three or more pronunciations many of these are fast speech variants phonemes there are 39 phonemes as shown below phoneme example translation phoneme example translation aa odd aa d ae at ae t ah hut hh ah t ao ought ao t aw cow k aw ay hide hh ay d b be b iy ch cheese ch iy z d dee d iy dh thee dh iy eh ed eh d er hurt hh er t ey ate ey t f fee f iy g green g r iy n hh he hh iy ih it ih t iy eat iy t jh gee jh iy k key k iy l lee l iy m me m iy n knee n iy ng ping p ih ng ow oat ow t oy toy t oy p pee p iy r read r iy d s sea s iy sh she sh iy t tea t iy th theta th ey t ah uh hood hh uh d uw two t uw v vee v iy w we w iy y yield y iy l d z zee z iy zh seizure s iy zh er return the cmudict lexicon as a list of entries containing word transcriptions tuples return a list of all words defined in the cmudict lexicon return the cmudict lexicon as a dictionary whose keys are lowercase words and whose values are lists of pronunciations read 100 at a time end of file
from nltk.corpus.reader.api import *
from nltk.corpus.reader.util import *
from nltk.util import Index


class CMUDictCorpusReader(CorpusReader):
    def entries(self):
        
        return concat(
            [
                StreamBackedCorpusView(fileid, read_cmudict_block, encoding=enc)
                for fileid, enc in self.abspaths(None, True)
            ]
        )

    def words(self):
        
        return [word.lower() for (word, _) in self.entries()]

    def dict(self):
        
        return dict(Index(self.entries()))


def read_cmudict_block(stream):
    entries = []
    while len(entries) < 100:  
        line = stream.readline()
        if line == "":
            return entries  
        pieces = line.split()
        entries.append((pieces[0].lower(), pieces[2:]))
    return entries