B
    >?ð[¸  ã               @   s¶   d Z ddlmZmZ ddlmZ ddlmZmZ ddl	m
Z
mZ ejG dd„ deƒƒZejG dd	„ d	eƒƒZej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é    )ÚdivisionÚunicode_literals)Úcompat)ÚLanguageModelÚ	Smoothing)Ú	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 expcected to be a string
        - context is expected to be something reasonably convertible to a tuple
        )Úcontext_countsZfreq)ÚselfÚwordÚcontext© r   ú-lib/python3.7/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                   s*   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                s   t t| ƒj||Ž || _d S )N)Úsuperr   Ú__init__Úgamma)r   r   ÚargsÚkwargs)Ú	__class__r   r   r   )   s    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_score)N)r   r   r   r   r   r   Ú__classcell__r   r   )r   r   r   !   s   r   c                   s    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 t| ƒjd|ž|Ž d S )Né   )r"   )r   r!   r   )r   r   r   )r   r   r   r   @   s    zLaplace.__init__)r   r   r   r   r   r    r   r   )r   r   r!   9   s   r!   c                   s*   e Zd ZdZ‡ fdd„Zddd„Z‡  ZS )ÚInterpolatedLanguageModelzuLogic common to all interpolated language models.

    The idea to abstract this comes from Chen & Goodman 1995.
    c                sF   t |tƒst‚| di ¡}tt| ƒj|f|Ž || j| jf|Ž| _	d S )NÚparams)
Ú
issubclassr   ÚAssertionErrorÚpopr   r#   r   r   r   Ú	estimator)r   Zsmoothing_clsÚorderr   r$   )r   r   r   r   J   s    z"InterpolatedLanguageModel.__init__Nc             C   s>   |s| j  |¡S | j  ||¡\}}|||  ||dd … ¡  S )Nr"   )r(   Zunigram_scoreZalpha_gammar   )r   r   r   Zalphar   r   r   r   r   P   s    z(InterpolatedLanguageModel.unmasked_score)N)r   r   r   r   r   r   r    r   r   )r   r   r#   D   s   r#   c                   s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚWittenBellInterpolatedz.Interpolated version of Witten-Bell smoothing.c                s   t t| ƒjt|f|Ž d S )N)r   r*   r   r   )r   r)   r   )r   r   r   r   Z   s    zWittenBellInterpolated.__init__)r   r   r   r   r   r    r   r   )r   r   r*   W   s   r*   c                   s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚKneserNeyInterpolatedz-Interpolated version of Kneser-Ney smoothing.çš™™™™™¹?c                s&   t t| ƒjt|fdd|ii|—Ž d S )Nr$   Údiscount)r   r+   r   r   )r   r)   r-   r   )r   r   r   r   a   s    
zKneserNeyInterpolated.__init__)r,   )r   r   r   r   r   r    r   r   )r   r   r+   ^   s   r+   N)r   Z
__future__r   r   Znltkr   Znltk.lm.apir   r   Znltk.lm.smoothingr   r   Zpython_2_unicode_compatibler	   r   r!   r#   r*   r+   r   r   r   r   Ú<module>   s   
