
    rHJj#                         d Z ddlZddlZddlZddlmZ ddlmZ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 ddlmZmZ ddlmZ  G d	 d
ej0                        Zy)z.Abstract base class for phonemization backends    N)Logger)OptionalListAnyDictTupleUnionPattern)
get_logger)Punctuation)	Separatordefault_separator)chunksc                   <   e Zd ZdZ	 	 	 ddedeeeef      dedee	   fdZ
ed        Zed	        Zed
        Zeej$                  d               Zeej$                  d               Zeej$                  d               Zeej$                  deeef   fd              Zedefd       Z	 	 	 ddee   dee   dededee   f
dZedeee      fd       Zej$                  dee   dedededee   f
d       Zdee   de eeee   f   ef   fdZ!dee   dedefdZ"y)BaseBackendaI  Abstract base class of all the phonemization backends

    Provides a common interface to all backends. The central method is
    `phonemize()`

    Parameters
    ----------
    language: str
        The language code of the input text, must be supported by
        the backend. If ``backend`` is 'segments', the language can be a file with
        a grapheme to phoneme mapping.

    preserve_punctuation: bool
        When True, will keep the punctuation in the
        phonemized output. Not supported by the 'espeak-mbrola' backend. Default
        to False and remove all the punctuation.

    punctuation_marks: str
        The punctuation marks to consider when dealing with punctuation, either for removal or preservation.
        Can be defined as a string or regular expression. Default to Punctuation.default_marks().

    logger: logging.Logger
        the logging instance where to send
        messages. If not specified, use the default system logger.

    Raises
    ------
    RuntimeError
        if the backend is not available of if the `language` cannot be initialized.

    Nlanguagepunctuation_markspreserve_punctuationloggerc           	         |t        j                         }|
t               }| j                         s(t	        dj                  | j                                     || _        | j                  j                  d| j                         dj                  d | j                         D                     | j                  |      | _        || _        t        |      | _        y )Nz{} not installed on your systemzinitializing backend %s-%s.c              3   2   K   | ]  }t        |        y w)N)str).0vs     a/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/phonemizer/backend/base.py	<genexpr>z'BaseBackend.__init__.<locals>.<genexpr>S   s     !A.Q#a&.s   )r   default_marksr   is_availableRuntimeErrorformatname_loggerinfojoinversion_init_language	_language_preserve_punctuation_punctuator)selfr   r   r   r   s        r   __init__zBaseBackend.__init__@   s    
 $ + 9 9 ;>\F   "188EG G (IIK!A$,,.!AA	C
 ,,X6 &:"&'89    c                 h    | j                  |      s t        d| d| j                          d      |S )zpLanguage initialization

        This method may be overloaded in child classes (see Segments backend)

        z
language "z" is not supported by the z backend)is_supported_languager    r"   clsr   s     r   r'   zBaseBackend._init_language\   sB     ((2XJ&@88:,h() ) r-   c                     | j                   S )z0A logging.Logger instance where to send messages)r#   r+   s    r   r   zBaseBackend.loggeri   s     ||r-   c                     | j                   S )z9The language code configured to be used for phonemization)r(   r3   s    r   r   zBaseBackend.languagen   s     ~~r-   c                       y)zThe name of the backendN r6   r-   r   r"   zBaseBackend.names       r-   c                      y)z9Returns True if the backend is installed, False otherwiseNr6   r1   s    r   r   zBaseBackend.is_availablex   r7   r-   c                      y)z;Return the backend version as a tuple (major, minor, patch)Nr6   r9   s    r   r&   zBaseBackend.version}   r7   r-   returnc                       y)z@Return a dict of language codes -> name supported by the backendNr6   r6   r-   r   supported_languageszBaseBackend.supported_languages   r7   r-   c                 &    || j                         v S )z6Returns True if `language` is supported by the backend)r=   r0   s     r   r/   z!BaseBackend.is_supported_language   s     322444r-   text	separatorstripnjobsc           	          t        |t              rt        d      t         j	                  |      \  }}|dk(  r j                  |d      }ns j                  j                  d j                         |        t        j                  |       fdt        t        ||       D              } j                  |      } j                  ||      S )a  Returns the `text` phonemized for the given language

        Parameters
        ----------
        text: list of str
            The text to be phonemized. Each string in the list
            is considered as a separated line. Each line is considered as a text
            utterance. Any empty utterance will be ignored.

        separator: Separator
            string separators between phonemes, syllables
            and words, default to separator.default_separator. Syllable separator
            is considered only for the festival backend. Word separator is
            ignored by the 'espeak-mbrola' backend.

        strip: bool
            If True, don't output the last word and phone separators
            of a token, default to False.

        njobs : int
            The number of parallel jobs to launch. The input text is
            split in ``njobs`` parts, phonemized on parallel instances of the
            backend and the outputs are finally collapsed.

        Returns
        -------
        phonemized text: list of str
            The input ``text`` phonemized for the given ``language`` and ``backend``.

        Raises
        ------
        RuntimeError
            if something went wrong during the phonemization

        z;input text to phonemize() is str but it must be list of str   r   zrunning %s on %s jobs)n_jobsc              3   z   K   | ]2  } t        j                  j                        |d    |d          4 yw)r   rD   N)joblibdelayed_phonemize_aux)r   chunkr+   r@   rA   s     r   r   z(BaseBackend.phonemize.<locals>.<genexpr>   sD      78 7E 4t223!HeAh	5: 7s   8;)
isinstancer   r    r   _phonemize_preprocessrI   r   r$   r"   rG   Parallelzipr   _flatten_phonemize_postprocess)r+   r?   r@   rA   rB   r   
phonemizeds   ` ``   r   	phonemizezBaseBackend.phonemize   s    N dC MO O )I"&"<"<T"BA:,,T1iGJ KK4diik5I 76 78 !&u"56	78 8J z2J**:7H)UZ[[r-   rQ   c                 8    t        t        j                  |        S )zFlatten a list of lists into a single one

        From [[1, 2], [3], [4]] returns [1, 2, 3, 4]. This method is used to
        format the output as obtained using multiple jobs.

        )list	itertoolschain)rQ   s    r   rO   zBaseBackend._flatten   s     IOOZ011r-   offsetc                      y)a  The "concrete" phonemization method

        Must be implemented in child classes. `separator` and `strip`
        parameters are as given to the phonemize() method. `text` is as
        returned by _phonemize_preprocess(). `offset` is line number of the
        first line in `text` with respect to the original text (this is only
        usefull with running on chunks in multiple jobs. When using a single
        jobs the offset is 0).

        Nr6   )r+   r?   rW   r@   rA   s        r   rI   zBaseBackend._phonemize_aux   r7   r-   c                     | j                   r| j                  j                  |      S | j                  j                  |      g fS )zPreprocess the text before phonemization

        Removes the punctuation (keep trace of punctuation marks for further
        restoration if required by the `preserve_punctuation` option).

        )r)   r*   preserveremove)r+   r?   s     r   rL   z!BaseBackend._phonemize_preprocess   s@     %%##,,T22&&t,b00r-   c                 Z    | j                   r| j                  j                  ||||      S |S )z\Postprocess the raw phonemized output

        Restores the punctuation as needed.

        )r)   r*   restore)r+   rQ   r   r@   rA   s        r   rP   z"BaseBackend._phonemize_postprocess   s2     %%##++J8I9V[\\r-   )NFN)NFrD   )#__name__
__module____qualname____doc__r   r   r	   r
   boolr   r,   classmethodr'   propertyr   r   staticmethodabcabstractmethodr"   r   r&   r   r=   r/   r   r   intrR   r   rO   rI   r   rL   rP   r6   r-   r   r   r      sU   B EI.3,0: :$,U3<-@$A:'+: "&):8 
 
     &  & H  H J  J Oc3h O  O 5S 5 5
 48 % C\d3i C\%i0C\C\ C\ &*#YC\J 2T$s)_ 2 2 	
49 
c 
i 
X\ 
aefiaj 
 

1$s) 
1eCcN>SUY>Y8Z 
1c *3 '+r-   r   )ra   rf   rU   reloggingr   typingr   r   r   r   r   r	   r
   rG   phonemizer.loggerr   phonemizer.punctuationr   phonemizer.separatorr   r   phonemizer.utilsr   ABCr   r6   r-   r   <module>rq      s@    5 
  	  C C C  ( . = #`#'' `r-   