§
    íóÉjÓ#  ã                   óŒ   — d Z ddlmZ ddlmZ 	 ddlZn# e$ r dZY nw xY w G d„ d¦  «        Zd„ Z	e
dk    r e	¦   «          dS dS )	a˜  
A module for language identification using the TextCat algorithm.
An implementation of the text categorization algorithm
presented in Cavnar, W. B. and J. M. Trenkle,
"N-Gram-Based Text Categorization".

The algorithm takes advantage of Zipf's law and uses
n-gram frequencies to profile languages and text-yet to
be identified-then compares using a distance measure.

Language n-grams are provided by the "An Crubadan"
project. A corpus reader was created separately to read
those files.

For details regarding the algorithm, see:
https://www.let.rug.nl/~vannoord/TextCat/textcat.pdf

For details about An Crubadan, see:
https://borel.slu.edu/crubadan/index.html
é    )Úmaxsize)ÚtrigramsNc                   óH   — e Zd ZdZi ZdZdZi Zd„ Zd„ Z	d„ Z
d„ Zd„ Zdd
„ZdS )ÚTextCatNú<ú>c                 ó¸   — t           st          d¦  «        ‚ddlm} || _        | j                             ¦   «         D ]}| j                             |¦  «         Œd S )Nz›classify.textcat requires the regex module that supports unicode. Try '$ pip install regex' and see https://pypi.python.org/pypi/regex for further details.r   )Úcrubadan)ÚreÚOSErrorÚnltk.corpusr
   Ú_corpusÚlangsÚ	lang_freq)Úselfr
   Úlangs      úx/home/lighthouse/badge-speaker-benchmark/.benchmark-venv-community/lib/python3.11/site-packages/nltk/classify/textcat.pyÚ__init__zTextCat.__init__7   s|   € Ýð 	Ýð#ñô ð ð 	)Ð(Ð(Ð(Ð(Ð(àˆŒà”L×&Ò&Ñ(Ô(ð 	)ð 	)ˆDØŒL×"Ò" 4Ñ(Ô(Ð(Ð(ð	)ð 	)ó    c                 ó.   — t          j        dd|¦  «        S )z)Get rid of punctuation except apostrophesz[^\P{P}\']+Ú )r   Úsub)r   Útexts     r   Úremove_punctuationzTextCat.remove_punctuationG   s   € åŒv�n b¨$Ñ/Ô/Ð/r   c                 ó  — ddl m}m} |                      |¦  «        } ||¦  «        } |¦   «         }|D ]L}t	          | j        |z   | j        z   ¦  «        }d„ |D ¦   «         }	|	D ]}
|
|v r||
xx         dz  cc<   Œd||
<   ŒŒM|S )z'Create FreqDist of trigrams within textr   )ÚFreqDistÚword_tokenizec                 ó8   — g | ]}d                       |¦  «        ‘ŒS )r   )Újoin)Ú.0Útris     r   ú
<listcomp>z#TextCat.profile.<locals>.<listcomp>U   s"   € ÐKÐKÐK¨s˜bŸgšg c™lœlÐKÐKÐKr   é   )Únltkr   r   r   r   Ú_START_CHARÚ	_END_CHAR)r   r   r   r   Ú
clean_textÚtokensÚfingerprintÚtÚtoken_trigram_tuplesÚtoken_trigramsÚcur_trigrams              r   ÚprofilezTextCat.profileK   sÚ   € à0Ð0Ð0Ð0Ð0Ð0Ð0Ð0à×,Ò,¨TÑ2Ô2ˆ
Ø�˜zÑ*Ô*ˆà�h‘j”jˆØð 	1ð 	1ˆAÝ#+¨DÔ,<¸qÑ,@À4Ä>Ñ,QÑ#RÔ#RÐ ØKÐKÐ6JÐKÑKÔKˆNà-ð 1ð 1�Ø +Ð-Ð-Ø Ð,Ð,Ô,°Ñ1Ð,Ð,Ñ,Ð,à/0�K Ñ,Ð,ð	1ð Ðr   c                 óJ  — | j                              |¦  «        }d}||v r{t          |                     ¦   «         ¦  «                             |¦  «        }t          |                     ¦   «         ¦  «                             |¦  «        }t          ||z
  ¦  «        }nt          }|S )zgCalculate the "out-of-place" measure between the
        text and language profile for a single trigramr   )r   r   ÚlistÚkeysÚindexÚabsr   )r   r   ÚtrigramÚtext_profileÚlang_fdÚdistÚidx_lang_profileÚidx_texts           r   Ú	calc_distzTextCat.calc_dist_   s—   € ð ”,×(Ò(¨Ñ.Ô.ˆØˆà�gÐÐÝ# G§L¢L¡N¤NÑ3Ô3×9Ò9¸'ÑBÔBÐÝ˜L×-Ò-Ñ/Ô/Ñ0Ô0×6Ò6°wÑ?Ô?ˆHõ Ð'¨(Ñ2Ñ3Ô3ˆDˆDõ
 ˆDàˆr   c                 ó‚  — i }|                       |¦  «        }d„ t          |¦  «        D ¦   «         }| j        j                             ¦   «         D ]o}d„ t          | j                             |¦  «        ¦  «        D ¦   «         }d}|D ]2}||v r"|t          ||         ||         z
  ¦  «        z  }Œ(|t          z  }Œ3|||<   Œp|S )zOCalculate the "out-of-place" measure between
        the text and all languagesc                 ó   — i | ]\  }}||“Œ	S © r=   )r    Úir4   s      r   ú
<dictcomp>z&TextCat.lang_dists.<locals>.<dictcomp>€   s   € ÐFÐFÐF¡Z Q¨�g˜qÐFÐFÐFr   c                 ó   — i | ]\  }}||“Œ	S r=   r=   )r    r>   r*   s      r   r?   z&TextCat.lang_dists.<locals>.<dictcomp>‚   s   € ÐSÐSÐS¡4 1 a˜!˜QÐSÐSÐSr   r   )r.   Ú	enumerater   Ú_all_lang_freqr1   r   r3   r   )	r   r   Ú	distancesr.   Ú
text_ranksr   Ú
lang_ranksÚ	lang_distr4   s	            r   Ú
lang_distszTextCat.lang_distst   sâ   € ð ˆ	Ø—,’,˜tÑ$Ô$ˆð GÐFµ9¸WÑ3EÔ3EÐFÑFÔFˆ
Ø”LÔ/×4Ò4Ñ6Ô6ð 	(ð 	(ˆDØSÐS­9°T´\×5KÒ5KÈDÑ5QÔ5QÑ+RÔ+RÐSÑSÔSˆJØˆIØ"ð )ð )�Ø˜jÐ(Ð(Ø¥ Z°Ô%8¸:ÀgÔ;NÑ%NÑ!OÔ!OÑO�I�Ià¥Ñ(�I�IØ'ˆI�d‰OˆOàÐr   Fc                 óÂ  ‡— |                       |¦  «        | _        | j        s|rg S dS t          | j                             ¦   «         ¦  «        Šˆfd„| j                             ¦   «         D ¦   «         }t          | j                             ¦   «         ¦  «        }t          |¦  «        t          |¦  «        k    r|rg S dS |r|S t          |¦  «        dk    r|d         S dS )aâ  
        Determines the most likely language(s) for the given text.

        Parameters
        ----------
        text : str
            The text whose language is to be identified.
        return_all : bool, optional
            If False (default), returns a single ISO 639-3 language code as a str,
            or None if the language is ambiguous or cannot be determined.
            If True, returns a list of all language codes sharing the minimal distance.
            The list will have one element if there is a unique best match,
            multiple elements for ties, or be empty if no language is found.

        Returns
        -------
        str or None, or list of str
            If return_all is False:
                - str: language code if unique minimum found
                - None: if ambiguous or not classifiable
            If return_all is True:
                - list: possible language code(s), or empty list if not classifiable

        Examples
        --------
        >>> from nltk.classify.textcat import TextCat
        >>> cat = TextCat()
        >>> print(cat.guess_language('The quick brown fox jumps over the lazy dog.'))
        eng

        A case with no information, returns None or an empty list:

        >>> print(cat.guess_language('', return_all=True))
        []
        >>> print(cat.guess_language(''))
        None

        A case where a single short input ties between Catalan and French:

        >>> print(sorted(cat.guess_language('ent', return_all=True)))
        ['cat', 'fra']

        By default (`return_all=False`), in a tie, guess_language returns None:

        >>> print(cat.guess_language('ent'))
        None

        Note: For short or generic inputs, or for closely related languages,
        the classifier may return an unexpected language. For example,
        the following is a perfectly grammatical English sentence, but may
        be classified as Scots ('sco') due to profile similarity:

        >>> print(cat.guess_language('This is a short English sentence.'))
        sco

        This behavior is not a bug, but an artifact of the underlying n-gram profiles.
        The classifier should be used with sufficiently distinctive and longer text fragments
        for best accuracy.
        Nc                 ó&   •— g | ]\  }}|‰k    ¯|‘ŒS r=   r=   )r    r   r7   Úmin_dists      €r   r"   z*TextCat.guess_language.<locals>.<listcomp>Ï   s.   ø€ ð 
ð 
ð 
Ù�T˜4À$È(ÒBRÐBRˆDÐBRÐBRÐBRr   r#   r   )rG   Úlast_distancesÚminÚvaluesÚitemsr0   r1   Úlen)r   r   Ú
return_allÚ
candidatesÚall_languagesrJ   s        @r   Úguess_languagezTextCat.guess_language�   s  ø€ ðx #Ÿošo¨dÑ3Ô3ˆÔØÔ"ð 	Øð Ø�	Ø�4Ý�tÔ*×1Ò1Ñ3Ô3Ñ4Ô4ˆð
ð 
ð 
ð 
Ø#'Ô#6×#<Ò#<Ñ#>Ô#>ð
ñ 
ô 
ˆ
õ ˜TÔ0×5Ò5Ñ7Ô7Ñ8Ô8ˆõ ˆz‰?Œ?�c -Ñ0Ô0Ò0Ð0Øð Ø�	Ø�4àð 	ØÐÝˆz‰?Œ?˜aÒÐØ˜a”=Ð Øˆtr   )F)Ú__name__Ú
__module__Ú__qualname__r   Úfingerprintsr%   r&   rK   r   r   r.   r:   rG   rS   r=   r   r   r   r   /   s’   € € € € € Ø€GØ€LØ€KØ€Ià€Nð)ð )ð )ð 0ð 0ð 0ðð ð ð(ð ð ð*ð ð ð2Qð Qð Qð Qð Qð Qr   r   c                  óN  ‡
‡— ddl m}  g d¢}dddddd	d
dddœ	}t          ¦   «         }|D ]û}|                      |¦  «        Št	          ‰¦  «        dz
  }t          t          t          ‰¦  «        ¦  «        }d}t          d|¦  «        D ]@Š
dd                     ˆ
ˆfd„t          d|‰
         ¦  «        D ¦   «         ¦  «        z   }||z  }ŒAt          d|dd…         z   dz   ¦  «         | 
                    |¦  «        }	t          d|	› d||	         › d�¦  «         t          d¦  «         Œüd S )Nr   )Úudhr)	zKurdish-UTF8zAbkhaz-UTF8zFarsi_Persian-UTF8z
Hindi-UTF8zHawaiian-UTF8zRussian-UTF8zVietnamese-UTF8zSerbian_Srpski-UTF8zEsperanto-UTF8zNorthern KurdishÚ	AbkhazianzIranian PersianÚHindiÚHawaiianÚRussianÚ
VietnameseÚSerbianÚ	Esperanto)	ÚkmrÚabkÚpesÚhinÚhawÚrusÚvieÚsrpÚepor#   r   ú c                 ó,   •— g | ]}‰‰         |         ‘ŒS r=   r=   )r    Újr>   Úraw_sentencess     €€r   r"   zdemo.<locals>.<listcomp>  s#   ø€ Ð&VÐ&VÐ&V¸q }°QÔ'7¸Ô':Ð&VÐ&VÐ&Vr   zLanguage snippet: éŒ   z...zLanguage detection: z (ú)zŒ############################################################################################################################################)r   rY   r   ÚsentsrO   r0   ÚmapÚranger   ÚprintrS   )rY   r   ÚfriendlyÚtcÚcur_langÚrowsÚcolsÚsampleÚcur_sentÚguessr>   rm   s             @@r   Údemor|   á   s‚  øø€ Ø Ð Ð Ð Ð Ð ð
ð 
ð 
€Eð "ØØ ØØØØØØð
ð 
€Hõ 
‰Œ€Bàð ð ˆàŸ
š
 8Ñ,Ô,ˆÝ�=Ñ!Ô! AÑ%ˆÝ•C�˜]Ñ+Ô+Ñ,Ô,ˆàˆõ �q˜$‘”ð 	ð 	ˆAØ˜SŸXšXÐ&VÐ&VÐ&VÐ&VÐ&VÅEÈ!ÈTÐRSÌWÑDUÔDUÐ&VÑ&VÔ&VÑWÔWÑWˆHØ�hÑˆFˆFõ 	Ð" V¨A¨c¨E¤]Ñ2°UÑ:Ñ;Ô;Ð;Ø×!Ò! &Ñ)Ô)ˆÝÐ@ UÐ@Ð@¨h°u¬oÐ@Ð@Ð@ÑAÔAÐAÝˆiÑÔÐÐð#ð r   Ú__main__)Ú__doc__Úsysr   Ú	nltk.utilr   Úregexr   ÚImportErrorr   r|   rT   r=   r   r   ú<module>rƒ      sÒ   ððð ð* Ð Ð Ð Ð Ð à Ð Ð Ð Ð Ð ðØÐÐÐÐøØð ð ð Ø	€B€B€Bðøøøðoð oð oð oð oñ oô oð oðd.ð .ð .ðb ˆzÒÐØ€D�F„F€F€F€Fð Ðs   � •ž