ó
ù`]c           @  sa  d  Z  d d l m Z m Z y d d l Z Wn e k
 r? n Xd d l Z d d l Z d d l m	 Z	 d d l
 m Z d d l m Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z m Z m Z d d l m Z m Z m Z d d l m Z m  Z  m! Z! d Z" e j# d e f d „  ƒ  Yƒ Z$ e$ 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 e' f d „  ƒ  YZ, d e- e- d „ Z. d „  Z/ d  „  Z0 d e- e- d! „ Z1 d" „  Z2 d# „  Z3 d e- e- d$ d% „ Z4 d& e$ f d' „  ƒ  YZ5 d( „  Z6 e7 d) k r]e6 ƒ  n  d S(*   uä  
A classifier model based on maximum entropy modeling framework.  This
framework considers all of the probability distributions that are
empirically consistent with the training data; and chooses the
distribution with the highest entropy.  A probability distribution is
"empirically consistent" with a set of training data if its estimated
frequency with which a class and a feature vector value co-occur is
equal to the actual frequency in the data.

Terminology: 'feature'
======================
The term *feature* is usually used to refer to some property of an
unlabeled token.  For example, when performing word sense
disambiguation, we might define a ``'prevword'`` feature whose value is
the word preceding the target word.  However, in the context of
maxent modeling, the term *feature* is typically used to refer to a
property of a "labeled" token.  In order to prevent confusion, we
will introduce two distinct terms to disambiguate these two different
concepts:

  - An "input-feature" is a property of an unlabeled token.
  - A "joint-feature" is a property of a labeled token.

In the rest of the ``nltk.classify`` module, the term "features" is
used to refer to what we will call "input-features" in this module.

In literature that describes and discusses maximum entropy models,
input-features are typically called "contexts", and joint-features
are simply referred to as "features".

Converting Input-Features to Joint-Features
-------------------------------------------
In maximum entropy models, joint-features are required to have numeric
values.  Typically, each input-feature ``input_feat`` is mapped to a
set of joint-features of the form:

|   joint_feat(token, label) = { 1 if input_feat(token) == feat_val
|                              {      and label == some_label
|                              {
|                              { 0 otherwise

For all values of ``feat_val`` and ``some_label``.  This mapping is
performed by classes that implement the ``MaxentFeatureEncodingI``
interface.
iÿÿÿÿ(   t   print_functiont   unicode_literalsN(   t   defaultdict(   t   integer_types(   t   compat(   t   gzip_open_unicode(   t   OrderedDict(   t   DictionaryProbDist(   t   ClassifierI(   t   CutoffCheckert   accuracyt   log_likelihood(   t
   call_megamt   write_megam_filet   parse_megam_weights(   t	   call_tadmt   write_tadm_filet   parse_tadm_weightsu
   epytext ent   MaxentClassifierc           B  s§   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d d „ Z
 d	 d
 „ Z d	 d d „ Z d „  Z d d d d g Z e d d d d d d „ ƒ Z RS(   uî  
    A maximum entropy classifier (also known as a "conditional
    exponential classifier").  This classifier is parameterized by a
    set of "weights", which are used to combine the joint-features
    that are generated from a featureset by an "encoding".  In
    particular, the encoding maps each ``(featureset, label)`` pair to
    a vector.  The probability of each label is then computed using
    the following equation::

                                dotprod(weights, encode(fs,label))
      prob(fs|label) = ---------------------------------------------------
                       sum(dotprod(weights, encode(fs,l)) for l in labels)

    Where ``dotprod`` is the dot product::

      dotprod(a,b) = sum(x*y for (x,y) in zip(a,b))
    c         C  s=   | |  _  | |  _ | |  _ | j ƒ  t | ƒ k s9 t ‚ d S(   u{  
        Construct a new maxent classifier model.  Typically, new
        classifier models are created using the ``train()`` method.

        :type encoding: MaxentFeatureEncodingI
        :param encoding: An encoding that is used to convert the
            featuresets that are given to the ``classify`` method into
            joint-feature vectors, which are used by the maxent
            classifier model.

        :type weights: list of float
        :param weights:  The feature weight vector for this classifier.

        :type logarithmic: bool
        :param logarithmic: If false, then use non-logarithmic weights.
        N(   t	   _encodingt   _weightst   _logarithmict   lengtht   lent   AssertionError(   t   selft   encodingt   weightst   logarithmic(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyt   __init__h   s    			c         C  s   |  j  j ƒ  S(   N(   R   t   labels(   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR      s    c         C  s.   | |  _  |  j j ƒ  t | ƒ k s* t ‚ d S(   u¨   
        Set the feature weight vector for this classifier.
        :param new_weights: The new feature weight vector.
        :type new_weights: list of float
        N(   R   R   R   R   R   (   R   t   new_weights(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyt   set_weights‚   s    	c         C  s   |  j  S(   ug   
        :return: The feature weight vector for this classifier.
        :rtype: list of float
        (   R   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   ‹   s    c         C  s   |  j  | ƒ j ƒ  S(   N(   t   prob_classifyt   max(   R   t
   featureset(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyt   classify’   s    c   	      C  sÒ   i  } x° |  j  j ƒ  D]Ÿ } |  j  j | | ƒ } |  j ry d } x) | D]! \ } } | |  j | | 7} qG W| | | <q d } x) | D]! \ } } | |  j | | 9} q† W| | | <q Wt | d |  j d t ƒS(   Ng        g      ð?t   logt	   normalize(   R   R   t   encodeR   R   R   t   True(	   R   R#   t	   prob_dictt   labelt   feature_vectort   totalt   f_idt   f_valt   prod(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR!   •   s    	i   c           sU  d } d t  | d ƒ d } ˆ j | ƒ ‰  t ˆ  j ƒ  d ˆ  j d t ƒ} | |  } t d j | ƒ d j d	 „  | Dƒ ƒ ƒ t d
 d | d d t	 | ƒ ƒ t
 t ƒ ‰ xt | ƒ D]
\ } } ˆ j j | | ƒ } | j d ‡ f d †  d t ƒ xÇ | D]¿ \ }	 }
 ˆ j r/ˆ j |	 |
 } n ˆ j |	 |
 } ˆ j j |	 ƒ } | j d ƒ d } | d |
 7} t	 | ƒ d k r–| d  d } n  t | | | d d | f ƒ ˆ | c | 7<qWq¿ Wt d
 d | d d t	 | ƒ ƒ t d j | ƒ d j ‡ f d †  | Dƒ ƒ ƒ t d j | ƒ d j ‡  f d †  | Dƒ ƒ ƒ d S(   uË   
        Print a table showing the effect of each of the features in
        the given feature set, and how they combine to determine the
        probabilities of each label for that featureset.
        i2   u     %-i   u   s%s%8.3ft   keyt   reverseu	     Featureu    c         s  s!   |  ] } d  d | d  Vq d S(   u   %8su   %si   N(    (   t   .0t   l(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>·   s    u     u   -i   c           s   t  ˆ  j |  d ƒ S(   Ni    (   t   absR   (   t   fid__(   R   (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyt   <lambda>¾   t    u    and label is i    u    (%s)i/   i,   u   ...u    i   u     TOTAL:c         3  s   |  ] } d  ˆ  | Vq d S(   u   %8.3fN(    (   R2   R3   (   t   sums(    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>Î   s    u     PROBS:c         3  s"   |  ] } d  ˆ  j  | ƒ Vq d S(   u   %8.3fN(   t   prob(   R2   R3   (   t   pdist(    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>Ò   s    N(   t   strR!   t   sortedt   samplesR9   R(   t   printt   ljustt   joinR   R   t   intt	   enumerateR   R'   t   sortR   R   t   describet   split(   R   R#   t   columnst   descr_widtht   TEMPLATER   t   iR*   R+   R-   R.   t   scoret   descr(    (   R:   R   R8   s3   lib/python2.7/site-packages/nltk/classify/maxent.pyt   explain©   s>    !
$		$-i
   c           sb   t  ˆ  d ƒ r ˆ  j |  St t t t ˆ  j ƒ ƒ ƒ d ‡  f d †  d t ƒˆ  _ ˆ  j |  Sd S(   uW   
        Generates the ranked list of informative features from most to least.
        u   _most_informative_featuresR0   c           s   t  ˆ  j |  ƒ S(   N(   R4   R   (   t   fid(   R   (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR6   Þ   R7   R1   N(   t   hasattrt   _most_informative_featuresR<   t   listt   rangeR   R   R(   (   R   t   n(    (   R   s3   lib/python2.7/site-packages/nltk/classify/maxent.pyt   most_informative_featuresÕ   s    u   allc         C  sÅ   |  j  d ƒ } | d k rJ g  | D] } |  j | d k r" | ^ q" } n; | d k r… g  | D] } |  j | d k  r] | ^ q] } n  x9 | |  D]- } t d |  j | |  j j | ƒ f ƒ q Wd S(   u«   
        :param show: all, neg, or pos (for negative-only or positive-only)
        :type show: str
        :param n: The no. of top features
        :type n: int
        u   posi    u   negu   %8.3f %sN(   RS   t   NoneR   R>   R   RD   (   R   RR   t   showt   fidsRM   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyt   show_most_informative_featuresã   s    //c         C  s&   d t  |  j j ƒ  ƒ |  j j ƒ  f S(   Nu:   <ConditionalExponentialClassifier: %d labels, %d features>(   R   R   R   R   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyt   __repr__ó   s    u   GISu   IISu   MEGAMu   TADMi   i    c   
      K  s  | d k r d } n  x- | D]% } | d k r t d | ƒ ‚ q q W| j ƒ  } | d k rs t | | | | |  S| d k r• t | | | | |  S| d k rº t | | | | | |  S| d k r| }	 | |	 d <| |	 d <| |	 d <| |	 d <t j | |	  St d | ƒ ‚ d S(   u®	  
        Train a new maxent classifier based on the given corpus of
        training samples.  This classifier will have its weights
        chosen to maximize entropy while remaining empirically
        consistent with the training corpus.

        :rtype: MaxentClassifier
        :return: The new maxent classifier

        :type train_toks: list
        :param train_toks: Training data, represented as a list of
            pairs, the first member of which is a featureset,
            and the second of which is a classification label.

        :type algorithm: str
        :param algorithm: A case-insensitive string, specifying which
            algorithm should be used to train the classifier.  The
            following algorithms are currently available.

            - Iterative Scaling Methods: Generalized Iterative Scaling (``'GIS'``),
              Improved Iterative Scaling (``'IIS'``)
            - External Libraries (requiring megam):
              LM-BFGS algorithm, with training performed by Megam (``'megam'``)

            The default algorithm is ``'IIS'``.

        :type trace: int
        :param trace: The level of diagnostic tracing output to produce.
            Higher values produce more verbose output.
        :type encoding: MaxentFeatureEncodingI
        :param encoding: A feature encoding, used to convert featuresets
            into feature vectors.  If none is specified, then a
            ``BinaryMaxentFeatureEncoding`` will be built based on the
            features that are attested in the training corpus.
        :type labels: list(str)
        :param labels: The set of possible labels.  If none is given, then
            the set of all labels attested in the training data will be
            used instead.
        :param gaussian_prior_sigma: The sigma value for a gaussian
            prior on model weights.  Currently, this is supported by
            ``megam``. For other algorithms, its value is ignored.
        :param cutoffs: Arguments specifying various conditions under
            which the training should be halted.  (Some of the cutoff
            conditions are not supported by some algorithms.)

            - ``max_iter=v``: Terminate after ``v`` iterations.
            - ``min_ll=v``: Terminate after the negative average
              log-likelihood drops under ``v``.
            - ``min_lldelta=v``: Terminate if a single iteration improves
              log likelihood by less than ``v``.
        u   iisu   max_iteru   min_llu   min_lldeltau   max_accu   min_accdeltau   count_cutoffu   normu   explicitu	   bernoulliu   Unexpected keyword arg %ru   gisu   megamu   tadmu   traceu   encodingu   labelsu   gaussian_prior_sigmau   Unknown algorithm %sN(	   u   max_iteru   min_llu   min_lldeltau   max_accu   min_accdeltau   count_cutoffu   normu   explicitu	   bernoulli(	   RT   t	   TypeErrort   lowert    train_maxent_classifier_with_iist    train_maxent_classifier_with_gist"   train_maxent_classifier_with_megamt   TadmMaxentClassifiert   traint
   ValueError(
   t   clst
   train_tokst	   algorithmt   traceR   R   t   gaussian_prior_sigmat   cutoffsR0   t   kwargs(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR_   ý   s@    >	        	



N(   t   __name__t
   __module__t   __doc__R(   R   R   R    R   R$   R!   RL   RS   RW   RX   t
   ALGORITHMSt   classmethodRT   R_   (    (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   T   s$   						,	t   MaxentFeatureEncodingIc           B  s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   u´  
    A mapping that converts a set of input-feature values to a vector
    of joint-feature values, given a label.  This conversion is
    necessary to translate featuresets into a format that can be used
    by maximum entropy models.

    The set of joint-features used by a given encoding is fixed, and
    each index in the generated joint-feature vectors corresponds to a
    single joint-feature.  The length of the generated joint-feature
    vectors is therefore constant (for a given encoding).

    Because the joint-feature vectors generated by
    ``MaxentFeatureEncodingI`` are typically very sparse, they are
    represented as a list of ``(index, value)`` tuples, specifying the
    value of each non-zero joint-feature.

    Feature encodings are generally created using the ``train()``
    method, which generates an appropriate encoding based on the
    input-feature values and labels that are present in a given
    corpus.
    c         C  s   t  ƒ  ‚ d S(   uC  
        Given a (featureset, label) pair, return the corresponding
        vector of joint-feature values.  This vector is represented as
        a list of ``(index, value)`` tuples, specifying the value of
        each non-zero joint-feature.

        :type featureset: dict
        :rtype: list(tuple(int, int))
        N(   t   NotImplementedError(   R   R#   R*   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR'   ‚  s    
c         C  s   t  ƒ  ‚ d S(   u’   
        :return: The size of the fixed-length joint-feature vectors
            that are generated by this encoding.
        :rtype: int
        N(   Rn   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   Ž  s    c         C  s   t  ƒ  ‚ d S(   uÞ   
        :return: A list of the "known labels" -- i.e., all labels
            ``l`` such that ``self.encode(fs,l)`` can be a nonzero
            joint-feature vector for some value of ``fs``.
        :rtype: list
        N(   Rn   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   –  s    c         C  s   t  ƒ  ‚ d S(   u¦   
        :return: A string describing the value of the joint-feature
            whose index in the generated feature vectors is ``fid``.
        :rtype: str
        N(   Rn   (   R   RM   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyRD   Ÿ  s    c         C  s   t  ƒ  ‚ d S(   uo  
        Construct and return new feature encoding, based on a given
        training corpus ``train_toks``.

        :type train_toks: list(tuple(dict, str))
        :param train_toks: Training data, represented as a list of
            pairs, the first member of which is a feature dictionary,
            and the second of which is a classification label.
        N(   Rn   (   Ra   Rb   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR_   §  s    
(   Rh   Ri   Rj   R'   R   R   RD   R_   (    (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyRm   k  s   					t#   FunctionBackedMaxentFeatureEncodingc           B  s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   u‹   
    A feature encoding that calls a user-supplied function to map a
    given featureset/label pair to a sparse joint-feature vector.
    c         C  s   | |  _  | |  _ | |  _ d S(   ug  
        Construct a new feature encoding based on the given function.

        :type func: (callable)
        :param func: A function that takes two arguments, a featureset
             and a label, and returns the sparse joint feature vector
             that encodes them::

                 func(featureset, label) -> feature_vector

             This sparse joint feature vector (``feature_vector``) is a
             list of ``(index,value)`` tuples.

        :type length: int
        :param length: The size of the fixed-length joint-feature
            vectors that are generated by this encoding.

        :type labels: list
        :param labels: A list of the "known labels" for this
            encoding -- i.e., all labels ``l`` such that
            ``self.encode(fs,l)`` can be a nonzero joint-feature vector
            for some value of ``fs``.
        N(   t   _lengtht   _funct   _labels(   R   t   funcR   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   º  s    		c         C  s   |  j  | | ƒ S(   N(   Rq   (   R   R#   R*   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR'   Ö  s    c         C  s   |  j  S(   N(   Rp   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   Ù  s    c         C  s   |  j  S(   N(   Rr   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   Ü  s    c         C  s   d S(   Nu   no description available(    (   R   RM   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyRD   ß  s    (   Rh   Ri   Rj   R   R'   R   R   RD   (    (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyRo   ´  s   				t   BinaryMaxentFeatureEncodingc           B  sV   e  Z d  Z e e d „ Z d „  Z d „  Z d „  Z d „  Z e	 d d d „ ƒ Z RS(	   uø  
    A feature encoding that generates vectors containing a binary
    joint-features of the form:

    |  joint_feat(fs, l) = { 1 if (fs[fname] == fval) and (l == label)
    |                      {
    |                      { 0 otherwise

    Where ``fname`` is the name of an input-feature, ``fval`` is a value
    for that input-feature, and ``label`` is a label.

    Typically, these features are constructed based on a training
    corpus, using the ``train()`` method.  This method will create one
    feature for each combination of ``fname``, ``fval``, and ``label``
    that occurs at least once in the training corpus.

    The ``unseen_features`` parameter can be used to add "unseen-value
    features", which are used whenever an input feature has a value
    that was not encountered in the training corpus.  These features
    have the form:

    |  joint_feat(fs, l) = { 1 if is_unseen(fname, fs[fname])
    |                      {      and l == label
    |                      {
    |                      { 0 otherwise

    Where ``is_unseen(fname, fval)`` is true if the encoding does not
    contain any joint features that are true when ``fs[fname]==fval``.

    The ``alwayson_features`` parameter can be used to add "always-on
    features", which have the form::

    |  joint_feat(fs, l) = { 1 if (l == label)
    |                      {
    |                      { 0 otherwise

    These always-on features allow the maxent model to directly model
    the prior probabilities of each label.
    c           s  t  | j ƒ  ƒ t  t t | ƒ ƒ ƒ k r9 t d ƒ ‚ n  t | ƒ ˆ  _ | ˆ  _ t | ƒ ˆ  _ d ˆ  _
 d ˆ  _ | r¸ t ‡  f d †  t | ƒ Dƒ ƒ ˆ  _
 ˆ  j t ˆ  j
 ƒ 7_ n  | rt  d „  | Dƒ ƒ } t ‡  f d †  t | ƒ Dƒ ƒ ˆ  _ ˆ  j t | ƒ 7_ n  d S(   u»  
        :param labels: A list of the "known labels" for this encoding.

        :param mapping: A dictionary mapping from ``(fname,fval,label)``
            tuples to corresponding joint-feature indexes.  These
            indexes must be the set of integers from 0...len(mapping).
            If ``mapping[fname,fval,label]=id``, then
            ``self.encode(..., fname:fval, ..., label)[id]`` is 1;
            otherwise, it is 0.

        :param unseen_features: If true, then include unseen value
           features in the generated joint-feature vectors.

        :param alwayson_features: If true, then include always-on
           features in the generated joint-feature vectors.
        uH   Mapping values must be exactly the set of integers from 0...len(mapping)c         3  s(   |  ] \ } } | | ˆ  j  f Vq d  S(   N(   Rp   (   R2   RI   R*   (   R   (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>4  s    c         s  s   |  ] \ } } } | Vq d  S(   N(    (   R2   t   fnamet   fvalR*   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>9  s    c         3  s(   |  ] \ } } | | ˆ  j  f Vq d  S(   N(   Rp   (   R2   RI   Ru   (   R   (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>;  s    N(   t   sett   valuesRQ   R   R`   RP   Rr   t   _mappingRp   RT   t	   _alwaysont   _unseent   dictRB   (   R   R   t   mappingt   unseen_featurest   alwayson_featurest   fnames(    (   R   s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR     s"    *			""c         C  s  g  } xÀ | j  ƒ  D]² \ } } | | | f |  j k r] | j |  j | | | f d f ƒ q |  j r x\ |  j D]" } | | | f |  j k rp Pqp qp W| |  j k rÅ | j |  j | d f ƒ qÅ q q W|  j rþ | |  j k rþ | j |  j | d f ƒ n  | S(   Ni   (   t   itemsRy   t   appendR{   Rr   Rz   (   R   R#   R*   R   Ru   Rv   t   label2(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR'   ?  s    &	$c         C  sy  t  | t ƒ s t d ƒ ‚ n  y |  j WnW t k
 r‚ d g t |  j ƒ |  _ x. |  j j ƒ  D] \ } } | |  j | <qb Wn X| t |  j ƒ k  r¿ |  j | \ } } } d | | | f S|  j r| |  j j	 ƒ  k rx• |  j j ƒ  D]  \ } } | | k rí d | Sqí Wna |  j
 ri| |  j
 j	 ƒ  k rix@ |  j
 j ƒ  D]  \ } } | | k rBd | SqBWn t d ƒ ‚ d  S(   Nu   describe() expected an intiÿÿÿÿu   %s==%r and label is %ru   label is %ru   %s is unseenu   Bad feature id(   t
   isinstanceR   RY   t   _inv_mappingt   AttributeErrorR   Ry   R   Rz   Rx   R{   R`   (   R   R-   t   infoRI   Ru   Rv   R*   t   f_id2(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyRD   Z  s(    c         C  s   |  j  S(   N(   Rr   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   s  s    c         C  s   |  j  S(   N(   Rp   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   w  s    i    c         K  s  i  } t  ƒ  } t t ƒ } xÃ | D]» \ } }	 | rS |	 | k rS t d |	 ƒ ‚ n  | j |	 ƒ xz | j ƒ  D]l \ }
 } | |
 | f c d 7<| |
 | f | k rm |
 | |	 f | k rÙ t | ƒ | |
 | |	 f <qÙ qm qm Wq" W| d k rö | } n  |  | | |  S(   uª  
        Construct and return new feature encoding, based on a given
        training corpus ``train_toks``.  See the class description
        ``BinaryMaxentFeatureEncoding`` for a description of the
        joint-features that will be included in this encoding.

        :type train_toks: list(tuple(dict, str))
        :param train_toks: Training data, represented as a list of
            pairs, the first member of which is a feature dictionary,
            and the second of which is a classification label.

        :type count_cutoff: int
        :param count_cutoff: A cutoff value that is used to discard
            rare joint-features.  If a joint-feature's value is 1
            fewer than ``count_cutoff`` times in the training corpus,
            then that joint-feature is not included in the generated
            encoding.

        :type labels: list
        :param labels: A list of labels that should be used by the
            classifier.  If not specified, then the set of labels
            attested in ``train_toks`` will be used.

        :param options: Extra parameters for the constructor, such as
            ``unseen_features`` and ``alwayson_features``.
        u   Unexpected label %si   N(   Rw   R   RA   R`   t   addR   R   RT   (   Ra   Rb   t   count_cutoffR   t   optionsR}   t   seen_labelst   countt   tokR*   Ru   Rv   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR_   {  s    	'	N(   Rh   Ri   Rj   t   FalseR   R'   RD   R   R   Rl   RT   R_   (    (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyRt   ã  s   '3				t   GISEncodingc           B  sJ   e  Z d  Z e e d d „ Z e d „  ƒ Z d „  Z d „  Z	 d „  Z
 RS(   u  
    A binary feature encoding which adds one new joint-feature to the
    joint-features defined by ``BinaryMaxentFeatureEncoding``: a
    correction feature, whose value is chosen to ensure that the
    sparse vector always sums to a constant non-negative number.  This
    new feature is used to ensure two preconditions for the GIS
    training algorithm:

      - At least one feature vector index must be nonzero for every
        token.
      - The feature vector must sum to a constant non-negative number
        for every token.
    c         C  sU   t  j |  | | | | ƒ | d k rH t t d „  | Dƒ ƒ ƒ d } n  | |  _ d S(   u	  
        :param C: The correction constant.  The value of the correction
            feature is based on this value.  In particular, its value is
            ``C - sum([v for (f,v) in encoding])``.
        :seealso: ``BinaryMaxentFeatureEncoding.__init__``
        c         s  s   |  ] \ } } } | Vq d  S(   N(    (   R2   Ru   Rv   R*   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>Ì  s    i   N(   Rt   R   RT   R   Rw   t   _C(   R   R   R}   R~   R   t   C(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   ¿  s
    	#c         C  s   |  j  S(   uO   The non-negative constant that all encoded feature vectors
        will sum to.(   R‘   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR’   Ï  s    c         C  sv   t  j |  | | ƒ } t  j |  ƒ } t d „  | Dƒ ƒ } | |  j k rX t d ƒ ‚ n  | j | |  j | f ƒ | S(   Nc         s  s   |  ] \ } } | Vq d  S(   N(    (   R2   t   ft   v(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>Û  s    u&   Correction feature is not high enough!(   Rt   R'   R   t   sumR‘   R`   R‚   (   R   R#   R*   R   t   base_lengthR,   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR'   Õ  s    c         C  s   t  j |  ƒ d S(   Ni   (   Rt   R   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   ã  s    c         C  s4   | t  j |  ƒ k r  d |  j St  j |  | ƒ Sd  S(   Nu   Correction feature (%s)(   Rt   R   R‘   RD   (   R   R-   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyRD   æ  s    N(   Rh   Ri   Rj   R   RT   R   t   propertyR’   R'   R   RD   (    (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   °  s   		t   TadmEventMaxentFeatureEncodingc           B  sP   e  Z e e d  „ Z d „  Z d „  Z d „  Z d „  Z e d d d „ ƒ Z
 RS(   c         C  s;   t  | ƒ |  _ t  ƒ  |  _ t j |  | |  j | | ƒ d  S(   N(   R   Ry   t   _label_mappingRt   R   (   R   R   R}   R~   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   î  s    c         C  sÉ   g  } x¼ | j  ƒ  D]® \ } } | | f |  j k rS t |  j ƒ |  j | | f <n  | |  j k rš t | t ƒ sŠ t |  j ƒ |  j | <qš | |  j | <n  | j |  j | | f |  j | f ƒ q W| S(   N(   R   Ry   R   R™   R„   RA   R‚   (   R   R#   R*   R   t   featuret   value(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR'   õ  s    %c         C  s   |  j  S(   N(   Rr   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR     s    c         C  sA   x: |  j  D]/ \ } } |  j  | | f | k r
 | | f Sq
 Wd  S(   N(   Ry   (   R   RM   Rš   R*   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyRD     s    c         C  s   t  |  j ƒ S(   N(   R   Ry   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR     s    i    c   	      K  sË   t  ƒ  } | s g  } n  t | ƒ } x0 | D]( \ } } | | k r+ | j | ƒ q+ q+ Wxa | D]Y \ } } xJ | D]B } x9 | D]1 } | | f | k r~ t | ƒ | | | f <q~ q~ Wqq Wq^ W|  | | |  S(   N(   R   RP   R‚   R   (	   Ra   Rb   RŠ   R   R‹   R}   R#   R*   Rš   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR_     s    		%N(   Rh   Ri   R   R   R'   R   RD   R   Rl   RT   R_   (    (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR˜   í  s   				t   TypedMaxentFeatureEncodingc           B  sV   e  Z d  Z e e d „ Z d „  Z d „  Z d „  Z d „  Z e	 d d d „ ƒ Z RS(	   uZ  
    A feature encoding that generates vectors containing integer,
    float and binary joint-features of the form:

    Binary (for string and boolean features):

    |  joint_feat(fs, l) = { 1 if (fs[fname] == fval) and (l == label)
    |                      {
    |                      { 0 otherwise

    Value (for integer and float features):

    |  joint_feat(fs, l) = { fval if     (fs[fname] == type(fval))
    |                      {         and (l == label)
    |                      {
    |                      { not encoded otherwise

    Where ``fname`` is the name of an input-feature, ``fval`` is a value
    for that input-feature, and ``label`` is a label.

    Typically, these features are constructed based on a training
    corpus, using the ``train()`` method.

    For string and boolean features [type(fval) not in (int, float)]
    this method will create one feature for each combination of
    ``fname``, ``fval``, and ``label`` that occurs at least once in the
    training corpus.

    For integer and float features [type(fval) in (int, float)] this
    method will create one feature for each combination of ``fname``
    and ``label`` that occurs at least once in the training corpus.

    For binary features the ``unseen_features`` parameter can be used
    to add "unseen-value features", which are used whenever an input
    feature has a value that was not encountered in the training
    corpus.  These features have the form:

    |  joint_feat(fs, l) = { 1 if is_unseen(fname, fs[fname])
    |                      {      and l == label
    |                      {
    |                      { 0 otherwise

    Where ``is_unseen(fname, fval)`` is true if the encoding does not
    contain any joint features that are true when ``fs[fname]==fval``.

    The ``alwayson_features`` parameter can be used to add "always-on
    features", which have the form:

    |  joint_feat(fs, l) = { 1 if (l == label)
    |                      {
    |                      { 0 otherwise

    These always-on features allow the maxent model to directly model
    the prior probabilities of each label.
    c           s  t  | j ƒ  ƒ t  t t | ƒ ƒ ƒ k r9 t d ƒ ‚ n  t | ƒ ˆ  _ | ˆ  _ t | ƒ ˆ  _ d ˆ  _
 d ˆ  _ | r¸ t ‡  f d †  t | ƒ Dƒ ƒ ˆ  _
 ˆ  j t ˆ  j
 ƒ 7_ n  | rt  d „  | Dƒ ƒ } t ‡  f d †  t | ƒ Dƒ ƒ ˆ  _ ˆ  j t | ƒ 7_ n  d S(   u½  
        :param labels: A list of the "known labels" for this encoding.

        :param mapping: A dictionary mapping from ``(fname,fval,label)``
            tuples to corresponding joint-feature indexes.  These
            indexes must be the set of integers from 0...len(mapping).
            If ``mapping[fname,fval,label]=id``, then
            ``self.encode({..., fname:fval, ...``, label)[id]} is 1;
            otherwise, it is 0.

        :param unseen_features: If true, then include unseen value
           features in the generated joint-feature vectors.

        :param alwayson_features: If true, then include always-on
           features in the generated joint-feature vectors.
        uH   Mapping values must be exactly the set of integers from 0...len(mapping)c         3  s(   |  ] \ } } | | ˆ  j  f Vq d  S(   N(   Rp   (   R2   RI   R*   (   R   (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>†  s    c         s  s   |  ] \ } } } | Vq d  S(   N(    (   R2   Ru   Rv   R*   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>‹  s    c         3  s(   |  ] \ } } | | ˆ  j  f Vq d  S(   N(   Rp   (   R2   RI   Ru   (   R   (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>  s    N(   Rw   Rx   RQ   R   R`   RP   Rr   Ry   Rp   RT   Rz   R{   R|   RB   (   R   R   R}   R~   R   R€   (    (   R   s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   ^  s"    *			""c         C  sd  g  } x"| j  ƒ  D]\ } } t | t t f ƒ r | t | ƒ | f |  j k r'| j |  j | t | ƒ | f | f ƒ q'q | | | f |  j k r¿ | j |  j | | | f d f ƒ q |  j r x\ |  j D]" } | | | f |  j k rÒ PqÒ qÒ W| |  j k r'| j |  j | d f ƒ q'q q W|  j	 r`| |  j	 k r`| j |  j	 | d f ƒ n  | S(   Ni   (
   R   R„   R   t   floatt   typeRy   R‚   R{   Rr   Rz   (   R   R#   R*   R   Ru   Rv   Rƒ   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR'   ‘  s     /&	$c         C  sy  t  | t ƒ s t d ƒ ‚ n  y |  j WnW t k
 r‚ d g t |  j ƒ |  _ x. |  j j ƒ  D] \ } } | |  j | <qb Wn X| t |  j ƒ k  r¿ |  j | \ } } } d | | | f S|  j r| |  j j	 ƒ  k rx• |  j j ƒ  D]  \ } } | | k rí d | Sqí Wna |  j
 ri| |  j
 j	 ƒ  k rix@ |  j
 j ƒ  D]  \ } } | | k rBd | SqBWn t d ƒ ‚ d  S(   Nu   describe() expected an intiÿÿÿÿu   %s==%r and label is %ru   label is %ru   %s is unseenu   Bad feature id(   R„   R   RY   R…   R†   R   Ry   R   Rz   Rx   R{   R`   (   R   R-   R‡   RI   Ru   Rv   R*   Rˆ   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyRD   ±  s(    c         C  s   |  j  S(   N(   Rr   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   Ê  s    c         C  s   |  j  S(   N(   Rp   (   R   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR   Î  s    i    c         K  s-  i  } t  ƒ  } t t ƒ } xê | D]â \ } }	 | rS |	 | k rS t d |	 ƒ ‚ n  | j |	 ƒ x¡ | j ƒ  D]“ \ }
 } t | ƒ t t f k r  t | ƒ } n  | |
 | f c d 7<| |
 | f | k rm |
 | |	 f | k r t | ƒ | |
 | |	 f <q qm qm Wq" W| d k r| } n  |  | | |  S(   u)  
        Construct and return new feature encoding, based on a given
        training corpus ``train_toks``.  See the class description
        ``TypedMaxentFeatureEncoding`` for a description of the
        joint-features that will be included in this encoding.

        Note: recognized feature values types are (int, float), over
        types are interpreted as regular binary features.

        :type train_toks: list(tuple(dict, str))
        :param train_toks: Training data, represented as a list of
            pairs, the first member of which is a feature dictionary,
            and the second of which is a classification label.

        :type count_cutoff: int
        :param count_cutoff: A cutoff value that is used to discard
            rare joint-features.  If a joint-feature's value is 1
            fewer than ``count_cutoff`` times in the training corpus,
            then that joint-feature is not included in the generated
            encoding.

        :type labels: list
        :param labels: A list of labels that should be used by the
            classifier.  If not specified, then the set of labels
            attested in ``train_toks`` will be used.

        :param options: Extra parameters for the constructor, such as
            ``unseen_features`` and ``alwayson_features``.
        u   Unexpected label %si   N(
   Rw   R   RA   R`   R‰   R   Rž   R   R   RT   (   Ra   Rb   RŠ   R   R‹   R}   RŒ   R   RŽ   R*   Ru   Rv   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR_   Ò  s"    	'	N(   Rh   Ri   Rj   R   R   R'   RD   R   R   Rl   RT   R_   (    (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyRœ   %  s   73	 			i   c         K  s  | j  d d ƒ t | ƒ } | d k r@ t j |  d | ƒ} n  t | d ƒ s^ t d ƒ ‚ n  d | j } t |  | ƒ } t	 t
 j | d k ƒ d ƒ } t
 j t | ƒ d ƒ }	 x | D] }
 t
 j |	 |
 <q¸ Wt | |	 ƒ } t
 j | ƒ } ~ | d k rt d	 | d ƒ n  | d
 k r;t ƒ  t d ƒ t d ƒ n  yö xï t r/| d
 k r¦| j pht | |  ƒ } | j p€t | |  ƒ } | j } t d | | | f ƒ n  t | |  | ƒ } x | D] }
 | |
 c d 7<q¿Wt
 j | ƒ } ~ | j ƒ  }	 |	 | | | 7}	 | j |	 ƒ | j | |  ƒ rAPqAqAWWn$ t k
 rNt d ƒ n
 ‚  n X| d
 k r™t | |  ƒ } t | |  ƒ } t d | | f ƒ n  | S(   u†  
    Train a new ``ConditionalExponentialClassifier``, using the given
    training samples, using the Generalized Iterative Scaling
    algorithm.  This ``ConditionalExponentialClassifier`` will encode
    the model that maximizes entropy from all the models that are
    empirically consistent with ``train_toks``.

    :see: ``train_maxent_classifier()`` for parameter descriptions.
    u   max_iterid   R   u   CuJ   The GIS algorithm requires an encoding that defines C (e.g., GISEncoding).g      ð?i    u   du     ==> Training (%d iterations)i   u-         Iteration    Log Likelihood    Accuracyu-         ---------------------------------------u        %9d    %14.5f    %9.3fi   u*         Training stopped: keyboard interruptu!            Final    %14.5f    %9.3fN(   t
   setdefaultR	   RT   R   R_   RN   RY   R’   t   calculate_empirical_fcountRw   t   numpyt   nonzerot   zerosR   t   NINFt    ConditionalExponentialClassifiert   log2R>   R(   t   llR   t   accR
   t   itert   calculate_estimated_fcountR   R    t   checkt   KeyboardInterrupt(   Rb   Rd   R   R   Rf   t   cutoffcheckert   Cinvt   empirical_fcountt
   unattestedR   RM   t
   classifiert   log_empirical_fcountR§   R¨   t   iternumt   estimated_fcountt   log_estimated_fcount(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR\     sb    
		c         C  sf   t  j | j ƒ  d ƒ } xG |  D]? \ } } x0 | j | | ƒ D] \ } } | | c | 7<q> Wq W| S(   Nu   d(   R¡   R£   R   R'   (   Rb   R   t   fcountRŽ   R*   t   indext   val(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR    r  s
    c   
      C  sŸ   t  j | j ƒ  d ƒ } x€ | D]x \ } } |  j | ƒ } xZ | j ƒ  D]L } | j | ƒ } x4 | j | | ƒ D]  \ } }	 | | c | |	 7<qo WqG Wq W| S(   Nu   d(   R¡   R£   R   R!   R=   R9   R'   (
   R±   Rb   R   R¶   RŽ   R*   R:   R9   RM   Rv   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyRª   |  s     c      	   K  sŒ  | j  d d ƒ t | ƒ } | d k r@ t j |  d | ƒ} n  t |  | ƒ t |  ƒ } t |  | ƒ } t j	 t
 | d | j ƒd ƒ } t j | t | ƒ d f ƒ }	 t t j | d k ƒ d ƒ }
 t j t | ƒ d ƒ } x |
 D] } t j | | <qå Wt | | ƒ } | d k r,t d | d ƒ n  | d	 k rVt ƒ  t d
 ƒ t d ƒ n  yÊ xÃ t r| d	 k rÁ| j pƒt | |  ƒ } | j p›t | |  ƒ } | j } t d | | | f ƒ n  t |  | |
 | | | |	 | ƒ } | j ƒ  } | | 7} | j | ƒ | j | |  ƒ r\Pq\q\WWn$ t k
 r=t d ƒ n
 ‚  n X| d	 k rˆt | |  ƒ } t | |  ƒ } t d | | f ƒ n  | S(   u‚  
    Train a new ``ConditionalExponentialClassifier``, using the given
    training samples, using the Improved Iterative Scaling algorithm.
    This ``ConditionalExponentialClassifier`` will encode the model
    that maximizes entropy from all the models that are empirically
    consistent with ``train_toks``.

    :see: ``train_maxent_classifier()`` for parameter descriptions.
    u   max_iterid   R   R0   u   di   i    u     ==> Training (%d iterations)i   u-         Iteration    Log Likelihood    Accuracyu-         ---------------------------------------u        %9d    %14.5f    %9.3fu*         Training stopped: keyboard interruptu!            Final    %14.5f    %9.3fN(   RŸ   R	   RT   Rt   R_   R    R   t   calculate_nfmapR¡   t   arrayR<   t   __getitem__t   reshapeRw   R¢   R£   R¤   R¥   R>   R(   R§   R   R¨   R
   R©   t   calculate_deltasR   R    R«   R¬   (   Rb   Rd   R   R   Rf   R­   t   empirical_ffreqt   nfmapt   nfarrayt   nftransposeR°   R   RM   R±   R§   R¨   R³   t   deltas(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR[   Ž  sb    !
			
c         C  sz   t  ƒ  } xT |  D]L \ } } x= | j ƒ  D]/ } | j t d „  | j | | ƒ Dƒ ƒ ƒ q) Wq Wt d „  t | ƒ Dƒ ƒ S(   uó  
    Construct a map that can be used to compress ``nf`` (which is
    typically sparse).

    *nf(feature_vector)* is the sum of the feature values for
    *feature_vector*.

    This represents the number of features that are active for a
    given labeled text.  This method finds all values of *nf(t)*
    that are attested for at least one token in the given list of
    training tokens; and constructs a dictionary mapping these
    attested values to a continuous range *0...N*.  For example,
    if the only values of *nf()* that were attested were 3, 5, and
    7, then ``_nfmap`` might return the dictionary ``{3:0, 5:1, 7:2}``.

    :return: A map that can be used to compress ``nf`` to a dense
        vector.
    :rtype: dict(int -> int)
    c         s  s   |  ] \ } } | Vq d  S(   N(    (   R2   t   idR¸   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>  s    c         s  s!   |  ] \ } } | | f Vq d  S(   N(    (   R2   RI   t   nf(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>  s    (   Rw   R   R‰   R•   R'   R|   RB   (   Rb   R   t   nfsetRŽ   t   _R*   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR¹   ê  s
    	1c         C  sÞ  d } d }	 t  j | j ƒ  d ƒ }
 t  j t | ƒ | j ƒ  f d ƒ } x  |  D]˜ \ } } | j | ƒ } xz | j ƒ  D]l } | j | | ƒ } t d „  | Dƒ ƒ } x; | D]3 \ } } | | | | f c | j	 | ƒ | 7<q¬ Wqw WqO W| t |  ƒ :} xÜ t
 |	 ƒ D]Î } t  j | |
 ƒ } d | } | | } t  j | | d d ƒ} t  j | | d d ƒ} x | D] } | | c d 7<qmW|
 | | | 8}
 t  j t | | ƒ ƒ t  j t |
 ƒ ƒ } | | k  r|
 SqW|
 S(	   u
  
    Calculate the update values for the classifier weights for
    this iteration of IIS.  These update weights are the value of
    ``delta`` that solves the equation::

      ffreq_empirical[i]
             =
      SUM[fs,l] (classifier.prob_classify(fs).prob(l) *
                 feature_vector(fs,l)[i] *
                 exp(delta[i] * nf(feature_vector(fs,l))))

    Where:
        - *(fs,l)* is a (featureset, label) tuple from ``train_toks``
        - *feature_vector(fs,l)* = ``encoding.encode(fs,l)``
        - *nf(vector)* = ``sum([val for (id,val) in vector])``

    This method uses Newton's method to solve this equation for
    *delta[i]*.  In particular, it starts with a guess of
    ``delta[i]`` = 1; and iteratively updates ``delta`` with:

    | delta[i] -= (ffreq_empirical[i] - sum1[i])/(-sum2[i])

    until convergence, where *sum1* and *sum2* are defined as:

    |    sum1[i](delta) = SUM[fs,l] f[i](fs,l,delta)
    |    sum2[i](delta) = SUM[fs,l] (f[i](fs,l,delta).nf(feature_vector(fs,l)))
    |    f[i](fs,l,delta) = (classifier.prob_classify(fs).prob(l) .
    |                        feature_vector(fs,l)[i] .
    |                        exp(delta[i] . nf(feature_vector(fs,l))))

    Note that *sum1* and *sum2* depend on ``delta``; so they need
    to be re-computed each iteration.

    The variables ``nfmap``, ``nfarray``, and ``nftranspose`` are
    used to generate a dense encoding for *nf(ltext)*.  This
    allows ``_deltas`` to calculate *sum1* and *sum2* using
    matrices, which yields a significant performance improvement.

    :param train_toks: The set of training tokens.
    :type train_toks: list(tuple(dict, str))
    :param classifier: The current classifier.
    :type classifier: ClassifierI
    :param ffreq_empirical: An array containing the empirical
        frequency for each feature.  The *i*\ th element of this
        array is the empirical frequency for feature *i*.
    :type ffreq_empirical: sequence of float
    :param unattested: An array that is 1 for features that are
        not attested in the training data; and 0 for features that
        are attested.  In other words, ``unattested[i]==0`` iff
        ``ffreq_empirical[i]==0``.
    :type unattested: sequence of int
    :param nfmap: A map that can be used to compress ``nf`` to a dense
        vector.
    :type nfmap: dict(int -> int)
    :param nfarray: An array that can be used to uncompress ``nf``
        from a dense vector.
    :type nfarray: array(float)
    :param nftranspose: The transpose of ``nfarray``
    :type nftranspose: array(float)
    gê-™—q=i,  u   dc         s  s   |  ] \ } } | Vq d  S(   N(    (   R2   RÃ   R¸   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pys	   <genexpr>`  s    i   t   axisi    i   (   R¡   t   onesR   R£   R   R!   R   R'   R•   R9   RQ   t   outerR4   (   Rb   R±   R°   t   ffreq_empiricalR¿   RÀ   RÁ   R   t   NEWTON_CONVERGEt
   MAX_NEWTONRÂ   t   ARŽ   R*   t   distR+   RÄ   RÃ   R¸   t   rangenumt   nf_deltat   exp_nf_deltat   nf_exp_nf_deltat   sum1t   sum2RM   t   n_error(    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR½     s2    I$3


,i    c         K  sÄ  t  } t  } d | k r% | d } n  d | k r> | d } n  | d k r} | j d d ƒ } t j |  | d | d t  ƒ} n | d k	 r˜ t d ƒ ‚ n  y] t j d d	 ƒ \ }	 }
 t |
 d
 ƒ # } t	 |  | | d | d | ƒWd QXt
 j |	 ƒ Wn, t t t f k
 r#} t d | ƒ ‚ n Xg  } | d d d g 7} | rS| d g 7} n  | si| d g 7} n  | r€d | d } n d } | d d | d g 7} | d k  r¹| d g 7} n  d | k rà| d d | d g 7} n  d | k r| d d t | d ƒ g 7} n  t | d  ƒ r,| d! g 7} n  | d" |
 g 7} t | ƒ } y t
 j |
 ƒ Wn- t t f k
 rˆ} t d# |
 | f ƒ n Xt | | j ƒ  | ƒ } | t j t j ƒ 9} t | | ƒ S($   u›  
    Train a new ``ConditionalExponentialClassifier``, using the given
    training samples, using the external ``megam`` library.  This
    ``ConditionalExponentialClassifier`` will encode the model that
    maximizes entropy from all the models that are empirically
    consistent with ``train_toks``.

    :see: ``train_maxent_classifier()`` for parameter descriptions.
    :see: ``nltk.classify.megam``
    u   explicitu	   bernoulliu   count_cutoffi    R   R   u$   Specify encoding or labels, not botht   prefixu   nltk-u   wt   explicitt	   bernoulliNu,   Error while creating megam training file: %su   -nobiasu   -repeatu   10u	   -explicitu   -fvalsg      ð?i   u   -lambdau   %.2fu   -tunei   u   -quietu   max_iteru   -maxiu   %su   ll_deltau   -dppu   costu   -multilabelu
   multiclassu    Warning: unable to delete %s: %s(   R(   RT   t   getRt   R_   R`   t   tempfilet   mkstempt   openR   t   ost   closet   OSErrort   IOErrorR4   RN   R   t   removeR>   R   R   R¡   R¦   t   eR   (   Rb   Rd   R   R   Re   Rg   R×   RØ   RŠ   t   fdt   trainfile_namet	   trainfileRâ   R‹   t   inv_variancet   stdoutR   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR]   Œ  s^    !R^   c           B  s   e  Z e d  „  ƒ Z RS(   c         K  s]  | j  d d ƒ } | j  d d ƒ } | j  d d  ƒ } | j  d d  ƒ } | j  d d ƒ } | j  d	 d ƒ } | j  d
 ƒ }	 | j  d ƒ }
 | s« t j | | d | ƒ} n  t j d d d d ƒ \ } } t j d d ƒ \ } } t | d ƒ } t | | | ƒ | j ƒ  g  } | j	 d g ƒ | j	 d | g ƒ | rW| j	 d d | d g ƒ n  |	 rw| j	 d d |	 g ƒ n  |
 r| j	 d d t
 |
 ƒ g ƒ n  | j	 d | g ƒ | j	 d | g ƒ | d k  râ| j	 d g ƒ n | j	 d g ƒ t | ƒ t | d ƒ  } t | ƒ } Wd  QXt j | ƒ t j | ƒ | t j t j ƒ 9} |  | | ƒ S(    Nu	   algorithmu   tao_lmvmu   tracei   u   encodingu   labelsu   gaussian_prior_sigmai    u   count_cutoffu   max_iteru   min_lldeltaR   RÖ   u   nltk-tadm-events-t   suffixu   .gzu   nltk-tadm-weights-u   wu   -monitoru   -methodu   -l2u   %.6fi   u   -max_itu   %du   -fatolu
   -events_inu   -params_outu   2>&1u   -summaryu   r(   RÙ   RT   R˜   R_   RÚ   RÛ   R   R   RÞ   t   extendR4   R   RÜ   R   RÝ   Rá   R¡   R¦   Râ   (   Ra   Rb   Rg   Rc   Rd   R   R   t   sigmaRŠ   t   max_itert   ll_deltat   trainfile_fdRä   t   weightfile_fdt   weightfile_nameRå   R‹   t
   weightfileR   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR_   é  sL    	
 
(   Rh   Ri   Rl   R_   (    (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyR^   è  s   c          C  s#   d d l  m }  |  t j ƒ } d  S(   Niÿÿÿÿ(   t
   names_demo(   t   nltk.classify.utilRñ   R   R_   (   Rñ   R±   (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyt   demo%  s    u   __main__(8   Rj   t
   __future__R    R   R¡   t   ImportErrorRÚ   RÝ   t   collectionsR   t   sixR   t   nltkR   t	   nltk.dataR   t	   nltk.utilR   t   nltk.probabilityR   t   nltk.classify.apiR   Rò   R	   R
   R   t   nltk.classify.megamR   R   R   t   nltk.classify.tadmR   R   R   t   __docformat__t   python_2_unicode_compatibleR   R¥   t   objectRm   Ro   Rt   R   R˜   Rœ   RT   R\   R    Rª   R[   R¹   R½   R]   R^   Ró   Rh   (    (    (    s3   lib/python2.7/site-packages/nltk/classify/maxent.pyt   <module>5   sN   	ÿ I/Í=8ìa	
	[		‡[=	