
    Pi                         U d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 e
d         Ze	ed<   ded	ed
eddfdZ G d dej                  Z G d de          Z G d de          Z G d de          ZdS )u  Manages language switches for the espeak backend

This module is used in phonemizer.backend.EspeakBackend and should be
considered private.

It manages languages switches that occur during phonemization, where a part of
a text is phonemized in a language different from the target language. For
instance the sentence "j'aime le football" in French will be phonemized by
espeak as "ʒɛm lə (en)fʊtbɔːl(fr)", "football" be pronounced as an English
word. This may cause two issues to end users. First it introduces undesirable
(.) language switch flags. It may introduce extra phones that are not present
in the target language phoneset.

This module implements 3 alternative solutions the user can choose when
initializing the espeak backend:
- 'keep-flags' preserves the language switch flags,
- 'remove-flags' removes the flags (.) but preserves the words with alternative
  phoneset,
- 'remove-utterance' removes the utterances where flags are detected.

    N)Logger)ListTuple)	TypeAliasLiteralz
keep-flagszremove-flagszremove-utteranceLanguageSwitchmodeloggerlanguagereturnBaseLanguageSwitchc           
          t           t          t          d}	  ||          ||          S # t          $ r< t	          d|  dd                    |                                                     dw xY w)a?  Returns a language switch processor initialized from `mode`

    The `mode` can be one of the following:
    - 'keep-flags' to preserve the switch flags
    - 'remove-flags' to suppress the switch flags
    - 'remove-utterance' to suppress the entire utterance

    Raises a RuntimeError if the `mode` is unknown.

    r   zmode "z" invalid, must be in , N)	KeepFlagsRemoveFlagsRemoveUtterancesKeyErrorRuntimeErrorjoinkeys)r
   r   r   
processorss       }/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/phonemizer/backend/espeak/language_switch.pyget_language_switch_processorr   .   s      #,. .J
z$111   OTOO:??;L;L1M1MOO
 
	s
   ) AA/c                       e Zd ZdZ ej        d          ZdedefdZ	e
dedefd            Ze
ej        dedeeef         fd	                        Zej        d
ee         fd            ZdS )r   a0  The base class for language switch processors

    Parameters
    ----------
    logger (logging.Logger) : a logger instance to send warnings when language
        switches are detected.
    language (str) : the language code currently in use by the phonemizer, to
        customize warning content

    z\(.+?\)r   r   c                 "    || _         || _        d S N)_logger	_language)selfr   r   s      r   __init__zBaseLanguageSwitch.__init__V   s    !    	utterancer   c                 P    t          | j                            |                    S )z?Returns True is a language switch is present in the `utterance`)bool_ESPEAK_FLAGS_REsearchclsr#   s     r   is_language_switchz%BaseLanguageSwitch.is_language_switchZ   s#     C(//	::;;;r"   c                     dS )a  Detects and process language switches according to the mode

        This method is called on each utterance as a phonemization
        post-processing step.

        Returns
        -------
        processed_utterance (str) : the utterance either preserved, deleted (as
            '') or with the switch removed
        has_switch (bool): True if a language switch flag is found in the
            `utterance` and False otherwise

        N r(   s     r   processzBaseLanguageSwitch.process_         r"   switchesc                     dS )aM  Sends warnings to the logger with recorded language switches

        This method is called a single time at the very end of the
        phonemization process.

        Parameters
        ----------
        switches (list of int) : the line numbers where language switches has
            been detected during phonemization

        Nr,   )r    r/   s     r   warningzBaseLanguageSwitch.warningp   r.   r"   N)__name__
__module____qualname____doc__recompiler&   r   strr!   classmethodr%   r*   abcabstractmethodr   r-   r   intr1   r,   r"   r   r   r   F   s        	 	 "rz*--"v " " " " " <3 <4 < < < [<  c4i(8     [ 	S	      r"   c                   Z    e Zd ZdZededeeef         fd            Zde	e
         fdZdS )r   z>Preserves utterances even if language switch flags are presentr#   r   c                 0    ||                      |          fS r   r*   r(   s     r   r-   zKeepFlags.process   s    #00;;;;r"   r/   c           
      ,   |sd S t          |          }| j                            d|d                    d t	          |          D                                  | j                            d| j                   | j                            d           d S )N6%s utterances containing language switches on lines %sr   c              3   4   K   | ]}t          |          V  d S r   r8   .0switchs     r   	<genexpr>z$KeepFlags.warning.<locals>.<genexpr>   (      AAfc&kkAAAAAAr"   ,extra phones may appear in the "%s" phonesetzClanguage switch flags have been kept (applying "keep-flags" policy)lenr   r1   r   sortedr   r    r/   	nswitchess      r   r1   zKeepFlags.warning   s     	FMM	$IIAAx0@0@AAAAA	C 	C 	C 	:DN	L 	L 	L-	. 	. 	. 	. 	.r"   Nr2   r3   r4   r5   r9   r8   r   r%   r-   r   r<   r1   r,   r"   r   r   r      sn        HH< <c4i(8 < < < [<.S	 . . . . . .r"   r   c                   Z    e Zd ZdZededeeef         fd            Zde	e
         fdZdS )r   z/Removes the language switch flags when detectedr#   r   c                 n    |                      |          rt          j        | j        d|          dfS |dfS )N TF)r*   r6   subr&   r(   s     r   r-   zRemoveFlags.process   s@    !!),, 	E6#.I>>DD%r"   r/   c           
      ,   |sd S t          |          }| j                            d|d                    d t	          |          D                                  | j                            d| j                   | j                            d           d S )NrA   r   c              3   4   K   | ]}t          |          V  d S r   rC   rD   s     r   rG   z&RemoveFlags.warning.<locals>.<genexpr>   rH   r"   rI   zHlanguage switch flags have been removed (applying "remove-flags" policy)rJ   rM   s      r   r1   zRemoveFlags.warning   s     	FMM	$IIAAx0@0@AAAAA	C 	C 	C 	:DN	L 	L 	L/	0 	0 	0 	0 	0r"   NrO   r,   r"   r   r   r      sn        99   c4i(8       [ 0S	 0 0 0 0 0 0r"   r   c                   Z    e Zd ZdZededeeef         fd            Zde	e
         fdZdS )r   zCRemove the entire utterance when a language switch flag is detectedr#   r   c                 8    |                      |          rdS |dfS )N)rR   TFr?   r(   s     r   r-   zRemoveUtterances.process   s(    !!),, 	8%r"   r/   c                 b    |sd S t          |          }| j                            d|           d S )NzWremoved %s utterances containing language switches (applying "remove-utterance" policy))rK   r   r1   rM   s      r   r1   zRemoveUtterances.warning   sF     	FMM	34=	? 	? 	? 	? 	?r"   NrO   r,   r"   r   r   r      sn        MM   c4i(8       [ ?S	 ? ? ? ? ? ?r"   r   )r5   r:   r6   loggingr   typingr   r   typing_extensionsr   r   r	   __annotations__r8   r   ABCr   r   r   r   r,   r"   r   <module>r^      sj    , 


 				               0 0 0 0 0 0 0 0#$TU	 U U U  RU Zn    06 6 6 6 6 6 6 6r. . . . ." . . ..0 0 0 0 0$ 0 0 04? ? ? ? ?) ? ? ? ? ?r"   