Package nltk :: Package model :: Module ngram :: Class NgramModel
[hide private]
[frames] | no frames]

type NgramModel

source code

object --+    
         |    
api.ModelI --+
             |
            NgramModel

A processing interface for assigning a probability to the next word.

Instance Methods [hide private]
 
__init__(self, n, train, estimator=None)
Creates an ngram language model to capture patterns in n consecutive words of training text.
source code
 
prob(self, word, context)
Evaluate the probability of this word in this context.
source code
 
_alpha(self, tokens) source code
 
_beta(self, tokens) source code
 
logprob(self, word, context)
Evaluate the (negative) log probability of this word in this context.
source code
 
choose_random_word(self, context)
Randomly select a word that is likely to appear in this context.
source code
 
generate(self, num_words, context=())
Generate random text based on the language model.
source code
 
_generate_one(self, context) source code
 
entropy(self, text)
Evaluate the total entropy of a text with respect to the model.
source code
 
__contains__(self, item) source code
 
__getitem__(self, item) source code
 
__repr__(self) source code
Method Details [hide private]

__init__(self, n, train, estimator=None)
(Constructor)

source code 

Creates an ngram language model to capture patterns in n consecutive words of training text. An estimator smooths the probabilities derived from the text and may allow generation of ngrams not seen during training.

Parameters:
  • n (int) - the order of the language model (ngram size)
  • train (list of string) - the training text
  • estimator (a function that takes a ConditionalFreqDist and returns a ConditionalProbDist) - a function for generating a probability distribution
Overrides: api.ModelI.__init__

prob(self, word, context)

source code 

Evaluate the probability of this word in this context.

Overrides: api.ModelI.prob

logprob(self, word, context)

source code 

Evaluate the (negative) log probability of this word in this context.

Overrides: api.ModelI.logprob

choose_random_word(self, context)

source code 

Randomly select a word that is likely to appear in this context.

Overrides: api.ModelI.choose_random_word

generate(self, num_words, context=())

source code 

Generate random text based on the language model.

Overrides: api.ModelI.generate

entropy(self, text)

source code 

Evaluate the total entropy of a text with respect to the model. This is the sum of the log probability of each word in the message.

Overrides: api.ModelI.entropy

__repr__(self)
(Representation operator)

source code 
Overrides: object.__repr__
(inherited documentation)