§
    |Øj~�  ã                   óÜ  — U d Z ddlZddlZddlZddlZddlZddlmZm	Z	m
Z
mZmZ ddlmZ ddlmZ eeeef         eee         eeeeef         f         f         f         Zi dd“dd“d	d
“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd “d!d"“d#d$“d%d&“d'd(d)d*d+d,d-d.d/d0d1d2d3d4œ¥Zeeef         ed5<   d6„ e                     ¦   «         D ¦   «         Zeeef         ed7<   d8ed9efd:„Zej        d9efd;„¦   «         Zi dd“dd“d
d“dd“dd“dd“dd“dd“dd“dd“d d“d"d“d$d“d&d“d'd“d)d“d(d“dddddd	dddddddd<œ¥Zeeef         ed=<   d	Z dZ!d	Z"d>Z#dZ$ej        d?ed9efd@„¦   «         Z%dAedBedCedDed9ef
dE„Z&d8edFedGedHee         dIeeeeef         f         d9eedz  eeef         fdJ„Z' e¦   «         fd8edKedLedMee         d9edz  f
dN„Z(d8edFedGedHee         dIeeeeef         f         d9edz  fdO„Z)ej        dHee         d9eeef         fdP„¦   «         Z*eZ+ej        dQed9ee         fdR„¦   «         Z,dFedGedMee         d9ee         dz  fdS„Z-dTe	dUeee.eedz  eef                  f         dQedMee         dz  d9e/f
dV„Z0d8edHee         d9e	dz  fdW„Z1 e¦   «         fd8edFedGedHee         dMee         d9edz  fdX„Z2ej        dFedGed9ee         fdY„¦   «         Z3ej        dFedGedHee         d9eeeeef         f         fdZ„¦   «         Z4d8edFedGedHee         d[ee         dz  d\ee         dz  d9edz  fd]„Z5d^edFedGed9eeef         dz  fd_„Z6d`Z7daZ8dbZ9dcZ:d8edde.e         d9e.e         fde„Z;dS )fa  Confusion group resolution for similar single-byte encodings.

At runtime, loads pre-computed distinguishing byte maps from confusion.bin
and uses them to resolve statistical scoring ties between similar encodings.

Build-time computation (``compute_confusion_groups``, ``compute_distinguishing_maps``,
``serialize_confusion_data``) lives in ``scripts/confusion_training.py``.

Note: ``from __future__ import annotations`` is intentionally omitted because
this module is compiled with mypyc, which does not support PEP 563 string
annotations.
é    N)ÚART_LANGUAGEÚBigramProfileÚget_enc_indexÚget_idf_weightsÚscore_with_profile)ÚDetectionResult)Úlookup_encodingÚLué   ÚLlé   ÚLté   ÚLmé   ÚLoé   ÚMné   ÚMcé   ÚMeé   ÚNdé	   ÚNlé
   ÚNoé   ÚPcé   ÚPdé   ÚPsé   ÚPeé   ÚPié   ÚPfÚPoÚSmÚScÚSkÚSoÚZsÚZlÚZpÚCcÚCfÚCsÚCoÚCn)é   é   é   é   é   é   é   é   é   é   é   é   é   Ú_INT_TO_CATEGORYc                 ó   — i | ]\  }}||“Œ	S © rG   )Ú.0ÚkÚvs      ú�/Users/tedpeters/Documents/Projects/Claude Projects/Misc Tools/notename-sheets/.venv/lib/python3.11/site-packages/chardet/pipeline/confusion.pyú
<dictcomp>rL   K   s   € Ð"MÐ"MÐ"M©D¨A¨q 1 aÐ"MÐ"MÐ"Mó    ÚCATEGORY_TO_INTÚdataÚreturnc                 óä  — i }d}t          j        d| |¦  «        \  }|dz  }t          |¦  «        D �]=}t          j        d| |¦  «        \  }|dz  }| |||z   …                              d¦  «        }||z  }t          j        d| |¦  «        \  }|dz  }| |||z   …                              d¦  «        }||z  }t          j        d| |¦  «        \  }	|dz  }g }
i }t          |	¦  «        D ]o}t          j        d| |¦  «        \  }}}|dz  }|
                     |¦  «         t
                               |d	¦  «        t
                               |d	¦  «        f||<   Œpt          |
¦  «        |f|||f<   �Œ?|S )
z¿Load confusion group data from raw bytes.

    :param data: The raw binary content of a confusion.bin file.
    :returns: A :data:`DistinguishingMaps` dictionary keyed by encoding pairs.
    r   z!Hr   z!Br   zutf-8z!BBBr   r7   )ÚstructÚunpack_fromÚrangeÚdecodeÚappendrE   ÚgetÚ	frozenset)rO   ÚresultÚoffsetÚ	num_pairsÚ_Ú
name_a_lenÚname_aÚ
name_b_lenÚname_bÚ	num_diffsÚdiff_bytes_listÚ
categoriesÚbvÚ	cat_a_intÚ	cat_b_ints                  rK   Ú%deserialize_confusion_data_from_bytesrg   N   s­  € ð "$€FØ€FÝÔ% d¨D°&Ñ9Ô9�L€YØ
ˆa�K€Få�9ÑÔð Lñ LˆÝÔ*¨4°°vÑ>Ô>‰ˆØ�!‰ˆØ�f˜v¨
Ñ2Ð2Ô3×:Ò:¸7ÑCÔCˆØ�*ÑˆåÔ*¨4°°vÑ>Ô>‰ˆØ�!‰ˆØ�f˜v¨
Ñ2Ð2Ô3×:Ò:¸7ÑCÔCˆØ�*ÑˆåÔ)¨$°°fÑ=Ô=‰ˆØ�!‰ˆà%'ˆØ13ˆ
Ý�yÑ!Ô!ð 	ð 	ˆAÝ'-Ô'9¸&À$ÈÑ'OÔ'OÑ$ˆB�	˜9Ø�a‰KˆFØ×"Ò" 2Ñ&Ô&Ð&å ×$Ò$ Y°Ñ5Ô5Ý ×$Ò$ Y°Ñ5Ô5ðˆJ�r‰NˆNõ %.¨oÑ$>Ô$>À
Ð#Kˆ�˜ÐÑ Ñ à€MrM   c                  óê  — t           j                             d¦  «                             d¦  «        } |                      ¦   «         }|st          j        dt          d¬¦  «         i S 	 t          |¦  «        }n3# t          j
        t          f$ r}d|› �}t          |¦  «        |‚d}~ww xY wi }|                     ¦   «         D ]1\  \  }}}t          |¦  «        p|}	t          |¦  «        p|}
|||	|
f<   Œ2|S )z“Load confusion group data from the bundled confusion.bin file.

    :returns: A :data:`DistinguishingMaps` dictionary keyed by encoding pairs.
    zchardet.modelszconfusion.binuZ   chardet confusion.bin is empty â€” confusion resolution disabled; reinstall chardet to fixr   )Ú
stacklevelzcorrupt confusion.bin: N)Ú	importlibÚ	resourcesÚfilesÚjoinpathÚ
read_bytesÚwarningsÚwarnÚRuntimeWarningrg   rR   ÚerrorÚUnicodeDecodeErrorÚ
ValueErrorÚitemsr	   )ÚrefÚrawÚraw_mapsÚeÚmsgÚ
normalizedÚaÚbÚvalueÚnorm_aÚnorm_bs              rK   Úload_confusion_datar�   v   s%  € õ Ô
×
#Ò
#Ð$4Ñ
5Ô
5×
>Ò
>¸Ñ
OÔ
O€CØ
�.Š.Ñ
Ô
€CØð ÝŒð'åØð		
ñ 	
ô 	
ð 	
ð ˆ	ð%Ý8¸Ñ=Ô=ˆˆøÝŒLÕ,Ð-ð %ð %ð %Ø+¨Ð+Ð+ˆÝ˜‰oŒo 1Ð$øøøøð%øøøð &(€JØ!ŸšÑ)Ô)ð -ð -‰‰ˆˆA�Ý  Ñ#Ô#Ð( qˆÝ  Ñ#Ô#Ð( qˆØ',ˆ
�F˜FÐ#Ñ$Ð$ØÐs   Á(A8 Á8B(ÂB#Â#B()r.   r/   r0   r1   r2   r4   r3   r6   r5   r7   r   r   r   Ú_CATEGORY_PREFERENCEé   Úencodingc                 ó^  — t          d¦  «        }t          d¦  «        D ]€}	 t          |g¦  «                             | ¦  «        }n# t          $ r Y Œ3w xY wt          |¦  «        dk    rŒKt          j        |¦  «        }|dk    rd||<   Œk|d         dk    s|dv rd||<   Œ�t          |¦  «        S )u£  256-entry table: 0 = non-letter, 1 = uppercase letter, 2 = other letter.

    Combining marks count as letters: in decomposed text (Vietnamese under
    windows-1258) a base letter's neighbor is its diacritic, which is
    word-internal, not a word boundary.  Whitespace deliberately counts as
    a plain non-letter: exempting space-adjacent letters from the
    isolated-letter demotion (to spare one-letter words like French ``Ã ``)
    was tried and falsified by the accuracy suite â€” Irish/Finnish po files
    and the EBCDIC record set depend on space-adjacent demotions, so the
    cross-family decisive-override gate handles the ``Ã `` failure mode
    instead.
    rƒ   r   r
   r   ÚL)r   r   r   )Ú	bytearrayrT   ÚbytesrU   rs   ÚlenÚunicodedataÚcategory)r„   Útabler}   ÚchÚcats        rK   Ú_letter_case_tabler�   ×   sË   € õ �c‰NŒN€EÝ�3‰ZŒZð ð ˆð	Ý˜�s‘”×"Ò" 8Ñ,Ô,ˆBˆBøÝ!ð 	ð 	ð 	ØˆHð	øøøõ
 ˆr‰7Œ7�aŠ<ˆ<ØÝÔ" 2Ñ&Ô&ˆØ�$Š;ˆ;ØˆE�!‰HˆHØ�ŒV�sŠ]ˆ]˜c \Ð1Ð1ØˆE�!‰HøÝ�‰<Œ<Ðs   ¢#AÁ
AÁArŽ   ÚleftÚrightÚ
case_tablec                 óÄ   — t                                | d¦  «        }| d         dk    r|S ||         }||         }|dk    r|dk    rt          S | dk    r|dk    rt          S |S )a¤  Preference for reading a byte as *cat*, adjusted for word shape.

    A letter reading only deserves its high preference when its neighbors
    make it look like part of a word under the same encoding: a letter with
    no letter neighbors is quoted/isolated punctuation in disguise, and a
    lowercase letter immediately followed by an uppercase one is not a word
    shape any of the supported languages produce.
    r   r†   r   r   )r‚   rW   Ú_IMPLAUSIBLE_LETTER_PREFERENCE)rŽ   r�   r‘   r’   ÚprefÚ	left_kindÚ
right_kinds          rK   Ú_context_preferencer˜   ø   sr   € õ  ×#Ò# C¨Ñ+Ô+€DØ
ˆ1„v�‚}€}ØˆØ˜4Ô €IØ˜EÔ"€JØ�A‚~€~˜*¨š/˜/Ý-Ð-Ø
ˆd‚{€{�z Q’�Ý-Ð-Ø€KrM   Úenc_aÚenc_bÚ
diff_bytesrc   c                 óæ  — t          |¦  «        \  }}t          |                      d|¦  «        ¦  «        }|sdS t          |¦  «        }t          |¦  «        }	d}
d}d}d}d}d}t	          | ¦  «        dz
  }|D �]Y}||         \  }}t          |f¦  «        }|                      |¦  «        }d}|dk    �r|t          k     �r|dk    r| |dz
           nd}||k     r| |dz            nd}t          ||||¦  «        }t          ||||	¦  «        }||k    rF|d         dk    r|d         dk    rnx|
||z
  z  }
|d         dk    r|t          k    r|||z
  z  }|dz  }nK||k    rE|d         dk    r|d         dk    rn,|||z
  z  }|d         dk    r|t          k    r|||z
  z  }|dz  }|dz  }|                      ||dz   ¦  «        }|dk    r|t          k     �°�Œ[|
|k    r	||
|z
  ||fS ||
k    r	|||
z
  ||fS dS )uS  Context-aware category voting.

    Returns ``(winner, margin, demotion_margin, demotion_events)``.

    For each occurrence of a distinguishing byte, compare the two
    encodings' readings: Unicode category preference, adjusted for word
    shape (see :func:`_context_preference`).  The reading that makes more
    linguistic sense of the byte *in its context* collects the vote;
    occurrences vote independently, so repeated evidence counts.

    ``demotion_margin`` counts only the winner's votes earned where the
    *losing* side's letter reading was word-shape-implausible â€” evidence
    against an impossible reading, which is far stronger than the naive
    letters-beat-symbols preference.  ``demotion_events`` counts how many
    distinct occurrences contributed to it, so callers can tell repeated
    evidence from one loud byte.
    N)Nr   r   r   r   r   r†   ÚP)
Ú_pair_byte_tablesrX   Ú	translater�   r‰   rˆ   ÚfindÚ_MAX_VOTE_OCCURRENCESr˜   r”   )rO   r™   rš   r›   rc   Únon_diffr\   ÚrelevantÚtable_aÚtable_bÚvotes_aÚvotes_bÚ
demotion_aÚ
demotion_bÚevents_aÚevents_bÚendrd   Úcat_aÚcat_bÚneedleÚposÚexaminedr�   r‘   Úpref_aÚpref_bs                              rK   Ú_vote_with_marginr´     sr  € õ8 $ JÑ/Ô/�K€HˆaÝ˜Ÿš¨¨hÑ7Ô7Ñ8Ô8€HØð Øˆ}Ý  Ñ'Ô'€GÝ  Ñ'Ô'€GØ€GØ€GØ€JØ€JØ€HØ€HÝ
ˆd‰)Œ)�a‰-€CØð "-ñ "-ˆØ! "”~‰ˆˆuÝ˜�u‘”ˆØ�iŠi˜ÑÔˆØˆØ�QŠh‰h˜8Õ&;Ò;Ñ;Ø$'¨!¢G G�4˜˜a™”=�=°ˆDØ%(¨3¢Y Y�D˜˜q™”M�M°AˆEÝ(¨°°e¸WÑEÔEˆFÝ(¨°°e¸WÑEÔEˆFð ˜ŠˆØ˜”8˜s’?�? u¨Q¤x°3¢ Øà˜v¨™Ñ.�GØ˜Q”x 3’�¨6Õ5SÒ+SÐ+SØ" f¨v¡oÑ5˜
Ø  A™˜øØ˜&’�Ø˜”8˜s’?�? u¨Q¤x°3¢ Øà˜v¨™Ñ.�GØ˜Q”x 3’�¨6Õ5SÒ+SÐ+SØ" f¨v¡oÑ5˜
Ø  A™˜Ø˜‰MˆHØ—)’)˜F C¨!¡GÑ,Ô,ˆCð; �QŠhˆh˜8Õ&;Ò;Ñ;ùð< �ÒÐØ�g Ñ'¨°XÐ=Ð=Ø�ÒÐØ�g Ñ'¨°XÐ=Ð=Øˆ=rM   Úenc_xÚenc_yÚ	languagesc                 óv  — t          ¦   «         }t          |||¦  «        }|€dS ||         \  }}|\  }}	t          | ||	||¦  «        \  }
}}}|
�0|t          k    r%|t          k    rt          |¦  «        t          k     r|
S t          | ||	||¦  «        }t          |¦  «        t          k    r|�||
k    r|S dS |�|n|
S )u  Return the byte-evidence winner between two encodings, or ``None``.

    Mirrors the in-band pairwise rule of :func:`resolve_confusion_groups`
    (decisive demotion vote, else bigram rescore, else category vote) for
    callers outside the ranked-results scan â€” e.g. the classic-Mac
    line-ending promotion, whose platform prior must not override
    distinguishing-byte evidence.  Returns ``None`` when the pair has no
    distinguishing map or the evidence is inconclusive.

    *languages* must carry what the two results being compared report, or
    the mirror breaks: the rescore would arbitrate the same pair under a
    different rule than the confusion stage just did, and a veto built on
    that answer can reverse a promotion the stage had settled.
    N)r�   Ú_find_pair_keyr´   Ú_DECISIVE_VOTE_MARGINÚ_DECISIVE_MIN_EVENTSr‰   Ú_CROSS_FAMILY_MIN_DIFFSÚresolve_by_bigram_rescore)rO   rµ   r¶   r·   ÚmapsÚpair_keyr›   rc   r™   rš   Ú
cat_winnerÚ_marginÚdemotion_marginÚdemotion_eventsÚbigram_winners                  rK   Úconfusion_pair_winnerrÅ   `  sð   € õ( Ñ Ô €DÝ˜d E¨5Ñ1Ô1€HØÐØˆtØ! (œ^Ñ€J�
Ø�L€Eˆ5Ý<MØˆe�U˜J¨
ñ=ô =Ñ9€J�˜¨/ð 	ÐØÕ4Ò4Ð4ØÕ3Ò3Ð3Ý�
‰OŒOÕ5Ò5Ð5àÐÝ-¨d°E¸5À*ÈiÑXÔX€MÝ
ˆ:�„Õ1Ò1Ð1ð Ð$¨¸*Ò)DÐ)DØ Ð ØˆtØ)Ð5ˆ=ˆ=¸:ÐErM   c                 ó6   — t          | ||||¦  «        \  }}}}|S )z†Resolve between two encodings using context-aware category voting.

    :returns: The winning encoding name, or ``None`` if tied.
    )r´   )	rO   r™   rš   r›   rc   ÚwinnerrÁ   Ú	_demotionÚ_eventss	            rK   Úresolve_by_category_votingrÊ   Ž  s.   € õ +<Øˆe�U˜J¨
ñ+ô +Ñ'€FˆG�Y ð €MrM   c                 ó¨   ‡— t          d¦  «        Š| D ]}d‰|<   Œt          ˆfd„t          d¦  «        D ¦   «         ¦  «        }|t          ‰¦  «        fS )a­  Return ``(non_diff_delete, membership)`` byte tables for a pair.

    ``non_diff_delete`` holds every byte value *not* in *diff_bytes* (for
    ``bytes.translate`` deletion) and ``membership`` is a 256-entry table
    with 1 at each distinguishing byte (native indexing under mypyc, where
    frozenset probes are boxed).  Cached because *diff_bytes* comes from
    the fixed per-pair confusion maps loaded once per process.
    rƒ   r   c              3   ó,   •K  — | ]}‰|         °
|V — Œd S ©NrG   )rH   r}   Úmembers     €rK   ú	<genexpr>z$_pair_byte_tables.<locals>.<genexpr>¬  s,   øè è € Ð<Ð<˜1°&¸´)Ð<�QÐ<Ð<Ð<Ð<Ð<Ð<rM   )r‡   rˆ   rT   )r›   r}   r¢   rÎ   s      @rK   rž   rž   Ÿ  sc   ø€ õ �s‰^Œ^€FØð ð ˆØˆˆq‰	ˆ	ÝÐ<Ð<Ð<Ð<¥ c¡
¤
Ð<Ñ<Ô<Ñ<Ô<€HØ•U˜6‘]”]Ð"Ð"rM   Úencc                 ót   — t          d„ t          ¦   «                              | g ¦  «        D ¦   «         ¦  «        S )z'Languages *enc* has a bigram model for.c              3   ó&   K  — | ]\  }}}|®|V — Œd S rÍ   rG   )rH   Úlangr\   s      rK   rÏ   z&_modelled_languages.<locals>.<genexpr>¹  s6   è è € ð ð Ù��q˜!¸tÐ?OˆÐ?OÐ?OÐ?OÐ?Oðð rM   )rX   r   rW   )rÐ   s    rK   Ú_modelled_languagesrÔ   ¶  sF   € õ ð ð Ý,™œ×2Ò2°3¸Ñ;Ô;ðñ ô ñ ô ð rM   c                 óž   — |sdS t          | ¦  «        }t          |¦  «        }||z  }||k    sdS t          |v s	t          |v r|t          hz  S |S )u�  Languages to score *enc_a* and *enc_b* under, or ``None`` for all.

    ``None`` means *unrestricted* â€” score every variant, the original
    max-over-models comparison.  It is not an abstention.

    An encoding should not win on language coverage the other side lacks.
    On a Hungarian document the u-double-acute bytes score 0.014 against
    iso8859-2's *Czech* model (which reads them as a common r-hacek) and
    only 0.006 against iso8859-16's Hungarian one, so max-over-variants
    hands Hungarian text to the Czech reading.  Dropping the languages
    only one side models removes that particular unfairness.

    This is deliberately a narrow rule, and two tempting generalisations
    were measured and rejected against the accuracy suite:

    * Restricting to *languages* themselves rather than the shared set
      costs 12 tests.  As a consequence the restriction is a no-op for
      pairs whose language coverage already matches (67 of the 236
      confusion pairs), which is accepted â€” those pairs have no coverage
      asymmetry to correct in the first place.
    * Restricting when *languages* is not wholly inside the shared set
      costs 6 tests: cp1125 models only Ukrainian, so a Belarusian cp866
      document shares just ``uk`` with it, and scoring the *right*
      encoding under the *wrong* language loses to cp1125.  Falling back
      to unrestricted is also what keeps a Vietnamese windows-1258
      document, whose rival cp1252 models no Vietnamese, resolving
      correctly.  The cost is that pairs modelling disjoint languages
      (koi8-r/koi8-u, mac-roman/mac-turkish) never restrict at all â€”
      about 20% of calls over the corpus.
    N)rÔ   Ú_ART_LANGUAGE)r™   rš   r·   Úlangs_aÚlangs_bÚshareds         rK   Ú_comparable_languagesrÚ   ¾  so   € ðF ð ØˆtÝ! %Ñ(Ô(€GÝ! %Ñ(Ô(€GØ�wÑ€FØ˜ÒÐØˆtõ ˜ÐÐ¥=°GÐ#;Ð#;Ø�˜Ñ'Ð'Ø€MrM   ÚprofileÚindexc                 ót   ‡ ‡— |                      |¦  «        }|sdS t          ˆˆ fd„|D ¦   «         d¬¦  «        S )aŠ  Return the best bigram score for *enc*, restricted to *languages*.

    *languages* of ``None`` means every variant, the unrestricted
    max-over-models comparison.  The ``default`` guards a caller-supplied
    set that names no variant of *enc*; note that scoring 0.0 hands the
    comparison to the rival rather than abstaining, so callers wanting an
    abstention must not rely on it.
    g        c              3   óN   •K  — | ]\  }}}‰�|‰v ¯t          ‰||¦  «        V — Œ d S rÍ   )r   )rH   rÓ   ÚmodelÚ	model_keyr·   rÛ   s       €€rK   rÏ   z&_best_variant_score.<locals>.<genexpr>  sQ   øè è € ð 	
ð 	
á&��e˜YØÐ  D¨IÐ$5Ð$5õ ˜w¨¨yÑ9Ô9à$5Ð$5Ð$5Ð$5ð	
ð 	
rM   )Údefault)rW   Úmax)rÛ   rÜ   rÐ   r·   Úvariantss   `  ` rK   Ú_best_variant_scorerä   ó  si   øø€ ð �yŠy˜‰~Œ~€HØð ØˆsÝð	
ð 	
ð 	
ð 	
ð 	
à*2ð	
ñ 	
ô 	
ð
 ðñ ô ð rM   c                 óî  — t          | ¦  «        dk     rdS t          |¦  «        \  }}t          |                      d|¦  «        ¦  «        }|sdS t          ¦   «         }i }t          | ¦  «        dz
  }|t          z  t          | ¦  «        k     ròt          ¦   «         }t          |                      d|¦  «        ¦  «        D ]�}	t          |	f¦  «        }
|                      |
¦  «        }|dk    rT|r| 	                    |dz
  ¦  «         ||k     r| 	                    |¦  «         |                      |
|dz   ¦  «        }|dk    °TŒ‚|D ];}| |         dz  | |dz            z  }| 
                    |d¦  «        ||         z   ||<   Œ<nat          |¦  «        D ]Q}| |         }| |dz            }||         ||         z  sŒ'|dz  |z  }| 
                    |d¦  «        ||         z   ||<   ŒR|sdS t          j        |¦  «        S )a  Build the bigram profile of *data* restricted to *diff_bytes* context.

    The profile holds only bigrams where at least one byte is in
    *diff_bytes*, weighted by IDF like the full-input profile, so scoring a
    model against it asks how well that model explains the bytes two
    encodings read differently, and nothing else.  Returns ``None`` when
    *data* cannot form a bigram or contains no distinguishing byte.

    :param data: The raw byte data to examine.
    :param diff_bytes: Byte values where the two encodings differ.
    r   Nr   r   r   )r‰   rž   rŸ   r   Ú_DENSE_HIT_DIVISORÚsetrX   rˆ   r    ÚaddrW   rT   r   Úfrom_weighted_freq)rO   r›   r¢   Úis_diffÚhitsÚidfÚfreqÚlimitÚstartsrd   r¯   r°   ÚiÚidxÚb1Úb2s                   rK   Úbuild_focused_profilerô     s   € õ ˆ4�y„y�1‚}€}Øˆtõ *¨*Ñ5Ô5Ñ€HˆgÝˆt�~Š~˜d HÑ-Ô-Ñ.Ô.€DØð Øˆtå
Ñ
Ô
€CØ€DÝ�‰IŒI˜‰M€EØÕ Ñ ¥3 t¡9¤9Ò,Ð,õ ™5œ5ˆÝ˜DŸNšN¨4°Ñ:Ô:Ñ;Ô;ð 	1ð 	1ˆBÝ˜B˜5‘\”\ˆFØ—)’)˜FÑ#Ô#ˆCØ˜’(�(Øð (Ø—J’J˜s Q™wÑ'Ô'Ð'Ø˜’;�;Ø—J’J˜s‘O”O�OØ—i’i ¨¨a©Ñ0Ô0�ð ˜’(�(øð ð 	4ð 	4ˆAØ˜”7˜a‘< 4¨¨A©¤;Ñ.ˆCØŸš  aÑ(Ô(¨3¨s¬8Ñ3ˆD�‰IˆIð	4õ �u‘”ð 	4ð 	4ˆAØ�a”ˆBØ�a˜!‘e”ˆBØ˜B”K '¨"¤+Ñ-ð ØØ˜‘7˜b‘.ˆCØŸš  aÑ(Ô(¨3¨s¬8Ñ3ˆD�‰IˆIð ð ØˆtåÔ+¨DÑ1Ô1Ð1rM   c                 óÔ   — t          | |¦  «        }|€dS t          |||¦  «        }t          ¦   «         }t          ||||¦  «        }t          ||||¦  «        }	||	k    r|S |	|k    r|S dS )uþ  Resolve between two encodings by re-scoring only distinguishing bigrams.

    Builds a focused bigram profile containing only bigrams where at least one
    byte is a distinguishing byte, then scores both encodings under the
    languages they can be compared in (see :func:`_comparable_languages`).

    There is no abstention path here: when the pair has no comparable
    language the comparison widens to every variant rather than declining
    to answer.  That is why a Danish mac-roman/mac-turkish document, whose
    pair models disjoint languages, is still decided by the Turkish model
    the rescore has no business consulting â€” the caller's category vote is
    better placed on such evidence, and overriding this to abstain was
    measured as costing more accuracy than it recovers.

    :param data: The raw byte data to examine.
    :param enc_a: First encoding name.
    :param enc_b: Second encoding name.
    :param diff_bytes: Byte values where the two encodings differ.
    :param languages: Languages the ranked results report for this pair;
        empty when the caller has no ranking to draw on, which scores
        every variant.
    :returns: The winning encoding name, or ``None`` if tied or if no
        distinguishing byte occurs in *data*.
    N)rô   rÚ   r   rä   )
rO   r™   rš   r›   r·   rÛ   Ú
comparablerÜ   Úbest_aÚbest_bs
             rK   r½   r½   R  s~   € õ> $ D¨*Ñ5Ô5€GØ€Øˆtå& u¨e°YÑ?Ô?€Jå‰OŒO€EÝ  ¨%°¸
ÑCÔC€FÝ  ¨%°¸
ÑCÔC€Fà�‚€ØˆØ�‚€ØˆØˆ4rM   c                 ó\  — t          ¦   «         }t          dd¦  «        D ]}t          |f¦  «        }	 |                     | ¦  «        }n# t          $ r d}Y nw xY w	 |                     |¦  «        }n# t          $ r d}Y nw xY w||k    r|                     |¦  «         Œ€t          |¦  «        S )aƒ  Byte values >= 0x80 that *enc_a* and *enc_b* decode to different text.

    The distinguishing set for a pair the confusion maps do not cover,
    computed from the codecs themselves.  A byte only one side can decode
    counts as differing.  Bytes below 0x80 are left out: the callers ask
    about high-byte evidence, and every single-byte Latin family agrees
    on ASCII anyway.
    é€   rƒ   N)rç   rT   rˆ   rU   rs   rè   rX   )r™   rš   Úoutr}   rw   Útext_aÚtext_bs          rK   Údiffering_high_bytesrþ   ‚  sÒ   € õ ‘E”E€CÝ�4˜ÑÔð ð ˆÝ�Q�D‰kŒkˆð	Ø!$§¢¨EÑ!2Ô!2ˆFˆFøÝ!ð 	ð 	ð 	ØˆFˆFˆFð	øøøð	Ø!$§¢¨EÑ!2Ô!2ˆFˆFøÝ!ð 	ð 	ð 	ØˆFˆFˆFð	øøøà�VÒÐØ�GŠG�A‰JŒJˆJøÝ�S‰>Œ>Ðs#   ²AÁAÁAÁA1Á1B Á?B c                 óX  — i }|D ]¤}g }| |fD ]ˆ}	 t          |g¦  «                             |¦  «        }n%# t          $ r |                     d¦  «         Y ŒHw xY w|                     t	          |¦  «        dk    rt          j        |¦  «        nd¦  «         Œ‰|d         |d         f||<   Œ¥|S )a›  Unicode general categories of each distinguishing byte under both encodings.

    The confusion maps carry this table for their own pairs; callers that
    arbitrate a pair the maps do not cover (the niche-Latin demotion's
    candidate against its swap target) build it here.  A byte one side
    cannot decode reads as unassigned (``Cn``), which the vote treats as
    the least plausible reading of all.
    r7   r   r   )rˆ   rU   rs   rV   r‰   rŠ   r‹   )r™   rš   r›   rŒ   r}   ÚcatsrÐ   r�   s           rK   Ú_pair_categoriesr  œ  sØ   € ð )+€EØð 	&ð 	&ˆØˆØ˜5�>ð 	Lð 	LˆCðÝ˜A˜3‘Z”Z×&Ò& sÑ+Ô+��øÝ%ð ð ð Ø—’˜DÑ!Ô!Ð!Ø�ðøøøð �KŠKµC¸±G´G¸q²L°L�Ô,¨RÑ0Ô0Ð0ÀdÑKÔKÐKÐKØ˜”G˜T !œWÐ%ˆˆa‰ˆØ€Ls   �#4´AÁAÚlanguages_aÚlanguages_bc                óü   — t          | |¦  «        }|�Bt          ¦   «         }t          ||||¦  «        }t          ||||¦  «        }	||	k    r|S |	|k    r|S t          | |||t	          |||¦  «        ¦  «        \  }
}}}|
S )aû  Decide a pair on its distinguishing bytes: model rescore, then context.

    The in-band rule of :func:`resolve_confusion_groups` for a pair the
    confusion maps do not cover, with one difference: each side is scored
    under the languages the caller names for it rather than the shared
    set.  The bigram rescore decides when the models have an opinion; when
    they score the distinguishing bigrams equally (usually both at zero,
    a byte neither model has seen in that context), the category vote
    reads word shape instead, so a letter between letters still beats a
    superscript between letters.  Returns ``None`` when neither step can
    tell the two apart.

    :param data: The raw byte data to examine.
    :param enc_a: First encoding name.
    :param enc_b: Second encoding name.
    :param diff_bytes: Byte values where the two encodings differ.
    :param languages_a: Variants of *enc_a* to score; ``None`` for all.
    :param languages_b: Variants of *enc_b* to score; ``None`` for all.
    )rô   r   rä   r´   r  )rO   r™   rš   r›   r  r  rÛ   rÜ   r÷   rø   rÇ   rÁ   rÈ   rÉ   s                 rK   Úarbitrate_distinguishing_bytesr  ¶  s›   € õ8 $ D¨*Ñ5Ô5€GØÐÝ‘”ˆÝ$ W¨e°U¸KÑHÔHˆÝ$ W¨e°U¸KÑHÔHˆØ�FŠ?ˆ?ØˆLØ�FŠ?ˆ?ØˆLÝ*;Øˆe�U˜JÕ(8¸ÀÀzÑ(RÔ(Rñ+ô +Ñ'€FˆG�Y ð €MrM   r¾   c                 ó.   — ||f| v r||fS ||f| v r||fS dS )zEFind the canonical key for a pair of encodings in the confusion maps.NrG   )r¾   r™   rš   s      rK   r¹   r¹   á  s;   € ð 	ˆu€~˜ÐÐØ�uˆ~ÐØˆu€~˜ÐÐØ�uˆ~ÐØˆ4rM   é4   g{®Gázt?g      à?gš™™™™™É?Úresultsc                 óš  ‡‡— t          |¦  «        dk     r|S |d         }|j        €|S |j        t          k    r|S t	          ¦   «         }|j        }|t          z  }dŠ|}|j        }t          dt          |¦  «        ¦  «        D �]€Š|‰         }|j        €Œ‰dk    p||j        z
  t          k    }	|	s|t          k    s|j        |k     r �n;t          |||j        ¦  «        }
|
€Œ`||
         \  }}|
\  }}t          | ||||¦  «        \  }}}}|�1|t          k    r&|t          k    rt          |¦  «        t          k     r|}nft          d„ |j        |j        fD ¦   «         ¦  «        }t!          | ||||¦  «        }|	rt          |¦  «        t          k     r|�|n|}n|�	||k    r|}nd}|�||j        k    r�Œ0|	rCt#          |j        ||j        |j        ¦  «        }ˆfd„t'          |¦  «        D ¦   «         }|g|¢c S ‰Š|}|j        }�Œ‚‰dk    r|S t#          |j        ||j        |j        ¦  «        }ˆfd„t'          |¦  «        D ¦   «         }|g|¢S )a  Resolve confusion between similar encodings in the top results.

    Checks the top result against each candidate within a confidence band.
    Always checks position 1 (preserving original top-2 behavior); for
    positions 2+ only checks within the band.  Uses bigram re-scoring
    with category voting as fallback.

    :param data: The raw byte data to examine.
    :param results: Detection results sorted by confidence descending.
    :returns: A reordered list of :class:`DetectionResult` with the winner first.
    r   r   Nr   c              3   ó   K  — | ]}|®|V — Œ	d S rÍ   rG   )rH   rÓ   s     rK   rÏ   z+resolve_confusion_groups.<locals>.<genexpr>q  s4   è è € ð ð àØÐ#ð à#Ð#Ð#Ð#ðð rM   c                 ó&   •— g | ]\  }}|‰k    ¯|‘ŒS rG   rG   )rH   ÚjÚrrð   s      €rK   ú
<listcomp>z,resolve_confusion_groups.<locals>.<listcomp>�  s"   ø€ Ð?Ð?Ð?™$˜!˜Q¸¸Qº¸�A¸¸¸rM   c                 ó&   •— g | ]\  }}|‰k    ¯|‘ŒS rG   rG   )rH   r  r  Úchampion_idxs      €rK   r  z,resolve_confusion_groups.<locals>.<listcomp>¨  s'   ø€ ÐBÐBÐB‘$�!�Q°°\Ò0AÐ0AˆAÐ0AÐ0AÐ0ArM   )r‰   r„   ÚlanguagerÖ   r�   Ú
confidenceÚCONFUSION_FLOOR_RATIOrT   ÚCONFUSION_BANDÚSTRICT_TIER_MAX_CONFr¹   r´   rº   r»   r¼   rX   r½   r   Ú	mime_typeÚ	enumerate)rO   r  Útopr¾   Útop_confÚfloorÚchampionÚchampion_encÚ	candidateÚin_bandr¿   r›   rc   r™   rš   rÀ   Ú_vote_marginrÂ   rÃ   rÇ   ÚlangsrÄ   ÚpromotedÚrestr  rð   s                           @@rK   Úresolve_confusion_groupsr#    s  øø€ õ ˆ7�|„|�aÒÐØˆà
�!Œ*€CØ
„|ÐØˆð „|•}Ò$Ð$ØˆåÑ Ô €DØŒ~€HØÕ,Ñ,€Eà€LØ€Hð
 ”<€LÝ�1•c˜'‘l”lÑ#Ô#ð \*ñ \*ˆØ˜A”Jˆ	ØÔÐ%Øð �q’&ÐM˜H yÔ';Ñ;½~ÒMˆØð 	ØÕ,Ò,Ð,°	Ô0DÀuÒ0LÐ0Là‰Eå! $¨°iÔ6HÑIÔIˆØÐØà!% h¤Ñˆ
�JØ‰ˆˆuåEVØ�%˜ 
¨JñF
ô F
ÑBˆ
�L /°?ð( Ð"ØÕ#8Ò8Ð8ØÕ#7Ò7Ð7Ý�J‘”Õ"9Ò9Ð9àˆFˆFõ
 ð ð à%Ô.°	Ô0BÐCðñ ô ñ ô ˆEõ
 6Ø�e˜U J°ñô ˆMð ð 	�3˜z™?œ?Õ-DÒDÐDØ*7Ð*C˜˜È��ð
 Ð*¨}À
Ò/JÐ/JØ&��à�àˆ>˜V yÔ'9Ò9Ð9ÙØð 
	%õ 'ØÔ"ØØÔ"ØÔ#ñ	ô ˆHð @Ð?Ð?Ð?¥)¨GÑ"4Ô"4Ð?Ñ?Ô?ˆDØÐ$˜tÐ$Ð$Ð$Ð$ð ˆØˆØ Ô)ˆ‰à�qÒÐØˆõ ØÔØØÔØÔñ	ô €Hð CÐBÐBÐB�) GÑ,Ô,ÐBÑBÔB€DØÐ�tÐÐrM   )<Ú__doc__Ú	functoolsÚimportlib.resourcesrj   rR   rŠ   ro   Úchardet.modelsr   r   r   r   r   Úchardet.pipeliner   Úchardet.registryr	   ÚdictÚtupleÚstrrX   ÚintÚDistinguishingMapsrE   Ú__annotations__ru   rN   rˆ   rg   Úcacher�   r‚   r”   rº   r»   r¡   ræ   r�   r˜   r´   rÅ   rÊ   rž   rÖ   rÔ   rÚ   ÚlistÚfloaträ   rô   r½   rþ   r  r  r¹   r¼   r  r  r  r#  rG   rM   rK   ú<module>r3     s	  ððð ð ð Ð Ð Ð Ø Ð Ð Ð Ø €€€Ø Ð Ð Ð Ø €€€ðð ð ð ð ð ð ð ð ð ð ð ð ð ð -Ð ,Ð ,Ð ,Ð ,Ð ,Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,ð Ø	ˆ#ˆsˆ(„OØ	ˆ)�CŒ.˜$˜s E¨#¨s¨(¤OÐ3Ô4Ð
4Ô5ð7ôÐ ð$Ø€tð$à€tð$ð €tð$ð €tð	$ð
 €tð$ð €tð$ð €tð$ð €tð$ð €tð$ð €tð$ð ˆð$ð ˆð$ð ˆð$ð ˆð$ð ˆð$ð  ˆð!$ð" ˆð#$ð$ 	ØØØØØØØØØØØØð=$ð $ð $Ð �$�s˜C�x”.ð ð ñ ðH #NÐ"MÐ4D×4JÒ4JÑ4LÔ4LÐ"MÑ"MÔ"M€��c˜3�h”Ð MÐ MÑ Mð%°ð %Ð:Lð %ð %ð %ð %ðP „ðÐ/ð ð ð ñ „ðð>(Øˆ"ð(àˆ"ð(ð 	ˆ"ð(ð 	ˆ!ð	(ð
 	ˆ!ð(ð 	ˆ!ð(ð 	ˆ!ð(ð 	ˆ!ð(ð 	ˆ!ð(ð 	ˆ!ð(ð 	ˆ!ð(ð 	ˆ!ð(ð 	ˆ!ð(ð 	ˆ!ð(ð 	ˆ!ð(ð  	ˆ!ð!(ð" 	ˆ!ð#(ð$ Ø
Ø
Ø
Ø
Ø
Ø
Ø
Ø
Ø
Ø
Ø
Ø
ð=(ð (ð (Ð �d˜3 ˜8”nð ð ñ ðH "#Ð ð Ð ð Ð ð Ð ð Ð ð „ð ð ¨ð ð ð ñ „ðð@˜Sð ¨ð °Cð ÀUð Èsð ð ð ð ð*PØ
ðPàðPð ðPð ˜#”ð	Pð
 �S˜%  S œ/Ð)Ô*ðPð ˆ3�‰:�s˜C Ð$Ô%ðPð Pð Pð Pðn !* 	¡¤ð	+Fð +FØ
ð+Fàð+Fð ð+Fð ˜Œ~ð	+Fð
 	ˆ4�Zð+Fð +Fð +Fð +Fð\Ø
ðàðð ðð ˜#”ð	ð
 �S˜%  S œ/Ð)Ô*ðð 	ˆ4�Zðð ð ð ð" „ð# )¨C¤.ð #°U¸5À%¸<Ô5Hð #ð #ð #ñ „ð#ð& €ð „ð˜Sð  Y¨s¤^ð ð ð ñ „ðð2Øð2àð2ð ˜Œ~ð2ð ˆs„^�dÑð	2ð 2ð 2ð 2ðjØðà��T˜%  d¡
¨E°3Ð 6Ô7Ô8Ð8Ô9ðð 
ðð ˜Œ~ Ñ$ð	ð
 ðð ð ð ð6A2Ø
ðA2Ø& sœ^ðA2à�TÑðA2ð A2ð A2ð A2ðR !* 	¡¤ð-ð -Ø
ð-àð-ð ð-ð ˜#”ð	-ð
 ˜Œ~ð-ð 	ˆ4�Zð-ð -ð -ð -ð` „ð ð ¨Cð °I¸c´Nð ð ð ñ „ðð2 „ðØðØðØ(1°#¬ðà	ˆ#ˆu�S˜#�XŒÐ
Ôðð ð ñ „ðð2(Ø
ð(àð(ð ð(ð ˜#”ð	(ð ˜3” $Ñ&ð(ð ˜3” $Ñ&ð(ð 	ˆ4�Zð(ð (ð (ð (ðV
Ø
ð
àð
ð ð
ð ˆ3�ˆ8„_�tÑð	
ð 
ð 
ð 
ð( Ð ð €ð Ð ð Ð ðWØ
ðWà�/Ô"ðWð 
ˆ/ÔðWð Wð Wð Wð Wð WrM   