U
    ,:%e!                  
   @   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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 ]n  drtj	| jd }t|<}t| } dd | j fddt|D  W 5 Q R X 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!   Z/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/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 )zTEDLIUM 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 )zdict[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   

