o
    Sir                      @   s  d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZ ddlZddlmZ ddlmZmZ ddlmZmZ dd	lmZmZ dd
lmZmZ ddlmZ dZ 	d(de!de!dee!e!f fddZ"				d)dedee!ee! f de
e de#de!dee!ee!eeef f f fddZ$			d*dee!ef dee!ef de
e# d e
e# d!e
e# defd"d#Z%	d(ded$e!d%e!de!de
eeef  f
d&d'Z&dS )+a  
KsponSpeech is a large-scale spontaneous speech corpus of Korean.
This corpus contains 969 hours of open-domain dialogue utterances,
spoken by about 2,000 native Korean speakers in a clean environment.

All data were constructed by recording the dialogue between two people
freely conversing on a variety of topics and manually transcribing the utterances.

The transcription provides a dual transcription consisting of orthography and pronunciation,
and disfluency tags for the spontaneity of speech, such as filler words, repeated words, and word fragments.

The original audio data has a PCM extension.
During preprocessing, it is converted into a file in the FLAC extension and saved anew.

KsponSpeech is publicly available on an open data hub site of the Korea government.
The dataset must be downloaded manually.

For more details, please visit:

Dataset: https://aihub.or.kr/aihubdata/data/view.do?currMenu=115&topMenu=100&aihubDataSe=realm&dataSetSn=123
Paper: https://www.mdpi.com/2076-3417/10/19/6936
    N)ThreadPoolExecutor)Path)DictListOptionalSequenceTupleUnion)tqdm)fix_manifests$validate_recordings_and_supervisions)	RecordingRecordingSet)manifests_existread_manifests_if_cached)SupervisionSegmentSupervisionSet)Pathlike)traindev
eval_clean
eval_otherdefaultraw_contentnormalize_textreturnc                 C   s   t | dkrdS | d\}}|dkr||fS |dkrHtdd|}tdd|}|d	d}|d
d}|dd}tdd|}|| fS dS )a  
    Normalizing KsponSpeech text datasets with '.trn' extension.
    Perform the following processing.

    1. Separate file name and text labeling from raw content using separator ' :: ';
    2. Remove noise labeling characters (e.g. `o/`, `b/`...);
    3. Remove the actual pronunciation from the text labeling; use the spelling content;
    4. Remove other special characters and double spaces from text labeling.

    :param raw_content: a raw text labeling content containing file name and text labeling.
    :param normalize_text: str, the text normalization type, "default" or "none".
    :return: a tuple with file name and normalized text labeling.
    r    z :: noner   z[a-z]/z\((.*?)\)/\((.*?)\)z\1*+/z\s+ N)lensplitresubreplacestrip)r   r   original_content_idcontent r*   N/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/recipes/ksponspeech.py	normalize/   s   r,   all   
corpus_dirdataset_parts
output_dirnum_jobsc                 C   s  t | } |  sJ d|  |dkrtt}nt|tr |g}i }|dur:t |}|jddd t||dddd}t|}t	|d	d
D ]}t
d|  t||dddrat
d| d qEg }g }	g }
| | d }t|}|D ]}|
|t| ||| quW d   n1 sw   Y  t	|
dddD ]}| }|du rq|\}}|| |	| qt|}t|	}t||\}}t|| |dur||d| d  ||d| d  ||d||< qEW d   |S 1 sw   Y  |S )a  
    Returns the manifests which consist of the Recordings and Supervisions.
    When all the manifests are available in the ``output_dir``, it will simply read and return them.

    :param corpus_dir: Pathlike, the path of the data dir.
    :param dataset_parts: string or sequence of strings representing dataset part names, e.g. 'train', 'dev'.
        By default, we will infer all parts.
    :param output_dir: Pathlike, the path where to write the manifests.
    :param num_jobs: int, number of parallel threads used for 'parse_utterance' calls.
    :param normalize_text: str, the text normalization type, "default" or "none".
    :return: a Dict whose key is the dataset part, and the value is Dicts with the keys 'audio' and 'supervisions'.
    zNo such directory: r-   NT)parentsexist_okksponspeechzjsonl.gz)r0   r1   prefixsuffixlazyzDataset parts)desczProcessing KsponSpeech subset: )partr1   r6   r7   zKsponSpeech subset: z already prepared - skipping.z.trn
ProcessingF)r9   leaveksponspeech_supervisions_z	.jsonl.gzksponspeech_recordings_)
recordingssupervisions)r   is_dirsetKSPONSPEECH
isinstancestrmkdirr   r   r
   logginginfor   openappendsubmitparse_utteranceresultr   from_recordingsr   from_segmentsr   r   to_file)r/   r0   r1   r2   r   	manifestsexr:   r?   r@   futures
trans_pathflinefuturerM   	recordingsegmentrecording_setsupervision_setr*   r*   r+   prepare_ksponspeechS   s   









66r\   >     pcm_path	flac_pathsample_ratechannels	bit_depthc                 C   s>   t | } t |}tj| ||ddd\}}tj|||dd |S )NRAWPCM_16)rb   
samplerateformatsubtypeFLAC)rg   )r   sfreadwrite)r_   r`   ra   rb   rc   data_r*   r*   r+   pcm_to_flac   s   
ro   r:   rV   c           
   	   C   s   t | } t||\}}d|v r|jdddd }| | }|j}| s-td|  d S |d}t||}t	j
||d}t||d|jd	d
|d}	||	fS )Nevalr    r.   )maxsplitzNo such file: z.flac)recording_idg        r   Korean)idrr   startdurationchannellanguagetext)r   r,   r#   stemis_filerG   warningwith_suffixro   r   	from_filer   rv   )
r/   r:   rV   r   
audio_pathnormalized_linerr   r`   rX   rY   r*   r*   r+   rL      s,   

	rL   )r   )r-   Nr.   r   )r]   r.   r^   )'__doc__rG   r$   concurrent.futures.threadr   pathlibr   typingr   r   r   r   r   r	   	soundfilerj   	tqdm.autor
   lhotser   r   lhotse.audior   r   lhotse.recipes.utilsr   r   lhotse.supervisionr   r   lhotse.utilsr   rC   rE   r,   intr\   ro   rL   r*   r*   r*   r+   <module>   s     


&
f


