Package nltk :: Package tag :: Module hmm :: Class HiddenMarkovModelTagger
[hide private]
[frames] | no frames]

Class HiddenMarkovModelTagger

source code

 object --+    
          |    
api.TaggerI --+
              |
             HiddenMarkovModelTagger

Hidden Markov model class, a generative model for labelling sequence data. These models define the joint probability of a sequence of symbols and their labels (state transitions) as the product of the starting state probability, the probability of each state transition, and the probability of each observation being generated from each state. This is described in more detail in the module documentation.

This implementation is based on the HMM description in Chapter 8, Huang, Acero and Hon, Spoken Language Processing and includes an extension for training shallow HMM parsers or specializaed HMMs as in Molina et. al, 2002. A specialized HMM modifies training data by applying a specialization function to create a new training set that is more appropriate for sequential tagging with an HMM. A typical use case is chunking.

Instance Methods [hide private]
 
__init__(self, symbols, states, transitions, outputs, priors, **kwargs)
Creates a hidden markov model parametised by the the states, transition probabilities, output probabilities and priors.
source code
float
probability(self, sequence)
Returns the probability of the given symbol sequence.
source code
float
log_probability(self, sequence)
Returns the log-probability of the given symbol sequence.
source code
list
tag(self, unlabeled_sequence)
Tags the sequence with the highest probability state sequence.
source code
 
_tag(self, unlabeled_sequence) source code
float
_output_logprob(self, state, symbol)
Returns: the log probability of the symbol being observed in the given state
source code
 
_create_cache(self)
The cache is a tuple (P, O, X, S) where:
source code
 
_update_cache(self, symbols) source code
sequence of any
best_path(self, unlabeled_sequence)
Returns the state sequence of the optimal (most probable) path through the HMM.
source code
 
_best_path(self, unlabeled_sequence) source code
sequence of any
best_path_simple(self, unlabeled_sequence)
Returns the state sequence of the optimal (most probable) path through the HMM.
source code
 
_best_path_simple(self, unlabeled_sequence) source code
list
random_sample(self, rng, length)
Randomly sample the HMM to generate a sentence of a given length.
source code
 
_sample_probdist(self, probdist, p, samples) source code
 
entropy(self, unlabeled_sequence)
Returns the entropy over labellings of the given sequence.
source code
 
point_entropy(self, unlabeled_sequence)
Returns the pointwise entropy over the possible states at each position in the chain, given the observation sequence.
source code
 
_exhaustive_entropy(self, unlabeled_sequence) source code
 
_exhaustive_point_entropy(self, unlabeled_sequence) source code
array
_forward_probability(self, unlabeled_sequence)
Return the forward probability matrix, a T by N array of log-probabilities, where T is the length of the sequence and N is the number of states.
source code
array
_backward_probability(self, unlabeled_sequence)
Return the backward probability matrix, a T by N array of log-probabilities, where T is the length of the sequence and N is the number of states.
source code
 
test(self, test_sequence, **kwargs)
Tests the HiddenMarkovModelTagger instance.
source code
 
__repr__(self)
repr(x)
source code

Inherited from api.TaggerI: batch_tag

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

Class Methods [hide private]
 
_train(cls, labeled_sequence, test_sequence=None, unlabeled_sequence=None, **kwargs) source code
HiddenMarkovModelTagger
train(cls, labeled_sequence, test_sequence=None, unlabeled_sequence=None, **kwargs)
Train a new HiddenMarkovModelTagger using the given labeled and unlabeled training instances.
source code
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, symbols, states, transitions, outputs, priors, **kwargs)
(Constructor)

source code 

Creates a hidden markov model parametised by the the states, transition probabilities, output probabilities and priors.

Parameters:
  • symbols (seq of any) - the set of output symbols (alphabet)
  • states (seq of any) - a set of states representing state space
  • transitions (ConditionalProbDistI) - transition probabilities; Pr(s_i | s_j) is the probability of transition from state i given the model is in state_j
  • outputs (ConditionalProbDistI) - output probabilities; Pr(o_k | s_i) is the probability of emitting symbol k when entering state i
  • priors (ProbDistI) - initial state distribution; Pr(s_i) is the probability of starting in state i
  • transform (function or HiddenMarkovModelTaggerTransform) - an optional function for transforming training instances, defaults to the identity function.
Overrides: object.__init__

train(cls, labeled_sequence, test_sequence=None, unlabeled_sequence=None, **kwargs)
Class Method

source code 

Train a new HiddenMarkovModelTagger using the given labeled and unlabeled training instances. Testing will be performed if test instances are provided.

Parameters:
  • labeled_sequence (list of list) - a sequence of labeled training instances, i.e. a list of sentences represented as tuples
  • test_sequence (list of list) - a sequence of labeled test instances
  • unlabeled_sequence (list of list) - a sequence of unlabeled training instances, i.e. a list of sentences represented as words
  • transform (function) - an optional function for transforming training instances, defaults to the identity function, see transform()
  • estimator (class or function) - an optional function or class that maps a condition's frequency distribution to its probability distribution, defaults to a Lidstone distribution with gamma = 0.1
  • verbose (bool) - boolean flag indicating whether training should be verbose or include printed output
  • max_iterations (int) - number of Baum-Welch interations to perform
Returns: HiddenMarkovModelTagger
a hidden markov model tagger

probability(self, sequence)

source code 

Returns the probability of the given symbol sequence. If the sequence is labelled, then returns the joint probability of the symbol, state sequence. Otherwise, uses the forward algorithm to find the probability over all label sequences.

Parameters:
  • sequence (Token) - the sequence of symbols which must contain the TEXT property, and optionally the TAG property
Returns: float
the probability of the sequence

log_probability(self, sequence)

source code 

Returns the log-probability of the given symbol sequence. If the sequence is labelled, then returns the joint log-probability of the symbol, state sequence. Otherwise, uses the forward algorithm to find the log-probability over all label sequences.

Parameters:
  • sequence (Token) - the sequence of symbols which must contain the TEXT property, and optionally the TAG property
Returns: float
the log-probability of the sequence

tag(self, unlabeled_sequence)

source code 

Tags the sequence with the highest probability state sequence. This uses the best_path method to find the Viterbi path.

Parameters:
  • unlabeled_sequence (list) - the sequence of unlabeled symbols
Returns: list
a labelled sequence of symbols
Overrides: api.TaggerI.tag

_output_logprob(self, state, symbol)

source code 
Returns: float
the log probability of the symbol being observed in the given state

_create_cache(self)

source code 

The cache is a tuple (P, O, X, S) where:

  • S maps symbols to integers. I.e., it is the inverse mapping from self._symbols; for each symbol s in self._symbols, the following is true:
     self._symbols[S[s]] == s
    
  • O is the log output probabilities:
       O[i,k] = log( P(token[t]=sym[k]|tag[t]=state[i]) )
    
  • X is the log transition probabilities:
       X[i,j] = log( P(tag[t]=state[j]|tag[t-1]=state[i]) )
    
  • P is the log prior probabilities:
       P[i] = log( P(tag[0]=state[i]) )
    

best_path(self, unlabeled_sequence)

source code 

Returns the state sequence of the optimal (most probable) path through the HMM. Uses the Viterbi algorithm to calculate this part by dynamic programming.

Parameters:
  • unlabeled_sequence (list) - the sequence of unlabeled symbols
Returns: sequence of any
the state sequence

best_path_simple(self, unlabeled_sequence)

source code 

Returns the state sequence of the optimal (most probable) path through the HMM. Uses the Viterbi algorithm to calculate this part by dynamic programming. This uses a simple, direct method, and is included for teaching purposes.

Parameters:
  • unlabeled_sequence (list) - the sequence of unlabeled symbols
Returns: sequence of any
the state sequence

random_sample(self, rng, length)

source code 

Randomly sample the HMM to generate a sentence of a given length. This samples the prior distribution then the observation distribution and transition distribution for each subsequent observation and state. This will mostly generate unintelligible garbage, but can provide some amusement.

Parameters:
  • rng (Random (or any object with a random() method)) - random number generator
  • length (int) - desired output length
Returns: list
the randomly created state/observation sequence, generated according to the HMM's probability distributions. The SUBTOKENS have TEXT and TAG properties containing the observation and state respectively.

entropy(self, unlabeled_sequence)

source code 

Returns the entropy over labellings of the given sequence. This is given by:

H(O) = - sum_S Pr(S | O) log Pr(S | O)

where the summation ranges over all state sequences, S. Let Z = Pr(O) = sum_S Pr(S, O) where the summation ranges over all state sequences and O is the observation sequence. As such the entropy can be re-expressed as:

H = - sum_S Pr(S | O) log [ Pr(S, O) / Z ]
  = log Z - sum_S Pr(S | O) log Pr(S, 0)
  = log Z - sum_S Pr(S | O) [ log Pr(S_0) + sum_t Pr(S_t | S_{t-1})
                                          + sum_t Pr(O_t | S_t) ]

The order of summation for the log terms can be flipped, allowing dynamic programming to be used to calculate the entropy. Specifically, we use the forward and backward probabilities (alpha, beta) giving:

H = log Z - sum_s0 alpha_0(s0) beta_0(s0) / Z * log Pr(s0)
        + sum_t,si,sj alpha_t(si) Pr(sj | si) Pr(O_t+1 | sj) beta_t(sj)
                        / Z * log Pr(sj | si)
        + sum_t,st alpha_t(st) beta_t(st) / Z * log Pr(O_t | st)

This simply uses alpha and beta to find the probabilities of partial sequences, constrained to include the given state(s) at some point in time.

_forward_probability(self, unlabeled_sequence)

source code 

Return the forward probability matrix, a T by N array of log-probabilities, where T is the length of the sequence and N is the number of states. Each entry (t, s) gives the probability of being in state s at time t after observing the partial symbol sequence up to and including t.

Parameters:
  • unlabeled_sequence (list) - the sequence of unlabeled symbols
Returns: array
the forward log probability matrix

_backward_probability(self, unlabeled_sequence)

source code 

Return the backward probability matrix, a T by N array of log-probabilities, where T is the length of the sequence and N is the number of states. Each entry (t, s) gives the probability of being in state s at time t after observing the partial symbol sequence from t .. T.

Parameters:
  • unlabeled_sequence (list) - the sequence of unlabeled symbols
Returns: array
the backward log probability matrix

test(self, test_sequence, **kwargs)

source code 

Tests the HiddenMarkovModelTagger instance.

Parameters:
  • test_sequence (list of list) - a sequence of labeled test instances
  • verbose (bool) - boolean flag indicating whether training should be verbose or include printed output

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)