o
    
j1                     @   sZ   d dl Z d dlZd dlmZ d dlZdZe dddgZG dd deZG d	d
 d
Z	dS )    N)Enumu   ;:,.!?¡¿—…"«»“”_punc_indexpuncpositionc                   @   s   e Zd ZdZdZdZdZdS )PuncPositionz#Enum for the punctuations positionsr         N)__name__
__module____qualname____doc__BEGINENDMIDDLE r   r   Q/home/kuhnn/.local/lib/python3.10/site-packages/TTS/tts/utils/text/punctuation.pyr      s
    r   c                   @   sx   e Zd ZdZefdefddZedd Ze	dd Z
e
jd	d Z
d
d Zdd Zdd Zedd Zedd ZdS )PunctuationaF  Handle punctuations in text.

    Just strip punctuations from text or strip and restore them later.

    Args:
        puncs (str): The punctuations to be processed. Defaults to `_DEF_PUNCS`.

    Example:
        >>> punc = Punctuation()
        >>> punc.strip("This is. example !")
        'This is example'

        >>> text_striped, punc_map = punc.strip_to_restore("This is. example !")
        >>> ' '.join(text_striped)
        'This is example'

        >>> text_restored = punc.restore(text_striped, punc_map)
        >>> text_restored[0]
        'This is. example !'
    puncsc                 C   s
   || _ d S N)r   )selfr   r   r   r   __init__*   s   
zPunctuation.__init__c                   C   s   t S )z#Return default set of punctuations.)
_DEF_PUNCSr   r   r   r   default_puncs-   s   zPunctuation.default_puncsc                 C   s   | j S r   )_puncs)r   r   r   r   r   2   s   zPunctuation.puncsc                 C   sN   t |tjs
tddttt|| _t	
dt	| j d| _d S )Nz%[!] Punctuations must be of type str. z(\s*[z]+\s*)+)
isinstancesixstring_types
ValueErrorjoinlistdictfromkeysr   recompileescapepuncs_regular_exp)r   valuer   r   r   r   6   s    c                 C   s   t | jd|  S )zRemove all the punctuations by replacing with `space`.

        Args:
            text (str): The text to be processed.

        Example::

            "This is. example !" -> "This is example "
         )r#   subr&   rstriplstrip)r   textr   r   r   strip=   s   
zPunctuation.stripc                 C   s   |  |\}}||fS )zRemove punctuations from text to restore them later.

        Args:
            text (str): The text to be processed.

        Examples ::

            "This is. example !" -> [["This is", "example"], [".", "!"]]

        )_strip_to_restore)r   r,   r   r   r   r   strip_to_restoreI   s   zPunctuation.strip_to_restorec                 C   s>  t t| j|}|s|gg fS t|dkr'|d  |kr'g t|tjgfS g }|D ]0}tj	}||d krA|
| rAtj}n||d krQ|| rQtj}|t| | q+g }t|D ]8\}}||j}	|	d |j|	dd }
}|}|
dkrqb||
 |t|d krt|dkr|| qb||fS )z+Auxiliary method for Punctuation.preserve()r   r   Nr   )r    r#   finditerr&   lengroup	_PUNC_IDXr   r   r   
startswithendswithr   append	enumeratesplitr   r   )r   r,   matchesr   matchr   splitted_textidxr   r9   prefixsuffixr   r   r   r.   W   s2   


zPunctuation._strip_to_restorec                 C   s   |  ||S )a  Restore punctuation in a text.

        Args:
            text (str): The text to be processed.
            puncs (List[str]): The list of punctuations map to be used for restoring.

        Examples ::

            ['This is', 'example'], ['.', '!'] -> "This is. example!"

        )_restore)clsr,   r   r   r   r   restorew   s   zPunctuation.restorec                 C   s   |s|S |sd dd |D gS |d }|jtjkr1| |j|d  g|dd  |dd S |jtjkrM|d |j g| |dd |dd  S t|dkrc| |d |j g|dd S | |d |j |d  g|dd  |dd S )z*Auxiliary method for Punctuation.restore()r   c                 s   s    | ]}|j V  qd S r   )r   ).0mr   r   r   	<genexpr>   s    z'Punctuation._restore.<locals>.<genexpr>r   r   Nr   )r   r   r   r   r@   r   r   r2   )rA   r,   r   currentr   r   r   r@      s   ,, 4zPunctuation._restoreN)r	   r
   r   r   r   strr   staticmethodr   propertyr   setterr-   r/   r.   classmethodrB   r@   r   r   r   r   r      s     


 
r   )
collectionsr#   enumr   r   r   
namedtupler4   r   r   r   r   r   r   <module>   s    