+
    ÖÇÜh
  ã                   óv   € R t ^ RIHt ^ RIHt ^ RIHt R t ! R R]4      t ! R R]4      t	 ! R	 R
]4      t
R# )z…Smoothing algorithms for language modeling.

According to Chen & Goodman 1995 these should work with both Backoff and
Interpolation.
)Úmethodcaller)Ú	Smoothing)ÚConditionalFreqDistc                ó’   a€ \        V \        4      '       d   \        R4      MR o\        V3R lV P	                  4        4       4      # )z©Count values that are greater than zero in a distribution.

Assumes distribution is either a mapping with counts as values or
an instance of `nltk.ConditionalFreqDist`.
ÚNc                 ó   € V # ©N© )Úcounts   &Ú3/usr/lib/python3/dist-packages/nltk/lm/smoothing.pyÚ<lambda>Ú'_count_values_gt_zero.<locals>.<lambda>   s   € ™5ó    c              3   óF   <"  € T F  pS! V4      ^ 8”  g   K  ^x € K  	  R# 5i)é    Nr	   )Ú.0Údist_or_countÚas_counts   & €r   Ú	<genexpr>Ú(_count_values_gt_zero.<locals>.<genexpr>   s$   øé € ð Ù4ˆm¹ÀÓ8OÐRSÑ8S�ŠÓ4ùs   ƒ!—
!)Ú
isinstancer   r   ÚsumÚvalues)Údistributionr   s   &@r   Ú_count_values_gt_zeror      sJ   ø€ ô �lÔ$7×8Ò8ô 	�SÔá ð ô ô Ø+×2Ñ2Ô4óó ð r   c                   óH   a a€ ] tR t^$t oRtV 3R ltR tR tR tRt	Vt
V ;t# )Ú
WittenBellzWitten-Bell smoothing.c                ó*   <€ \         SV `  ! W3/ VB  R # r   )ÚsuperÚ__init__)ÚselfÚ
vocabularyÚcounterÚkwargsÚ	__class__s   &&&,€r   r   ÚWittenBell.__init__'   s   ø€ Ü‰Ò˜Ñ7°Ô7r   c                óŒ   € V P                   V,          P                  V4      pV P                  V4      pR V,
          V,          V3# )g      ð?)ÚcountsÚfreqÚ_gamma©r    ÚwordÚcontextÚalphaÚgammas   &&&  r   Úalpha_gammaÚWittenBell.alpha_gamma*   s=   € Ø—‘˜GÕ$×)Ñ)¨$Ó/ˆØ—‘˜GÓ$ˆØ�e•˜uÕ$ eÐ+Ð+r   c                ó–   € \        V P                  V,          4      pW"V P                  V,          P                  4       ,           ,          # r   )r   r'   r   ©r    r,   Ún_pluss   && r   r)   ÚWittenBell._gamma/   s5   € Ü& t§{¡{°7Õ';Ó<ˆØ $§+¡+¨gÕ"6×"8Ñ"8Ó":Õ:Õ;Ð;r   c                óL   € V P                   P                  P                  V4      # r   ©r'   Úunigramsr(   ©r    r+   s   &&r   Úunigram_scoreÚWittenBell.unigram_score3   ó   € Ø�{‰{×#Ñ#×(Ñ(¨Ó.Ð.r   r	   ©Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r/   r)   r9   Ú__static_attributes__Ú__classdictcell__Ú__classcell__©r$   Ú__classdict__s   @@r   r   r   $   s!   ù‡ € Ù õ8ò,ò
<÷/ò /r   r   c                   óL   a a€ ] tR t^7t oRtRV 3R lltR tR tR tRt	Vt
V ;t# )ÚAbsoluteDiscountingz!Smoothing with absolute discount.c                ó6   <€ \         SV `  ! W3/ VB  W0n        R # r   )r   r   Údiscount)r    r!   r"   rJ   r#   r$   s   &&&&,€r   r   ÚAbsoluteDiscounting.__init__:   s   ø€ Ü‰Ò˜Ñ7°Ò7Ø Žr   c                óà   € \        V P                  V,          V,          V P                  ,
          ^ 4      V P                  V,          P                  4       ,          pV P	                  V4      pW43# )r   )Úmaxr'   rJ   r   r)   r*   s   &&&  r   r/   ÚAbsoluteDiscounting.alpha_gamma>   sZ   € ä�—‘˜GÕ$ TÕ*¨T¯]©]Õ:¸AÓ>Ø�k‰k˜'Õ"×$Ñ$Ó&õ'ð 	ð —‘˜GÓ$ˆØˆ|Ðr   c                ó¬   € \        V P                  V,          4      pV P                  V,          V P                  V,          P                  4       ,          # r   )r   r'   rJ   r   r2   s   && r   r)   ÚAbsoluteDiscounting._gammaF   s;   € Ü& t§{¡{°7Õ';Ó<ˆØ—‘ Õ&¨$¯+©+°gÕ*>×*@Ñ*@Ó*BÕBÐBr   c                óL   € V P                   P                  P                  V4      # r   r6   r8   s   &&r   r9   Ú!AbsoluteDiscounting.unigram_scoreJ   r;   r   )rJ   )g      è?r<   rE   s   @@r   rH   rH   7   s"   ù‡ € Ù+÷!òòC÷/ò /r   rH   c                   ó\   a a€ ] tR t^Nt oRtRV 3R lltR tR t]! 4       3R lt	Rt
VtV ;t# )Ú	KneserNeyaÑ  Kneser-Ney Smoothing.

This is an extension of smoothing with a discount.

Resources:
- https://pages.ucsd.edu/~rlevy/lign256/winter2008/kneser_ney_mini_example.pdf
- https://www.youtube.com/watch?v=ody1ysUTD7o
- https://medium.com/@dennyc/a-simple-numerical-example-for-kneser-ney-smoothing-nlp-4600addf38b8
- https://www.cl.uni-heidelberg.de/courses/ss15/smt/scribe6.pdf
- https://www-i6.informatik.rwth-aachen.de/publications/download/951/Kneser-ICASSP-1995.pdf
c                óB   <€ \         SV `  ! W3/ VB  W@n        W0n        R # r   )r   r   rJ   Ú_order)r    r!   r"   ÚorderrJ   r#   r$   s   &&&&&,€r   r   ÚKneserNey.__init__[   s   ø€ Ü‰Ò˜Ñ7°Ò7Ø ŒØŽr   c                ó8   € V P                  V4      w  r#W#,          # r   )Ú_continuation_counts)r    r+   Úword_continuation_countÚtotal_counts   &&  r   r9   ÚKneserNey.unigram_score`   s   € Ø/3×/HÑ/HÈÓ/NÑ,ÐØ&Õ4Ð4r   c                óR  € V P                   V,          p\        V4      ^,           V P                  8X  d   W1,          VP                  4       3MV P	                  W4      w  rE\        W@P                  ,
          R4      V,          pV P                  \        V4      ,          V,          pWg3# )é   g        )r'   ÚlenrV   r   rZ   rM   rJ   r   )r    r+   r,   Úprefix_countsr[   r\   r-   r.   s   &&&     r   r/   ÚKneserNey.alpha_gammad   s�   € ØŸ™ GÕ,ˆô �7‹|˜aÕ 4§;¡;Ô.ð Õ  -§/¡/Ó"3Ñ4à×*Ñ*¨4Ó9ñ 	-Ðô
 Ð+¯m©mÕ;¸SÓAÀKÕOˆØ—‘Ô 5°mÓ DÕDÀ{ÕRˆØˆ|Ðr   c                óò   a€ V3R lV P                   \        S4      ^,           ,          P                  4        4       p^ ^ rTV F0  pV\        Wa,          ^ 8„  4      ,          pV\	        V4      ,          pK2  	  WE3# )zèCount continuations that end with context and word.

Continuations track unique ngram "types", regardless of how many
instances were observed for each "type".
This is different than raw ngram counts which track number of instances.
c              3   óL   <"  € T F  w  rVR ,          S8X  g   K  Vx € K  	  R# 5i):r_   NNNr	   )r   Úprefix_ngramr'   r,   s   &  €r   r   Ú1KneserNey._continuation_counts.<locals>.<genexpr>v   s*   øé € ð ,
á(MÑ$�Ø˜BÕ 7Ñ*÷ ŠFÛ(Mùs   ƒ$š
$)r'   r`   ÚitemsÚintr   )r    r+   r,   Ú higher_order_ngrams_with_contextÚ#higher_order_ngrams_with_word_countÚtotalr'   s   &&f    r   rZ   ÚKneserNey._continuation_countso   ss   ø€ ô,
à(,¯©´C¸³LÀ1Õ4DÕ(E×(KÑ(KÔ(Mó,
Ð(ð
 67¸¨UÛ6ˆFØ/´3°vµ|ÀaÑ7GÓ3HÕHÐ/ØÔ*¨6Ó2Õ2ŠEñ 7ð 3Ð9Ð9r   )rV   rJ   )gš™™™™™¹?)r=   r>   r?   r@   rA   r   r9   r/   ÚtuplerZ   rB   rC   rD   rE   s   @@r   rT   rT   N   s*   ù‡ € ñ
÷ò
5ò	ñ 27³÷ :ô :r   rT   N)rA   Úoperatorr   Únltk.lm.apir   Únltk.probabilityr   r   r   rH   rT   r	   r   r   Ú<module>rq      s>   ðñõ
 "å !Ý 0òô"/�ô /ô&/˜)ô /ô.1:�	ö 1:r   