
    *pjN                        S r SSKrSSKJr  SSKJr  SSKJrJrJ	r	J
r
Jr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJrJr  SSKJr  SSKJr  SSKJ r J!r!J"r"  SSK#J$r$  SSK%J&r&  SSK'J(r(  SSK)J*r*J+r+  \
" S\SSS9r,Sr-Sr.S\/S\0\/   S\/S\1\
   4S jr2Sr3Sr4Sr5S r6S!r7S"\8S#\9\*S$4   S%\S\9\*S$4   4S& jr:S'S(.S"\8S)\1\9\/\;4      S#\9\*S$4   S%\S*\<S\1\
   4S+ jjr=S,\
S\
4S- jr>\4SSS.S/S'S'S0.S"\8S1\S2\?S3\0\/   S-  S4\0\/   S-  S5\/S6\/S*\<S7\<S\1\
   4S8 jjjr@\4SSS.S/S'S'S0.S"\8S1\S2\?S3\0\/   S-  S4\0\/   S-  S5\/S6\/S*\<S7\<S\1\
   4S9 jjjrAg):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                 t    X;  a(  [         R                  " U SU < S3[        SS9  [        /$ [	        U SSS9/$ )zReturn 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      Y/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/chardet/pipeline/orchestrator.py_make_fallback_or_noner,   8   sO     l!H< (K L		
 ~X$NOO    g?   gffffff?      datavalid_candidates.ctxc                 D   / nU GH  nUR                   (       a  [        XU5      nXRR                  UR                  '   U[        :  a  ME  UR
                  c0  [        U 5      [        U R                  S[        5      5      -
  Ul        UR
                  [        :  a  M  [        XX"R
                  S9nXbR                  UR                  '   U[        :  a  M  UR
                  [        :  a  [        XU5      nU[        :  a  M  UR!                  U5        GM     [#        U5      $ )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/3?H&.MM#((#++""*&)$i#dnnT:6V2W&W#""%77;30C0CM )6OOCHH%55""&BB!<T!L!$;;S)  * <r-   Ffull_rankingstructural_scoresrJ   c          	      r  ^ U Vs0 s H"  oUR                   (       d  M  UR                  U_M$     snm[        U4S jU 5       5      n[        S U 5       5      n[        [	        U S[
         / UQUQ7US95      n/ n	U H  n
U
R                  (       a&  UR                  R                  U
R                  S5      OSnUS:  aM  U	R                  [        U
R                  U
R                  SU-   -  U
R                  U
R                  5      5        M  U	R                  U
5        M     U	R                  S S	S
9  U	$ s  snf )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   B   >#    U  H  u  pUT;   d  M  TU   v   M     g 7fN ).0r8   _sc
enc_lookups      r+   	<genexpr>/_score_structural_candidates.<locals>.<genexpr>   s&      *;YTtz?Q
4*;s   c              3   J   #    U  H  oR                   (       a  M  Uv   M     g 7frN   )r6   )rP   es     r+   rS   rT      s     J#3a>>#3s   #	#NrI           gffffff?   c                     U R                   $ rN   )r   xs    r+   <lambda>._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   rV   valid_mbsingle_byteresultsboostedrcoveragerR   s               @r+   _score_structural_candidatesrk      s#   4 ,++a~~		++J  *; H J#3JJK''(%h%%%	
G &(G;<::3??&&qzz373tNNJJH =qzz1;; NN1  LL+TL:N9+s
   D4D4resultc                     U R                   b  U $ U R                  b  SOSn[        U R                  U R                  U R                  U5      $ )zXDefault ``mime_type`` to ``text/plain`` (text) or ``application/octet-stream`` (binary).z
text/plainr   )r    r   r   r   r   )rl   mimes     r+   _with_default_mimero      sD    #!??6<<VD6??F,=,=vPTUUr-   cp1252zutf-8)include_encodingsexclude_encodingsno_match_encodingempty_input_encodingrJ   input_truncatedencoding_era	max_bytesrq   rr   rs   rt   ru   c                   [        5       n	U=(       d    [        U 5      U:  nU SU n [        XU5      n
[        S U
 5       5      nU (       d  [	        XkS5      $ [        U 5      nUb  UR                  U;   a  U/$ [        U 5      nUb  UR                  U;   a  U/$ [        U 5      nUb   UR                  b  UR                  U;   a  U/$ [        U 5      nUb  U/$ [        U 5      n[        U 5      nUc  Uc  [        XS9(       a  [        /$ [        U 5      nUb  UR                  U;   at  Uba  UR                  UR                  :w  aG  UR                  U;   a7  [        UR                  UR                   UR"                  UR$                  5      /$ ['        U UU5      nU/$ Ub  UR                  U;   a  U/$ Ub  UR                  U;   a  U/$ [)        X
5      nU(       d  [	        X[S5      $ [+        U UU	5      nU(       d  [	        X[S5      $ / nU Hp  nUR,                  (       d  M  U	R.                  R1                  UR2                  5      nUc  [5        U UU	5      nUS:  d  MS  UR7                  UR2                  U45        Mr     U(       aB  UR9                  S SS	9  US
   u  nnU[:        :  a  [=        U UUU	US9nU(       a  [?        U UUS9$ U S[@         n[C        [E        U[G        U5      US95      nU(       d  [	        X[S5      $ [?        U UUS9$ )zBCore pipeline logic. Returns list of results sorted by confidence.Nc              3   8   #    U  H  oR                   v   M     g 7frN   )r8   )rP   rD   s     r+   rS   %_run_pipeline_core.<locals>.<genexpr>   s     'GJSJs   rt   )rw   rs   rW   c                     U S   $ )NrX   rO   rZ   s    r+   r\   $_run_pipeline_core.<locals>.<lambda>p  s    QqTr-   Tr^   r   rI   )ru   )$r	   r:   r   	frozensetr,   r   r   r   r   r   r   r
   r   _BINARY_RESULTr   r   r   r   r    r   r   rH   r6   r7   rc   r8   r   rA   rd    _STRUCTURAL_CONFIDENCE_THRESHOLDrk   r   rb   ra   r   rB   )r1   rv   rw   rq   rr   rs   rt   rJ   ru   r3   
candidatesr!   
bom_resultutf1632_resultescape_resultmagic_resultutf8_precheckascii_precheckmarkup_resultr2   rK   rD   score_
best_scorerg   	stat_datas                              r+   _run_pipeline_corer      sb    
C%>TY)>O
D
  ARSJ''GJ'GGG% +A
 	
 D!J*"5"5"@|
 -T2N!n&=&=&H
 +40M!"".""g-  %L~  %M "$'N
 	"d0
 *$/M ]%;%;w%F %&&-*@*@@&&'1  !**!,,!**!++	  0mWM !n&=&=&H  ]%;%;w%F *$;%&7BUVV ,D2BCH%&7BUVV 24MM%%chh/E}0sC@s{!((#((E):;   >4@)!,:992! )G *'?  ++,IE*:$;,WG %&7BUVVtWoNNr-   c                   [        U UUUUUUUUS9	n	[        X	5      n	U	 V
s/ s HA  n
U
R                  S:X  a,  [        U
R                  U
R
                  SU
R                  5      OU
PMC     n	n
U	 V
s/ s H  n
[        U
5      PM     n	n
U	(       d  Sn[        U5      eU	 V
s/ s HU  n
U
R
                  S:  a@  [        U
R                  [        U
R
                  S5      U
R                  U
R                  5      OU
PMW     sn
$ s  sn
f s  sn
f s  sn
f )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.
)ru   rq   rr   rs   rt   rJ   zxxNz/pipeline must always return at least one resultg      ?)
r   r   r   r   r   r   r    ro   RuntimeErrormin)r1   rv   rw   rq   rr   rs   rt   rJ   ru   rg   ri   msgs               r+   run_pipeliner     s%   @ !'+++1!
G T+G 	 A :: 	

ALL$D	 	   /66g!!$gG6?3 	 A <<# 	

Cc$:AJJT	 	  7s   AC=/DAD)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   r~   r   rb   strr}   ra   r,   r9   r<   r>   r@   r?   bytesrB   rH   floatboolrk   ro   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-   