
    Mi                         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      h/var/www/eduai.edurigo.com/storigo/production/storigo_env/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   r2   r3   s   @r   r5   r5   5   s    
- -r   r5   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                 N   U(       a#  U R                   S-
  n[        U5      U:  a  X#* S  nU(       d%  U R                  R                  R	                  U5      $ U R                  U5      nXA   nUR                  5       nUS:  a  XV-  $ U R                  U R                  XSS  5      -  $ )Nr8   r   )	orderr+   r-   unigramsr   r   r*   r?   r   )r   r   r   max_ctxr-   r.   r/   s          r   r   StupidBackoff.unmasked_scoreL   s    jj1nG7|g%!(),;;'',,T22$$W-\
XXZ
>**:: 3 3D!"+ FFFr   )r?   )g?r   r1   r3   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$ )InterpolatedLanguageModel`   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_clsrB   r%   rJ   r&   s        r   r"   "InterpolatedLanguageModel.__init__g   s@    Hb))&)&tzz4;;I&Ir   c                 8   U(       a#  U R                   S-
  n[        U5      U:  a  X#* S  nU(       d  U R                  R                  U5      $ U R                  U   (       d  Su  pEOU R                  R                  X5      u  pEXEU R                  XSS  5      -  -   $ )Nr8   )r   r8   )rB   r+   rL   unigram_scorer-   alpha_gammar   )r   r   r   rD   r?   r#   s         r   r   (InterpolatedLanguageModel.unmasked_scorel   s    jj1nG7|g%!(),>>//55{{7#  LE5>>55dDLEt224EEEEr   )rL   r   r1   r3   s   @r   rG   rG   `   s    J
F Fr   rG   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )WittenBellInterpolated   z.Interpolated version of Witten-Bell smoothing.c                 2   > [         TU ]  " [        U40 UD6  g r   )r!   r"   r   )r   rB   r%   r&   s      r   r"   WittenBellInterpolated.__init__   s    U5f5r   r   r:   r3   s   @r   rT   rT      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$ )AbsoluteDiscountingInterpolated   z9Interpolated version of smoothing with absolute discount.c                 :   > [         TU ]  " [        U4SSU00UD6  g )NrJ   discount)r!   r"   r   r   rB   r\   r%   r&   s       r   r"   (AbsoluteDiscountingInterpolated.__init__   s*    	
0:H/E	
IO	
r   r   )g      ?r:   r3   s   @r   rY   rY      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   r8   zCDiscount must be between 0 and 1 for probabilities to sum to unity.rJ   )r\   rB   )
ValueErrorr!   r"   r   r]   s       r   r"   KneserNeyInterpolated.__init__   sG    X""U  	u	
2:%K	
OU	
r   r   )g?r:   r3   s   @r   r`   r`      s    7
 
r   r`   N)r   nltk.lm.apir   r   nltk.lm.smoothingr   r   r   r	   r   r5   r<   rG   rT   rY   r`   r   r   r   <module>rg      s~     0 H H7- 7 W} W.-h -GM GBF FB66 6
&? 


5 

r   