
    *pj*                        S r SSKJrJrJrJrJrJr  SSKJ	r	  SSK
JrJrJr  SSKJrJrJr  SSKJr  \S\-  -   rSrS	\S
\\S4   S\S\\\\\S-  4      4S jrS
\\S4   S\S\\\\\S-  \\\4      \\\\\\S-  \\4      4   4S jrS
\\S4   S\S\\\\\S-  4      4S jrSS.S	\S
\\S4   S\S\\	   4S jjr g)zStage 3: Statistical bigram scoring.

Note: ``from __future__ import annotations`` is intentionally omitted because
this module is compiled with mypyc, which does not support PEP 563 string
annotations.
    )BigramProfile_get_model_normsget_enc_index
get_rowmaxscore_best_languagescore_with_profile)DetectionResult)_CONFUSION_BAND_CONFUSION_FLOOR_RATIO_STRICT_TIER_MAX_CONF)_COMMON_LATIN_ENCODINGS_DEMOTION_CANDIDATES_RARE_ARBITRATION_MARGIN)EncodingInfo   @   data
candidates.profilereturnNc                     / nU H>  n[        XR                  US9u  pVUS:  d  M!  UR                  UR                  XV45        M@     U$ )zFScore every candidate fully (no pruning).  Returns (enc, score, lang).)r           )r   nameappend)r   r   r   scoresencslangs          X/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/chardet/pipeline/statistical.py
_score_allr    +   sH     35F%dHHgFs7MM388Q-.  M    c           
         [        5       n[        5       n[        5       nUR                  nUR                  nUR
                  n/ n/ n	U  H  n
UR                  U
R                  5      nUc  M#  U
R                  (       a8  [        U5       H'  u  nu  pnUR                  U
R                  XX45        M)     Ml  [        U5       Hv  u  nu  pnXO   nSnU H  nUUU   UU   -  -  nM     UR                  U5      nUc  [        S5      nOUS:  a	  UUU-  -  nOSnU	R                  UXR                  XU45        Mx     M     U	R                  S SS9  X4$ )uZ  Flatten candidate model variants for pruned scoring.

Returns ``(mb_entries, sb_entries)`` where multi-byte entries are
``(enc, lang, table, key, variant_index)`` and single-byte entries are
``(upper_bound, variant_index, enc, lang, table, key)`` sorted by
descending bound.  The upper bound multiplies each lead byte's total
profile weight by the model's maximum weight for that lead byte — at
most 256 terms versus one term per distinct bigram for a full score.
``variant_index`` is the variant's position in the encoding index, so
exact score ties resolve to the same variant the full path keeps.
r   infr   c                     U S   $ )Nr    )es    r   <lambda>!_split_variants.<locals>.<lambda>l   s    !A$r!   Tkeyreverse)r   r   r   row_freqnonzero_rows
input_normgetr   is_multibyte	enumerater   floatsort)r   r   indexnormsrowmaxr,   r-   r.   
mb_entries
sb_entriesr   variantsvir   tabler*   rmub_dotb1
model_normubs                        r   _split_variantsrA   9   s^   $ OEE\FH''L##J@BJGIJ99SXX&*3H*=&&T#!!388T#"BC +>&/&9"B"cBF""R&8B<// #3J!5\c!zJ67 r2xxcBC ': 0 OOO5!!r!   c           
      d  ^^^^^^ [        5       n[        X5      u  p40 m0 m0 mSmSmSmS[        S[        S[        S-  S[        SS4
UUUUUU4S	 jjnU H  u  pgpn
U" U[        XU	5      Xz5        M     U HH  u  ppgpT[        -
  nT[        :  a  [        UT[        -  5      nX:  a    OU" U[        XU	5      Xz5        MJ     T[        -
  nT[        :  a  [        UT[        -  5      nTR                  5        VVs/ s H  u  pX:  d  M  UPM     nnn/ n[        S
 U 5       5      (       a  UR                  [        5        SU;   a  UR                  S5        U(       ak  U  He  nUR                  U;  a  M  [!        UR#                  UR                  / 5      5       H'  u  n
u  pxn	U" UR                  [        XU	5      Xz5        M)     Mg     U  Vs/ s HN  nTR#                  UR                  S5      S:  d  M%  UR                  TUR                     TUR                     4PMP     sn$ s  snnf s  snf )uF  Score candidates, skipping single-byte variants that provably cannot matter.

Multi-byte variants are always scored fully — the orchestrator may later
boost their confidence based on structural coverage, so no raw-score
bound can rule them out.  Single-byte variants are scored in descending
upper-bound order (see :func:`_split_variants`) and skipped once their
bound falls more than ``_PRUNE_MARGIN`` below the running second-best
encoding score: such variants can affect neither the winner, nor
position 1, nor any candidate within the confusion band of the top
score.

Encodings that ``postprocess_results`` inspects regardless of rank
(the common Western Latin trio for niche-Latin demotion, KOI8-T for the
KOI8-R promotion) are force-scored when their trigger could fire.
Because confusion resolution can promote position 1 or any candidate
within the band into position 0 before those triggers are evaluated,
the trigger check covers every encoding near the top, not just the
statistical winner.

Returns (enc, score, lang) tuples for encodings scoring above zero, in
candidate order.
 r   enc_namer   r   Nr:   r   c                    > TR                  U 5      nUb  X:  d  X:X  a
  UTU    :  a  g UTU '   UTU '   UTU '   U T	:X  a  Umg UT:  a  Tm
UmU m	g UT
:  a  Um
g g N)r/   )rD   r   r   r:   prev	best_lang
best_scorebest_vitop1top1_enctop2s        r   record_score_pruned.<locals>.record   s    ~~h'aiB'(BS<S  
8"	(xDXDDHXD r!   c              3   2   #    U  H  o[         ;   v   M     g 7frF   )r   ).0r&   s     r   	<genexpr> _score_pruned.<locals>.<genexpr>   s     
7h$$hs   zkoi8-rzkoi8-t)r   rA   strr2   intr   _PRUNE_MARGINr   minr   itemsanyextendr   r   r   r1   r/   )r   r   r4   r7   r8   rN   rD   r   r;   r*   r:   r@   	thresholdtrigger_floorr&   r   near_topforcedr   rH   rI   rJ   rK   rL   rM   s                      @@@@@@r   _score_prunedr_   p   s/   4 OE,ZAJ#%J')I G HDD  cDj c d  & +5&Bx+GC@$K +5 /9* =(	''It.D'DEI> x+GC@$K /98 =(M##M42H+HI(..0G0daA4F0HGF

7h
777-.8hCxxv% +4EIIchh4K*L&&T#sxx!3GC!H$S +M  C>>#((C(3. 	>:chh'388)<=  Hs   <H'H'$H-7-H-F)full_rankingr`   c                H   U (       a  U(       d  / $ [        U 5      nUR                  S:X  a  / $ U(       d  [        UR                  5      [        :  a  [        XU5      nO[        X5      nUR                  S SS9  U VVVs/ s H  u  pVn[        XVUS9PM     snnn$ s  snnnf )am  Score all candidates and return results sorted by confidence descending.

:param data: The raw byte data to score.
:param candidates: Encoding candidates to evaluate.
:param full_ranking: When ``True``, score every candidate fully so the
    returned list is complete (needed by ``detect_all``).  When ``False``
    (the default), single-byte candidates that provably cannot affect the
    top of the ranking may be skipped; the winner, position 1, and all
    candidates within the confusion band of the top score are identical
    to the full ranking.
:returns: A list of :class:`DetectionResult` sorted by confidence.
r   c                     U S   $ )N   r%   )xs    r   r'   "score_candidates.<locals>.<lambda>   s    adr!   Tr)   )encoding
confidencelanguage)	r   r.   lennonzero_MIN_NONZERO_FOR_PRESCREENr    r_   r3   r	   )r   r   r`   r   r   r   r   r   s           r   score_candidatesrl      s    $ z	D!GS 	s7??+.HHDg6z3
KKNDK1 $#MDT 	dC#  s   B)!__doc__chardet.modelsr   r   r   r   r   r   chardet.pipeliner	   chardet.pipeline.confusionr
   r   r   chardet.pipeline.postprocessr   r   r   chardet.registryr   rV   rk   bytestuplelistrT   r2   r    rU   rA   r_   boolrl   r%   r!   r   <module>rw      s    - 
 
 * )1+>>   
lC'(  
%UC$J&
'(	4"lC'(4"4" sC$JsC/	01uc3d
E36	78:4"nnlC'(nn 
%UC$J&
'(nj 	"
"lC'(" 	"
 
/"r!   