o
    
j                  
   @   s   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 d dlZd dlT d dlT dddZd	d
 Z				ddee	e ef dede
e	e	 e	e	 f fddZdd Zdd ZdddZdS )    N)Counter)Path)CallableDictListTupleUnion)*{Gz?c           	      C   s6  dd | D }t t|dk}|dkrt|}n|r&t|tt | | }ntt | | }|dks=J ddt |  tjd tj|  |rg }dd | D }t	|}t ||k rtj
dt | }| | d }|| dkr|| |  ||  d8  < | |= t ||k s^|| fS | d| | |d fS )	aj  Split a dataset into train and eval. Consider speaker distribution in multi-speaker training.

    Args:
        items (List[List]):
            A list of samples. Each sample is a list of `[audio_path, text, speaker_id]`.

        eval_split_max_size (int):
            Number maximum of samples to be used for evaluation in proportion split. Defaults to None (Disabled).

        eval_split_size (float):
            If between 0.0 and 1.0 represents the proportion of the dataset to include in the evaluation set.
            If > 1, represents the absolute number of evaluation samples. Defaults to 0.01 (1%).
    c                 S      g | ]}|d  qS speaker_name .0itemr   r   L/home/kuhnn/.local/lib/python3.10/site-packages/TTS/tts/datasets/__init__.py
<listcomp>       z!split_dataset.<locals>.<listcomp>   r   z [!] You do not have enough samples for the evaluation set. You can work around this setting the 'eval_split_size' parameter to a minimum of {}c                 S   r   r   r   r   r   r   r   r   .   r   r   N)lensetintminformatnprandomseedshuffler   randintappend)	itemseval_split_max_sizeeval_split_sizespeakersis_multi_speaker
items_evalspeaker_counteritem_idxspeaker_to_be_removedr   r   r   split_dataset   s8   


r*   c                 C   sN   | D ]"}||d< t jt j|d |d d }| d| }||d< q| S )Nlanguage
audio_file	root_pathr   #audio_unique_name)ospathsplitextrelpath)metadatar+   dataset_namer   relfilepathr/   r   r   r   add_extra_keys;   s   "
r7   Tdatasets	formatterreturnc                 C   s  g }|rg nd}t | ts| g} | D ]}|d }|d }	|d }
|d }|d }|d }|d }|du r8t|}||
||d	}t|d
ksOJ d|
 d| t|||	}tdt| dt|
   |r|rw||
||d	}t|||	}n|r|t|  nd}t|||\}}||7 }||7 }|j	rt
t|d }t|D ]\}}||d   }|| d|i q|rt|D ]\}}||d   }|| d|i qd}q||fS )a  Parse the dataset from the datasets config, load the samples as a List and load the attention alignments if provided.
    If `formatter` is not None, apply the formatter to the samples else pick the formatter from the available ones based
    on the dataset name.

    Args:
        datasets (List[Dict], Dict): A list of datasets or a single dataset dictionary. If multiple datasets are
            in the list, they are all merged.

        eval_split (bool, optional): If true, create a evaluation split. If an eval split provided explicitly, generate
            an eval split automatically. Defaults to True.

        formatter (Callable, optional): The preprocessing function to be applied to create the list of samples. It
            must take the root_path and the meta_file name and return a list of samples in the format of
            `[[text, audio_path, speaker_id], ...]]`. See the available formatters in `TTS.tts.dataset.formatter` as
            example. Defaults to None.

        eval_split_max_size (int):
            Number maximum of samples to be used for evaluation in proportion split. Defaults to None (Disabled).

        eval_split_size (float):
            If between 0.0 and 1.0 represents the proportion of the dataset to include in the evaluation set.
            If > 1, represents the absolute number of evaluation samples. Defaults to 0.01 (1%).

    Returns:
        Tuple[List[List], List[List]: training and evaluation splits of the dataset.
    Nr9   r5   r1   meta_file_trainmeta_file_valignored_speakersr+   )r=   r   z" [!] No training samples found in /z | > Found z
 files in meta_file_attn_maskr,   alignment_file)
isinstancelist_get_formatter_by_namer   r7   printr   resolver*   r?   dictload_attention_mask_meta_data	enumeratestripupdate)r8   
eval_splitr9   r"   r#   meta_data_train_allmeta_data_eval_alldatasetformatter_namer5   r-   r;   r<   r=   r+   meta_data_trainmeta_data_evaleval_size_per_dataset	meta_dataidxins	attn_filer   r   r   load_tts_samplesF   sJ   !
  rW   c                 C   sd   t | ddd}| }W d   n1 sw   Y  g }|D ]}|d\}}|||g q|S )z9Load meta data file created by compute_attention_masks.pyrzutf-8)encodingN|)open	readlinessplitr    )metafile_pathflinesrS   linewav_filerV   r   r   r   rG      s   
rG   c                 C   s   t jt }t||  S )z.Returns the respective preprocessing function.)sysmodules__name__getattrlower)name
thismoduler   r   r   rC      s   
rC   c                 C   s   d dd | D }t|}tdd |}dd |D }t|}|rOtdt|  td	d t|  td
d t|  tdd t|  |S )N c                 s   s    | ]}|d  V  qdS )r   Nr   r   r   r   r   	<genexpr>   s    z$find_unique_chars.<locals>.<genexpr>c                 S   s   |   S )N)islower)cr   r   r   <lambda>   s    z#find_unique_chars.<locals>.<lambda>c                 S   s   g | ]}|  qS r   )rg   )r   rm   r   r   r   r      r   z%find_unique_chars.<locals>.<listcomp>z  > Number of unique characters: z > Unique characters: z > Unique lower characters: z* > Unique all forced to lower characters: )joinr   filterrD   r   sorted)data_samplesverbosetextscharslower_charschars_force_lowerr   r   r   find_unique_chars   s   rx   )Nr
   )TNNr
   )T)r0   rc   collectionsr   pathlibr   typingr   r   r   r   r   numpyr   TTS.tts.datasets.datasetTTS.tts.datasets.formattersr*   r7   rW   rG   rC   rx   r   r   r   r   <module>   s0    
.
Q