
    j                        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      X/lsinfo/ai/hellotax_ai/base_platform/venv/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   r6   r   r    r!   s       r   r   zStupidBackoff.__init__H   r"   r   Nc                 \   |r(| j         dz
  }t          |          |k    r|| d          }|s| j        j                            |          S |                     |          }||         }|                                }|dk    r||z  S | j        |                     ||dd                    z  S )Nr0   r   )	orderr%   r'   unigramsr   r   r$   r6   r   )r   r   r   max_ctxr'   r(   r)   s          r   r   zStupidBackoff.unmasked_scoreL   s     	-j1nG7||g%%!7()), 	3;',,T222$$W--D\
XXZZ
>>
**: 3 3D'!""+ F FFFr   )r4   r   r*   r,   s   @r   r3   r3   ?   sc              G G G G G G G Gr   r3   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_clsr8   r    r>   r!   s        r   r   z"InterpolatedLanguageModel.__init__g   sX    Hb))))&)))&tz4;II&IIr   Nc                 8   |r(| j         dz
  }t          |          |k    r|| d          }|s| j                            |          S | j        |         sd\  }}n| j                            ||          \  }}|||                     ||dd                    z  z   S )Nr0   )r   r0   )r8   r%   r@   unigram_scorer'   alpha_gammar   )r   r   r   r:   r6   r   s         r   r   z(InterpolatedLanguageModel.unmasked_scorel   s     	-j1nG7||g%%!7()), 	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<   `   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   r8   r    r!   s      r   r   zWittenBellInterpolated.__init__   s*    U55f55555r   r1   r,   s   @r   rF   rF      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   r8   rL   r    r!   s       r   r   z(AbsoluteDiscountingInterpolated.__init__   sG    	
 	
0:H/E	
IO	
 	
 	
 	
 	
r   )rJ   r1   r,   s   @r   rI   rI      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   r0   zCDiscount must be between 0 and 1 for probabilities to sum to unity.r>   )rL   r8   )
ValueErrorr   r   r   rM   s       r   r   zKneserNeyInterpolated.__init__   s    X""""""""U   	u	
 	
2:U%K%K	
OU	
 	
 	
 	
 	
r   )rP   r1   r,   s   @r   rO   rO      sC        77
 
 
 
 
 
 
 
 
 
r   rO   N)r   nltk.lm.apir   r   nltk.lm.smoothingr   r   r   r	   r   r.   r3   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BF F F F F F F FB6 6 6 6 66 6 6 6
 
 
 
 
&? 
 
 


 

 

 

 

5 

 

 

 

 

r   