§
    íóÉjÚ  ã                   óP   — d Z ddlmZ ddlmZ ddlmZmZ dZ G d„ d¦  «        Z	dS )	z/
Language Model Counter
----------------------
é    )Údefaultdict)ÚSequence)ÚConditionalFreqDistÚFreqDisti€–˜ c                   óD   — e Zd ZdZdd„Zd„ Zd„ Zd„ Zd„ Zd„ Z	d	„ Z
d
„ ZdS )ÚNgramCounterad  Class for counting ngrams.

    Will count any ngram sequence you give it ;)

    First we need to make sure we are feeding the counter sentences of ngrams.

    >>> text = [["a", "b", "c", "d"], ["a", "c", "d", "c"]]
    >>> from nltk.util import ngrams
    >>> text_bigrams = [ngrams(sent, 2) for sent in text]
    >>> text_unigrams = [ngrams(sent, 1) for sent in text]

    The counting itself is very simple.

    >>> from nltk.lm import NgramCounter
    >>> ngram_counts = NgramCounter(text_bigrams + text_unigrams)

    You can conveniently access ngram counts using standard python dictionary notation.
    String keys will give you unigram counts.

    >>> ngram_counts['a']
    2
    >>> ngram_counts['aliens']
    0

    If you want to access counts for higher order ngrams, use a list or a tuple.
    These are treated as "context" keys, so what you get is a frequency distribution
    over all continuations after the given context.

    >>> sorted(ngram_counts[['a']].items())
    [('b', 1), ('c', 1)]
    >>> sorted(ngram_counts[('a',)].items())
    [('b', 1), ('c', 1)]

    This is equivalent to specifying explicitly the order of the ngram (in this case
    2 for bigram) and indexing on the context.

    >>> ngram_counts[2][('a',)] is ngram_counts[['a']]
    True

    Note that the keys in `ConditionalFreqDist` cannot be lists, only tuples!
    It is generally advisable to use the less verbose and more flexible square
    bracket notation.

    To get the count of the full ngram "a b", do this:

    >>> ngram_counts[['a']]['b']
    1

    Specifying the ngram order as a number can be useful for accessing all ngrams
    in that order.

    >>> ngram_counts[2]
    <ConditionalFreqDist with 4 conditions>

    The keys of this `ConditionalFreqDist` are the contexts we discussed earlier.
    Unigrams can also be accessed with a human-friendly alias.

    >>> ngram_counts.unigrams is ngram_counts[1]
    True

    Similarly to `collections.Counter`, you can update counts after initialization.

    >>> ngram_counts['e']
    0
    >>> ngram_counts.update([ngrams(["d", "e", "f"], 1)])
    >>> ngram_counts['e']
    1

    Nc                 ó²   — t          t          ¦  «        | _        t          ¦   «         x| j        d<   | _        d| _        |r|                      |¦  «         dS dS )aK  Creates a new NgramCounter.

        If `ngram_text` is specified, counts ngrams from it, otherwise waits for
        `update` method to be called explicitly.

        :param ngram_text: Optional text containing sentences of ngrams, as for `update` method.
        :type ngram_text: Iterable(Iterable(tuple(str))) or None

        é   r   N)r   r   Ú_countsr   ÚunigramsÚ	_distinctÚupdate)ÚselfÚ
ngram_texts     úr/home/lighthouse/badge-speaker-benchmark/.benchmark-venv-community/lib/python3.11/site-packages/nltk/lm/counter.pyÚ__init__zNgramCounter.__init__c   s[   € õ #Õ#6Ñ7Ô7ˆŒÝ*2©*¬*Ð4ˆŒ�Q‰˜$œ-àˆŒàð 	$Ø�KŠK˜
Ñ#Ô#Ð#Ð#Ð#ð	$ð 	$ó    c           
      óJ  — |D �]}|D �]}t          |t          ¦  «        s0t          d                     |t	          |¦  «        ¦  «        ¦  «        ‚t          |¦  «        }|dk    r?|d         | j        vr|                      ¦   «          | j        |d         xx         dz  cc<   Œœ|dd…         |d         }}| j         	                    |¦  «        }|�| 	                    |¦  «        nd}|�||vr|                      ¦   «          | |         |         |xx         dz  cc<   �Œ�Œ dS )aR  Updates ngram counts from `ngram_text`.

        Expects `ngram_text` to be a sequence of sentences (sequences).
        Each sentence consists of ngrams as tuples of strings.

        :param Iterable(Iterable(tuple(str))) ngram_text: Text containing sentences of ngrams.
        :raises TypeError: if the ngrams are not tuples.

        z Ngram <{}> isn't a tuple, but {}r
   r   Néÿÿÿÿ)
Ú
isinstanceÚtupleÚ	TypeErrorÚformatÚtypeÚlenr   Ú_note_new_ngramr   Úget)	r   r   ÚsentÚngramÚngram_orderÚcontextÚwordÚorder_countsÚcontext_countss	            r   r   zNgramCounter.updateu   sh  € ð ð 	6ñ 	6ˆDØð 6ñ 6�Ý! %­Ñ/Ô/ð Ý#Ø=×DÒDÀUÍDÐQVÉKÌKÑXÔXñô ð õ " %™jœj�Ø !Ò#Ð#Ø˜Q”x t¤}Ð4Ð4Ø×,Ò,Ñ.Ô.Ð.Ø”M %¨¤(Ð+Ð+Ô+¨qÑ0Ð+Ð+Ñ+Øà % c r c¤
¨E°"¬I˜�ð  $œ|×/Ò/°Ñ<Ô<�à1=Ð1I�L×$Ò$ WÑ-Ô-Ð-Ètð ð "Ð)¨T¸Ð-GÐ-GØ×(Ò(Ñ*Ô*Ð*Ø�[Ô! 'Ô*¨4Ð0Ð0Ô0°AÑ5Ð0Ð0Ñ0Ñ0ñ56ð	6ð 	6r   c                 ót   — | j         t          k    rt          dt          z  ¦  «        ‚| xj         dz  c_         dS )aØ  Account for one newly-stored distinct ngram and enforce the bound.

        The counter retains every distinct ngram, so without a bound an untrusted
        corpus of distinct tokens grows memory without limit and OOM-kills the
        worker (CWE-770; CVE-2026-12928). Refuse once ``MAX_NGRAMS`` distinct
        ngrams have been stored. The bound is checked *before* incrementing so a
        refused ngram (which is never stored) does not inflate the counter.
        zØRefusing to count further: NgramCounter exceeded the limit of %d distinct ngrams (CWE-770). Training on this corpus would grow memory without bound. Use a smaller corpus or order, or raise nltk.lm.counter.MAX_NGRAMS.r
   N)r   Ú
MAX_NGRAMSÚ
ValueError©r   s    r   r   zNgramCounter._note_new_ngram�   sH   € ð Œ>�ZÒ'Ð'Ýð.õ 1;ñ;ñô ð ð 	ˆŒ˜!ÑˆŒˆˆr   c                 ób   — t          d„ | j                             ¦   «         D ¦   «         ¦  «        S )a/  Returns grand total number of ngrams stored.

        This includes ngrams from all orders, so some duplication is expected.
        :rtype: int

        >>> from nltk.lm import NgramCounter
        >>> counts = NgramCounter([[("a", "b"), ("c",), ("d", "e")]])
        >>> counts.N()
        3

        c              3   ó>   K  — | ]}|                      ¦   «         V — Œd S ©N)ÚN)Ú.0Úvals     r   ú	<genexpr>z!NgramCounter.N.<locals>.<genexpr>»   s*   è è € Ð<Ð<˜s�3—5’5‘7”7Ð<Ð<Ð<Ð<Ð<Ð<r   )Úsumr   Úvaluesr(   s    r   r,   zNgramCounter.N¯   s/   € õ Ð<Ð< d¤l×&9Ò&9Ñ&;Ô&;Ð<Ñ<Ô<Ñ<Ô<Ð<r   c                 óX  — t          |t          ¦  «        r| j        |         S t          |t          ¦  «        r | j                             d¦  «        |         S t          |t
          ¦  «        r=| j                             t          |¦  «        dz   ¦  «        t          |¦  «                 S dS )z%User-friendly access to ngram counts.r
   N)r   Úintr   ÚstrÚ__getitem__r   r   r   ©r   Úitems     r   r5   zNgramCounter.__getitem__½   sš   € å�d�CÑ Ô ð 	HØ”< Ô%Ð%Ý˜�cÑ"Ô"ð 	HØ”<×+Ò+¨AÑ.Ô.¨tÔ4Ð4Ý˜�hÑ'Ô'ð 	HØ”<×+Ò+­C°©I¬I¸©MÑ:Ô:½5À¹;¼;ÔGÐGð	Hð 	Hr   c                 óŒ   — d                      | j        j        t          | j        ¦  «        |                      ¦   «         ¦  «        S )Nz'<{} with {} ngram orders and {} ngrams>)r   Ú	__class__Ú__name__r   r   r,   r(   s    r   Ú__str__zNgramCounter.__str__Æ   s8   € Ø8×?Ò?ØŒNÔ#¥S¨¬Ñ%6Ô%6¸¿º¹¼ñ
ô 
ð 	
r   c                 ó4   — | j                              ¦   «         S r+   )r   Ú__len__r(   s    r   r=   zNgramCounter.__len__Ë   s   € ØŒ|×#Ò#Ñ%Ô%Ð%r   c                 ó   — || j         v S r+   )r   r6   s     r   Ú__contains__zNgramCounter.__contains__Î   s   € Ø�t”|Ð#Ð#r   r+   )r:   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r,   r5   r;   r=   r?   © r   r   r   r      s¢   € € € € € ðDð DðL$ð $ð $ð $ð$&6ð &6ð &6ðPð ð ð$=ð =ð =ðHð Hð Hð
ð 
ð 
ð
&ð &ð &ð$ð $ð $ð $ð $r   r   N)
rB   Úcollectionsr   Úcollections.abcr   Únltk.probabilityr   r   r&   r   rC   r   r   ú<module>rG      s�   ððð ð
 $Ð #Ð #Ð #Ð #Ð #Ø $Ð $Ð $Ð $Ð $Ð $à :Ð :Ð :Ð :Ð :Ð :Ð :Ð :ð €
ðs$ð s$ð s$ð s$ð s$ñ s$ô s$ð s$ð s$ð s$r   