
    g+F                     j    d Z ddlZddlZddl ddlmZ ddlmZ  G d de      Z	 G d d	      Z
d
 Zy)a  
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 function 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.

    N)*)import_from_stdlib)Treec                       e Zd ZdZdZ	 dZddZddZ	 ddZd Z	d	 Z
d
 Zd Zd Zd ZddZddZddZddZddZddZddZddZddZd ZddZy)TimitCorpusReadera  
    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:

      - <utterance-id>.txt: text content of utterances
      - <utterance-id>.wrd: tokenized text content of utterances
      - <utterance-id>.phn: phonetic transcription of utterances
      - <utterance-id>.wav: utterance sound file
    z<(\w+-\w+/\w+\.(phn|txt|wav|wrd))|timitdic\.txt|spkrinfo\.txtz\w+-\w+/\w+\.txtc                    t        |t              rdd|fg}t        j                  | |t	        || j
                        |       t	        || j                        D cg c]  }|dd 	 c}| _        	 d| _        || _	        t        | j                  D ch c]  }|j                  d      d    c}      | _        yc c}w c c}w )z
        Construct a new TIMIT corpus reader in the given directory.
        :param root: The root directory for this corpus.
        )z.*\.wavNz.*)encodingN/r   )
isinstancestrCorpusReader__init__find_corpus_fileids_FILE_RE_UTTERANCE_RE_utterances_speakerinfo_rootsortedsplitspeakers)selfrootr	   nameus        M/var/www/openai/venv/lib/python3.12/site-packages/nltk/corpus/reader/timit.pyr   zTimitCorpusReader.__init__   s     h$*T8,<=H$+D$--@8 	 	

 #6dD<N<N"O
"O$D"I"O
	 !
9I9IJ9IAQ9IJK
  Ks   B:B?Nc                     |t         j                  |       S |dv r| j                  D cg c]	  }| d|  c}S |dk(  rddgS t        d|z        c c}w )aW  
        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``,
        )txtwrdphnwav.metadatatimitdic.txtspkrinfo.txtzBad value for filetype: %r)r   fileidsr   
ValueError)r   filetyper   s      r   r'   zTimitCorpusReader.fileids   sv     ''--55/3/?/?@/?!qc8*%/?@@#"N339HDEE	 As   Ac                    t        |t              r|g}t        |t              r|g}t        |t              r|g}t        |t              r|g}t        |t              r|g}| j                  dd }||D cg c]  }|d   |v s| }}||D cg c]  }|d   |v s| }}||D cg c]  }|dd |v s| }}||D cg c]  }|d   |v s| }}||D cg c]  }|dd |v s| }}|S c c}w c c}w c c}w c c}w c c}w )z
        :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.
        N      	      
   )r   r   r   )r   dialectsexspkrid	sent_typesentid
utterancesr   s           r   utteranceidszTimitCorpusReader.utteranceids   s:    gs#iGc3%Cfc"XFi%"Ifc"XF%%a(
%/CZ1Q47?!ZJC?%/?Z1Q43;!ZJ?%/CZ1Ra5F?!ZJC %/FZ1R5I3E!ZJF%/DZ1RS6V3C!ZJD D?CFDs<   5C.C.C3C3'C84C8 C=C=D&Dc                 H   i }| j                  d      5 }|D ]r  }|j                         r|d   dk(  rt        j                  d|      }|st	        d|z        |j                  d      j                         ||j                  d      <   t 	 ddd       |S # 1 sw Y   |S xY w)	zf
        :return: A dictionary giving the 'standard' transcription for
            each word.
        r%   r   ;z\s*(\S+)\s+/(.*)/\s*$zBad line: %rr+      N)openstriprematchr(   groupr   )r   _transcriptionsfplinems        r   transcription_dictz$TimitCorpusReader.transcription_dict   s    
 YY~&"zz|tAw#~HH5t<$^d%:;;./ggaj.>.>.@
+  '  ' s   A8BB!c                 *    |j                  d      d   S )Nr   r   r   r   	utterances     r   r2   zTimitCorpusReader.spkrid       s#A&&    c                 *    |j                  d      d   S )Nr   r9   rE   rF   s     r   r4   zTimitCorpusReader.sentid   rH   rI   c                     | d| S )Nr    )r   r2   r4   s      r   rG   zTimitCorpusReader.utterance   s    6(##rI   c                 h    | j                   D cg c]  }|j                  |dz         r| c}S c c}w )z`
        :return: A list of all utterances associated with a given
            speaker.
        r   )r   
startswith)r   speakerrG   s      r   spkrutteranceidsz"TimitCorpusReader.spkrutteranceids   sA     "--
-	##GcM2 -
 	
 
s   /c                    || j                   v r| j                  |      }| j                  i | _        | j                  d      5 }|D ]  }|j	                         r|d   dk(  r|j	                         j                  dd      }d|d    d|d	   j                          |d   j                          }t        | | j                  |<    	 ddd       | j                  |   S # 1 sw Y   xY w)
z=
        :return: A dictionary mapping .. something.
        Nr&   r   r8   r-   drr+   -r9   )r   r2   r   r:   r;   r   lowerSpeakerInfo)r   rO   r@   rA   reckeys         r   spkrinfozTimitCorpusReader.spkrinfo  s     d&&&kk'*G$ "D>*bD::<47c> **,,,T15Cs1vhaA'7A7GHC-8#->D%%c*  +   )) +*s   BC!!C*c                     g }| j                  |d      D ]W  }| j                  |      5 }|D ]5  }|j                         s|j                  |j	                         d          7 	 d d d        Y |S # 1 sw Y   exY w)N.phn_utterance_fileidsr:   r;   appendr   r   r5   resultsfileidr@   rA   s         r   phoneszTimitCorpusReader.phones  o    --j&AF6"bDzz|tzz|B'78  #" B
 	 #"   A1 $A11A:	c                 h   g }| j                  |d      D ]  }| j                  |      5 }|D ]l  }|j                         s|j                  |j	                         d   t        |j	                         d         t        |j	                         d         f       n 	 ddd        |S # 1 sw Y   xY w)zE
        offset is represented as a number of 16kHz samples!
        rZ   r+   r   r9   Nr]   r:   r;   r^   r   intr_   s         r   phone_timeszTimitCorpusReader.phone_times"  s     --j&AF6"bDzz| $

Q #DJJLO 4 #DJJLO 4  #" B  #"   B( AB((B1	c                     g }| j                  |d      D ]W  }| j                  |      5 }|D ]5  }|j                         s|j                  |j	                         d          7 	 d d d        Y |S # 1 sw Y   exY wN.wrdr[   r\   r_   s         r   wordszTimitCorpusReader.words4  rc   rd   c                 h   g }| j                  |d      D ]  }| j                  |      5 }|D ]l  }|j                         s|j                  |j	                         d   t        |j	                         d         t        |j	                         d         f       n 	 d d d         |S # 1 sw Y   xY w)Nrl   r+   r   r9   rf   r_   s         r   
word_timeszTimitCorpusReader.word_times=  s    --j&AF6"bDzz| $

Q #DJJLO 4 #DJJLO 4  #" B  #"ri   c           	         g }| j                  |d      D ]]  }| j                  |      5 }|j                  |D cg c]&  }|j                         s|j	                         d   ( c}       d d d        _ |S c c}w # 1 sw Y   pxY wrk   )r]   r:   r^   r;   r   r_   s         r   sentszTimitCorpusReader.sentsL  sy    --j&AF6"bRPRT4::<

R 0RPQ #" B   Q #"s"   A<A7A7$A<7A<<B	c                 N   | j                  |d      D cg c]  }| j                  |      D ]l  }|j                         rZ|j                  d d      d   j                         t	        |j                         d         t	        |j                         d         fn  c}}S c c}}w )Nz.txtr+   r[   r   r9   )r]   r:   r;   r   rg   )r   r5   ra   rA   s       r   
sent_timeszTimitCorpusReader.sent_timesS  s     11*fE	
 F		&)zz| 

4#B'--/DJJLO$DJJLO$ *
 F	
 		
 	
s   BB!c                 z   || j                   }t        |t              r|g}g }|D ]  }| j                  |      }| j	                  |      }| j                  |      }|s:|j                  d      \  }}}	|j                  t        dg              |rO|rM|d   d   |d   d   k  r<|d   j                  |j                  d      d          |r|r|d   d   |d   d   k  r<|r|d   d   |	k  r|j                  d      \  }
}}|d   j                  t        |
g              |rB|d   d   |k  r7|d   d   j                  |j                  d      d          |r|d   d   |k  r7|r|d   d   |	k  r|r?|d   d   |	k  r4|d   j                  |j                  d      d          |r|d   d   |	k  r4|rX |S )Nr   Sr+   r9   r[   )	r   r   r   ro   rh   rs   popr^   r   )r   r5   treesrG   ro   rh   rs   sent
sent_startsent_endword
word_startword_ends                r   phone_treeszTimitCorpusReader.phone_trees`  s   ))Jj#&$J#I3J**95K3J/9~~a/@,z8T#r]+;;q>!3D
STVWHX3X"I$$[__Q%7%:; ;;q>!3D
STVWHX3X !Z]1%5%A3=>>!3D0T:x"I$$T$^4%+a.*;x*Gb	",,[__Q-?-BC &+a.*;x*G !Z]1%5%A
 "k!nQ&78&C"I$$[__Q%7%:; "k!nQ&78&C  $& rI   c                    t        d      }|j                  | j                  |dz         d      }||j                         }|j                  |       |j                  ||z
        }t        j                         }|j                  |d      }|j                  |j                                |j                  |       |j                          |j                  d       |j                         S )Nwave.wavrbwr   )r   r:   
getnframes
readframestempfileTemporaryFile	setparams	getparamswriteframescloseseekread)	r   rG   startendr   r   framestfouts	            r   r"   zTimitCorpusReader.wav  s    !&)IIdii	F 23T:;,,.C 	
UcEk* ##%iiC  	akkm$		 	
wwyrI   c                     |||kD  sJ d}| j                  |dz         5 }||j                         }n|j                  ||dz  z         }d d d        ||dz  z   d  S # 1 sw Y   xY w)N,   r   r+   )r:   r   )r   rG   r   r   
headersizer@   datas          r   	audiodatazTimitCorpusReader.audiodata  sy    {cEk))
YYy6)*b{wwywwzC!G34	 +
 J*,-- +*s   +AA'c                 v    || j                   }t        |t              r|g}|D cg c]  }| | 
 c}S c c}w N)r   r   r   )r   r5   	extensionr   s       r   r]   z$TimitCorpusReader._utterance_fileids  sE    ))Jj#&$J+56:a1#i[!:666s   6c                    	 ddl }	 |j                  d      }|j                  |j                         |j	                  d       |j                  d       |j                  | j                  |||             |j                          y# t        $ rJ}t        dt        j                         t        dt        |      t        j                         Y d}~yd}~ww xY w# t        $ r Y nw xY w	 ddl}ddl}|j"                  j%                  d       |j!                  | j'                  |||            }	|j"                  j)                  |	      j+                          |j"                  j-                         r0t/        j0                  d	       |j"                  j-                         r0y# t        $ r Y nw xY wt        d
t        j                         y)zp
        Play the given audio sample.

        :param utterance: The utterance id of the sample to play
        r   Nr   r9   i>  zBcan't acquire the audio device; please activate your audio device.)filezsystem error message:g{Gz?z:you must install pygame or ossaudiodev for audio playback.)ossaudiodevr:   setfmtAFMT_S16_LEchannelsspeedwriter   r   OSErrorprintsysstderrr   ImportErrorpygame.mixerStringIOmixerinitr"   Soundplayget_busytimesleep)
r   rG   r   r   r   dspepygamer   fs
             r   r   zTimitCorpusReader.play  sv   	H!&&s+

;223Q		% 		$..E3?@		   H6  -s1vCJJGGH  			LLe$!!$((9eS"ABALLq!&&(,,'')

4  ,,'') 		 	L	
sI   C B B 	CA CC CC 	C*)C*.B7F' '	F32F3)utf8r   )NNNNN)r   N)__name__
__module____qualname____doc__r   r   r   r'   r6   rC   r2   r4   rG   rP   rX   rb   rh   rm   ro   rq   rs   r~   r"   r   r]   r   rL   rI   r   r   r      s    " UHH'ML.F( KOB ''$	
*&$
>8.71
rI   r   c                       e Zd Z	 ddZd Zy)rU   Nc                     || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        y r   )
idr1   rR   userecdate	birthdatehtraceeducomments)r   r   r1   rR   r   r   r   r   r   r   r   s              r   r   zSpeakerInfo.__init__  sJ     "	 rI   c           	          d}|j                         D cg c]  }| dt        | |       }}ddj                  |      z  S c c}w )Nz4id sex dr use recdate birthdate ht race edu comments=zSpeakerInfo(%s)z, )r   getattrjoin)r   attribsattrargss       r   __repr__zSpeakerInfo.__repr__  sM    H>EmmoNod4&'$-01oN DIIdO44 Os   Ar   )r   r   r   r   r   rL   rI   r   rU   rU     s    LP!5rI   rU   c                 Z    | j                         }|sg S |j                  dd      \  }}|gS )zt
    Block reader for timit tagged sentences, which are preceded by a sentence
    number that will be ignored.
     r9   )readliner   )streamrA   nrx   s       r   read_timit_blockr     s3    
 ??D	jja GAt6MrI   )r   r   r   nltk.corpus.reader.apinltk.internalsr   	nltk.treer   r   r   rU   r   rL   rI   r   <module>r      s;   m\   $ - \
 \
~
5 5*	rI   