U
    CŒÑhú!  ã                
   @   s¾   d dl Z d dlmZ d dlmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ dd	d
dddddgddœddddddddgddœddddddddgddœdœZG dd„ de
ƒZdS )é    N)ÚPath)ÚTupleÚUnion)ÚTensor)ÚDataset)Údownload_url_to_file)Ú_extract_tarZTEDLIUM_release1z:http://www.openslr.org/resources/7/TEDLIUM_release1.tar.gzZ@30301975fd8c5cac4040c261c0852f57cfa8adbbad2ce78e77e4986957445f27Ú ÚtrainÚtestÚdevzTEDLIUM.150K.dic)Úfolder_in_archiveÚurlÚchecksumÚ	data_pathÚsubsetÚsupported_subsetsÚdictZTEDLIUM_release2z;http://www.openslr.org/resources/19/TEDLIUM_release2.tar.gzZ@93281b5fcaaae5c88671c9d000b443cb3c7ea3499ad12010b3934ca41a7b9c58zTEDLIUM.152k.diczTEDLIUM_release-3z9http://www.openslr.org/resources/51/TEDLIUM_release-3.tgzZ@ad1e454d14d1ad550bc2564c462d87c7a7ec83d4dc2b9210f22ab4973b9eccdbzdata/)Úrelease1Zrelease2Úrelease3c                
   @   s¬   e Zd ZdZdeeef eeeeddœdd	„Zee	ee
ee	ee	e	e	f d
œdd„Zdeeee	ee	gdœdd„Ze	e
ee	ee	e	e	f dœdd„Ze	dœdd„Zedd„ ƒZdS )ÚTEDLIUMaÎ  *Tedlium* :cite:`rousseau2012tedlium` dataset (releases 1,2 and 3).

    Args:
        root (str or Path): Path to the directory where the dataset is found or downloaded.
        release (str, optional): Release version.
            Allowed values are ``"release1"``, ``"release2"`` or ``"release3"``.
            (default: ``"release1"``).
        subset (str, optional): The subset of dataset to use. Valid options are ``"train"``, ``"dev"``,
            and ``"test"``. Defaults to ``"train"``.
        download (bool, optional):
            Whether to download the dataset if it is not found at root path. (default: ``False``).
        audio_ext (str, optional): extension for audio file (default: ``".sph"``)
    r   r
   Fú.sphN)ÚrootÚreleaser   ÚdownloadÚ	audio_extÚreturnc              
      s.  || _ |t ¡ kr@t| d }t| d }|r2|n
t| d }ntd |t ¡ ¡ƒ‚|t| d kr|td |t| d ¡ƒ‚t |¡}tj |¡}tj 	||¡}	| 
d¡d }|d	krð|d
krÚtj 	||t| d ¡| _ntj 	||d|¡| _ntj 	||t| d |¡| _|�rTtj | j¡�svtj |	¡�sJt| d }
t||	|
d� t|	ƒ n"tj | j¡�svtd| j› d�ƒ‚g | _tj 	| jd¡}tt |¡ƒD ]n‰ ˆ  d¡�rštj 	| jdˆ ¡}t|ƒ�<}t| ¡ ƒ}ˆ  dd¡‰ | j ‡ fdd„t|ƒD ƒ¡ W 5 Q R X �qštj 	||t| d ¡| _d | _d S )Nr   r   r   zFThe release {} does not match any of the supported tedlium releases{} r   zDThe subset {} does not match any of the supported tedlium subsets{} Ú.r   r   r
   r   Úlegacyr   )Zhash_prefixz	The path zT doesn't exist. Please check the ``root`` path or set `download=True` to download itÚstmú.stmr	   c                 3   s   | ]}ˆ |fV  qd S )N© )Ú.0Úline©Úfiler!   úI/tmp/user/2202/pip-unpacked-wheel-p89lpdx5/torchaudio/datasets/tedlium.pyÚ	<genexpr>~   s     z#TEDLIUM.__init__.<locals>.<genexpr>r   )Ú
_ext_audioÚ_RELEASE_CONFIGSÚkeysÚRuntimeErrorÚformatÚosÚfspathÚpathÚbasenameÚjoinÚsplitÚ_pathÚisdirÚisfiler   r   ÚexistsÚ	_filelistÚsortedÚlistdirÚendswithÚopenÚlenÚ	readlinesÚreplaceÚextendÚrangeÚ
_dict_pathÚ_phoneme_dict)Úselfr   r   r   r   r   r   r   r0   Úarchiver   Zstm_pathÚfÚlr!   r$   r&   Ú__init__:   s`    þÿ
þÿ

ÿ
,zTEDLIUM.__init__)Úfileidr#   r/   r   c              	   C   sˆ   t j |d|¡}t|d ƒ�,}| ¡ | }| dd¡\}}}	}
}}}W 5 Q R X t j |d|¡}| j|| j |
|d�\}}|||||	|fS )aÙ  Loads a TEDLIUM dataset sample given a file name and corresponding sentence name.

        Args:
            fileid (str): File id to identify both text and audio files corresponding to the sample
            line (int): Line identifier for the sample inside the text file
            path (str): Dataset root path

        Returns:
            (Tensor, int, str, int, int, int):
            ``(waveform, sample_rate, transcript, talk_id, speaker_id, identifier)``
        r   r    ú é   Zsph)Ú
start_timeÚend_time)r-   r/   r1   r;   r=   r2   Ú_load_audior(   )rC   rH   r#   r/   Ztranscript_pathrE   Z
transcriptZtalk_idÚ_Z
speaker_idrK   rL   Ú
identifierZ	wave_pathZwaveformÚsample_rater!   r!   r&   Ú_load_tedlium_itemƒ   s    $zTEDLIUM._load_tedlium_itemé€>  )r/   rK   rL   rP   r   c                 C   s<   t t|ƒ| ƒ}t t|ƒ| ƒ}||| dœ}tj|f|ŽS )a"  Default load function used in TEDLIUM dataset, you can overwrite this function to customize functionality
        and load individual sentences from a full ted audio talk file.

        Args:
            path (str): Path to audio file
            start_time (int): Time in seconds where the sample sentence stars
            end_time (int): Time in seconds where the sample sentence finishes
            sample_rate (float, optional): Sampling rate

        Returns:
            [Tensor, int]: Audio tensor representation and sample rate
        )Zframe_offsetZ
num_frames)ÚintÚfloatÚ
torchaudioÚload)rC   r/   rK   rL   rP   Úkwargsr!   r!   r&   rM   ™   s    zTEDLIUM._load_audio)Únr   c                 C   s   | j | \}}|  ||| 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 the following items;

            Tensor:
                Waveform
            int:
                Sample rate
            str:
                Transcript
            int:
                Talk ID
            int:
                Speaker ID
            int:
                Identifier
        )r7   rQ   r3   )rC   rX   rH   r#   r!   r!   r&   Ú__getitem__­   s    zTEDLIUM.__getitem__)r   c                 C   s
   t | jƒS )z†TEDLIUM dataset custom function overwritting len default behaviour.

        Returns:
            int: TEDLIUM dataset length
        )r<   r7   )rC   r!   r!   r&   Ú__len__Æ   s    zTEDLIUM.__len__c              	   C   sf   | j s\i | _ t| jddd��:}| ¡ D ]*}| ¡  ¡ }t|dd… ƒ| j |d < q&W 5 Q R X | j  ¡ S )zƒdict[str, tuple[str]]: Phonemes. Mapping from word to tuple of phonemes.
        Note that some words have empty phonemes.
        Úrzutf-8)Úencodingé   Nr   )rB   r;   rA   r=   Ústripr2   ÚtupleÚcopy)rC   rE   r#   Úcontentr!   r!   r&   Úphoneme_dictÎ   s    &zTEDLIUM.phoneme_dict)r   r
   Fr   )rR   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr   ÚboolrG   rS   r   r   rQ   rT   rM   rY   rZ   Úpropertyrb   r!   r!   r!   r&   r   +   s&       ú
ùI$ r   )r-   Úpathlibr   Útypingr   r   rU   Ztorchr   Ztorch.utils.datar   Ztorchaudio._internalr   Ztorchaudio.datasets.utilsr   r)   r   r!   r!   r!   r&   Ú<module>   sB   ù
ù
ùí