o
    rZŽhš  ã                   @   s¬   d Z ddlmZmZ ddlmZmZmZ G dd„ deƒZG dd„ deƒZ	G dd	„ d	e	ƒZ
G d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )zLanguage Modelsé    )ÚLanguageModelÚ	Smoothing)ÚAbsoluteDiscountingÚ	KneserNeyÚ
WittenBellc                   @   s   e Zd ZdZddd„ZdS )ÚMLEzbClass for providing MLE ngram model scores.

    Inherits initialization from BaseNgramModel.
    Nc                 C   s   |   |¡ |¡S )zÃReturns the MLE score for a word given a context.

        Args:
        - word is expected to be a string
        - context is expected to be something reasonably convertible to a tuple
        )Úcontext_countsÚfreq)ÚselfÚwordÚcontext© r   ú=/var/www/auris/lib/python3.10/site-packages/nltk/lm/models.pyÚunmasked_score   s   zMLE.unmasked_score©N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s    r   c                       ó*   e Zd ZdZ‡ fdd„Zddd„Z‡  ZS )ÚLidstonez«Provides Lidstone-smoothed scores.

    In addition to initialization arguments from BaseNgramModel also requires
    a number by which to increase the counts, gamma.
    c                    ó   t ƒ j|i |¤Ž || _d S r   )ÚsuperÚ__init__Úgamma)r
   r   ÚargsÚkwargs©Ú	__class__r   r   r   %   ó   
zLidstone.__init__Nc                 C   s8   |   |¡}|| }| ¡ }|| j |t| jƒ| j   S )ztAdd-one smoothing: Lidstone or Laplace.

        To see what kind, look at `gamma` attribute on the class.

        )r   ÚNr   ÚlenÚvocab©r
   r   r   ÚcountsZ
word_countZ
norm_countr   r   r   r   )   s   
zLidstone.unmasked_scorer   ©r   r   r   r   r   r   Ú__classcell__r   r   r   r   r      s    r   c                       ó    e Zd ZdZ‡ fdd„Z‡  ZS )ÚLaplacezwImplements Laplace (add one) smoothing.

    Initialization identical to BaseNgramModel because gamma is always 1.
    c                    s   t ƒ jdg|¢R i |¤Ž d S )Né   )r   r   )r
   r   r   r   r   r   r   ;   s   zLaplace.__init__©r   r   r   r   r   r&   r   r   r   r   r(   5   s    r(   c                       s,   e Zd ZdZd‡ fdd„	Zd	dd„Z‡  ZS )
ÚStupidBackoffa8  Provides StupidBackoff scores.

    In addition to initialization arguments from BaseNgramModel also requires
    a parameter alpha with which we scale the lower order probabilities.
    Note that this is not a true probability distribution as scores for ngrams
    of the same order do not sum up to unity.
    çš™™™™™Ù?c                    r   r   )r   r   Úalpha)r
   r-   r   r   r   r   r   r   H   r   zStupidBackoff.__init__Nc                 C   sV   |s	| j j |¡S |  |¡}|| }| ¡ }|dkr|| S | j|  ||dd … ¡ S )Nr   r)   )r$   Zunigramsr	   r   r    r-   r   r#   r   r   r   r   L   s   
zStupidBackoff.unmasked_score)r,   r   r%   r   r   r   r   r+   ?   s    r+   c                       r   )ÚInterpolatedLanguageModelz¡Logic common to all interpolated language models.

    The idea to abstract this comes from Chen & Goodman 1995.
    Do not instantiate this class directly!
    c                    s<   |  di ¡}tƒ j|fi |¤Ž || j| jfi |¤Ž| _d S )NÚparams)Úpopr   r   r"   r$   Ú	estimator)r
   Zsmoothing_clsÚorderr   r/   r   r   r   r   `   s   z"InterpolatedLanguageModel.__init__Nc                 C   sR   |s| j  |¡S | j| sd\}}n	| j  ||¡\}}|||  ||dd … ¡  S )N)r   r)   r)   )r1   Zunigram_scorer$   Zalpha_gammar   )r
   r   r   r-   r   r   r   r   r   e   s   

z(InterpolatedLanguageModel.unmasked_scorer   r%   r   r   r   r   r.   Y   s    r.   c                       r'   )ÚWittenBellInterpolatedz.Interpolated version of Witten-Bell smoothing.c                    s   t ƒ jt|fi |¤Ž d S r   )r   r   r   )r
   r2   r   r   r   r   r   v   s   zWittenBellInterpolated.__init__r*   r   r   r   r   r3   s   s    r3   c                       ó"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚAbsoluteDiscountingInterpolatedz9Interpolated version of smoothing with absolute discount.ç      è?c                    s"   t ƒ jt|fdd|ii|¤Ž d S )Nr/   Údiscount)r   r   r   ©r
   r2   r7   r   r   r   r   r   }   s   ÿÿ
ÿz(AbsoluteDiscountingInterpolated.__init__)r6   r*   r   r   r   r   r5   z   ó    r5   c                       r4   )ÚKneserNeyInterpolatedz-Interpolated version of Kneser-Ney smoothing.çš™™™™™¹?c                    sH   d|  krdkst dƒ‚ t dƒ‚tƒ jt|fd||dœi|¤Ž d S )Nr   r)   zCDiscount must be between 0 and 1 for probabilities to sum to unity.r/   )r7   r2   )Ú
ValueErrorr   r   r   r8   r   r   r   r   †   s   ÿÿÿÿÿ
ÿzKneserNeyInterpolated.__init__)r;   r*   r   r   r   r   r:   ƒ   r9   r:   N)r   Znltk.lm.apir   r   Znltk.lm.smoothingr   r   r   r   r   r(   r+   r.   r3   r5   r:   r   r   r   r   Ú<module>   s   
	