ó <¿CVc@sÞdZddlmZmZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl mZddlTddlTdefd „ƒYZe jd efd „ƒYƒZd „ZdS( uÿ Read tokens, phonemes and audio data from the NLTK TIMIT Corpus. This corpus contains selected portion of the TIMIT corpus. - 16 speakers from 8 dialect regions - 1 male and 1 female from each dialect region - total 130 sentences (10 sentences per speaker. Note that some sentences are shared among other speakers, especially sa1 and sa2 are spoken by all speakers.) - total 160 recording of sentences (10 recordings per speaker) - audio format: NIST Sphere, single channel, 16kHz sampling, 16 bit sample, PCM encoding Module contents =============== The timit corpus reader provides 4 functions and 4 data items. - utterances List of utterances in the corpus. There are total 160 utterances, each of which corresponds to a unique utterance of a speaker. Here's an example of an utterance identifier in the list:: dr1-fvmh0/sx206 - _---- _--- | | | | | | | | | | | | | | `--- sentence number | | | `----- sentence type (a:all, i:shared, x:exclusive) | | `--------- speaker ID | `------------ sex (m:male, f:female) `-------------- dialect region (1..8) - speakers List of speaker IDs. An example of speaker ID:: dr1-fvmh0 Note that if you split an item ID with colon and take the first element of the result, you will get a speaker ID. >>> itemid = 'dr1-fvmh0/sx206' >>> spkrid , sentid = itemid.split('/') >>> spkrid 'dr1-fvmh0' The second element of the result is a sentence ID. - dictionary() Phonetic dictionary of words contained in this corpus. This is a Python dictionary from words to phoneme lists. - spkrinfo() Speaker information table. It's a Python dictionary from speaker IDs to records of 10 fields. Speaker IDs the same as the ones in timie.speakers. Each record is a dictionary from field names to values, and the fields are as follows:: id speaker ID as defined in the original TIMIT speaker info table sex speaker gender (M:male, F:female) dr speaker dialect region (1:new england, 2:northern, 3:north midland, 4:south midland, 5:southern, 6:new york city, 7:western, 8:army brat (moved around)) use corpus type (TRN:training, TST:test) in this sample corpus only TRN is available recdate recording date birthdate speaker birth date ht speaker height race speaker race (WHT:white, BLK:black, AMR:american indian, SPN:spanish-american, ORN:oriental,???:unknown) edu speaker education level (HS:high school, AS:associate degree, BS:bachelor's degree (BS or BA), MS:master's degree (MS or MA), PHD:doctorate degree (PhD,JD,MD), ??:unknown) comments comments by the recorder The 4 functions are as follows. - tokenized(sentences=items, offset=False) Given a list of items, returns an iterator of a list of word lists, each of which corresponds to an item (sentence). If offset is set to True, each element of the word list is a tuple of word(string), start offset and end offset, where offset is represented as a number of 16kHz samples. - phonetic(sentences=items, offset=False) Given a list of items, returns an iterator of a list of phoneme lists, each of which corresponds to an item (sentence). If offset is set to True, each element of the phoneme list is a tuple of word(string), start offset and end offset, where offset is represented as a number of 16kHz samples. - audiodata(item, start=0, end=None) Given an item, returns a chunk of audio samples formatted into a string. When the fuction is called, if start and end are omitted, the entire samples of the recording will be returned. If only end is omitted, samples from the start offset to the end of the recording will be returned. - play(data) Play the given audio samples. The audio samples can be obtained from the timit.audiodata function. iÿÿÿÿ(tprint_functiontunicode_literalsN(tcompat(tTree(timport_from_stdlib(t*tTimitCorpusReadercBseZdZddZdZdd„Zdd„Zdddddd„Zd„Z d „Z d „Z d „Z d „Z d „Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zddd„Zddd„Zd„Zddd„ZRS(u– Reader for the TIMIT corpus (or any other corpus with the same file layout and use of file formats). The corpus root directory should contain the following files: - timitdic.txt: dictionary of standard transcriptions - spkrinfo.txt: table of speaker information In addition, the root directory should contain one subdirectory for each speaker, containing three files for each utterance: - .txt: text content of utterances - .wrd: tokenized text content of utterances - .phn: phonetic transcription of utterances - .wav: utterance sound file u!(\w+-\w+/\w+\.(phn|txt|wav|wrd))|utimitdic\.txt|spkrinfo\.txtu\w+-\w+/\w+\.txtuutf8cCs°t|tjƒr'dd|fg}ntj||t||jƒd|ƒgt||jƒD]}|d ^q_|_ d|_ ||_ t t d„|j Dƒƒƒ|_dS(u† Construct a new TIMIT corpus reader in the given directory. :param root: The root directory for this corpus. u.*\.wavu.*tencodingiüÿÿÿcss"|]}|jdƒdVqdS(u/iN(tsplit(t.0tu((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pys ³sN(u.*\.wavN(t isinstanceRt string_typestNonet CorpusReadert__init__tfind_corpus_fileidst_FILE_REt _UTTERANCE_REt _utterancest _speakerinfot_roottsortedtsettspeakers(tselftrootRtname((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyRŸs )  cCss|d krtj|ƒS|d krIg|jD]}d||f^q/S|dkr_ddgStd |ƒ‚d S( uW Return a list of file identifiers for the files that make up this corpus. :param filetype: If specified, then ``filetype`` indicates that only the files that have the given type should be returned. Accepted values are: ``txt``, ``wrd``, ``phn``, ``wav``, or ``metadata``, utxtuwrduphnuwavu%s.%sumetadatau timitdic.txtu spkrinfo.txtuBad value for filetype: %rN(utxtuwrduphnuwav(R RtfileidsRt ValueError(RtfiletypeR ((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyRµs   $  cCs¼t|tjƒr|g}nt|tjƒr<|g}nt|tjƒrZ|g}nt|tjƒrx|g}nt|tjƒr–|g}n|j}|dk rØg|D]}|d|kr³|^q³}n|dk rg|D]}|d|krë|^që}n|dk rHg|D]}|d |kr#|^q#}n|dk r€g|D]}|d|kr[|^q[}n|dk r¸g|D]}|d|kr“|^q“}n|S(uÜ :return: A list of the utterance identifiers for all utterances in this corpus, or for the given speaker, dialect region, gender, sentence type, or sentence number, if specified. iii i i N(R RR RR (Rtdialecttsextspkridt sent_typetsentidt utterancesR ((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyt utteranceidsÈs,       , , , , ,cCs”i}x‡|jdƒD]v}|jƒ s|ddkr?qntjd|ƒ}|sjtd|ƒ‚n|jdƒjƒ||jdƒ}g|j|ƒD]"}|jƒr)|jƒd^q)^qS(Nu.wrdiÿÿÿÿ(R8R&R'R(RR$R9R,((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pytsents)scCs„g|j|dƒD]m}|j|ƒD]W}|jƒr&|jddƒdjƒt|jƒdƒt|jƒdƒf^q&qS(Nu.txtiiÿÿÿÿii(R8R&R'RR R;(RR$R9R,((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyt sent_times.sc CsÛ|dkr|j}nt|tjƒr6|g}ng}x˜|D]}|j|ƒ}|j|ƒ}|j|ƒ}xZ|rÒ|jdƒ\}}} |j t dgƒƒxJ|rù|rù|dd|ddkrù|dj |jdƒdƒq°Wx“|r|dd| kr|jdƒ\} } } |dj t | gƒƒx@|r‹|dd| kr‹|ddj |jdƒdƒqLWqýWx<|rÎ|dd| krÎ|dj |jdƒdƒq“WqyWqCW|S(NiuSiiiÿÿÿÿ( R RR RR R>R<R@tpoptappendR( RR$ttreesR/R>R<R@tsentt sent_starttsent_endtwordt word_starttword_end((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyt phone_trees4s.    "**ic CsÅtdƒ}|j|j|dƒdƒ}|dkrF|jƒ}n|j|ƒ|j||ƒ}tjƒ}|j|dƒ}|j|jƒƒ|j |ƒ|j ƒ|j dƒ|j ƒS(Nuwaveu.wavurbuwi( RR&R t getnframest readframesttempfilet TemporaryFilet setparamst getparamst writeframestclosetseektread( RR/tstarttendtwavetwtframesttftout((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pytwavPs       cCs€|dks||kst‚d}|dkrL|j|dƒjƒ}n$|j|dƒj||dƒ}|||dS(Ni,u.wavi(R tAssertionErrorR&RT(RR/RURVt headersizetdata((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyt audiodatals  $cCsW|dkr|j}nt|tjƒr6|g}ng|D]}d||f^q=S(Nu%s%s(R RR RR (RR$t extensionR ((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyR8us   c Cs‚y¼ddl}yc|jdƒ}|j|jƒ|jdƒ|jdƒ|j|j|||ƒƒ|jƒWnBt k r¶}t ddt j ƒt dt |ƒdt j ƒnXdSWntk rÏnXy‡ddl}ddl}|jjdƒ|j|j|||ƒƒ} |jj| ƒjƒx |jjƒrQtjd ƒq2WdSWntk rjnXt d dt j ƒdS( up Play the given audio sample. :param utterance: The utterance id of the sample to play iÿÿÿÿNuwii€>uBcan't acquire the audio device; please activate your audio device.tfileusystem error message:g{®Gáz„?u:you must install pygame or ossaudiodev for audio playback.(t ossaudiodevR&tsetfmtt AFMT_S16_LEtchannelstspeedtwriteR`RRtIOErrortprinttsyststderrtstrt ImportErrort pygame.mixertStringIOtmixertinitR\tSoundtplaytget_busyttimetsleep( RR/RURVRctdsptetpygameRptf((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyRtzs8         N(t__name__t __module__t__doc__RRRR RR%R.R!R#R/R2R7R:R<R=R>R?R@RJR\R`R8Rt(((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyRˆs2                R4cBseZdd„Zd„ZRS(c Cs^||_||_||_||_||_||_||_||_| |_| |_ dS(N( tidR tdrtusetrecdatet birthdatethttracetedutcomments( RRR R€RR‚RƒR„R…R†R‡((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyR¦s         cCsId}g|jƒD]}d|t||ƒf^q}ddj|ƒS(Nu4id sex dr use recdate birthdate ht race edu commentsu%s=%ruSpeakerInfo(%s)u, (Rtgetattrtjoin(Rtattribstattrtargs((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyt__repr__³s/N(R|R}R RR(((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pyR4¤s cCs5|jƒ}|sgS|jddƒ\}}|gS(ut Block reader for timit tagged sentences, which are preceded by a sentence number that will be ignored. u i(treadlineR(tstreamR,tnRD((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pytread_timit_blockºs  (R~t __future__RRRktosR(RMRvtnltkRt nltk.treeRtnltk.internalsRtnltk.corpus.reader.utiltnltk.corpus.reader.apiRRtpython_2_unicode_compatibletobjectR4R‘(((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/timit.pytxs        ÿ