+
    ÖÇÜhÚ   ã                   ó¬   € R t ^ RIt^ RIt^ RIHtHt ^ RIHt ^ RIHt ^ RI	H
t
 ^ RIHt ^ RIHt  ! R R	]R
7      tR tR tRR lt ! R R]R
7      tR# )zLanguage Model Interface.N)ÚABCMetaÚabstractmethod)Úbisect)Ú
accumulate)ÚNgramCounter)Ú	log_base2)Ú
Vocabularyc                   óJ   a € ] tR t^t o RtR t]R 4       t]R 4       tRt	V t
R# )Ú	SmoothingzÜNgram Smoothing Interface

Implements Chen & Goodman 1995's idea that all smoothing algorithms have
certain features in common. This should ideally allow smoothing algorithms to
work both with Backoff and Interpolation.
c                ó   € Wn         W n        R# )z¼
:param vocabulary: The Ngram vocabulary object.
:type vocabulary: nltk.lm.vocab.Vocabulary
:param counter: The counts of the vocabulary items.
:type counter: nltk.lm.counter.NgramCounter
N)ÚvocabÚcounts)ÚselfÚ
vocabularyÚcounters   &&&Ú-/usr/lib/python3/dist-packages/nltk/lm/api.pyÚ__init__ÚSmoothing.__init__   s   € ð  Œ
ØŽó    c                ó   € \        4       h©N©ÚNotImplementedError)r   Úwords   &&r   Úunigram_scoreÚSmoothing.unigram_score&   ó   € ä!Ó#Ð#r   c                ó   € \        4       hr   r   ©r   r   Úcontexts   &&&r   Úalpha_gammaÚSmoothing.alpha_gamma*   r   r   )r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   r   r    Ú__static_attributes__Ú__classdictcell__©Ú__classdict__s   @r   r
   r
      s7   ø‡ € ñòð ñ$ó ð$ð ñ$ó ö$r   r
   )Ú	metaclassc                ó8   € \        V 4      \        V 4      ,          # )z0Return average (aka mean) for sequence of items.)ÚsumÚlen)Úitemss   &r   Ú_meanr0   /   s   € äˆu‹:œ˜E›
Õ"Ð"r   c                 ór   € \        V \        P                  4      '       d   V # \        P                  ! V 4      # r   )Ú
isinstanceÚrandomÚRandom)Úseed_or_generators   &r   Ú_random_generatorr6   4   s*   € ÜÐ#¤V§]¡]×3Ò3Ø Ð Ü�=Š=Ð*Ó+Ð+r   c                óü   € V '       g   \        R4      h\        V 4      \        V4      8w  d   \        R4      h\        \        V4      4      pVR,          pVP	                  4       pV \        W4V,          4      ,          # )zXLike random.choice, but with weights.

Heavily inspired by python 3.6 `random.choices`.
z"Can't choose from empty populationz3The number of weights does not match the populationéÿÿÿÿ)Ú
ValueErrorr.   Úlistr   r3   r   )Ú
populationÚweightsÚrandom_generatorÚcum_weightsÚtotalÚ	thresholds   &&&   r   Ú_weighted_choicerA   :   sj   € ÷
 ÜÐ=Ó>Ð>Ü
ˆ:ƒœ#˜g›,Ô&ÜÐNÓOÐOÜ”z 'Ó*Ó+€KØ˜�O€EØ ×'Ñ'Ó)€IØ”f˜[°)Õ*;Ó<Õ=Ð=r   c                   ó|   a € ] tR t^It o RtRR ltRR ltRR lt]RR l4       t	RR lt
R tR	 tR
 tRR ltRtV tR# )ÚLanguageModelzCABC for Language Models.

Cannot be directly instantiated itself.

Nc                ó  € Wn         V'       dH   \        V\        4      '       g2   \        P                  ! RV P
                  P                  : R2^R7       Vf   \        4       MTV n        Vf   \        4       V n	        R# TV n	        R# )a  Creates new LanguageModel.

:param vocabulary: If provided, this vocabulary will be used instead
    of creating a new one when training.
:type vocabulary: `nltk.lm.Vocabulary` or None
:param counter: If provided, use this object to count ngrams.
:type counter: `nltk.lm.NgramCounter` or None
:param ngrams_fn: If given, defines how sentences in training text are turned to ngram
    sequences.
:type ngrams_fn: function or None
:param pad_fn: If given, defines how sentences in training text are padded.
:type pad_fn: function or None
z$The `vocabulary` argument passed to z- must be an instance of `nltk.lm.Vocabulary`.)Ú
stacklevelN)
Úorderr2   r   ÚwarningsÚwarnÚ	__class__r"   r   r   r   )r   rF   r   r   s   &&&&r   r   ÚLanguageModel.__init__P   si   € ð Œ
ßœj¨´Z×@Ò@Ü�MŠMØ6°t·~±~×7NÑ7NÑ6Qð R?ð ?àõð
 &0Ò%7”Z”\¸ZˆŒ
Ø(/ª”l“nˆŽ¸WˆŽr   c                óÊ   a € S P                   '       g+   Vf   \        R4      hS P                   P                  V4       S P                  P                  V 3R lV 4       4       R# )zUTrains the model on a text.

:param text: Training text as a sequence of sentences.

Nz:Cannot fit without a vocabulary or text to create it from.c              3   óZ   <"  € T F   pSP                   P                  V4      x € K"  	  R # 5ir   )r   Úlookup)Ú.0Úsentr   s   & €r   Ú	<genexpr>Ú$LanguageModel.fit.<locals>.<genexpr>t   s#   øé € ÐD¹t°t˜4Ÿ:™:×,Ñ,¨T×2Ð2»tùs   ƒ(+)r   r9   Úupdater   )r   ÚtextÚvocabulary_texts   f&&r   ÚfitÚLanguageModel.fith   sP   ø€ ð �z�zˆzØÒ&Ü ØPóð ð �J‰J×Ñ˜oÔ.Ø�‰×ÑÔD¹tÓDÖDr   c                ó¦   € T P                  V P                  P                  V4      V'       d    V P                  P                  V4      4      # R4      # )z‘Masks out of vocab (OOV) words and computes their model score.

For model-specific logic of calculating scores, see the `unmasked_score`
method.
N)Úunmasked_scorer   rM   r   s   &&&r   ÚscoreÚLanguageModel.scorev   sI   € ð ×"Ñ"Ø�J‰J×Ñ˜dÓ#Ç7 T§Z¡Z×%6Ñ%6°wÓ%?ó
ð 	
ØPTó
ð 	
r   c                ó   € \        4       h)aŠ  Score a word given some optional context.

Concrete models are expected to provide an implementation.
Note that this method does not mask its arguments with the OOV label.
Use the `score` method for that.

:param str word: Word for which we want the score
:param tuple(str) context: Context the word is in.
    If `None`, compute unigram score.
:param context: tuple(str) or None
:rtype: float
r   r   s   &&&r   rX   ÚLanguageModel.unmasked_score€   s   € ô "Ó#Ð#r   c                ó6   € \        V P                  W4      4      # )zwEvaluate the log score of this word in this context.

The arguments are the same as for `score` and `unmasked_score`.

)r   rY   r   s   &&&r   ÚlogscoreÚLanguageModel.logscore�   s   € ô ˜Ÿ™ DÓ2Ó3Ð3r   c                ó’   € V'       d+   V P                   \        V4      ^,           ,          V,          # V P                   P                  # )zšHelper method for retrieving counts for a given context.

Assumes context has been checked and oov words in it masked.
:type context: tuple(str) or None

)r   r.   Úunigrams)r   r   s   &&r   Úcontext_countsÚLanguageModel.context_counts˜   s7   € ÷ 7>ˆD�K‰Kœ˜G› qÕ(Õ)¨'Õ2ð	
ØCGÇ;Á;×CWÑCWð	
r   c                ó‚   € R\        V Uu. uF  q P                  VR,          VRR 4      NK   	  up4      ,          # u upi )a  Calculate cross-entropy of model for given evaluation text.

This implementation is based on the Shannon-McMillan-Breiman theorem,
as used and referenced by Dan Jurafsky and Jordan Boyd-Graber.

:param Iterable(tuple(str)) text_ngrams: A sequence of ngram tuples.
:rtype: float

Nr8   )r0   r^   )r   Útext_ngramsÚngrams   && r   ÚentropyÚLanguageModel.entropy£   s@   € ð ”EÙ?JÓK¹{°e�]‰]˜5 �9 e¨C¨R jÖ1¹{ÑKó
õ 
ð 	
ùÚKs   ‹$<
c                ó8   € \        RV P                  V4      4      # )z~Calculates the perplexity of the given text.

This is simply 2 ** cross-entropy for the text, so the arguments are the same.

g       @)Úpowrg   )r   re   s   &&r   Ú
perplexityÚLanguageModel.perplexity±   s   € ô �3˜Ÿ™ [Ó1Ó2Ð2r   c           	     óÒ  a a€ Vf   . M
\        V4      p\        V4      pV^8X  Ed   \        V4      S P                  8¼  d   VS P                  ) ^,           R MToS P	                  S P
                  P                  S4      4      pS'       dP   V'       gH   \        S4      ^8”  d
   SR,          M. oS P	                  S P
                  P                  S4      4      pKW  \        V4      p\        T\        ;QJ d    . VV 3R lV 4       F  NK  	  5V4      # ! VV 3R lV 4       4      V4      # . p\        V4       F,  pVP                  S P                  ^W&,           VR7      4       K.  	  V# )ah  Generate words from the model.

:param int num_words: How many words to generate. By default 1.
:param text_seed: Generation can be conditioned on preceding context.
:param random_seed: A random seed or an instance of `random.Random`. If provided,
    makes the random sampling part of generation reproducible.
:return: One (str) word or a list of words generated from model.

Examples:

>>> from nltk.lm import MLE
>>> lm = MLE(2)
>>> lm.fit([[("a", "b"), ("b", "c")]], vocabulary_text=['a', 'b', 'c'])
>>> lm.fit([[("a",), ("b",), ("c",)]])
>>> lm.generate(random_seed=3)
'a'
>>> lm.generate(text_seed=['a'])
'b'

N:é   NNc              3   óH   <"  € T F  pSP                  VS4      x € K  	  R # 5ir   )rY   )rN   Úwr   r   s   & €€r   rP   Ú)LanguageModel.generate.<locals>.<genexpr>á   s   øé € Ð>±g°�d—j‘j  G×,Ð,³gùs   ƒ")Ú	num_wordsÚ	text_seedÚrandom_seed)r:   r6   r.   rF   rb   r   rM   ÚsortedrA   ÚtupleÚrangeÚappendÚgenerate)	r   rr   rs   rt   r=   ÚsamplesÚ	generatedÚ_r   s	   f&&&    @r   ry   ÚLanguageModel.generate¹   sD  ù€ ð* $Ò+‘B´°i³ˆ	Ü,¨[Ó9Ðà˜�>ô �y“> T§Z¡ZÔ/ð ˜4Ÿ:™:˜+¨�/Ð+Ñ,àð ð
 ×)Ñ)¨$¯*©*×*;Ñ*;¸GÓ*DÓEˆGß§'Ü),¨W«¸Ô)9˜' "ž+¸r�Ø×-Ñ-¨d¯j©j×.?Ñ.?ÀÓ.HÓI’ô ˜W“oˆGÜ#Øß”Õ>±gÓ>—Ø óð àÕ>±gÓ>Ó>Ø óð ð ˆ	Ü�yÖ!ˆAØ×ÑØ—‘ØØ'Õ3Ø 0ð ó öñ "ð Ðr   )r   rF   r   )NNr   )rn   NN)r"   r#   r$   r%   r&   r   rU   rY   r   rX   r^   rb   rg   rk   ry   r'   r(   r)   s   @r   rC   rC   I   sM   ø‡ € ñôEô0Eô
ð ó$ó ð$ô4ò	
ò
ò3÷5ò 5r   rC   r   )r&   r3   rG   Úabcr   r   r   Ú	itertoolsr   Únltk.lm.counterr   Únltk.lm.utilr   Únltk.lm.vocabularyr   r
   r0   r6   rA   rC   © r   r   Ú<module>r„      sK   ðñ  ã Û ß 'Ý Ý  å (Ý "Ý )ô$˜'õ $ò6#ò
,ô>ôe˜g÷ er   