
    Ng                        d Z ddlmZmZ ddlmZmZmZ  G d de          Z G d de          Z	 G d d	e	          Z
 G d
 de          Z G d de          Z G d de          Z G d de          Z G d de          ZdS )zLanguage Models    )LanguageModel	Smoothing)AbsoluteDiscounting	KneserNey
WittenBellc                       e Zd ZdZddZdS )MLEzbClass for providing MLE ngram model scores.

    Inherits initialization from BaseNgramModel.
    Nc                 R    |                      |                              |          S )zReturns 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contexts      J/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/lm/models.pyunmasked_scorezMLE.unmasked_score   s&     ""7++00666    N)__name__
__module____qualname____doc__r    r   r   r	   r	      s2         
7 7 7 7 7 7r   r	   c                   *     e Zd ZdZ fdZddZ xZS )LidstonezProvides Lidstone-smoothed scores.

    In addition to initialization arguments from BaseNgramModel also requires
    a number by which to increase the counts, gamma.
    c                 H     t                      j        |i | || _        d S r   )super__init__gamma)r   r   argskwargs	__class__s       r   r   zLidstone.__init__%   *    $)&)))


r   Nc                     |                      |          }||         }|                                }|| j        z   |t          | j                  | j        z  z   z  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   counts
word_count
norm_counts         r   r   zLidstone.unmasked_score)   sS     $$W--D\
XXZZ
TZ'JTZ4:9U,UVVr   r   r   r   r   r   r   r   __classcell__r!   s   @r   r   r      s^             	W 	W 	W 	W 	W 	W 	W 	Wr   r   c                   "     e Zd ZdZ fdZ xZS )LaplacezwImplements Laplace (add one) smoothing.

    Initialization identical to BaseNgramModel because gamma is always 1.
    c                 B     t                      j        dg|R i | d S )N   )r   r   )r   r   r    r!   s      r   r   zLaplace.__init__;   s0    ,T,,,V,,,,,r   r   r   r   r   r   r,   r-   s   @r   r/   r/   5   sB         
- - - - - - - - -r   r/   c                   ,     e Zd ZdZd fd	ZddZ x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                 H     t                      j        |i | || _        d S r   )r   r   alpha)r   r7   r   r    r!   s       r   r   zStupidBackoff.__init__H   r"   r   Nc                    |s| j         j                            |          S |                     |          }||         }|                                }|dk    r||z  S | j        |                     ||dd                    z  S )Nr   r1   )r(   unigramsr   r   r$   r7   r   r'   s         r   r   zStupidBackoff.unmasked_scoreL   s     	3;',,T222$$W--D\
XXZZ
>>
**: 3 3D'!""+ F FFFr   )r5   r   r+   r-   s   @r   r4   r4   ?   sc              
G 
G 
G 
G 
G 
G 
G 
Gr   r4   c                   *     e Zd ZdZ fdZddZ xZS )InterpolatedLanguageModelzLogic common to all interpolated language models.

    The idea to abstract this comes from Chen & Goodman 1995.
    Do not instantiate this class directly!
    c                     |                     di           } t                      j        |fi |  || j        | j        fi || _        d S )Nparams)popr   r   r&   r(   	estimator)r   smoothing_clsorderr    r=   r!   s        r   r   z"InterpolatedLanguageModel.__init__`   sX    Hb))))&)))&tz4;II&IIr   Nc                     |s| j                             |          S | j        |         sd\  }}n| j                             ||          \  }}|||                     ||dd                    z  z   S )N)r   r1   r1   )r?   unigram_scorer(   alpha_gammar   )r   r   r   r7   r   s        r   r   z(InterpolatedLanguageModel.unmasked_scoree   s     	6>//555{7# 	E  LE55>55dGDDLE5ut224EEEEEr   r   r+   r-   s   @r   r;   r;   Y   sc         J J J J J
F F F F F F F Fr   r;   c                   "     e Zd ZdZ fdZ xZS )WittenBellInterpolatedz.Interpolated version of Witten-Bell smoothing.c                 H     t                      j        t          |fi | d S r   )r   r   r   )r   rA   r    r!   s      r   r   zWittenBellInterpolated.__init__v   s*    U55f55555r   r2   r-   s   @r   rF   rF   s   s>        886 6 6 6 6 6 6 6 6r   rF   c                   $     e Zd ZdZd fd	Z xZS )AbsoluteDiscountingInterpolatedz9Interpolated version of smoothing with absolute discount.      ?c                 P     t                      j        t          |fdd|ii| d S )Nr=   discount)r   r   r   r   rA   rL   r    r!   s       r   r   z(AbsoluteDiscountingInterpolated.__init__}   sG    	
 	
0:H/E	
IO	
 	
 	
 	
 	
r   )rJ   r2   r-   s   @r   rI   rI   z   sC        CC
 
 
 
 
 
 
 
 
 
r   rI   c                   $     e Zd ZdZd fd	Z xZS )KneserNeyInterpolatedz-Interpolated version of Kneser-Ney smoothing.皙?c                     d|cxk    rdk    sn t          d           t                      j        t          |fd||di| d S )Nr   r1   zCDiscount must be between 0 and 1 for probabilities to sum to unity.r=   )rL   rA   )
ValueErrorr   r   r   rM   s       r   r   zKneserNeyInterpolated.__init__   s    X""""""""U   	u	
 	
2:U%K%K	
OU	
 	
 	
 	
 	
r   )rP   r2   r-   s   @r   rO   rO      sC        77
 
 
 
 
 
 
 
 
 
r   rO   N)r   nltk.lm.apir   r   nltk.lm.smoothingr   r   r   r	   r   r/   r4   r;   rF   rI   rO   r   r   r   <module>rU      s     0 0 0 0 0 0 0 0 H H H H H H H H H H7 7 7 7 7- 7 7 7 W W W W W} W W W.- - - - -h - - -G G G G GM G G G4F F F F F F F F46 6 6 6 66 6 6 6
 
 
 
 
&? 
 
 


 

 

 

 

5 

 

 

 

 

r   