U
    CŒÑhf  ã                8   @   sø   d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	m
Z
 d dlmZ dddœZd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@h8Zee eee dAœdBdC„ZG dDdE„ dEe
ƒZdS )Fé    N)ÚPath)ÚIterableÚListÚTupleÚUnion)ÚDataset)Údownload_url_to_fileZ@209a8b4cd265013e96f4658632a9878103b0c5abf62b50d4ef3ae1be226b29e4Z@408ccaae803641c6d7b626b6299949320c2dbca96b2220fd3fb17887b023b027)úBhttp://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7búJhttp://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b.symbolsz!EXCLAMATION-POINTz"CLOSE-QUOTEz"DOUBLE-QUOTEz"END-OF-QUOTEz
"END-QUOTEz
"IN-QUOTESz"QUOTEz"UNQUOTEz
#HASH-MARKz#POUND-SIGNz#SHARP-SIGNz%PERCENTz
&AMPERSANDz'END-INNER-QUOTEz
'END-QUOTEz'INNER-QUOTEz'QUOTEz'SINGLE-QUOTEz(BEGIN-PARENSz(IN-PARENTHESESz(LEFT-PARENz(OPEN-PARENTHESESz(PARENz(PARENSz(PARENTHESESz)CLOSE-PARENz)CLOSE-PARENTHESESz
)END-PARENz)END-PARENSz)END-PARENTHESESz)END-THE-PARENz)PARENz)PARENSz)RIGHT-PARENz)UN-PARENTHESESz+PLUSz,COMMAz--DASHz-DASHz-HYPHENz...ELLIPSISz.DECIMALz.DOTz
.FULL-STOPz.PERIODz.POINTz/SLASHz:COLONz;SEMI-COLONz;SEMI-COLON(1)z?QUESTION-MARKz{BRACEz{LEFT-BRACEz{OPEN-BRACEz}CLOSE-BRACEz}RIGHT-BRACE)ÚlinesÚexclude_punctuationsÚreturnc                 C   sš   t  d¡}g }| D ]‚}|r| d¡r&q| ¡  d¡\}}|tkrn|rFq| d¡rVd}n| d¡rfd}n|d }t  |d|¡}| d¡}| ||f¡ q|S )	Nz
\([0-9]+\)z;;;z  z...z--r   Ú ú )ÚreÚcompileÚ
startswithÚstripÚsplitÚ_PUNCTUATIONSÚsubÚappend)r   r   Z_alt_reZcmudictÚlineÚwordZphones© r   úI/tmp/user/2202/pip-unpacked-wheel-p89lpdx5/torchaudio/datasets/cmudict.pyÚ_parse_dictionaryJ   s$    



r   c                	   @   s|   e Zd ZdZdddddœeeef eeeeddœd	d
„Ze	e
eee f dœdd„Ze	dœdd„Zeee dœdd„ƒZdS )ÚCMUDictaZ  *CMU Pronouncing Dictionary* :cite:`cmudict` (CMUDict) dataset.

    Args:
        root (str or Path): Path to the directory where the dataset is found or downloaded.
        exclude_punctuations (bool, optional):
            When enabled, exclude the pronounciation of punctuations, such as
            `!EXCLAMATION-POINT` and `#HASH-MARK`.
        download (bool, optional):
            Whether to download the dataset if it is not found at root path. (default: ``False``).
        url (str, optional):
            The URL to download the dictionary from.
            (default: ``"http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b"``)
        url_symbols (str, optional):
            The URL to download the list of symbols from.
            (default: ``"http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b.symbols"``)
    TFr	   r
   )ÚdownloadÚurlÚurl_symbolsN)Úrootr   r   r   r    r   c          
   	   C   s  || _ t|ƒ| _tj | j¡s,td|› �ƒ‚| jtj |¡ }| jtj |¡ }tj |¡s†|sntd|› �ƒ‚t	 
|d ¡}t|||ƒ tj |¡s¼|s¤td|› �ƒ‚t	 
|d ¡}t|||ƒ t|dƒ�}	dd„ |	 ¡ D ƒ| _W 5 Q R X t|ddd��}	t|	 ¡ | j d	�| _W 5 Q R X d S )
Nz#The root directory does not exist; z`The dictionary file is not found in the following location. Set `download=True` to download it. z\The symbol file is not found in the following location. Set `download=True` to download it. Úrc                 S   s   g | ]}|  ¡ ‘qS r   )r   )Ú.0r   r   r   r   Ú
<listcomp>Ÿ   s     z$CMUDict.__init__.<locals>.<listcomp>zlatin-1)Úencoding)r   )r   r   Z
_root_pathÚosÚpathÚisdirÚRuntimeErrorÚbasenameÚexistsÚ
_CHECKSUMSÚgetr   ÚopenÚ	readlinesÚ_symbolsr   Ú_dictionary)
Úselfr!   r   r   r   r    Z	dict_fileZsymbol_fileZchecksumÚtextr   r   r   Ú__init__{   s0    

ÿÿzCMUDict.__init__)Únr   c                 C   s
   | j | S )a  Load the n-th sample from the dataset.

        Args:
            n (int): The index of the sample to be loaded.

        Returns:
            Tuple of a word and its phonemes

            str:
                Word
            List[str]:
                Phonemes
        )r1   )r2   r5   r   r   r   Ú__getitem__¤   s    zCMUDict.__getitem__)r   c                 C   s
   t | jƒS )N)Úlenr1   ©r2   r   r   r   Ú__len__´   s    zCMUDict.__len__c                 C   s
   | j  ¡ S )zLlist[str]: A list of phonemes symbols, such as ``"AA"``, ``"AE"``, ``"AH"``.)r0   Úcopyr8   r   r   r   Úsymbols·   s    zCMUDict.symbols)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr   Úboolr4   Úintr   r   r6   r9   Úpropertyr;   r   r   r   r   r   i   s$    ýù
ø)r   )r&   r   Úpathlibr   Útypingr   r   r   r   Ztorch.utils.datar   Ztorchaudio._internalr   r,   r   r@   rA   r   r   r   r   r   r   Ú<module>   s†   þÈ<