
    *pj+                     v    S r SSKJrJrJrJrJrJr  SSKJ	r	  Sr
S\S\S\S-  4S	 jrS\S
\\	   S\\	   4S jrg)a  Three-tier language detection for filling DetectionResult languages.

Tier 1: hardcoded mapping for single-language encodings (e.g. Big5 -> Chinese).
Tier 2: statistical bigram scoring against the encoding's language-model variants.
Tier 3: decode to UTF-8 and score against the UTF-8 byte-level language models.

Note: ``from __future__ import annotations`` is intentionally omitted because
this module is compiled with mypyc, which does not support PEP 563 string
annotations.
    )_THIN_RARE_MAX_BYTESRARE_LANGUAGESBigramProfilehas_model_variantsinfer_languagescore_best_language)DetectionResulti   dataencodingreturnNc                     US:X  a  U $  U R                  USS9R                  SSS9$ ! [        [        [        4 a     gf = f)a<  Decode data from encoding and re-encode as UTF-8 for language scoring.

Returns None if the encoding is unknown. For UTF-8, returns data as-is.
Uses ``errors="ignore"`` because the data already passed byte-validity
filtering for the detected encoding; any residual invalid bytes are
irrelevant for language scoring.
utf-8ignore)errorssurrogatepassN)decodeencodeLookupError	TypeError
ValueError)r
   r   s     U/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/chardet/pipeline/language.py_to_utf8r      s[     7{{8H{5<<O = 
 	
 J/ s   ( A A resultsc           	         U S[          n S[        U 5      s=:  =(       a	    [        :  Os  n/ nSnSnSnU GH  nU=(       a=    UR                  SL=(       a(    UR                  [        ;   =(       a    UR
                  SLnUR
                  b  UR                  b  U(       d  UR                  U5        M~  UR
                  n	U(       a  SO
[        U	5      n
U
c1  U (       a*  [        U	5      (       a  Uc  [        U 5      n[        X	XBS9u  pU=(       a    U
SL=(       a	    U
[        ;   nU
b  U(       aZ  U (       aS  [        S5      (       aC  [        X	5      nU(       a1  X:w  a  [        U5      nUn[        USXRS9u  pU
b  Ub  U[        ;  a  Un
U(       a!  U
b
  U
[        ;   a  UR                  U5        GM{  U
c  UR                  U5        GM  UR                  [        XR                  XR                  5      5        GM     U$ )u&  Fill missing ``language`` fields on text results via the three-tier algorithm.

Tier 1: single-language encodings via hardcoded map (instant).
Tier 2: multi-language encodings via statistical bigram scoring (lazy).
Tier 3: decode to UTF-8, score against UTF-8 language models (universal fallback).

Binary results (``encoding is None``) are passed through unchanged, as
are results that already have a non-``None`` language — except a
:data:`~chardet.models.RARE_LANGUAGES` label on a thin input, which is
re-derived through the same scoring so the thin-rare demotion band
applies to statistically-attached labels too, not only to labels this
function computes.  A re-derivation can only *demote* to a prevalent
language; it never swaps one rare label for another.

:param data: The raw byte data the results were produced from.  Truncated
    to the first 2 KB internally — bigram language models converge quickly.
:param results: A list of :class:`DetectionResult` from the pipeline.
:returns: A list of results with ``language`` filled in where possible.
Nr   )profiledemote_thin_rarer   )_LANG_SCORE_MAX_BYTESlenr   languager   r   appendr   r   r   r   r   r	   
confidence	mime_type)r
   r   thinfilledr   utf8_profileutf8_profile_srcresultrecheckr   lang_escalate	utf8_data	utf8_langs                  r   fill_languagesr.   .   s   , &&'D
 s4y////D$&F$(G)-L%) ,t+,>1, t+	 	 ??"v'B7MM&!?? tN8$<<D%7%A%A'-)GA GD,G1GLH$3Eg3N3N 0I0#0#;L'0$2w  <)i~.M$D(> MM&!\MM&!MM*;*;TCSCSTc h M    )__doc__chardet.modelsr   r   r   r   r   r   chardet.pipeliner	   r   bytesstrr   listr.    r/   r   <module>r7      sj   	  -
  5 C EDL $T
T/T	/Tr/   