
    /h                         S r SSKJrJr  SSKJrJrJr   " S S\5      r " S S\5      r	 " S S	\	5      r
 " S
 S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)zLanguage Models    )LanguageModel	Smoothing)AbsoluteDiscounting	KneserNey
WittenBellc                   "    \ rS rSrSrSS jrSrg)MLE   zZClass for providing MLE ngram model scores.

Inherits initialization from BaseNgramModel.
Nc                 B    U R                  U5      R                  U5      $ )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      F/var/www/auris/envauris/lib/python3.13/site-packages/nltk/lm/models.pyunmasked_scoreMLE.unmasked_score   s      ""7+0066     N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r   r   r   r	   r	      s    
7r   r	   c                   6   ^  \ rS rSrSrU 4S jrSS jrSrU =r$ )Lidstone   zProvides Lidstone-smoothed scores.

In addition to initialization arguments from BaseNgramModel also requires
a number by which to increase the counts, gamma.
c                 2   > [         TU ]  " U0 UD6  Xl        g r   )super__init__gamma)r   r#   argskwargs	__class__s       r   r"   Lidstone.__init__%       $)&)
r   c                     U R                  U5      nX1   nUR                  5       nX@R                  -   U[        U R                  5      U R                  -  -   -  $ )zdAdd-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   Lidstone.unmasked_score)   sN     $$W-\
XXZ
ZZ'JTZZ4::9U,UVVr   )r#   r   	r   r   r   r   r   r"   r   r   __classcell__r&   s   @r   r   r      s    	W 	Wr   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )Laplace5   zoImplements Laplace (add one) smoothing.

Initialization identical to BaseNgramModel because gamma is always 1.
c                 .   > [         TU ]  " S/UQ70 UD6  g )N   )r!   r"   )r   r$   r%   r&   s      r   r"   Laplace.__init__;   s    ,T,V,r   r   r   r   r   r   r   r"   r   r3   r4   s   @r   r6   r6   5   s    
- -r   r6   c                   :   ^  \ rS rSrSrSU 4S jjrSS jrSrU =r$ )StupidBackoff?   a$  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                 2   > [         TU ]  " U0 UD6  Xl        g r   )r!   r"   alpha)r   r@   r$   r%   r&   s       r   r"   StupidBackoff.__init__H   r(   r   c                     U(       d%  U R                   R                  R                  U5      $ U R                  U5      nX1   nUR	                  5       nUS:  a  XE-  $ U R
                  U R                  XSS  5      -  $ )Nr   r9   )r.   unigramsr   r   r*   r@   r   r-   s         r   r   StupidBackoff.unmasked_scoreL   sr    ;;'',,T22$$W-\
XXZ
>**:: 3 3D!"+ FFFr   )r@   )g?r   r2   r4   s   @r   r=   r=   ?   s    
G 
Gr   r=   c                   6   ^  \ rS rSrSrU 4S jrSS jrSrU =r$ )InterpolatedLanguageModelY   zLogic common to all interpolated language models.

The idea to abstract this comes from Chen & Goodman 1995.
Do not instantiate this class directly!
c                    > UR                  S0 5      n[        TU ]  " U40 UD6  U" U R                  U R                  40 UD6U l        g )Nparams)popr!   r"   r,   r.   	estimator)r   smoothing_clsorderr%   rI   r&   s        r   r"   "InterpolatedLanguageModel.__init__`   s@    Hb))&)&tzz4;;I&Ir   c                     U(       d  U R                   R                  U5      $ U R                  U   (       d  Su  p4OU R                   R                  X5      u  p4X4U R	                  XSS  5      -  -   $ )N)r   r9   r9   )rK   unigram_scorer.   alpha_gammar   )r   r   r   r@   r#   s        r   r   (InterpolatedLanguageModel.unmasked_scoree   sf    >>//55{{7#  LE5>>55dDLEt224EEEEr   )rK   r   r2   r4   s   @r   rF   rF   Y   s    J
F Fr   rF   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )WittenBellInterpolateds   z.Interpolated version of Witten-Bell smoothing.c                 2   > [         TU ]  " [        U40 UD6  g r   )r!   r"   r   )r   rM   r%   r&   s      r   r"   WittenBellInterpolated.__init__v   s    U5f5r   r   r;   r4   s   @r   rT   rT   s   s    86 6r   rT   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )AbsoluteDiscountingInterpolatedz   z9Interpolated version of smoothing with absolute discount.c                 :   > [         TU ]  " [        U4SSU00UD6  g )NrI   discount)r!   r"   r   r   rM   r\   r%   r&   s       r   r"   (AbsoluteDiscountingInterpolated.__init__}   s*    	
0:H/E	
IO	
r   r   )g      ?r;   r4   s   @r   rY   rY   z   s    C
 
r   rY   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )KneserNeyInterpolated   z-Interpolated version of Kneser-Ney smoothing.c                 n   > SUs=::  a  S::  d  O  [        S5      e[        TU ]  " [        U4SX!S.0UD6  g )Nr   r9   zCDiscount must be between 0 and 1 for probabilities to sum to unity.rI   )r\   rM   )
ValueErrorr!   r"   r   r]   s       r   r"   KneserNeyInterpolated.__init__   sG    X""U  	u	
2:%K	
OU	
r   r   )g?r;   r4   s   @r   r`   r`      s    7
 
r   r`   N)r   nltk.lm.apir   r   nltk.lm.smoothingr   r   r   r	   r   r6   r=   rF   rT   rY   r`   r   r   r   <module>rg      s|     0 H H7- 7 W} W.-h -GM G4F F466 6
&? 


5 

r   