o
    PhKg                     @   s   d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZmZmZ d dlmZmZmZmZm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mZ d d
l m!Z! d dl"m#Z# G dd dedZ$G dd deZ%G dd de$Z&dS )    )ABCMetaabstractmethod)
ThreadPool)Union)BeautifulSoup)NavigableStringPageElementPreformattedStringTag)ParameterTypeErrorParameterValueErrorTranslatepyExceptionUnsupportedMethodUnsupportedLanguage)Language)DictionaryResultExampleResultLanguageResultSpellcheckResultTextToSpechResultTranslationResultTransliterationResult)List)LRUDictCache)remove_spacesc                   @   s   e Zd ZdZdZdS )ABCzHelper class that provides a standard way to create an ABC using
    inheritance.

    Added in the ABC module in Python 3.4
     N)__name__
__module____qualname____doc__	__slots__r   r   r   W/var/www/html/env_mimamsha/lib/python3.10/site-packages/translatepy/translators/base.pyr      s    r   )	metaclassc                       s0   e Zd Zi Zddef fddZdd Z  ZS )	BaseTranslateExceptionNstatus_codec                    sB   d |}|d u r| j||| _n|| _|| _t | j d S )NzUnknown error. Error code: {})formaterror_codesgetmessager&   super__init__)selfr&   r*   unknown_status_code_msg	__class__r   r"   r,      s   
zBaseTranslateException.__init__c                 C   s   d | j| jS )Nz{} | {})r'   r&   r*   r-   r   r   r"   __str__*      zBaseTranslateException.__str__)r%   N)r   r   r   r(   intr,   r2   __classcell__r   r   r/   r"   r$      s    r$   c                   @   sV  e Zd ZdZe Ze Ze Ze Ze Z	e Z
edZi Zd@dedededefdd	Zdedededefd
dZdAdeeeeef dededededeeeeef fddZd@dedededefddZdededefddZd@dededefddZdededefddZdedefddZdedefddZd@dededede fdd Z!dededede"fd!d"Z#d@dededede$fd#d$Z%dededede"fd%d&Z&dBded(ed)edede'f
d*d+Z(ded(ed)edede)f
d,d-Z*e+defd.d/Z,e+defd0d1Z-d2edefd3d4Z.dedd5fd6d7Z/d8d9 Z0dCd:d;Z1defd<d=Z2defd>d?Z3d5S )DBaseTranslatorz5
    Base abstract class for a translate service
       autotextdestination_languagesource_languagereturnc                 C      |  | | |}| |}| || t|||d}|| jv r*| j| \}}n| |||\}}||f| j|< t| || || ||dS )aR  
        Translates text from a given language to another specific language.

        Parameters:
        ----------
            text : str
                The text to be translated.
            destination_language : str
                If str it expects the language code that the `text` should be translated to.
                to check the list of languages that a `Translator` supports, and use `.get_language` to
                search for a language of the `Translator`, and find it's code.
            source_language : str
                If str it expects the code of the language that the `text` is written in. When using the default value (`auto`),
                the `Translator` will try to find the language automatically.

        Returns:
        --------
            TranslationResult:
                Translation result.

        tdsservicesourcer;   r:   result)_validate_text_detect_and_validate_lang_validate_language_pairstr_translations_cache
_translater   _language_denormalize)r-   r9   r:   r;   	dest_codesource_code
_cache_keytranslationr   r   r"   	translateB       



zBaseTranslator.translatec                 C      t  )z
        Private method that concrete Translators must implement to hold the concrete
        logic for the translations. Receives the validated and normalized parameters and must
        return a translation (str).
        r   r-   r9   r:   r;   r   r   r"   rK   |      zBaseTranslator._translatehtml.parserd   htmlparserthreads_limitc           
         s   t | t |dtf fdd}t|tttfs%tt|t|}n|}dd |jdddD }t|}	|		|| W d   n1 sHw   Y  t|tttfrW|S t|S )	uF  
        Translates the given HTML string or BeautifulSoup object to the given language

        i.e
         English: `<div class="hello"><h1>Hello</h1> everyone and <a href="/welcome">welcome</a> to <span class="w-full">my website</span></div>`
         French: `<div class="hello"><h1>Bonjour</h1>tout le monde et<a href="/welcome">Bienvenue</a>à<span class="w-full">Mon site internet</span></div>`

        Note: This method is not perfect since it is not tag/context aware. Example: `<span>Hello <strong>everyone</strong></span>` will not be understood as
        "Hello everyone" with "everyone" in bold but rather "Hello" and "everyone" separately.

        Warning: If you give a `bs4.BeautifulSoup`, `bs4.element.PageElement` or `bs4.element.Tag` input (which are mutable), they will be modified.
        If you don't want this behavior, please make sure to pass the string version of the element:
        >>> result = BaseTranslator().translate_html(str(page_element), "French")

        Parameters:
        ----------
            html : str | bs4.element.PageElement | bs4.element.Tag | bs4.BeautifulSoup
                The HTML string to be translated. This can also be an instance of BeautifulSoup's `BeautifulSoup` element, `PageElement` or `Tag` element.
            destination_language : str
                The language the HTML string needs to be translated in.
            source_language : str, default = "auto"
                The language of the HTML string.
            parser : str, default = "html.parser"
                The parser that BeautifulSoup will use to parse the HTML string.
            threads_limit : int, default = 100
                The maximum number of threads that will be spawned by translate_html

        Returns:
        --------
            BeautifulSoup:
                The result will be the same element as the input `html` parameter with the values modified if the given
                input is of bs4.BeautifulSoup, bs4.element.PageElement or bs4.element.Tag instance.
            str:
                The result will be a string in any other case.

        nodec                    s8   z|  jt|  dj W d S  ty   Y d S w )N)r:   r;   )replace_withrQ   rI   rE   	Exception)r\   	dest_langr-   source_langr   r"   rK      s
   "z1BaseTranslator.translate_html.<locals>._translatec                 S   s&   g | ]}t |tst|d kr|qS ) )
isinstancer	   r   ).0tagr   r   r"   
<listcomp>   s   & z1BaseTranslator.translate_html.<locals>.<listcomp>T)r9   	recursiveN)
r   r   rc   r   r
   r   rI   find_allr   map)
r-   rY   r:   r;   rZ   r[   rK   pagenodespoolr   r_   r"   translate_html   s   %
zBaseTranslator.translate_htmlc                 C   r=   )a  
        Transliterates text from a given language to another specific language.

        Args:
            text: The text to be transliterated.
            destination_language: If str it expects the language code that the `text` should be translated to.
                to check the list of languages that a `Translator` supports, and use `.get_language` to
                search for a language of the `Translator`, and find it's code. Default value = English
            source_language: If str it expects the code of the language that the `text` is written in. When using the default value (`auto`),
                the `Translator` will try to find the language automatically.

        Returns:
            A `TransliterationResult` object with the results of the translation.
        r>   rB   )rF   rG   rH   rI   _transliterations_cache_transliterater   rL   )r-   r9   r:   r;   rM   rN   rO   transliterationr   r   r"   transliterate   s    



zBaseTranslator.transliteratec                 C   rS   )z
        Private method that concrete Translators must implement to hold the concrete
        logic for the transliteration. Receives the validated and normalized parameters and must
        return a transliteration (str).
        rT   rU   r   r   r"   ro      rV   zBaseTranslator._transliteratec                 C   sp   |  | | |}t||d}|| jv r| j| \}}n| ||\}}||f| j|< t| || ||dS )a  
        Checks text spelling in a given language.

        Args:
            text: The text to be checks.
            source_language: If str it expects the code of the language that the `text` is written in. When using the default value (`auto`),
                the `Translator` will try to find the language automatically.

        Returns:
            A `SpellcheckResult` object with the results of the corrected text.

        )r?   rA   )rC   rD   r;   rE   )rF   rG   rI   _spellchecks_cache_spellcheckr   rL   )r-   r9   r;   rN   rO   
spellcheckr   r   r"   rt      s   


zBaseTranslator.spellcheckc                 C   rS   )z
        Private method that concrete Translators must implement to hold the concrete
        logic for the spellcheck. Receives the validated and normalized parameters and must
        return a spellchecked text (str).
        rT   )r-   r9   r;   r   r   r"   rs   $  rV   zBaseTranslator._spellcheckc                 C   sX   |  | td|i}|| jv r| j| }n
| |}|| j|< | |}t| ||dS )z
        Detect the language of the text

        Args:
            text: The text to be detect the language

        Returns:
            A `LanguageResult` object with the results of the detected language.

        r?   )rC   rD   rE   )rF   rI   _languages_cache	_languagerL   r   )r-   r9   rO   languagedenormalized_langr   r   r"   rw   ,  s   




zBaseTranslator.languagec                 C   rS   )z
        Private method that concrete Translators must implement to hold the concrete
        logic for the language. Receives the validated and normalized parameters and must
        return a language code (str).
        rT   r-   r9   r   r   r"   rv   Q  rV   zBaseTranslator._languagec                 C   r=   )a!  
        Returns a set of examples

        Parameters:
        ----------
            text : str
                The text to be translated.
            destination_language : str
                If str it expects the language code that the `text` should be translated to.
                to check the list of languages that a `Translator` supports, and use `.get_language` to
                search for a language of the `Translator`, and find it's code.
            source_language : str
                If str it expects the code of the language that the `text` is written in. When using the default value (`auto`),
                the `Translator` will try to find the language automatically.

        Returns:
        --------
            ExampleResult:
                Examples result.

        r>   rB   )rF   rG   rH   rI   _examples_cache_exampler   rL   )r-   r9   r:   r;   rM   rN   rO   exampler   r   r"   r|   Y  rR   zBaseTranslator.examplec                 C   rS   )z
        Private method that concrete Translators must implement to hold the concrete
        logic for the translations. Receives the validated and normalized parameters and must
        return a examples list (List).
        rT   rU   r   r   r"   r{     rV   zBaseTranslator._examplec                 C   r=   )a2  
        Returns a list of dictionary results.

        Parameters:
        ----------
            text : str
                The text to be translated.
            destination_language : str
                If str it expects the language code that the `text` should be translated to.
                to check the list of languages that a `Translator` supports, and use `.get_language` to
                search for a language of the `Translator`, and find it's code.
            source_language : str
                If str it expects the code of the language that the `text` is written in. When using the default value (`auto`),
                the `Translator` will try to find the language automatically.

        Returns:
        --------
            DictionaryResult:
                Dictionary result.

        r>   rB   )rF   rG   rH   rI   _dictionaries_cache_dictionaryr   rL   )r-   r9   r:   r;   rM   rN   rO   
dictionaryr   r   r"   r     rR   zBaseTranslator.dictionaryc                 C   rS   )z
        Private method that concrete Translators must implement to hold the concrete
        logic for the translations. Receives the validated and normalized parameters and must
        return a dictionary result list (List).
        rT   rU   r   r   r"   r~     rV   zBaseTranslator._dictionaryfemalespeedgenderc                 C   s   |  | | |}t| }|dvrtdj|dt||||d}|| jv r2| j| \}}n| ||||\}}||f| j|< t	| || 
||||dS )z
        Gives back the text to speech result for the given text

        Args:
            text: text for voice-over
            speed: text speed

        Returns:
            A `TextToSpechResult` object

        >   maler   z>Gender {gender} not supported. Supported genders: male, female)r   )r?   sprA   g)rC   rD   r;   r   r   rE   )rF   rG   r   lowerr   r'   rI   _text_to_speeches_cache_text_to_speechr   rL   )r-   r9   r   r   r;   rN   rO   text_to_speechr   r   r"   r     s$   


zBaseTranslator.text_to_speechc                 C   rS   )z
        Private method that concrete Translators must implement to hold the concrete
        logic for the translations.
        rT   )r-   r9   r   r   r;   r   r   r"   r     s   zBaseTranslator._text_to_speechc                 C      dS )z
        Private method that concrete Translators must implement to hold the concrete
        logic for the translations. Receives the Language instance and must
        return a normalized code language specific of translator (str).
        Nr   )r-   rw   r   r   r"   _language_normalize      z"BaseTranslator._language_normalizec                 C   r   )z
        Private method that concrete Translators must implement to hold the concrete
        logic for the translations. Receives the language code specific of translator and must
        return a Language instance.
        Nr   )r-   language_coder   r   r"   rL     r   z$BaseTranslator._language_denormalizerw   c                 C   sL   t |tr|}nt|}| |}| jr$|| jvr$tdj|t| d|S )zc
        Validates the language code, and converts the language code into a single format.
        z:The language {language_code} is not supported by {service})r   rC   )rc   r   r   _supported_languagesr   r'   rI   )r-   rw   rE   normalized_resultr   r   r"   rG   '  s   


z(BaseTranslator._detect_and_validate_langNc                 C   s6   t |tstdt|jt|dkrtddS )zp
        Performs text validation. Checks the text for the correct type,
        and if it is not empty
        z/Parameter 'text' must be a string, {} was givenrb   z"Parameter 'text' must not be emptyN)rc   rI   r   r'   typer   r   r   ry   r   r   r"   rF   8  s
   
zBaseTranslator._validate_textc                 C   s   ||krt ddS )z3
        Performs language pair validation
        zOParameter source_language cannot be equal to the destination_language parameterN)r   )r-   r;   r:   r   r   r"   rH   C  s   z&BaseTranslator._validate_language_pairc                 C   s@   | j   | j  | j  | j  | j  | j  dS )zB
        Cleans caches

        Returns:
            None
        N)rJ   clearrn   rr   ru   rz   r}   r1   r   r   r"   clean_cacheJ  s   




zBaseTranslator.clean_cachec                 C   s*   | j j}|d|d }|dkrdS |S )z8
        String representation of a translator.
        N	Translaterb   Unknown)r0   r   rfind)r-   
class_namer   r   r"   r2   X  s   zBaseTranslator.__str__c                 C   s   dj |  dS )NzTranslator({translator}))
translator)r'   r2   r1   r   r   r"   __repr__`  r3   zBaseTranslator.__repr__)r8   )r8   rW   rX   )rX   r   r8   )r<   N)4r   r   r   r    r   rJ   rn   ru   rr   rz   r}   r   r   rI   r   rQ   rK   r   r   r
   r   r4   rm   r   rq   ro   r   rt   rs   r   rw   rv   r   r|   r   r{   r   r   r~   r   r   bytesr   r   r   rL   rG   rF   rH   r   r2   r   r   r   r   r"   r6   3   sF    :<83-%:: 3
r6   N)'abcr   r   multiprocessing.poolr   typingr   bs4r   bs4.elementr   r   r	   r
   translatepy.exceptionsr   r   r   r   r   translatepy.languager   translatepy.modelsr   r   r   r   r   r   r   translatepy.utils.annotationsr   translatepy.utils.lru_cacherr   translatepy.utils.sanitizer   r   r$   r6   r   r   r   r"   <module>   s    $	