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

Class 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
 
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)
repr(x)
source code

Inherited from api.ModelI: choose_random_word, logprob

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __str__

Properties [hide private]

Inherited from object: __class__

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 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

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 

repr(x)

Overrides: object.__repr__
(inherited documentation)