³ò
ŽB_Kc           @   s¯  d  Z  d d k Z d d k Z d d k Td d k l Z l Z l Z l Z l	 Z	 l
 Z
 l Z l Z d d k l Z d d k l Z d d k l Z l Z l Z d d k Te d ƒ Z d	 Z d
 Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d e  f d „  ƒ  YZ! d e  f d „  ƒ  YZ" d „  Z# d „  Z$ d „  Z% e d ƒ e& d „ ƒ Z' d „  Z( d „  Z) d „  Z* d „  Z+ e, d j o e$ ƒ  e( ƒ  e* ƒ  n d S(   s»  
Hidden Markov Models (HMMs) largely used to assign the correct label sequence
to sequential data or assess the probability of a given label and data
sequence. These models are finite state machines characterised by a number of
states, transitions between these states, and output symbols emitted while in
each state. The HMM is an extension to the Markov chain, where each state
corresponds deterministically to a given event. In the HMM the observation is
a probabilistic function of the state. HMMs share the Markov chain's
assumption, being that the probability of transition from one state to another
only depends on the current state - i.e. the series of states that led to the
current state are not used. They are also time invariant.

The HMM is a directed graph, with probability weighted edges (representing the
probability of a transition between the source and sink states) where each
vertex emits an output symbol when entered. The symbol (or observation) is
non-deterministically generated. For this reason, knowing that a sequence of
output observations was generated by a given HMM does not mean that the
corresponding sequence of states (and what the current state is) is known.
This is the 'hidden' in the hidden markov model.

Formally, a HMM can be characterised by:
    - the output observation alphabet. This is the set of symbols which may be
      observed as output of the system. 
    - the set of states. 
    - the transition probabilities M{a_{ij} = P(s_t = j | s_{t-1} = i)}. These
      represent the probability of transition to each state from a given
      state. 
    - the output probability matrix M{b_i(k) = P(X_t = o_k | s_t = i)}. These
      represent the probability of observing each symbol in a given state.
    - the initial state distribution. This gives the probability of starting
      in each state.

To ground this discussion, take a common NLP application, part-of-speech (POS)
tagging. An HMM is desirable for this task as the highest probability tag
sequence can be calculated for a given sequence of word forms. This differs
from other tagging techniques which often tag each word individually, seeking
to optimise each individual tagging greedily without regard to the optimal
combination of tags for a larger unit, such as a sentence. The HMM does this
with the Viterbi algorithm, which efficiently computes the optimal path
through the graph given the sequence of words forms.

In POS tagging the states usually have a 1:1 correspondence with the tag
alphabet - i.e. each state represents a single tag. The output observation
alphabet is the set of word forms (the lexicon), and the remaining three
parameters are derived by a training regime. With this information the
probability of a given sentence can be easily derived, by simply summing the
probability of each distinct path through the model. Similarly, the highest
probability tagging sequence can be derived with the Viterbi algorithm,
yielding a state sequence which can be mapped into a tag sequence.

This discussion assumes that the HMM has been trained. This is probably the
most difficult task with the model, and requires either MLE estimates of the
parameters or unsupervised learning using the Baum-Welch algorithm, a variant
of EM.
iÿÿÿÿN(   t   *(   t   FreqDistt   ConditionalFreqDistt   ConditionalProbDistt   DictionaryProbDistt   DictionaryConditionalProbDistt   LidstoneProbDistt   MutableProbDistt   MLEProbDist(   t
   deprecated(   t   accuracy(   t   LazyMapt   LazyConcatenationt   LazyZips   -1e300i    i   t   HiddenMarkovModelTaggerc           B   sþ   e  Z d  Z d „  Z e e e d „ ƒ Z e e e d „ ƒ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s€  
    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.
    c         K   s˜   | |  _  | |  _ | |  _ | |  _ | |  _ d |  _ | i d t ƒ  ƒ |  _	 t
 |  i	 t i ƒ o t |  i	 ƒ |  _	 n t
 |  i	 t ƒ p ‚  n d S(   sP  
        Creates a hidden markov model parametised by the the states,
        transition probabilities, output probabilities and priors.

        @param symbols: the set of output symbols (alphabet)
        @type symbols: seq of any
        @param states: a set of states representing state space
        @type states: seq of any
        @param transitions: transition probabilities; Pr(s_i | s_j) is the
            probability of transition from state i given the model is in
            state_j
        @type transitions: C{ConditionalProbDistI}
        @param outputs: output probabilities; Pr(o_k | s_i) is the probability
            of emitting symbol k when entering state i
        @type outputs: C{ConditionalProbDistI}
        @param priors: initial state distribution; Pr(s_i) is the probability
            of starting in state i
        @type priors: C{ProbDistI}
        @kwparam transform: an optional function for transforming training
            instances, defaults to the identity function.         
        @type transform: C{function} or C{HiddenMarkovModelTaggerTransform}
        t	   transformN(   t   _statest   _transitionst   _symbolst   _outputst   _priorst   Nonet   _cachet   gett   IdentityTransformt
   _transformt
   isinstancet   typest   FunctionTypet   LambdaTransformt!   HiddenMarkovModelTaggerTransformI(   t   selft   symbolst   statest   transitionst   outputst   priorst   kwargs(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   __init__j   s    							
c         K   s  | i  d t ƒ  ƒ } t | t i ƒ o t | ƒ } n t | t ƒ p ‚  n | i  d d „  ƒ } t | i | ƒ } t	 t
 d „  | Dƒ ƒ ƒ } t	 t
 d „  | Dƒ ƒ ƒ } t | | ƒ }	 |	 i | d | ƒ}
 |  |
 i |
 i |
 i |
 i |
 i d | ƒ}
 | o# |
 i | d | i  d t ƒ ƒn | o[ | i  d d ƒ } |	 i | d	 |
 d | ƒ}
 | o# |
 i | d | i  d t ƒ ƒq‹n |
 S(
   NR   t	   estimatorc         S   s   t  |  d  | ƒ S(   gš™™™™™¹?(   R   (   t   fdt   bins(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   <lambda>™   s    c         s   s0   x) |  ]" } x | D] \ } } | Vq Wq Wd  S(   N(    (   t   .0t   sentt   wordt   tag(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pys	   <genexpr>   s   c         s   s0   x) |  ]" } x | D] \ } } | Vq Wq Wd  S(   N(    (   R+   R,   R-   R.   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pys	   <genexpr>Ÿ   s   t   verboset   max_iterationsi   t   model(   R   R   R   R   R   R   R   R   R   t   listt   sett   HiddenMarkovModelTrainert   train_supervisedR   R   R   R   R   t   testt   Falset   train_unsupervised(   t   clst   labeled_sequencet   test_sequencet   unlabeled_sequenceR%   R   R'   R    t   tag_sett   trainert   hmmR0   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   _train   s,    #	'c         K   s   |  i  | | | |  S(   s¬  
    	Train a new C{HiddenMarkovModelTagger} using the given labeled and
    	unlabeled training instances. Testing will be performed if test
    	instances are provided.
    	
    	@return: a hidden markov model tagger
    	@rtype: C{HiddenMarkovModelTagger}
    	@param labeled_sequence: a sequence of labeled training instances,
    	    i.e. a list of sentences represented as tuples
    	@type labeled_sequence: C{list} of C{list}
    	@param test_sequence: a sequence of labeled test instances
    	@type test_sequence: C{list} of C{list}
    	@param unlabeled_sequence: a sequence of unlabeled training instances,
    	    i.e. a list of sentences represented as words
    	@type unlabeled_sequence: C{list} of C{list}
        @kwparam transform: an optional function for transforming training
            instances, defaults to the identity function, see L{transform()}
        @type transform: C{function}
        @kwparam estimator: 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
        @type estimator: C{class} or C{function}
        @kwparam verbose: boolean flag indicating whether training should be
            verbose or include printed output
        @type verbose: C{bool}
        @kwparam max_iterations: number of Baum-Welch interations to perform
        @type max_iterations: C{int}
    	(   R@   (   R9   R:   R;   R<   R%   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   train³   s    c         C   s   d |  i  |  i i | ƒ ƒ S(   s   
        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.

        @return: the probability of the sequence
        @rtype: float
        @param sequence: the sequence of symbols which must contain the TEXT
            property, and optionally the TAG property
        @type sequence:  Token
        i   (   t   log_probabilityR   R   (   R   t   sequence(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   probabilityÕ   s    c   	      C   s(  |  i  i | ƒ } t | ƒ } t |  i ƒ } | d j o· | d t o¨ | d t } |  i i | ƒ |  i | i | d t ƒ } xa t	 d | ƒ D]P } | | t } | |  i
 | i | ƒ |  i | i | | t ƒ 7} | } q• W| Sn4 |  i | ƒ } t | | d d d … f Œ  } | Sd S(   s  
        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.

        @return: the log-probability of the sequence
        @rtype: float
        @param sequence: the sequence of symbols which must contain the TEXT
            property, and optionally the TAG property
        @type sequence:  Token
        i    i   N(   R   R   t   lenR   t   _TAGR   t   logprobR   t   _TEXTt   rangeR   t   _forward_probabilityt   _log_add(	   R   RC   t   Tt   Nt
   last_statet   pt   tt   statet   alpha(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyRB   ä   s"      
 c         C   s   |  i  i | ƒ } |  i | ƒ S(   sD  
        Tags the sequence with the highest probability state sequence. This
        uses the best_path method to find the Viterbi path.

        @return: a labelled sequence of symbols
        @rtype: list
        @param unlabeled_sequence: the sequence of unlabeled symbols 
        @type unlabeled_sequence: list
        (   R   R   t   _tag(   R   R<   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR.     s    
c         C   s   |  i  | ƒ } t | | ƒ S(   N(   t
   _best_patht   zip(   R   R<   t   path(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyRS     s    c         C   s   |  i  | i | ƒ S(   s€   
        @return: the log probability of the symbol being observed in the given
            state
        @rtype: float
        (   R   RG   (   R   RQ   t   symbol(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   _output_logprob  s    c         C   sb  |  i  pTt |  i ƒ } t |  i ƒ } t | t ƒ } t | | f t ƒ } t | | f t ƒ } x³ t | ƒ D]¥ } |  i | } |  i i | ƒ | | <x; t | ƒ D]- } |  i	 | i |  i | ƒ | | | f <q¤ Wx; t | ƒ D]- }	 |  i
 | i |  i |	 ƒ | | |	 f <qâ Wqn Wh  }
 x% t | ƒ D] }	 |	 |
 |  i |	 <q*W| | | |
 f |  _  n d S(   s—  
        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]) )
        N(   R   RE   R   R   t   zerost   float32RI   R   RG   R   R   (   R   RM   t   Mt   Pt   Xt   Ot   it   sit   jt   kt   S(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   _create_cache  s*    
  + / c      
   C   so  | od|  i  ƒ  |  i \ } } } } x; | D]3 } | |  i j o d  |  _ |  i i | ƒ q- q- W|  i pù t |  i ƒ } t |  i ƒ } | i d }	 t | t	 | | |	 f t
 ƒ g ƒ } xb t | ƒ D]T }
 |  i |
 } x> t |	 | ƒ D]- } |  i | i |  i | ƒ | |
 | f <qî WqË Wx( t |	 | ƒ D] } | | |  i | <q3W| | | | f |  _ qkn d  S(   Ni   (   Rd   R   R   R   t   appendRE   R   t   shapet   hstackRY   RZ   RI   R   RG   (   R   R    R\   R^   R]   Rc   RW   RM   R[   t   QR_   R`   Rb   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   _update_cacheF  s.    
 	
%  / c         C   s   |  i  i | ƒ } |  i | ƒ S(   sm  
        Returns the state sequence of the optimal (most probable) path through
        the HMM. Uses the Viterbi algorithm to calculate this part by dynamic
        programming.

        @return: the state sequence
        @rtype: sequence of any
        @param unlabeled_sequence: the sequence of unlabeled symbols 
        @type unlabeled_sequence: list
        (   R   R   RT   (   R   R<   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt	   best_pathb  s    c         C   sÊ  t  | ƒ } t  |  i ƒ } |  i ƒ  |  i | ƒ |  i \ } } } } t | | f t ƒ } t | | f t ƒ d }	 | | d  d  … | | d f | d <x¢ t	 d | ƒ D]‘ }
 xˆ t	 | ƒ D]z } | |
 d d  d  … f | d  d  … | f } t
 | ƒ } | | | | | | |
 f | |
 | f <| |	 |
 | f <q¾ Wq« Wt
 | | d d  d  … f ƒ } | g } xA t	 | d d d ƒ D]) }
 |	 |
 | f } | i | ƒ | } q€W| i ƒ  t |  i i | ƒ S(   Niÿÿÿÿi    i   (   RE   R   Rd   Ri   R   RY   RZ   t   onest   intRI   t   argmaxRe   t   reverset   mapt   __getitem__(   R   R<   RL   RM   R\   R^   R]   Rc   t   Vt   BRP   Ra   t   vst   bestt   currentRC   t   last(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyRT   p  s2    
&  .* 	 

c         C   s   |  i  i | ƒ } |  i | ƒ S(   sÀ  
        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.

        @return: the state sequence
        @rtype: sequence of any
        @param unlabeled_sequence: the sequence of unlabeled symbols 
        @type unlabeled_sequence: list
        (   R   R   t   _best_path_simple(   R   R<   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   best_path_simpleŒ  s    c      
   C   s]  t  | ƒ } t  |  i ƒ } t | | f t ƒ } h  } | d } xY t |  i ƒ D]H \ } } |  i i | ƒ |  i | | ƒ | d | f <d  | d | f <qP Wxø t	 d | ƒ D]ç }	 | |	 } xÔ t	 | ƒ D]Æ }
 |  i |
 } d  } xr t	 | ƒ D]d } |  i | } | |	 d | f |  i
 | i | ƒ } | p | | d j o | | f } qï qï W| d |  i | | ƒ | |	 |
 f <| d | |	 | f <qÉ Wq¬ Wd  } xX t	 | ƒ D]J } | | d | f } | p | | d j o | |  i | f } qªqªW| d } | g } xA t	 | d d d ƒ D]) }	 | |	 | f } | i | ƒ | } q"W| i ƒ  | S(   Ni    i   iÿÿÿÿ(   RE   R   RY   t   float64t	   enumerateR   RG   RX   R   RI   R   Re   Rn   (   R   R<   RL   RM   Rq   Rr   RW   R_   RQ   RP   Ra   t   sjRt   R`   t   vat   valRu   RC   Rv   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyRw   ›  sP    
  
  ($ 
	 

c         C   sÚ   g  } |  i  |  i | i ƒ  |  i ƒ } |  i  |  i | | i ƒ  |  i ƒ } | i | | f ƒ xt t d | ƒ D]c } |  i  |  i | | i ƒ  |  i ƒ } |  i  |  i | | i ƒ  |  i ƒ } | i | | f ƒ qo W| S(   ss  
        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.

        @return:        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.
        @rtype:         list
        @param rng:     random number generator
        @type rng:      Random (or any object with a random() method)
        @param length:  desired output length
        @type length:   int
        i   (	   t   _sample_probdistR   t   randomR   R   R   Re   RI   R   (   R   t   rngt   lengtht   tokensRQ   RW   R_   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   random_sampleÈ  s    ! c         C   sj   d } xQ | D]I } | i  | ƒ } | | j o | | j n o | Sn | | 7} q Wt d ƒ ‚ d  S(   Ni    s6   Invalid probability distribution - does not sum to one(   t   probt	   Exception(   R   t   probdistRO   t   samplest   cum_pt   samplet   add_p(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR~   í  s     "c         C   s1  |  i  i | ƒ } t | ƒ } t |  i ƒ } |  i | ƒ } |  i | ƒ } t | | d d d … f Œ  } | } x] t |  i ƒ D]L \ } }	 d | d | f | d | f | }
 | |
 |  i i	 |	 ƒ 8} q WxÖ t
 | d ƒ D]Ä } | d } x± t |  i ƒ D]  \ } } x‘ t |  i ƒ D]€ \ } } d | | | f |  i | i	 | ƒ |  i | i	 | | t ƒ | | | f | }
 | |
 |  i | i	 | ƒ 8} qWqWqâ Wx€ t
 | ƒ D]r } xi t |  i ƒ D]X \ } }	 d | | | f | | | f | }
 | |
 |  i |	 i	 | | t ƒ 8} qÍWq·W| S(   sF  
        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 M{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.
        i   Ni   i    (   R   R   RE   R   RJ   t   _backward_probabilityRK   Rz   R   RG   RI   R   R   RH   (   R   R<   RL   RM   RR   t   betat   normalisationt   entropyR_   RQ   RO   t   t0t   t1t   i0t   s0t   i1t   s1RP   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyRŽ   ÷  s:      & 
  S*  &c      
   C   s  |  i  i | ƒ } t | ƒ } t |  i ƒ } |  i | ƒ } |  i | ƒ } t | | d d d … f Œ  } t | t ƒ } t | t ƒ } xˆ t	 | ƒ D]z }	 x: t	 | ƒ D], }
 | |	 |
 f | |	 |
 f | | |
 <q© Wx4 t	 | ƒ D]& }
 | |	 c d | |
 | |
 8<qæ Wq– W| S(   s   
        Returns the pointwise entropy over the possible states at each
        position in the chain, given the observation sequence.
        i   Ni   (
   R   R   RE   R   RJ   R‹   RK   RY   Ry   RI   (   R   R<   RL   RM   RR   RŒ   R   t	   entropiest   probsRP   t   s(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   point_entropy<  s"       * (c      
   C   sf  |  i  i | ƒ } t | ƒ } t |  i ƒ } g  } |  i D] } | | g q; ~ } x] t | d ƒ D]K } | } g  } x6 | D]. }	 x% |  i D] } | i |	 | g ƒ qŽ Wq~ Wqe Wg  }
 xf | D]^ }	 | } x2 t |	 ƒ D]$ \ } } | | t | f | | <qÛ W|  i | ƒ } |
 i | ƒ qÁ Wt	 |
 Œ  } d } x* |
 D]" } | | 8} | d | | 8} q<W| S(   Ni   i    i   (
   R   R   RE   R   RI   Re   Rz   RH   RB   RK   (   R   R<   RL   RM   t   _[1]RQ   t
   labellingsRP   Ru   t	   labellingt	   log_probst   labelled_sequencet   labelt   lpR   RŽ   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   _exhaustive_entropyU  s:    '  
     
c         C   s4  |  i  i | ƒ } t | ƒ } t |  i ƒ } g  } |  i D] } | | g q; ~ } x] t | d ƒ D]K } | } g  } x6 | D]. }	 x% |  i D] } | i |	 | g ƒ qŽ Wq~ Wqe Wg  }
 xf | D]^ }	 | } x2 t |	 ƒ D]$ \ } } | | t | f | | <qÛ W|  i | ƒ } |
 i | ƒ qÁ Wt	 |
 Œ  } t
 | | f t ƒ } t | (xy t | |
 ƒ D]h \ }	 } | | 8} xO t |	 ƒ D]A \ } } |  i i | ƒ } t	 | | | f | ƒ | | | f <q~Wq[Wt
 | t ƒ } xW t | ƒ D]I } x@ t | ƒ D]2 } | | c d | | | f | | | f 8<qöWqãW| S(   Ni   i   (   R   R   RE   R   RI   Re   Rz   RH   RB   RK   RY   Ry   t   _NINFRU   t   index(   R   R<   RL   RM   R™   RQ   Rš   RP   Ru   R›   Rœ   R   Rž   RŸ   R   t   probabilitiesR¢   R•   R—   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   _exhaustive_point_entropy|  sN    '  
     
 +  4c         C   su  t  | ƒ } t  |  i ƒ } t | | f t ƒ } | d t } xM t |  i ƒ D]< \ } } |  i i | ƒ |  i | i | ƒ | d | f <qN Wxà t	 d | ƒ D]Ï } | | t } x¸ t |  i ƒ D]§ \ } }	 t
 | | | f <xb t |  i ƒ D]Q \ }
 } t | | | f | | d |
 f |  i | i |	 ƒ ƒ | | | f <qî W| | | f c |  i |	 i | ƒ 7<qÂ Wqž W| S(   s  
        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.

        @param unlabeled_sequence: the sequence of unlabeled symbols 
        @type unlabeled_sequence: list
        @return: the forward log probability matrix
        @rtype:  array
        i    i   (   RE   R   RY   Ry   RH   Rz   R   RG   R   RI   R¡   RK   R   (   R   R<   RL   RM   RR   RW   R_   RQ   RP   R`   Ra   R{   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyRJ   £  s(     %   !(.c      	   C   s0  t  | ƒ } t  |  i ƒ } t | | f t ƒ } t d ƒ | | d d d … f <xÙ t | d d d ƒ D]Á } | | d t } x¦ t |  i ƒ D]• \ } } t | | | f <xv t |  i ƒ D]e \ }	 }
 t	 | | | f |  i
 | i |
 ƒ |  i |
 i | ƒ | | d |	 f ƒ | | | f <q» Wq Wqg W| S(   sü  
        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.

        @return: the backward log probability matrix
        @rtype:  array
        @param unlabeled_sequence: the sequence of unlabeled symbols 
        @type unlabeled_sequence: list
        i   Ni   iÿÿÿÿ(   RE   R   RY   Ry   t   log2RI   RH   Rz   R¡   RK   R   RG   R   (   R   R<   RL   RM   RŒ   RP   RW   R_   R`   Ra   R{   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR‹   Ã  s         '.c         K   s  d „  } d „  } t  |  i i | ƒ } t  |  i t  | | ƒ ƒ } | i d t ƒ oMt | ƒ } t | ƒ } x2t | | ƒ D]\ } } d Gd i g  } | D]) \ }	 }
 | d t	 |	 ƒ t	 |
 ƒ f qž ~ ƒ GHHd Gd i g  } | D] \ }	 }
 | t	 |	 ƒ qè ~ ƒ GHHd Gd i g  } | D]) \ }	 }
 | d t	 |	 ƒ t	 |
 ƒ f q"~ ƒ GHHd	 G|  i
 g  } | D] \ }	 }
 | |	 d f ql~ ƒ GHHd
 d GHq} Wn t t  | | ƒ ƒ } t t  | | ƒ ƒ } t | | ƒ } t g  } | D] } | t | ƒ qé~ ƒ } d | | d f GHd S(   sK  
        Tests the C{HiddenMarkovModelTagger} instance.

    	@param test_sequence: a sequence of labeled test instances
        @type test_sequence: C{list} of C{list}
        @kwparam verbose: boolean flag indicating whether training should be
            verbose or include printed output
        @type verbose: C{bool}
        c         S   s%   g  } |  D] \ } } | | q ~ S(   N(    (   R,   R™   R-   R.   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   wordsï  s    c         S   s%   g  } |  D] \ } } | | q ~ S(   N(    (   R,   R™   R-   R.   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   tagsò  s    R/   s   Test:t    s   %s/%ss	   Untagged:s   HMM-tagged:s   Entropy:t   -i<   s   accuracy over %d tokens: %.2fid   N(   R   R   R   RS   R   R7   R2   RU   t   joint   strRŽ   R   R   t	   _accuracyt   sumRE   (   R   R;   R%   R¦   R§   t   predicted_sequencet	   test_sentt   predicted_sentR™   t   tokenR.   t   _[2]t   _[3]t   _[4]t	   test_tagst   predicted_tagst   acct   _[5]R,   t   count(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR6   ä  s>    			 858(-c         C   s    d t  |  i ƒ t  |  i ƒ f S(   Ns9   <HiddenMarkovModelTagger %d states and %d output symbols>(   RE   R   R   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   __repr__  s    (   t   __name__t
   __module__t   __doc__R&   t   classmethodR   R@   RA   RD   RB   R.   RS   RX   Rd   Ri   Rj   RT   Rx   Rw   Rƒ   R~   RŽ   R˜   R    R¤   RJ   R‹   R6   Rº   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR   Y   s:   	%" 		!				(					-	%	
	E		'	'	 	!	6R4   c           B   s>   e  Z d  Z e e d „ Z e e d „ Z d „  Z d „  Z RS(   s   
    Algorithms for learning HMM parameters from training data. These include
    both supervised learning (MLE) and unsupervised learning (Baum-Welch).
    c         C   s>   | o | |  _  n
 g  |  _  | o | |  _ n
 g  |  _ d S(   s×  
        Creates an HMM trainer to induce an HMM with the given states and
        output symbol alphabet. A supervised and unsupervised training
        method may be used. If either of the states or symbols are not given,
        these may be derived from supervised training.

        @param states:  the set of state labels
        @type states:   sequence of any
        @param symbols: the set of observation symbols
        @type symbols:  sequence of any
        N(   R   R   (   R   R!   R    (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR&   $  s    	c         K   sn   | p | p t  ‚ t } | o |  i | |  } n | o+ | o | | d <n |  i | |  } n | S(   s=  
        Trains the HMM using both (or either of) supervised and unsupervised
        techniques.

        @return: the trained model
        @rtype: HiddenMarkovModelTagger
        @param labelled_sequences: the supervised training data, a set of
            labelled sequences of observations
        @type labelled_sequences: list
        @param unlabeled_sequences: the unsupervised training data, a set of
            sequences of observations
        @type unlabeled_sequences: list
        @param kwargs: additional arguments to pass to the training methods
        R1   (   t   AssertionErrorR   R5   R8   (   R   t   labelled_sequencest   unlabeled_sequencesR%   R1   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyRA   9  s    c   &         sp  t  ˆ  i ƒ } t  ˆ  i ƒ } t ‡  f d †  t | ƒ Dƒ ƒ } | i d ƒ ‰ ˆ p{ t ˆ  i ƒ } t t ‡  f d †  ˆ  i Dƒ ƒ ƒ } t t ‡  f d †  ˆ  i Dƒ ƒ ƒ } t ˆ  i ˆ  i | | | ƒ ‰ n t	 ˆ i
 ˆ  i ƒ ˆ _
 t t ‡ ‡  f d †  ˆ  i Dƒ ƒ ƒ ˆ _ t t ‡ ‡  f d †  ˆ  i Dƒ ƒ ƒ ˆ _ t }	 d }
 d } | i d d	 ƒ } | i d
 d ƒ } xô|	 oë| | j  oÞt | | f t ƒ t } t | | f t ƒ t } t | t ƒ t } t | t ƒ t } d } xl| D]d} t | ƒ } | p qòn ˆ i | ƒ } ˆ i | ƒ } t  | ƒ } t | | d d d … f Œ  } | | 7} t | | f t ƒ t } t | | f t ƒ t } t | t ƒ t } t | t ƒ t } x°t | ƒ D]¢} | | t } | | d j  o | | d t } n | | } xZt | ƒ D]L}  ˆ  i |  }! | | d j  o¾ xˆ t | ƒ D]z }" ˆ  i |" }# t | |  |" f | | |  f ˆ i |! i |# ƒ ˆ i |# i | ƒ | | d |" f ƒ | |  |" f <qNWt | |  | | |  f | | |  f ƒ | |  <n0 t | |  | | |  f | | |  f ƒ | |  <t | |  | f | | |  f | | |  f ƒ | |  | f <qWqËWxâ t | ƒ D]Ô }  xE t | ƒ D]7 }" t | |  |" f | |  |" f | ƒ | |  |" f <q‘WxE t | ƒ D]7 }$ t | |  |$ f | |  |$ f | ƒ | |  |$ f <qÙWt | |  | |  | ƒ | |  <t | |  | |  | ƒ | |  <q~WqòWx» t | ƒ D]­ }  ˆ  i |  }! xJ t | ƒ D]< }" ˆ  i |" }# ˆ i |! i |# | |  |" f | |  ƒ q‡WxJ t | ƒ D]< }$ ˆ  i |$ }% ˆ i |! i |% | |  |$ f | |  ƒ qÔWqgW| d j o! t | |
 ƒ | j  o
 t }	 n d G| Gd G| GH| d 7} | }
 qxWˆ S(   sÎ  
        Trains the HMM using the Baum-Welch algorithm to maximise the
        probability of the data sequence. This is a variant of the EM
        algorithm, and is unsupervised in that it doesn't need the state
        sequences for the symbols. The code is based on 'A Tutorial on Hidden
        Markov Models and Selected Applications in Speech Recognition',
        Lawrence Rabiner, IEEE, 1989.

        @return: the trained model
        @rtype: HiddenMarkovModelTagger
        @param unlabeled_sequences: the training data, a set of
            sequences of observations
        @type unlabeled_sequences: list
        @param kwargs: may include the following parameters::
            model - a HiddenMarkovModelTagger instance used to begin
                the Baum-Welch algorithm
            max_iterations - the maximum number of EM iterations
            convergence_logprob - the maximum change in log probability to
                allow convergence
        c         3   s&   x |  ] } ˆ  i  | | f Vq Wd  S(   N(   R   (   R+   R_   (   R   (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pys	   <genexpr>j  s    R1   c         3   s(   x! |  ] } | t  ˆ  i ƒ f Vq Wd  S(   N(   t   UniformProbDistR   (   R+   RQ   (   R   (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pys	   <genexpr>r  s   	c         3   s(   x! |  ] } | t  ˆ  i ƒ f Vq Wd  S(   N(   RÂ   R   (   R+   RQ   (   R   (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pys	   <genexpr>u  s   	c         3   s2   x+ |  ]$ } | t  ˆ  i | ˆ i ƒ f Vq Wd  S(   N(   R   R   R   (   R+   R—   (   R1   R   (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pys	   <genexpr>}  s   	c         3   s2   x+ |  ]$ } | t  ˆ  i | ˆ i ƒ f Vq Wd  S(   N(   R   R   R   (   R+   R—   (   R1   R   (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pys	   <genexpr>€  s   	i    R0   iè  t   convergence_logprobgíµ ÷Æ°>i   Nt	   iterationRG   (   RE   R   R   t   dictRI   R   RÂ   R   R   R   R   R   R   R7   R   Rk   Ry   R¡   R2   RJ   R‹   RK   RH   RG   t   updatet   abst   True(&   R   RÁ   R%   RM   R[   t   symbol_dictR$   R"   t   outputt	   convergedt   last_logprobRÄ   R0   t   epsilont   A_numert   B_numert   A_denomt   B_denomRG   RC   RR   RŒ   RL   t   lpkt   local_A_numert   local_B_numert   local_A_denomt   local_B_denomRP   t   xt   xnextt   xiR_   R`   Ra   R{   Rb   t   ok(    (   R   R1   s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR8   R  sÄ    "  
 
  5&
)
%3  % %'   1$

c         K   su  | i  d ƒ } | t j o d „  } n t ƒ  } t ƒ  } t ƒ  } xÇ | D]¿ } t } x° | D]¨ }	 |	 t }
 |	 t } | t j o | i |
 ƒ n | | i |
 ƒ | |
 i | ƒ |
 } |
 |  i j o |  i i |
 ƒ n | |  i	 j o |  i	 i | ƒ q^ q^ WqK Wt
 |  i ƒ } | | | ƒ } t | | | ƒ } t | | t
 |  i	 ƒ ƒ } t |  i	 |  i | | | ƒ S(   sA  
        Supervised training maximising the joint probability of the symbol and
        state sequences. This is done via collecting frequencies of
        transitions between states, symbol observations while within each
        state and which states start a sentence. These frequency distributions
        are then normalised into probability estimates, which can be
        smoothed if desired.

        @return: the trained model
        @rtype: HiddenMarkovModelTagger
        @param labelled_sequences: the training data, a set of
            labelled sequences of observations
        @type labelled_sequences: list
        @param kwargs: may include an 'estimator' parameter, a function taking
            a C{FreqDist} and a number of bins and returning a C{ProbDistI};
            otherwise a MLE estimate is used
        R'   c         S   s
   t  |  ƒ S(    (   R   (   t   fdistR)   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR*   ú  s    (   R   R   R   R   RF   RH   t   incR   Re   R   RE   R   R   (   R   RÀ   R%   R'   t   startingR"   R#   RC   t   lastsR±   RQ   RW   RM   t   pit   ARr   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR5   ä  s6    			  

(   R»   R¼   R½   R   R&   RA   R8   R5   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR4     s
   	’t    HiddenMarkovModelTaggerTransformc           B   s   e  Z d  Z d „  Z RS(   sG   
    An abstract subclass of C{HiddenMarkovModelTaggerTransformI}.
    c         C   s!   |  i  t j o t d ‚ n d  S(   Ns&   Abstract classes can't be instantiated(   t	   __class__Rá   R¿   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR&      s    (   R»   R¼   R½   R&   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyRá     s   R   c           B   s    e  Z d  Z d „  Z d „  Z RS(   sš   
    A subclass of C{HiddenMarkovModelTaggerTransform} that is backed by an
    arbitrary user-defined function, instance method, or lambda function.
    c         C   s   | |  _  d S(   sj   
        @param func: a user-defined or lambda transform function
        @type func: C{function}
        N(   R   (   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR&   *  s    c         C   s   |  i  | ƒ S(   N(   R   (   R   t   labeled_symbols(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR   1  s    (   R»   R¼   R½   R&   R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR   %  s   	R   c           B   s   e  Z d  Z d „  Z RS(   s½   
    A subclass of C{HiddenMarkovModelTaggerTransform} that implements 
    L{transform()} as the identity function, i.e. symbols passed to 
    C{transform()} are returned unmodified.
    c         C   s   | S(   N(    (   R   Rã   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR   ;  s    (   R»   R¼   R½   R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR   5  s   c          G   s\   t  |  ƒ } | t j o; d } x  |  D] } | d | | 7} q& W| t | ƒ Sn | Sd S(   sJ   
    Adds the logged values, returning the logarithm of the addition.
    i    i   N(   t   maxR¡   R¥   (   t   valuesR×   t	   sum_diffst   value(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyRK   ?  s     c             s3  Hd GHHd d d g }  d d d g } d „  ‰  ‡  f d	 †  } t  d
 d d g d d d g d d d g g t ƒ } | | | | ƒ } t  d d d g d d
 d g d d d g g t ƒ } | | | |  ƒ } t  d d d g t ƒ } ˆ  | | ƒ } t d |  d | d | d | d | ƒ } d G| GHxd d g d d d g d g d d g d d g g D]Ö } g  } | D] }	 | |	 d  f qf~ }
 d G| GHd G| i |
 ƒ GHd G| i g  } |
 D] \ } } | | q²~ ƒ GHd G| i |
 ƒ GHd G| i |
 ƒ GHd G| i |
 ƒ GHd G| i |
 ƒ GHd G| i	 |
 ƒ GHHqUWd  S(    Ns    HMM probability calculation demot   upt   downt	   unchangedt   bullt   beart   staticc         S   s:   h  } x' t  |  | ƒ D] \ } } | | | <q Wt | ƒ S(   N(   RU   R   (   Rå   R‡   t   dRç   t   item(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   pdW  s
     c            sC   h  } x0 t  |  | ƒ D] \ } } ˆ  | | ƒ | | <q Wt | ƒ S(   N(   RU   R   (   t   arrayt
   conditionsR‡   Rî   Rå   t	   condition(   Rð   (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   cpd]  s
     g333333ã?gš™™™™™É?g      à?g333333Ó?gš™™™™™Ù?gš™™™™™¹?gffffffæ?R    R!   R"   R#   R$   t   Testingi   s   Testing with state sequences   probability =s   tagging =    s   p(tagged) =  s   H =          s   H_exh =      s   H(point) =   s   H_exh(point)=(
   Rñ   Ry   R   R   RD   R.   RŽ   R    R˜   R¤   (   R    R!   Rô   Rà   Rr   Rß   R1   R6   R™   RP   RC   R²   R-   R.   (    (   Rð   s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   demoL  s:    	33	 '	3c         C   sü   d d k  l } | i d d ƒ |   } t i d ƒ } t ƒ  } t ƒ  } g  } x“ | D]‹ } xu t t | ƒ ƒ D]a } | | \ }	 }
 |	 i ƒ  }	 | i	 |	 ƒ | i
 |
 ƒ i ƒ  }
 | i	 |
 ƒ |	 |
 f | | <qm W| | g 7} qT W| t | ƒ t | ƒ f S(   Niÿÿÿÿ(   t   brownt
   categoriest   newss   [*]|--|[^+*-]+(   t   nltk.corpusR÷   t   tagged_sentst   ret   compileR3   RI   RE   t   lowert   addt   matcht   groupR2   (   t	   num_sentsR÷   t	   sentencest   tag_reR=   R    t   cleaned_sentencest   sentenceR_   R-   R.   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   load_pos~  s$    		  s,   Use model.test(sentences, **kwargs) instead.c         C   s   |  i  | d | ƒS(   NR/   (   R6   (   R1   R  t   display(    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   test_pos•  s    c          C   sl   Hd GHHd GHt  d ƒ \ }  } } t | | ƒ } | i |  d d d „  ƒ} d GH| i |  d  d t ƒd  S(	   Ns   HMM POS tagging demos   Training HMM...iÈ   i
   R'   c         S   s   t  |  d  | ƒ S(   gš™™™™™¹?(   R   (   R(   R)   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR*   ¤  s    s
   Testing...R/   (   R  R4   R5   R6   RÈ   (   RÀ   R=   R    R>   R?   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   demo_pos™  s    c         C   s2   g  } x% |  D] } | i  d „  | Dƒ ƒ q W| S(   Nc         s   s#   x |  ] } | t  d  f Vq Wd  S(   N(   RH   R   (   R+   R±   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pys	   <genexpr>¬  s    (   Re   (   R  t	   unlabeledR  (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   _untag©  s
     c       
   C   sß   Hd GHHd GHt  d ƒ \ }  } } t ƒ  } x0 |  D]( } x | D] } | i | t ƒ q> Wq1 Wt | t | ƒ ƒ } | i |  d d !d d „  ƒ} d GHt |  d d !ƒ } | i | d	 | d
 d ƒ} | i	 |  d  d t
 ƒd  S(   Ns   Baum-Welch demo for POS taggings   Training HMM (supervised)...iÒ   i
   iÈ   R'   c         S   s   t  |  d  | ƒ S(   gš™™™™™¹?(   R   (   R(   R)   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyR*   ¿  s    s   Training (unsupervised)...R1   R0   i   R/   (   R  R3   Rÿ   RH   R4   R2   R5   R  R8   R6   RÈ   (   R  R=   R    R  R±   R>   R?   R  (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   demo_pos_bw¯  s$    	  c             s¡  Hd GHHd d d g }  d d d g } d „  ‰  ‡  f d	 †  } t  d
 d d g d d d g d d d g g t ƒ } | | | | ƒ } t  d d d g d d
 d g d d d g g t ƒ } | | | |  ƒ } t  d d d g t ƒ } ˆ  | | ƒ } t d |  d | d | d | d | ƒ } g  } d d  k } | i ƒ  }	 x= t d ƒ D]/ }
 | i |	 d ƒ } | i d „  | Dƒ ƒ q@Wt | |  ƒ } | i	 | d | d d ƒ} d  S(   Ns"   Baum-Welch demo for market exampleRè   Ré   Rê   Rë   Rì   Rí   c         S   s:   h  } x' t  |  | ƒ D] \ } } | | | <q Wt | ƒ S(   N(   RU   R   (   Rå   R‡   Rî   Rç   Rï   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyRð   Ò  s
     c            sC   h  } x0 t  |  | ƒ D] \ } } ˆ  | | ƒ | | <q Wt | ƒ S(   N(   RU   R   (   Rñ   Rò   R‡   Rî   Rå   Ró   (   Rð   (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyRô   Ø  s
     g333333ã?gš™™™™™É?g      à?g333333Ó?gš™™™™™Ù?gš™™™™™¹?gffffffæ?R    R!   R"   R#   R$   iÿÿÿÿi
   i   c         s   s#   x |  ] } | d  d f Vq Wd S(   i    N(   R   (   R+   R_   (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pys	   <genexpr>î  s    R1   R0   iè  (
   Rñ   Ry   R   R   t   RandomRI   Rƒ   Re   R4   R8   (   R    R!   Rô   Rà   Rr   Rß   R1   t   trainingR   R€   R_   Rï   R>   R?   (    (   Rð   s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pyt   demo_bwÆ  s2    	33 t   __main__(-   R½   Rü   R   t   numpyt   nltk.probabilityR   R   R   R   R   R   R   R   t   nltk.internalsR	   t   nltk.metricsR
   R¬   t	   nltk.utilR   R   R   t   apit   floatR¡   RH   RF   t   TaggerIR   t   objectR4   R   Rá   R   R   RK   Rö   R  R7   R	  R
  R  R  R  R»   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/tag/hmm.pys   <module>D   s>   
:
ÿ ÿ Èý	
		2						/