a
    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g ddddddddg ddddddddg d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)r
   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              	      sD  || _ |t v r@t| d }t| d }|r2|n
t| d }ntd|t |t| d vr|td|t| d t|}tj|}tj	||}	|
dd }|d	kr|d
krt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 ]  drtj	| jd }t|F}t| } dd | j fddt|D  W d    n1 s0    Y  qt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/var/www/auris/lib/python3.9/site-packages/torchaudio/datasets/tedlium.py	<genexpr>~       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   r1   archiver   Zstm_pathflr!   r$   r&   __init__:   s`    



BzTEDLIUM.__init__)fileidr#   r0   r   c                 C   s   t j|d|}t|d 6}| | }|dd\}}}	}
}}}W d   n1 sX0    Y  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        NZsph)
start_timeend_time)r.   r0   r2   r<   r>   r3   _load_audior)   )rD   rI   r#   r0   Ztranscript_pathrF   Z
transcriptZtalk_id_Z
speaker_idrL   rM   
identifierZ	wave_pathZwaveformsample_rater!   r!   r&   _load_tedlium_item   s    8zTEDLIUM._load_tedlium_item>  )r0   rL   rM   rQ   r   c                 C   s@   t t|| }t t|| }||| d}tj|fi |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)rD   r0   rL   rM   rQ   kwargsr!   r!   r&   rN      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
        )r8   rR   r4   )rD   rY   rI   r#   r!   r!   r&   __getitem__   s    zTEDLIUM.__getitem__)r   c                 C   s
   t | jS )zTEDLIUM dataset custom function overwritting len default behaviour.

        Returns:
            int: TEDLIUM dataset length
        )r=   r8   )rD   r!   r!   r&   __len__   s    zTEDLIUM.__len__c                 C   sz   | j spi | _ t| jdddD}| D ]*}|  }t|dd | j |d < q&W d   n1 sf0    Y  | j  S )zdict[str, tuple[str]]: Phonemes. Mapping from word to tuple of phonemes.
        Note that some words have empty phonemes.
        rzutf-8)encoding   Nr   )rC   r<   rB   r>   stripr3   tuplecopy)rD   rF   r#   contentr!   r!   r&   phoneme_dict   s    :zTEDLIUM.phoneme_dict)r   r
   Fr   )rS   )__name__
__module____qualname____doc__r   strr   boolrH   rT   r   r   rR   rU   rN   rZ   r[   propertyrc   r!   r!   r!   r&   r   +   s&       
I$ r   )r.   pathlibr   typingr   r   rV   Ztorchr   Ztorch.utils.datar   Ztorchaudio._internalr   Ztorchaudio.datasets.utilsr   r*   r   r!   r!   r!   r&   <module>   sB   

