§
    |Øjås  ã                   óÚ  — 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ZddlZddl	Z	ddl
mZ ddlmZmZ ddlmZmZmZmZmZ ddlmZmZ  ej        d¦  «         ej        d¦  «        fZej                             d¦  «        Zej        j        d	k    Z ed
„  e d¦  «        D ¦   «         ¦  «        Z!i Z"e#e$e$f         e%d<    ej&        ¦   «         D ]+Z' e(e'j)        ¦  «        dk    re'j)        d         e"e'j        <   Œ,ej*        de+e#e$ef         e#e$e,f         f         fd„¦   «         Z-de#e$ef         fd„Z.de#e$ef         de#e$e/e+e$dz  ee$f                  f         fd„Z0ej*        de#e$e/e+e$dz  ee$f                  f         fd„¦   «         Z1de$de$dz  fd„Z2de$de3fd„Z4de#e$e,f         fd„Z5ej*        de#e$ef         fd„¦   «         Z6ej*        de#e$e,f         fd„¦   «         Z7dZ8ej*        defd„¦   «         Z9ej*        defd„¦   «         Z: G d„ d¦  «        Z;	 d1d d!œd"e;d#d$d%e$d&e3de,f
d'„Z< e=h d(£¦  «        Z>e=e$         e%d)<   d*Z?d+Z@d,ZA	 d2d d d-œd.ede$d"e;dz  d/e3d&e3de+e,e$dz  f         fd0„ZBdS )3zÒModel loading and bigram scoring utilities.

Note: ``from __future__ import annotations`` is intentionally omitted because
this module is compiled with mypyc, which does not support PEP 563 string
annotations.
é    N)Ú_kernel)Ú
dot_packedÚpack_profile)Úlangnorm_from_tableÚparse_langnorm_binÚparse_models_binÚparse_rowmax_binÚrowmax_from_table)ÚREGISTRYÚlookup_encodingÚi)z.soz.pydÚcpythonc              #   ó&   K  — | ]}|d v rdndV — ŒdS ))é    é	   é
   é   é   é   é    é   r   N© )Ú.0Úbs     úŒ/Users/tedpeters/Documents/Projects/Claude Projects/Misc Tools/notename-sheets/.venv/lib/python3.11/site-packages/chardet/models/__init__.pyú	<genexpr>r   C   sB   è è € ð  ð  ØDEˆÐ8Ð	8Ð	8€A€A¸að ð  ð  ð  ð  ð  ó    é   Ú_SINGLE_LANG_MAPr   Úreturnc                  óð   — t           j                             d¦  «                             d¦  «        } |                      ¦   «         }|s t          j        dt          d¬¦  «         i i fS t          |¦  «        S )zkLoad and parse models.bin, returning (models, norms).

    Cached: only reads from disk on first call.
    úchardet.modelsú
models.binuX   chardet models.bin is empty â€” statistical detection disabled; reinstall chardet to fixé   ©Ú
stacklevel)	Ú	importlibÚ	resourcesÚfilesÚjoinpathÚ
read_bytesÚwarningsÚwarnÚRuntimeWarningr   ©ÚrefÚdatas     r   Ú_load_models_datar2   O   s}   € õ Ô
×
#Ò
#Ð$4Ñ
5Ô
5×
>Ò
>¸|Ñ
LÔ
L€CØ�>Š>ÑÔ€Dàð ÝŒð'åØð		
ñ 	
ô 	
ð 	
ð �2ˆvˆå˜DÑ!Ô!Ð!r   c                  ó*   — t          ¦   «         d         S )zôLoad all bigram models from the bundled models.bin file.

    Each model is a bytes object of length 65536 (256*256).
    Index: (b1 << 8) | b2 -> weight (0-255).

    :returns: A dict mapping model key strings to 65536-byte lookup tables.
    r   ©r2   r   r   r   Úload_modelsr5   d   s   € õ ÑÔ˜qÔ!Ð!r   Úmodelsc                 ó,  — i }|                       ¦   «         D ]J\  }}|                     dd¦  «        \  }}|                     |g ¦  «                             |||f¦  «         ŒKt	          |¦  «        D ]"}t          |¦  «        }|�||vr||         ||<   Œ#|S )zËBuild a grouped index from a models dict.

    :param models: Mapping of ``"lang/encoding"`` keys to 65536-byte tables.
    :returns: Mapping of encoding name to ``[(lang, model, model_key), ...]``.
    ú/r   )ÚitemsÚsplitÚ
setdefaultÚappendÚlistr   )r6   ÚindexÚkeyÚmodelÚlangÚencÚenc_nameÚ	canonicals           r   Ú_build_enc_indexrE   o   s±   € ð =?€EØ—l’l‘n”nð =ð =‰
ˆˆUØ—I’I˜c 1Ñ%Ô%‰	ˆˆcØ×Ò˜˜bÑ!Ô!×(Ò(¨$°°sÐ);Ñ<Ô<Ð<Ð<õ ˜‘K”Kð /ð /ˆÝ# HÑ-Ô-ˆ	ØÐ  Y°eÐ%;Ð%;Ø$ XœˆE�)Ñøà€Lr   c                  ó8   — t          t          ¦   «         ¦  «        S )zTReturn a pre-grouped index mapping encoding name -> [(lang, model, model_key), ...].)rE   r5   r   r   r   Úget_enc_indexrG   †   s   € õ �K™MœMÑ*Ô*Ð*r   Úencodingc                 ó6   — t                                | ¦  «        S )zÕReturn the language for a single-language encoding, or None.

    :param encoding: The canonical encoding name.
    :returns: An ISO 639-1 language code, or ``None`` if the encoding is
        multi-language.
    )r   Úget©rH   s    r   Úinfer_languagerL   Œ   s   € õ ×Ò Ñ)Ô)Ð)r   c                 ó"   — | t          ¦   «         v S )z¾Return True if the encoding has language variants in the model index.

    :param encoding: The canonical encoding name.
    :returns: ``True`` if bigram models exist for this encoding.
    )rG   rK   s    r   Úhas_model_variantsrN   –   s   € ð •}‘”Ð&Ð&r   c                  ó*   — t          ¦   «         d         S )zAReturn cached L2 norms for all models, keyed by model key string.r   r4   r   r   r   Ú_get_model_normsrP   Ÿ   s   € åÑÔ˜qÔ!Ð!r   c                  ó6  — t          ¦   «         } t          j                             d¦  «        }	 |                     d¦  «                             ¦   «         }t          j        |                     d¦  «                             ¦   «         ¦  «                             ¦   «         }n# t          t          f$ r d}d}Y nw xY wt          ||t          | ¦  «        ¦  «        }|�|S | rt          j        dt          d¬¦  «         d	„ |                      ¦   «         D ¦   «         S )
u  Return per-model row-maximum tables for upper-bound prescreening.

    For each model, entry ``b1`` of its 256-byte table holds the maximum
    weight in the model's row for lead byte ``b1``.  Because every bigram
    weight is bounded by its row maximum, a dot product against the row
    maxima (256 terms) upper-bounds the dot product against the full table
    (65536 terms) â€” statistical scoring uses this to rule out candidate
    models without scoring them fully.

    Loads the precomputed ``rowmax.bin`` (written by
    ``chardet.models._format.write_model_artifacts`` in the same model
    order as ``models.bin``).  The file starts with a ``CRM1`` magic and
    the SHA-256 of the ``models.bin`` it was derived from: a stale or
    mismatched file would silently under-estimate row maxima and break
    the upper bound that pruning depends on, so anything that does not match
    the *current* ``models.bin`` byte-for-byte is rejected and the tables
    are derived from the models directly (slower, but always correct).
    r"   z
rowmax.binr#   r   Nzpchardet rowmax.bin is missing or does not match models.bin; deriving row maxima from the models (slower startup)r$   r%   c                 ó4   — i | ]\  }}|t          |¦  «        “ŒS r   )r
   )r   r?   Útables      r   ú
<dictcomp>zget_rowmax.<locals>.<dictcomp>Í   s'   € ÐKÐKÐK©j¨c°5ˆCÕ" 5Ñ)Ô)ÐKÐKÐKr   )r5   r'   r(   r)   r*   r+   ÚhashlibÚsha256ÚdigestÚFileNotFoundErrorÚOSErrorr	   r=   r,   r-   r.   r9   )r6   r)   r1   Úmodels_digestÚtabless        r   Ú
get_rowmaxr\   ¤   s   € õ( ‰]Œ]€FÝÔ×%Ò%Ð&6Ñ7Ô7€EðØ�~Š~˜lÑ+Ô+×6Ò6Ñ8Ô8ˆÝœØ�NŠN˜<Ñ(Ô(×3Ò3Ñ5Ô5ñ
ô 
ç
Š&‰(Œ(ð 	ˆøõ �wÐ'ð ð ð ØˆØˆˆˆðøøøõ ˜d Mµ4¸±<´<Ñ@Ô@€FØÐØˆØð 
ÝŒðCåØð		
ñ 	
ô 	
ð 	
ð LÐK¸F¿LºL¹N¼NÐKÑKÔKÐKs   ¯A2B" Â"B:Â9B:c                  óX  ‡— t          ¦   «         } t          j                             d¦  «        }	 |                     d¦  «                             ¦   «         }t          j        |                     d¦  «                             ¦   «         ¦  «                             ¦   «         }n# t          t          f$ r d}d}Y nw xY wt          ||t          | ¦  «        ¦  «        }|�|S | rt          j        dt          d¬¦  «         t!          ¦   «         Šˆfd	„|                      ¦   «         D ¦   «         S )
aÖ  Return per-model norms for language scoring, keyed by model key.

    The language tiers score in an IDF-weighted space (see
    :meth:`BigramProfile.for_language`), where a model's length is its
    norm after each weight is scaled by the square root of its bigram's
    IDF.  Loads the precomputed ``langnorm.bin`` (written by
    ``chardet.models._format.write_model_artifacts`` in the same model
    order as ``models.bin``) and, like :func:`get_rowmax`, rejects a file
    whose embedded digest does not match the current ``models.bin``:
    a stale norm table would tilt every language comparison.  The
    fallback derives the norms from the tables directly, which is correct
    but costs a pass over every model.
    r"   zlangnorm.binr#   r   Nzvchardet langnorm.bin is missing or does not match models.bin; deriving language norms from the models (slower startup)r$   r%   c                 ó8   •— i | ]\  }}|t          |‰¦  «        “ŒS r   )r   )r   r?   rS   Úidfs      €r   rT   z&get_language_norms.<locals>.<dictcomp>ô   s*   ø€ ÐRÐRÐR±Z°S¸%ˆCÕ$ U¨CÑ0Ô0ÐRÐRÐRr   )r5   r'   r(   r)   r*   r+   rU   rV   rW   rX   rY   r   r=   r,   r-   r.   Úget_idf_weightsr9   )r6   r)   r1   rZ   Únormsr_   s        @r   Úget_language_normsrb   Ð   s1  ø€ õ ‰]Œ]€FÝÔ×%Ò%Ð&6Ñ7Ô7€EðØ�~Š~˜nÑ-Ô-×8Ò8Ñ:Ô:ˆÝœØ�NŠN˜<Ñ(Ô(×3Ò3Ñ5Ô5ñ
ô 
ç
Š&‰(Œ(ð 	ˆøõ �wÐ'ð ð ð ØˆØˆˆˆðøøøõ ˜t ]µD¸±L´LÑAÔA€EØÐØˆØð 
ÝŒðGåØð		
ñ 	
ô 	
ð 	
õ Ñ
Ô
€CØRÐRÐRÐRÀ6Ç<Â<Á>Ä>ÐRÑRÔRÐRs   °A2B# Â#B;Â:B;é   c                  ó¢   ‡— t          ¦   «         } t          d„ t          d¦  «        D ¦   «         ¦  «        Št          ˆfd„| D ¦   «         ¦  «        S )a{  Return the 65536-byte root-IDF table the language profile multiplies by.

    Entry ``i`` is ``round(16 * sqrt(idf[i]))``.  The language tiers
    compare an IDF-weighted profile against models weighted by the square
    root of IDF; folding the model-side factor into the profile, as the
    product of this table and the IDF table per occurrence, leaves the
    model tables untouched, so the same scoring loop serves both metrics
    and only the norm differs (see :func:`get_language_norms`).  A byte
    table keeps the profile walk on native indexing: a list of the
    products costs about as much again as the walk itself.
    c              3   óh   K  — | ]-}t          t          t          j        |¦  «        z  ¦  «        V — Œ.d S ©N)ÚroundÚ_LANGUAGE_SCALE_FACTORÚmathÚsqrt)r   Úvs     r   r   z%get_language_scale.<locals>.<genexpr>  s8   è è € ÐSÐSÀ1•%Õ.µ´¸1±´Ñ=Ñ>Ô>ÐSÐSÐSÐSÐSÐSr   r   c              3   ó(   •K  — | ]}‰|         V — Œd S rf   r   )r   rk   Úrootss     €r   r   z%get_language_scale.<locals>.<genexpr>  s'   øè è € Ð'Ð'˜a��q”Ð'Ð'Ð'Ð'Ð'Ð'r   )r`   ÚbytesÚrange)r_   rm   s    @r   Úget_language_scalerp   ý   sU   ø€ õ Ñ
Ô
€CÝÐSÐSÍÈcÉ
Ì
ÐSÑSÔSÑSÔS€EÝÐ'Ð'Ð'Ð' 3Ð'Ñ'Ô'Ñ'Ô'Ð'r   c                  ó  — t           j                             d¦  «                             d¦  «        } |                      ¦   «         }t          |¦  «        dk    r2t          j        dt          |¦  «        › d�t          d¬¦  «         ddz  S |S )	uÎ  Return a 65536-byte IDF weight table for bigram profile construction.

    Loads a precomputed table from ``idf.bin`` (generated at training time).
    For each bigram index, the weight reflects how discriminative that bigram
    is across all models:

    - Bigrams in every model (common ASCII) â†’ weight 1 (minimal signal)
    - Bigrams in one model â†’ weight 255 (maximum signal)
    - Bigrams not in any model â†’ weight 1 (unknown, treat as neutral)
    r"   zidf.biné   z chardet idf.bin has wrong size (z"), falling back to uniform weightsr$   r%   ó   )	r'   r(   r)   r*   r+   Úlenr,   r-   r.   r/   s     r   r`   r`     s•   € õ Ô
×
#Ò
#Ð$4Ñ
5Ô
5×
>Ò
>¸yÑ
IÔ
I€CØ�>Š>ÑÔ€DÝ
ˆ4�y„y�EÒÐÝŒð.­s°4©y¬yð .ð .ð .åØð		
ñ 	
ô 	
ð 	
ð ˜‰ÐØ€Kr   c            
       ó¶   — e Zd ZdZdZdeddfd„Zededd fd„¦   «         Zde	e
         d	e	e
         d
e	e
         de
ddf
d„Zedee
e
f         dd fd„¦   «         ZdS )ÚBigramProfileup  Pre-computed bigram frequency distribution for a data sample.

    Computing this once and reusing it across all models reduces per-model
    scoring from O(n) to O(distinct_bigrams).

    Each bigram is weighted by its IDF (inverse document frequency) across all
    models â€” bigrams unique to few models get high weight, bigrams common to
    all models get weight 1.  ``nonzero`` lists the indices carrying weight,
    in first-encounter order.  :meth:`for_language` builds the same profile
    under the language tiers' heavier weighting instead.

    The weights are reachable two ways, and which one a profile fills
    depends on how it was built:

    * ``idx_arr``/``val_arr`` â€” parallel ``array('i')`` buffers, what
      :func:`score_with_profile` reads for a streaming profile.  Filled by
      the streaming constructor only.
    * ``values`` â€” a plain list parallel to ``nonzero``, filled by
      :meth:`from_weighted_freq` for the small focused profiles confusion
      resolution builds, which are scored inline instead.
    * ``gather``/``dense_vals`` â€” an :func:`operator.itemgetter` over
      ``nonzero`` and the weights it picks out of the dense table, filled
      for a streaming profile only when the kernel is not compiled and
      the interpreter is CPython.
    ``row_freq`` aggregates the weights by lead byte (256 entries) and
    ``nonzero_rows`` lists the lead bytes with non-zero total; together with
    per-model row maxima (:func:`get_rowmax`) they let statistical scoring
    compute a cheap upper bound on a model's score.

    **Input limit.** Weights are packed as int32, which holds any value an
    input of at most 16 MB can produce (``255`` per occurrence against a
    2.1-billion ceiling).  Detection truncates to ``max_bytes`` long before
    that; construct a profile directly from a larger buffer and packing
    raises :exc:`OverflowError`.
    )Ú
dense_valsÚfreqÚgatherÚidx_arrÚ
input_normÚnonzeroÚnonzero_rowsÚrow_freqÚval_arrÚvaluesÚ
weight_sumr1   r    Nc                 ó"  — t          |¦  «        dz
  }|dk    rGg | _        g | _        g | _        t          \  | _        | _        g | _        g | _        d| _	        d| _
        dS t          ¦   «         }dgdz  }g }d}t          |¦  «        D ]o}||         }||dz            }	||	k    rt          |         rŒ)|dz  |	z  }
||
         }||
         dk    r|                     |
¦  «         ||
xx         |z  cc<   ||z  }Œp|                      ||g |¦  «         dS )a?  Compute the bigram frequency distribution for *data*.

        Each bigram is weighted by its IDF (inverse document frequency) across
        all loaded models.  Bigrams unique to few models get high weight;
        bigrams common to all models get weight 1.

        :param data: The raw byte data to profile.
        r   r   ç        Nrr   é   )rt   rx   r|   r€   Ú_EMPTY_PACKEDrz   r   r~   r}   r�   r{   r`   ro   Ú_ASCII_WHITESPACE_TABLEr<   Ú_finish)Úselfr1   Útotal_bigramsr_   rx   r|   Úw_sumr   Úb1Úb2ÚidxÚws               r   Ú__init__zBigramProfile.__init__[  s=  € õ ˜D™	œ	 A™ˆØ˜AÒÐà#%ˆDŒIØ&(ˆDŒLØ%'ˆDŒKÝ)6Ñ&ˆDŒL˜$œ,Ø')ˆDŒMØ+-ˆDÔØ#$ˆDŒOØ%(ˆDŒOØˆFåÑÔˆØ˜# ™+ˆØˆØˆÝ�}Ñ%Ô%ð 	ð 	ˆAØ�a”ˆBØ�a˜!‘e”ˆBð
 �RŠxˆxÕ3°BÔ7ˆxØØ˜‘7˜b‘.ˆCØ�C”ˆAØ�CŒy˜AŠ~ˆ~Ø—’˜sÑ#Ô#Ð#Ø�ˆIˆIŒI˜‰NˆIˆI‰IØ�Q‰JˆEˆEð 	�Š�T˜7 B¨Ñ.Ô.Ð.Ð.Ð.r   c                 óV  —  | d¦  «        }t          |¦  «        dz
  }|dk    r|S t          ¦   «         }dgdz  }g }t          |¦  «        D ]h}||         }||dz            }	||	k    rt          |         rŒ)|dz  |	z  }
||
         dk    r|                     |
¦  «         ||
xx         ||
         z  cc<   Œit          ¦   «         }d}d}|D ]'}
||
         }|||z  z  }|||
         z  }|||
<   ||z  }Œ(|                     ||g |¦  «         t          t          j	        |¦  «        z  |_
        |S )uã  Build the profile the language tiers score with.

        Same bigram walk as the streaming constructor, but each occurrence
        carries IDF times :func:`get_language_scale` (a byte-quantized
        root of IDF) rather than IDF alone.  Scored against
        :func:`get_language_norms`, this is the cosine between the
        IDF-weighted input and models weighted by the square root of IDF.
        The plain cosine lets one high-IDF bigram that
        two languages share, such as the UTF-8 pair for ``Ã©``, outweigh
        every bigram that tells them apart: it rates the model by the share
        that bigram takes of its *unweighted* length, so an accent-heavy
        language wins French text on the strength of ``Ã©`` alone.  Weighting
        the model side too charges each model for its rare bigrams, and the
        common bigrams decide again.

        :param data: The raw byte data to profile.
        :returns: A profile for use with ``language_weighting=True``.
        r   r   r   rr   r„   )rt   r`   ro   r†   r<   rp   r‡   rh   ri   rj   r{   )Úclsr1   Úprofiler‰   r_   rx   r|   r   r‹   rŒ   r�   ÚscalerŠ   Únorm_sqrk   rŽ   s                   r   Úfor_languagezBigramProfile.for_language‰  sh  € ð( �#�c‘(”(ˆÝ˜D™	œ	 A™ˆØ˜AÒÐØˆNÝÑÔˆØ˜# ™+ˆØˆÝ�}Ñ%Ô%ð 	"ð 	"ˆAØ�a”ˆBØ�a˜!‘e”ˆBØ�RŠxˆxÕ3°BÔ7ˆxØØ˜‘7˜b‘.ˆCØ�CŒy˜AŠ~ˆ~Ø—’˜sÑ#Ô#Ð#Ø�ˆIˆIŒI˜˜SœÑ!ˆIˆI‰IˆIõ #Ñ$Ô$ˆØˆØˆØð 	ð 	ˆCØ�S”	ˆAØ�q˜1‘uÑˆGØ�E˜#”J‘ˆAØˆD�‰IØ�Q‰JˆEˆEØ�Š˜˜g r¨5Ñ1Ô1Ð1Ý3µd´iÀÑ6HÔ6HÑHˆÔØˆr   rx   r|   r€   r�   c                 óö  ‡
— || _         || _        || _        d}dgdz  Š
|rIt          t	          |¦  «        ¦  «        D ]+}||         }|||z  z  }‰
||         dz	  xx         |z  cc<   Œ,n(|D ]%}||         }|||z  z  }‰
|dz	  xx         |z  cc<   Œ&t          j        |¦  «        | _        ‰
| _        ˆ
fd„t          d¦  «        D ¦   «         | _	        |st          st          \  | _        | _        nt          ||¦  «        \  | _        | _        |st          rg n|| _        d| _        d| _        | j        rAt	          |¦  «        dk    r0t$          r+t'          j        |Ž }	|	| _         |	|¦  «        | _        dS dS dS dS )aQ  Store the frequency data and derive the norm and row aggregates.

        Single finalization path shared by both constructors so pruning
        fields cannot silently diverge between them.

        Exactly one of *freq* and *values* carries the weights, never both.
        The streaming constructor passes the dense *freq* table it had to
        build and leaves *values* empty; the sparse constructor fills
        *values* parallel to *nonzero* and passes no table, which is what
        lets it skip a 65536-entry allocation per call.

        Neither is retained.  *freq* is read here to derive the norm, the row
        aggregates and the packed buffers, and is then dropped -- scoring
        reads ``idx_arr``/``val_arr`` or ``values``, never the dense table,
        so keeping it alive would pin 512 KB per profile for nothing.
        r   r   r„   c                 ó$   •— g | ]}‰|         ¯
|‘ŒS r   r   )r   r‹   r~   s     €r   ú
<listcomp>z)BigramProfile._finish.<locals>.<listcomp>è  s"   ø€ ÐEÐEÐE B¸À¼ÐE˜RÐEÐEÐEr   Nr   r   )r|   r€   r�   ro   rt   ri   rj   r{   r~   r}   Ú_KERNEL_COMPILEDr…   rz   r   r   rx   ry   rw   Ú_GATHER_DOTÚoperatorÚ
itemgetter)rˆ   rx   r|   r€   r�   r”   r   rk   r�   ry   r~   s             @r   r‡   zBigramProfile._finishÀ  s×  ø€ ð. ˆŒØˆŒØ$ˆŒØˆØ ˜c C™iˆØð 		(Ý�3˜w™<œ<Ñ(Ô(ð /ð /�Ø˜1”I�Ø˜1˜q™5Ñ �Ø˜ œ q™Ð)Ð)Ô)¨QÑ.Ð)Ð)Ñ)Ð)ð/ð
 ð (ð (�Ø˜”I�Ø˜1˜q™5Ñ �Ø˜ ™Ð"Ð"Ô" aÑ'Ð"Ð"Ñ"Ð"Ýœ) GÑ,Ô,ˆŒØ ˆŒØEÐEÐEÐE­%°©*¬*ÐEÑEÔEˆÔð ð 	EÕ)ð 	EÝ)6Ñ&ˆDŒL˜$œ,˜,å)5°g¸tÑ)DÔ)DÑ&ˆDŒL˜$œ,à!Ð@Õ%5Ð@�B�B¸DˆŒ	ð DHˆŒØ+-ˆŒØŒ9ð 	+�˜W™œ¨Ò)Ð)­kÐ)ÝÔ(¨'Ð2ˆFØ ˆDŒKØ$˜f T™lœlˆDŒOˆOˆOð	+ð 	+Ð)Ð)Ð)Ð)r   Úweighted_freqc                 óî   —  | d¦  «        }g }g }d}|                      ¦   «         D ]6\  }}|r/|                     |¦  «         |                     |¦  «         ||z  }Œ7|                     g |||¦  «         |S )u  Create a BigramProfile from pre-computed weighted frequencies.

        Computes ``weight_sum`` and ``input_norm`` from *weighted_freq* to
        ensure consistency between the stored fields.

        Deliberately does not build the dense 65536-entry ``freq`` table
        the streaming constructor uses.  Callers here pass a handful of
        bigrams â€” confusion resolution's focused profiles hold a median of
        eight â€” and allocating a 65536-element list per call cost about
        24us, which measured as roughly 40% of the whole bigram-rescore
        stage.  Scoring reads ``nonzero``/``values``, so the table is
        never needed.

        :param weighted_freq: Mapping of bigram index to weighted count.
        :returns: A new :class:`BigramProfile` instance.
        r   r   )r9   r<   r‡   )r‘   r�   r’   r|   r€   rŠ   r�   Úcounts           r   Úfrom_weighted_freqz BigramProfile.from_weighted_freqû  s’   € ð$ �#�c‘(”(ˆØˆØˆØˆØ'×-Ò-Ñ/Ô/ð 	ð 	‰JˆC�Øð Ø—’˜sÑ#Ô#Ð#Ø—’˜eÑ$Ô$Ð$Ø˜‘�øØ�Š˜˜G V¨UÑ3Ô3Ð3Øˆr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__rn   r�   Úclassmethodr•   r=   Úintr‡   Údictr    r   r   r   rv   rv   (  sÿ   € € € € € ð"ð "ðH€Ið,/˜Uð ,/ tð ,/ð ,/ð ,/ð ,/ð\ ð4 ð 4¨/ð 4ð 4ð 4ñ „[ð4ðl9+à�3Œið9+ð �c”ð9+ð �S”	ð	9+ð
 ð9+ð 
ð9+ð 9+ð 9+ð 9+ðv ð¨t°C¸°H¬~ð À/ð ð ð ñ „[ðð ð r   rv   Ú F©Úlanguage_weightingr’   r@   zbytes | bytearray | memoryviewÚ	model_keyr«   c                óT  — t          |t          ¦  «        st          |¦  «        }| j        dk    rdS |rt          ¦   «         nt	          ¦   «         }|r|                     |¦  «        nd}|€Y|rt          |t          ¦   «         ¦  «        }n:d}t          d¦  «        D ]}||         }|r|||z  z  }Œt          j
        |¦  «        }|dk    rdS | j        }	| j        }
|
r<d}t          t          |	¦  «        ¦  «        D ]}|||	|                  |
|         z  z  }Œn…t          rt          | j        | j        |¦  «        }nb| j        }|�7t'          t)          t*          j         ||¦  «        | j        ¦  «        ¦  «        }n"d}| j        }|	D ]}|||         ||         z  z  }Œ||| j        z  z  S )af  Score a pre-computed bigram profile against a single model using cosine similarity.

    ``bytearray``/``memoryview`` tables are accepted for compatibility but
    copied to ``bytes`` first: the narrow type lets mypyc compile the
    dot-product loop with native byte indexing (and keeps compiled and
    pure-Python installs accepting the same argument types).

    With *language_weighting* the profile must come from
    :meth:`BigramProfile.for_language` and the model is normalized by its
    :func:`get_language_norms` entry, which together give the cosine in
    the language tiers' IDF-weighted space.
    rƒ   Nr   rr   )Ú
isinstancern   r{   rb   rP   rJ   r   r`   ro   ri   rj   r|   r€   rt   r™   r   rz   r   ry   ÚsumÚmapr›   Úmulrw   rx   )r’   r@   r¬   r«   ra   Ú
model_normÚsq_sumr   rk   r|   r€   Údotry   rx   r�   s                  r   Úscore_with_profilerµ     sã  € õ& �e�UÑ#Ô#ð Ý�e‘”ˆØÔ˜SÒ Ð ØˆsØ$6ÐNÕÑ Ô Ð Õ<LÑ<NÔ<N€EØ)2Ð<�—’˜9Ñ%Ô%Ð%¸€JØÐØð 	+Ý,¨UµOÑ4EÔ4EÑFÔFˆJˆJàˆFÝ˜5‘\”\ð $ð $�Ø˜!”H�Øð $Ø˜a !™e‘O�FøÝœ 6Ñ*Ô*ˆJØ�SÒÐØˆsØŒo€GØŒ^€FØð .ð ˆÝ•s˜7‘|”|Ñ$Ô$ð 	1ð 	1ˆAØ�5˜ œÔ$ v¨a¤yÑ0Ñ0ˆCˆCð	1å	ð .Ý˜œ¨'¬/¸5ÑAÔAˆˆð ”ˆØÐÝ•c�(œ,¨¨¨u©¬°wÔ7IÑJÔJÑKÔKˆCˆCàˆCØ”<ˆDØð .ð .�Ø�u˜S”z D¨¤IÑ-Ñ-��Ø�*˜wÔ1Ñ1Ñ2Ð2r   >   ÚbrÚcyÚgaÚgdÚRARE_LANGUAGESÚzxxé€   g¸…ëQ¸®?)Údemote_thin_rarer«   r1   r½   c                ó˜  — | s|€dS t          ¦   «         }|                     |¦  «        }|€dS |€+|rt                               | ¦  «        nt          | ¦  «        }d}d}d}	d}
|D ]C\  }}}t	          ||||¬¦  «        }||k    r|}|}|r|t
          vr|t          k    r
||	k    r|}	|}
ŒD|r|�|t
          v r|
�||	z
  t          k     r|
}||fS )a»  Score data against all language variants of an encoding.

    Returns (best_score, best_language). Uses a pre-grouped index for O(L)
    lookup where L is the number of language variants for the encoding.

    If *profile* is provided, it is reused instead of recomputing the bigram
    frequency distribution from *data*.

    :param data: The raw byte data to score.
    :param encoding: The canonical encoding name to match against.
    :param profile: Optional pre-computed :class:`BigramProfile` to reuse.
    :param demote_thin_rare: Pass true only when the caller has judged the
        *original* input thin (under :data:`_THIN_RARE_MAX_BYTES` before
        any transcoding).  A :data:`RARE_LANGUAGES` winner that leads the
        best prevalent-language variant by less than the measured noise
        band is then reported under the prevalent language instead.  The
        length judgment deliberately lives with the caller: this function
        may receive transcoded bytes or a profile without its source data,
        so ``len(data)`` here is not a reliable proxy for input size.
        Language-fill callers pass this; encoding-ranking callers must not,
        so that candidate ordering stays byte-identical.
    :param language_weighting: Score in the language tiers' IDF-weighted
        space (see :meth:`BigramProfile.for_language`).  A *profile* passed
        alongside must have been built by that constructor.  The
        encoding-ranking caller leaves this off for the same reason as
        *demote_thin_rare*: its scores feed the confidence and ordering of
        encodings, and the weighted cosine lives on a different scale.
    :returns: A ``(score, language)`` tuple.  The score is always the best
        cosine similarity across variants; the language matches it except
        when ``demote_thin_rare`` fires, in which case the label is the
        best prevalent-language variant's while the score remains the
        rare winner's.
    N)rƒ   Nrƒ   rª   )rG   rJ   rv   r•   rµ   rº   ÚART_LANGUAGEÚ_THIN_RARE_MARGIN)r1   rH   r’   r½   r«   r>   ÚvariantsÚ
best_scoreÚ	best_langÚbest_prevalentÚbest_prevalent_langrA   r@   r¬   Úss                  r   Úscore_best_languagerÇ   u  sD  € ðR ð �G�OØˆyå‰OŒO€EØ�yŠy˜Ñ"Ô"€HØÐØˆyà€ð "ð%�M×&Ò& tÑ,Ô,Ð,å˜tÑ$Ô$ð 	ð €JØ €IØ€NØ&*ÐØ"*ð 'ð 'Ñˆˆe�YÝØ�U˜IÐ:Lð
ñ 
ô 
ˆð ˆzŠ>ˆ>ØˆJØˆIàð	'à�NÐ*Ð*Ø�Ò$Ð$Ø�NÒ"Ð"àˆNØ"&Ðøð 	ð(àÐ!Ø�Ð'Ð'ØÐ+Ø˜Ñ'Õ*;Ò;Ð;à'ˆ	à�yÐ Ð r   )r©   rf   )Cr¤   ÚarrayÚ	functoolsrU   Úimportlib.resourcesr'   ri   r›   Úsysr,   Úchardetr   Úchardet._kernelr   r   Úchardet.models._formatr   r   r   r	   r
   Úchardet.registryr   r   r…   Ú__file__Úendswithr™   ÚimplementationÚnamerš   rn   ro   r†   r   r¨   ÚstrÚ__annotations__r€   Ú_encrt   Ú	languagesÚcacheÚtupleÚfloatr2   r5   r=   rE   rG   rL   ÚboolrN   rP   r\   rb   rh   rp   r`   rv   rµ   Ú	frozensetrº   r¿   Ú_THIN_RARE_MAX_BYTESrÀ   rÇ   r   r   r   ú<module>rÞ      sg  ððð ð ð €€€Ø Ð Ð Ð Ø €€€Ø Ð Ð Ð Ø €€€Ø €€€Ø 
€
€
€
Ø €€€à Ð Ð Ð Ð Ð Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4ðð ð ð ð ð ð ð ð ð ð ð ð ð ð 7Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6ð �”˜SÑ!Ô! ; 5¤;¨sÑ#3Ô#3Ð4€ð  Ô#×,Ò,¨_Ñ=Ô=Ð ð Ô Ô%¨Ò2€ð  ˜%ð  ð  ØINÈÈsÉÌð ñ  ô  ñ ô Ð ð $&Ð �$�s˜C�x”.Ð %Ð %Ñ %ØˆHŒOÑÔð 8ð 8€DØ
€sˆ4Œ>ÑÔ˜aÒÐØ&*¤n°QÔ&7Ð˜œÑ#øð „ð"˜5  c¨5 jÔ!1°4¸¸U¸
Ô3CÐ!CÔDð "ð "ð "ñ „ð"ð("�T˜#˜u˜*Ô%ð "ð "ð "ð "ðØ��e�Ôðà	ˆ#ˆt�E˜# ™* e¨SÐ0Ô1Ô2Ð
2Ô3ðð ð ð ð. „ð+�t˜C  e¨C°$©J¸¸sÐ,BÔ&CÔ!DÐDÔEð +ð +ð +ñ „ð+ð
*˜Sð * S¨4¡Zð *ð *ð *ð *ð' ð '¨ð 'ð 'ð 'ð 'ð"˜$˜s E˜zÔ*ð "ð "ð "ð "ð
 „ð(L�D˜˜e˜Ô$ð (Lð (Lð (Lñ „ð(LðV „ð#S˜D  e Ô,ð #Sð #Sð #Sñ „ð#SðR Ð ð „ð(˜Eð (ð (ð (ñ „ð(ð" „ð˜ð ð ð ñ „ðð0oð oð oð oð oñ oô oð oðj ð;3ð
  %ð;3ð ;3ð ;3Øð;3à+ð;3ð ð;3ð
 ð;3ð ð;3ð ;3ð ;3ð ;3ðD "+ Ð+CÐ+CÐ+CÑ!DÔ!D€�	˜#”Ð DÐ DÑ Dð
 €ð Ð ð Ð ð %)ðU!ð
 #Ø$ðU!ð U!ð U!Ø
ðU!àðU!ð ˜TÑ!ðU!ð
 ðU!ð ðU!ð ˆ5�#˜‘*ÐÔðU!ð U!ð U!ð U!ð U!ð U!r   