Ë
    Dü´j’N  ã                   óš  — d Z ddlZddlmZ ddl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 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZ ddlmZ ddlm Z 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+  e
dedd¬«      Z,dZ-dZ.de/de0e/   de/de1e
   fd„Z2dZ3dZ4dZ5d Z6d!Z7d"e8d#e9e*d$f   d%ede9e*d$f   fd&„Z:d'd(œd"e8d)e1e9e/e;f      d#e9e*d$f   d%ed*e<de1e
   fd+„Z=d,e
de
fd-„Z>efddd.d/d'd'd0œd"e8d1ed2e?d3e0e/   dz  d4e0e/   dz  d5e/d6e/d*e<d7e<de1e
   fd8„Z@efddd.d/d'd'd0œd"e8d1ed2e?d3e0e/   dz  d4e0e/   dz  d5e/d6e/d*e<d7e<de1e
   fd9„ZAy):uç   Pipeline orchestrator â€” runs all detection stages in sequence.

Note: ``from __future__ import annotations`` is intentionally omitted because
this module is compiled with mypyc, which does not support PEP 563 string
annotations.
é    N)ÚDEFAULT_MAX_BYTES)ÚEncodingEra)Ú_NONE_RESULTÚDETERMINISTIC_CONFIDENCEÚ
HIGH_BYTESÚDetectionResultÚPipelineContext)Údetect_ascii)Ú	is_binary)Ú
detect_bom)Údetect_escape_encoding)Úfill_languages)Údetect_magic)Údetect_markup_charsetÚpromote_markup_superset)Úpostprocess_results)Úscore_candidates)Úcompute_lead_byte_diversityÚcompute_multibyte_byte_coverageÚcompute_structural_score)Údetect_utf8)Údetect_utf1632_patterns)Úfilter_by_validity)ÚEncodingInfoÚget_candidatesúapplication/octet-stream)ÚencodingÚ
confidenceÚlanguageÚ	mime_typeg333333ë?i @  r   ÚallowedÚ
param_nameÚreturnc                 óz   — | |vr)t        j                  |› d| ›d�t        d¬«       t        gS t	        | dd¬«      gS )zÞReturn a low-confidence result for *encoding*, or ``encoding=None`` if filtered out.

    ``stacklevel=5`` targets the public caller:
    detect() -> run_pipeline() -> _run_pipeline_core() -> _make_fallback_or_none().
    ú zL is excluded by include_encodings/exclude_encodings; returning encoding=Noneé   )Ú
stacklevelgš™™™™™¹?N)r   r   r   )ÚwarningsÚwarnÚUserWarningr   r   )r   r!   r"   s      ú[/root/workspace/ytshorts/venv/lib/python3.12/site-packages/chardet/pipeline/orchestrator.pyÚ_make_fallback_or_noner,   8   sQ   € ð �wÑÜ�‰Øˆl˜!˜H˜<ð (Kð LäØõ		
ô ˆ~ÐÜ X¸$ÈÔNÐOÐOó    gš™™™™™©?é   gffffffÖ?é   é   ÚdataÚvalid_candidates.Úctxc                 ó4  — g }|D �]  }|j                   ræt        | ||«      }||j                  |j                  <   |t        k  rŒ@|j
                  €0t        | «      t        | j                  dt        «      «      z
  |_        |j
                  t        k  rŒ�t        | |||j
                  ¬«      }||j                  |j                  <   |t        k  rŒÌ|j
                  t        k\  rt        | ||«      }|t        k  rŒö|j!                  |«       �Œ	 t#        |«      S )aÇ  Eliminate CJK multi-byte candidates that lack genuine multi-byte structure.

    Four checks are applied in order to each multi-byte candidate:

    1. **Structural pair ratio** (valid_pairs / lead_bytes) must be
       >= ``_CJK_MIN_MB_RATIO``.  Catches files with many orphan lead bytes.

    2. **Minimum non-ASCII byte count**: the data must contain at least
       ``_CJK_MIN_NON_ASCII`` bytes > 0x7F.  Tiny files with 1-5 high bytes
       can accidentally form perfect pairs and score 1.0 structurally.

    3. **Byte coverage** (non-ASCII bytes in valid multi-byte sequences /
       total non-ASCII bytes) must be >= ``_CJK_MIN_BYTE_COVERAGE``.  Latin
       text has many high bytes that are NOT consumed by multi-byte pairs;
       genuine CJK text has nearly all high bytes accounted for.

    4. **Lead byte diversity**: the number of distinct lead byte values in
       valid pairs must be >= ``_CJK_MIN_LEAD_DIVERSITY``.  Genuine CJK text
       draws from a wide repertoire of lead bytes; European false positives
       cluster in a narrow band (e.g. 0xC0-0xDF for accented Latin).

    Returns the filtered candidate list.  Structural scores are cached in
    ``ctx.mb_scores`` for reuse in Stage 2b.
    N)Únon_ascii_count)Úis_multibyter   Ú	mb_scoresÚnameÚ_CJK_MIN_MB_RATIOr5   ÚlenÚ	translater   Ú_CJK_MIN_NON_ASCIIr   Úmb_coverageÚ_CJK_MIN_BYTE_COVERAGEÚ_CJK_DIVERSITY_MIN_NON_ASCIIr   Ú_CJK_MIN_LEAD_DIVERSITYÚappendÚtuple)r1   r2   r3   ÚgatedÚencÚmb_scoreÚbyte_coverageÚlead_diversitys           r+   Ú_gate_cjk_candidatesrH   k   s  € ð: !#€EØó ˆØ×ÒÜ/°°c¸3Ó?ˆHØ&.ˆC�M‰M˜#Ÿ(™(Ñ#ØÔ+Ò+ØØ×"Ñ"Ð*Ü&)¨$£i´#°d·n±nÀTÌ:Ó6VÓ2WÑ&W�Ô#Ø×"Ñ"Ô%7Ò7ØÜ;Ø�c˜3°×0CÑ0CôˆMð )6ˆC�O‰O˜CŸH™HÑ%ØÔ5Ò5ØØ×"Ñ"Ô&BÒBÜ!<¸TÀ3ÈÓ!L�Ø!Ô$;Ò;ØØ�‰�SÖð)ô* �‹<Ðr-   F©Úfull_rankingÚstructural_scoresrJ   c          	      óZ  ‡— |D �ci c]  }|j                   sŒ|j                  |“Œ c}Št        ˆfd„|D «       «      }t        d„ |D «       «      }t        t	        | dt
         g |¢|¢­|¬«      «      }g }	|D ]˜  }
|
j                  r&|j                  j                  |
j                  d«      nd}|dk\  rL|	j                  t        |
j                  |
j                  d|z   z  |
j                  |
j                  «      «       Œˆ|	j                  |
«       Œš |	j                  d„ d	¬
«       |	S c c}w )a‘  Score structurally-valid CJK candidates using statistical bigrams.

    When multiple CJK encodings score equally high structurally, statistical
    scoring differentiates them (e.g. euc-jp vs big5 for Japanese data).
    Single-byte candidates are also scored and included so that the caller
    can compare CJK vs single-byte confidence.

    Multi-byte candidates with high byte coverage (>= 0.95) receive a
    confidence boost proportional to coverage.  When nearly all non-ASCII
    bytes form valid multi-byte pairs, the structural evidence is strong
    and should increase the candidate's ranking relative to single-byte
    alternatives whose bigram models may score higher on small samples.

    Note: boosted confidence values may exceed 1.0 and are used only for
    relative ranking among candidates.  ``run_pipeline`` clamps all
    confidence values to [0.0, 1.0] before returning to callers.
    c              3   ó8   •K  — | ]  \  }}|‰v sŒ‰|   –— Œ y ­w©N© )Ú.0r8   Ú_scÚ
enc_lookups      €r+   ú	<genexpr>z/_score_structural_candidates.<locals>.<genexpr>½   s&   øè ø€ ò Ù&˜T 3¸tÀzÒ?Qˆ
�4Õñùs   ƒ�
c              3   ó:   K  — | ]  }|j                   rŒ|–— Œ y ­wrN   )r6   )rP   Úes     r+   rS   z/_score_structural_candidates.<locals>.<genexpr>À   s   è ø€ ÒJ˜a¸1¿>»>œÑJùs   ‚”NrI   ç        gffffffî?é   c                 ó   — | j                   S rN   )r   ©Úxs    r+   ú<lambda>z._score_structural_candidates.<locals>.<lambda>Õ   s
   € ˜qŸ|™|€ r-   T©ÚkeyÚreverse)r6   r8   rB   Úlistr   Ú_STAT_SCORE_MAX_BYTESr   r=   ÚgetrA   r   r   r   r    Úsort)r1   rK   r2   r3   rJ   rU   Úvalid_mbÚsingle_byteÚresultsÚboostedÚrÚcoveragerR   s               @r+   Ú_score_structural_candidatesri   ¡   s#  ø€ ð4 ,ö+Ø¨q¯~«~ˆ�‰�‰	ò+€Jô ó Ø*;ôó €Hô ÑJÐ#3ÔJÓJ€KÜÜØÐ'Ô'Ð(Ø%ˆhÐ%˜Ñ%Ø%ô	
ó€Gð &(€GØò 	ˆØ;<¿:º:�3—?‘?×&Ñ& q§z¡z°3Ô7È3ˆØ�tÒØ�N‰NÜØ—J‘J §¡°°H±Ñ =¸q¿z¹zÈ1Ï;É;óõð �N‰N˜1Õð	ð ‡L�LÑ+°T€LÔ:Ø€Nùò9+s
   †D(˜D(Úresultc                 ó–   — | j                   �| S | j                  �dnd}t        | j                  | j                  | j                  |«      S )zXDefault ``mime_type`` to ``text/plain`` (text) or ``application/octet-stream`` (binary).z
text/plainr   )r    r   r   r   r   )rj   Úmimes     r+   Ú_with_default_mimerm   Ù   sD   € à×ÑÐ#ØˆØ!Ÿ?™?Ð6‰<Ð<V€DÜ˜6Ÿ?™?¨F×,=Ñ,=¸v¿¹ÐPTÓUÐUr-   Úcp1252zutf-8)Úinclude_encodingsÚexclude_encodingsÚno_match_encodingÚempty_input_encodingrJ   Úinput_truncatedÚencoding_eraÚ	max_bytesro   rp   rq   rr   rs   c                ó¬  — t        «       }	|xs t        | «      |kD  }| d| } t        |||«      }
t        d„ |
D «       «      }| st	        ||d«      S t        | «      }|�|j                  |v r|gS t        | «      }|�|j                  |v r|gS t        | «      }|�|j                  �|j                  |v r|gS t        | «      }|�|gS t        | «      }t        | «      }|€|€t        | |¬«      rt        gS t        | «      }|�~|j                  |v rp|�^|j                  |j                  k7  rE|j                  |v r7t        |j                  |j                   |j"                  |j$                  «      gS t'        | ||«      }|gS |�|j                  |v r|gS |�|j                  |v r|gS t)        | |
«      }|st	        ||d«      S t+        | ||	«      }|st	        ||d«      S g }|D ]f  }|j,                  sŒ|	j.                  j1                  |j2                  «      }|€t5        | ||	«      }|dkD  sŒJ|j7                  |j2                  |f«       Œh |rE|j9                  d„ d¬	«       |d
   \  }}|t:        k\  r t=        | |||	|¬«      }|rt?        | ||¬«      S | dt@         }tC        tE        |tG        |«      |¬«      «      }|st	        ||d«      S t?        | ||¬«      S )zBCore pipeline logic. Returns list of results sorted by confidence.Nc              3   ó4   K  — | ]  }|j                   –— Œ y ­wrN   )r8   )rP   rD   s     r+   rS   z%_run_pipeline_core.<locals>.<genexpr>ö   s   è ø€ Ò'G°S¨¯­Ñ'Gùs   ‚rr   )ru   rq   rV   c                 ó   — | d   S )NrW   rO   rY   s    r+   r[   z$_run_pipeline_core.<locals>.<lambda>p  s
   € ¨Q¨q©T€ r-   Tr\   r   rI   )rs   )$r	   r:   r   Ú	frozensetr,   r   r   r   r   r   r   r
   r   Ú_BINARY_RESULTr   r   r   r   r    r   r   rH   r6   r7   ra   r8   r   rA   rb   Ú _STRUCTURAL_CONFIDENCE_THRESHOLDri   r   r`   r_   r   rB   )r1   rt   ru   ro   rp   rq   rr   rJ   rs   r3   Ú
candidatesr!   Ú
bom_resultÚutf1632_resultÚescape_resultÚmagic_resultÚutf8_precheckÚascii_precheckÚmarkup_resultr2   rK   rD   ÚscoreÚ_Ú
best_scorere   Ú	stat_datas                              r+   Ú_run_pipeline_corerˆ   á   ss  € ô Ó
€CØ%Ò>¬¨T«°YÑ)>€OØ�
�Ð€Dô
   Ð.?ÐARÓS€JÜ'Ñ'G¸JÔ'GÓG€GáÜ%Ø  'Ð+Aó
ð 	
ô ˜DÓ!€JØÐ *×"5Ñ"5¸Ñ"@Øˆ|Ðô
 -¨TÓ2€NØÐ! n×&=Ñ&=ÀÑ&HØÐÐô
 +¨4Ó0€MàÐ!Ø×"Ñ"Ð.Ø×"Ñ" gÑ-àˆÐô   Ó%€LØÐØˆ~Ðô   Ó%€Mô " $Ó'€Nð
 	ÐØÐ"Ü�d iÕ0äÐÐô
 *¨$Ó/€MØÐ  ]×%;Ñ%;¸wÑ%Fð Ð%Ø×&Ñ&¨-×*@Ñ*@Ò@Ø×&Ñ&¨'Ñ1ô  Ø!×*Ñ*Ø!×,Ñ,Ø!×*Ñ*Ø!×+Ñ+ó	ðð ô 0°°mÀWÓMˆØˆÐð Ð! n×&=Ñ&=ÀÑ&HØÐÐð Ð  ]×%;Ñ%;¸wÑ%FØˆÐô *¨$°
Ó;ÐáÜ%Ð&7¸ÐBUÓVÐVô ,¨DÐ2BÀCÓHÐáÜ%Ð&7¸ÐBUÓVÐVð 24ÐØò <ˆØ×ÓØ—M‘M×%Ñ% c§h¡hÓ/ˆEØˆ}Ü0°°s¸CÓ@�Ø�s‹{Ø!×(Ñ(¨#¯(©(°EÐ):Õ;ð<ñ Ø×Ñ¡>¸4ÐÔ@Ø)¨!Ñ,‰ˆˆ:ØÔ9Ò9Ü2ØØ!Ø ØØ)ôˆGñ Ü*Ø˜'°?ôð ð Ð+Ô+Ð,€IÜÜ˜¤EÐ*:Ó$;È,ÔWó€Gñ Ü%Ð&7¸ÐBUÓVÐVä˜t W¸oÔNÐNr-   c                ó   — t        | ||||||||¬«	      }	t        | |	«      }	|	D �
cg c]?  }
|
j                  dk(  r,t        |
j                  |
j
                  d|
j                  «      n|
‘ŒA }	}
|	D �
cg c]  }
t        |
«      ‘Œ }	}
|	sd}t        |«      ‚|	D �
cg c]S  }
|
j
                  dkD  r@t        |
j                  t        |
j
                  d«      |
j                  |
j                  «      n|
‘ŒU c}
S c c}
w c c}
w c c}
w )aË  Run the full detection pipeline.

    :param data: The raw byte data to analyze.
    :param encoding_era: Filter candidates to a specific era of encodings.
    :param max_bytes: Maximum number of bytes to process.
    :param include_encodings: If not ``None``, only return these encodings.
    :param exclude_encodings: If not ``None``, never return these encodings.
    :param no_match_encoding: Encoding returned when no candidate survives.
    :param empty_input_encoding: Encoding returned for empty input.
    :param full_ranking: When ``True``, statistical scoring evaluates every
        candidate so the returned list is complete (``detect_all`` needs
        this).  When ``False``, candidates that provably cannot affect the
        top of the ranking may be omitted from the tail.
    :param input_truncated: Pass ``True`` when *data* is already a truncated
        view of the caller's input (``UniversalDetector`` caps its buffer at
        ``max_bytes``, which this function cannot see from ``len(data)``
        alone).  Truncation by the ``max_bytes`` slice here is detected
        either way; the flag only ever widens it.
    :returns: A list of :class:`DetectionResult` sorted by confidence descending.
    )rs   ro   rp   rq   rr   rJ   ÚzxxNz/pipeline must always return at least one resultg      ð?)
rˆ   r   r   r   r   r   r    rm   ÚRuntimeErrorÚmin)r1   rt   ru   ro   rp   rq   rr   rJ   rs   re   rg   Úmsgs               r+   Úrun_pipelinerŽ   Œ  s  € ô@ !ØØØØ'Ø+Ø+Ø+Ø1Ø!ô
€Gô ˜T 7Ó+€Gð ö	ð ð �:‰:˜Òô 	˜Ÿ
™
 A§L¡L°$¸¿¹ÔDàñ	ð€Gð ð /6Ö6¨Ô! !Õ$Ð6€GÐ6ÙØ?ˆÜ˜3ÓÐð ö	ð ð �<‰<˜#Òô 	˜Ÿ
™
¤C¨¯©°cÓ$:¸A¿J¹JÈÏÉÔTàñ	òð ùòùò 7ùòs   ¥AC1Á/C6ÂAC;)BÚ__doc__r(   Úchardet._utilsr   Úchardet.enumsr   Úchardet.pipeliner   r   r   r   r	   Úchardet.pipeline.asciir
   Úchardet.pipeline.binaryr   Úchardet.pipeline.bomr   Úchardet.pipeline.escaper   Úchardet.pipeline.languager   Úchardet.pipeline.magicr   Úchardet.pipeline.markupr   r   Úchardet.pipeline.postprocessr   Úchardet.pipeline.statisticalr   Úchardet.pipeline.structuralr   r   r   Úchardet.pipeline.utf8r   Úchardet.pipeline.utf1632r   Úchardet.pipeline.validityr   Úchardet.registryr   r   rz   r{   r`   Ústrry   r_   r,   r9   r<   r>   r@   r?   ÚbytesrB   rH   ÚfloatÚboolri   rm   Úintrˆ   rŽ   rO   r-   r+   ú<module>r¦      sæ  ðñó å ,Ý %÷õ õ 0Ý -Ý +Ý :Ý 4Ý /ß RÝ <Ý 9÷ñ õ
 .Ý <Ý 8ß 9á ØØ'ØØ(ô	€ð $(Ð  ð Ð ðPØðPà�s‰^ðPð ðPð 
ˆ/Ñó	Pð2 Ð ð Ð ð Ð ð Ð ð  "Ð ð3Ø
ð3à˜L¨#Ð-Ñ.ð3ð 
ð3ð ˆ<˜ÐÑó	3ðx ò5Ø
ð5à˜E # u *Ñ-Ñ.ð5ð ˜L¨#Ð-Ñ.ð5ð 
ð	5ð ð5ð 
ˆ/Ñó5ðpV˜ð V°?ó Vð 'ðhOð
 04Ø/3Ø%Ø 'ØØ!òhOØ
ðhOàðhOð ðhOð
 ! ‘~¨Ñ,ðhOð ! ‘~¨Ñ,ðhOð ðhOð ðhOð ðhOð ðhOð 
ˆ/ÑóhOð\ 'ðAð
 04Ø/3Ø%Ø 'ØØ!òAØ
ðAàðAð ðAð
 ! ‘~¨Ñ,ðAð ! ‘~¨Ñ,ðAð ðAð ðAð ðAð ðAð 
ˆ/ÑôAr-   