
    Ng                        d Z 	 ddlZn# e$ r Y nw xY wddlZddl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dlmZ dd	lmZ dd
lmZ dZ G d de          ZeZ G d d          Z G d de          Z G d de          Z  G d de           Z! G d de           Z" G d de          Z#	 d*dZ$d Z%d Z&	 d*dZ'd Z(d  Z)	 d+d!Z* G d" d#e          Z+d$ Z,d,d&Z-d' Z.d( Z/e0d)k    r e/             dS dS )-a  
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.
    N)defaultdict)ClassifierI)
call_megamparse_megam_weightswrite_megam_file)	call_tadmparse_tadm_weightswrite_tadm_file)CutoffCheckeraccuracylog_likelihood)gzip_open_unicode)DictionaryProbDist)OrderedDictz
epytext enc                       e Zd ZdZddZd Zd Zd Zd Zd Z	dd
Z
ddZddZd Zg dZe	 	 	 	 	 dd            ZdS )MaxentClassifiera  
    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))
    Tc                 ~    || _         || _        || _        |                                t	          |          k    sJ dS )a{  
        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)	_encoding_weights_logarithmiclengthlen)selfencodingweightslogarithmics       P/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/classify/maxent.py__init__zMaxentClassifier.__init__a   sA    " "'  CLL000000    c                 4    | j                                         S N)r   labelsr   s    r   r"   zMaxentClassifier.labelsx   s    ~$$&&&r   c                 l    || _         | j                                        t          |          k    sJ dS )z
        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   new_weightss     r   set_weightszMaxentClassifier.set_weights{   s8     $~$$&&#k*:*:::::::r   c                     | j         S )zg
        :return: The feature weight vector for this classifier.
        :rtype: list of float
        r   r#   s    r   r   zMaxentClassifier.weights   s    
 }r   c                 P    |                      |                                          S r!   )prob_classifymax)r   
featuresets     r   classifyzMaxentClassifier.classify   s"    !!*--11333r   c                 :   i }| j                                         D ]i}| j                             ||          }| j        r#d}|D ]\  }}|| j        |         |z  z  }|||<   Gd}|D ]\  }}|| j        |         |z  z  }|||<   jt          || j        d          S )Ng              ?T)log	normalize)r   r"   encoder   r   r   )	r   r,   	prob_dictlabelfeature_vectortotalf_idf_valprods	            r   r*   zMaxentClassifier.prob_classify   s    	^**,, 	( 	(E!^22:uEEN  
(#1 9 9KD%T]40588EE#(	%   #1 9 9KD%DM$/588DD#'	%   ")1BdSSSSr      c           	      <    d}dt          |dz
            z   dz   }                     |          t                                          j        d          }|d|         }t          d                    |          d	                    d
 |D                       z              t          dd|dz
  dt          |          z  z   z  z              t          t                    t          |          D ]\  }} j                            ||          }|                     fdd           |D ]\  }	}
 j        r j        |	         |
z  }n j        |	         |
z  } j                            |	          }|                    d          d         }|d|
z  z  }t          |          dk    r|dd         dz   }t          |||dz  dz  |fz             |xx         |z  cc<   t          dd|dz
  dt          |          z  z   z  z              t          d                    |          d	                    fd|D                       z              t          d                    |          d	                    fd|D                       z              dS )z
        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.
        2   z  %-   zs%s%8.3fTkeyreverseNz	  Feature c              3   6   K   | ]}d d|z  dd         z  V  dS )z%8s%sN    ).0ls     r   	<genexpr>z+MaxentClassifier.explain.<locals>.<genexpr>   s3      ??1eq"1"~.??????r   z  -   c                 D    t          j        | d                            S )Nr   absr   )fid__r   s    r   <lambda>z*MaxentClassifier.explain.<locals>.<lambda>   s    #dmE!H&=">"> r    and label is r   z (%s)/   ,   z...    z  TOTAL:c              3   .   K   | ]}d |         z  V  dS z%8.3fNrE   )rF   rG   sumss     r   rH   z+MaxentClassifier.explain.<locals>.<genexpr>   s,      3V3V!Gd1g4E3V3V3V3V3V3Vr   z  PROBS:c              3   H   K   | ]}d                      |          z  V  dS rV   )prob)rF   rG   pdists     r   rH   z+MaxentClassifier.explain.<locals>.<genexpr>   s2      >>!g

1->>>>>>r   )strr*   sortedsamplesrY   printljustjoinr   r   int	enumerater   r2   sortr   r   describesplit)r   r,   columnsdescr_widthTEMPLATEr"   ir4   r5   r7   r8   scoredescrrZ   rW   s   `            @@r   explainzMaxentClassifier.explain   s    Ca000:="":..UZFFF!k**gg???????@	
 	
 	
 	dSK!Oa#f++o=>>???3!&)) 	% 	%HAu!^22:uEEN>>>>       . % %e$ 9 M$/%7EE M$/58E//55$455a85(u::??!#2#J.Eh%Qe!<<===Uu$% 	dSK!Oa#f++o=>>???[))BGG3V3V3V3Vv3V3V3V,V,VV	
 	
 	
 	[))gg>>>>v>>>>>?	
 	
 	
 	
 	
r   
   c           	           t           d          r j        d|         S t          t          t	          t           j                                       fdd           _         j        d|         S )zW
        Generates the ranked list of informative features from most to least.
        _most_informative_featuresNc                 8    t          j        |                    S r!   rL   )fidr   s    r   rO   z<MaxentClassifier.most_informative_features.<locals>.<lambda>   s    DM#$6 7 7 r   Tr>   )hasattrro   r\   listranger   r   )r   ns   ` r   most_informative_featuresz*MaxentClassifier.most_informative_features   s     4566 	722A266.4U3t}--..//7777/ / /D+
 22A266r   allc                                           d          }|dk    r fd|D             }n|dk    r fd|D             }|d|         D ]:}t           j        |         dd j                            |                      ;dS )z
        :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
        Nposc                 6    g | ]}j         |         d k    |S r   r(   rF   rq   r   s     r   
<listcomp>zCMaxentClassifier.show_most_informative_features.<locals>.<listcomp>   *    BBBC4=+=+A+AC+A+A+Ar   negc                 6    g | ]}j         |         d k     |S r{   r(   r|   s     r   r}   zCMaxentClassifier.show_most_informative_features.<locals>.<listcomp>   r~   r   z8.3frS   )rv   r^   r   r   rd   )r   ru   showfidsrq   s   `    r   show_most_informative_featuresz/MaxentClassifier.show_most_informative_features   s     --d335==BBBB4BBBDDU]]BBBB4BBBD8 	O 	OCT]3'MMMt~/F/Fs/K/KMMNNNN	O 	Or   c                     dt          | j                                                  | j                                        fz  S )Nz:<ConditionalExponentialClassifier: %d labels, %d features>)r   r   r"   r   r#   s    r   __repr__zMaxentClassifier.__repr__   s?    K%%''((N!!##O
 
 	
r   )GISIISMEGAMTADMN   r   c                 n   |d}|D ]}|dvrt          d|z            |                                }|dk    rt          ||||fi |S |dk    rt          ||||fi |S |dk    rt	          |||||fi |S |dk    r(|}	||	d<   ||	d	<   ||	d
<   ||	d<   t          j        |fi |	S t          d|z            )a	  
        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``.
        Niis)	max_itermin_llmin_lldeltamax_accmin_accdeltacount_cutoffnormexplicit	bernoullizUnexpected keyword arg %rgismegamtadmtracer   r"   gaussian_prior_sigmazUnknown algorithm %s)	TypeErrorlower train_maxent_classifier_with_iis train_maxent_classifier_with_gis"train_maxent_classifier_with_megamTadmMaxentClassifiertrain
ValueError)
cls
train_toks	algorithmr   r   r"   r   cutoffsr?   kwargss
             r   r   zMaxentClassifier.train   sf   | I 	C 	CC 
 
 
   ;c ABBB
 OO%%	3E8V 7>   %3E8V 7>   '!!5E8V5I MT   &  F#F7O!)F:%F8-AF)*'-jCCFCCC3i?@@@r   )T)r:   )rm   )rm   rw   )Nr   NNr   )__name__
__module____qualname____doc__r   r"   r&   r   r-   r*   rl   rv   r   r   
ALGORITHMSclassmethodr   rE   r   r   r   r   N   s        $1 1 1 1.' ' '; ; ;  4 4 4T T T(*
 *
 *
 *
X7 7 7 7O O O O 
 
 
 100J aA aA aA [aA aA aAr   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )MaxentFeatureEncodingIa  
    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                     t                      )aC  
        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))
        NotImplementedErrorr   r,   r4   s      r   r2   zMaxentFeatureEncodingI.encode{       "###r   c                     t                      )z
        :return: The size of the fixed-length joint-feature vectors
            that are generated by this encoding.
        :rtype: int
        r   r#   s    r   r   zMaxentFeatureEncodingI.length       "###r   c                     t                      )z
        :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
        r   r#   s    r   r"   zMaxentFeatureEncodingI.labels  s     "###r   c                     t                      )z
        :return: A string describing the value of the joint-feature
            whose index in the generated feature vectors is ``fid``.
        :rtype: str
        r   r   rq   s     r   rd   zMaxentFeatureEncodingI.describe  r   r   c                     t                      )ao  
        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.
        r   )r   r   s     r   r   zMaxentFeatureEncodingI.train  r   r   N)	r   r   r   r   r2   r   r"   rd   r   rE   r   r   r   r   d  si         ,
$ 
$ 
$$ $ $$ $ $$ $ $
$ 
$ 
$ 
$ 
$r   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )#FunctionBackedMaxentFeatureEncodingz
    A feature encoding that calls a user-supplied function to map a
    given featureset/label pair to a sparse joint-feature vector.
    c                 0    || _         || _        || _        dS )ag  
        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)_length_func_labels)r   funcr   r"   s       r   r   z,FunctionBackedMaxentFeatureEncoding.__init__  s    0 
r   c                 .    |                      ||          S r!   )r   r   s      r   r2   z*FunctionBackedMaxentFeatureEncoding.encode  s    zz*e,,,r   c                     | j         S r!   r   r#   s    r   r   z*FunctionBackedMaxentFeatureEncoding.length  
    |r   c                     | j         S r!   r   r#   s    r   r"   z*FunctionBackedMaxentFeatureEncoding.labels  r   r   c                     dS )Nzno description availablerE   r   s     r   rd   z,FunctionBackedMaxentFeatureEncoding.describe  s    ))r   N)	r   r   r   r   r   r2   r   r"   rd   rE   r   r   r   r     si         
  8- - -    * * * * *r   r   c                   J    e Zd ZdZddZd Zd Zd Zd Ze	dd
            Z
d	S )BinaryMaxentFeatureEncodinga  
    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.
    Fc                 d    t          |                                          t          t          t          |                              k    rt	          d          t          |           _        	 | _        	 t          |           _        	 d _	        	 d _
        	 |rB fdt          |          D              _	         xj        t           j	                  z  c_        |rKd |D             } fdt          |          D              _
         xj        t          |          z  c_        dS dS )a  
        :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.
        HMapping values must be exactly the set of integers from 0...len(mapping)Nc                 ,    i | ]\  }}||j         z   S rE   r   rF   ri   r4   r   s      r   
<dictcomp>z8BinaryMaxentFeatureEncoding.__init__.<locals>.<dictcomp>,  3       ,6Qq4<'  r   c                     h | ]\  }}}|	S rE   rE   rF   fnamefvalr4   s       r   	<setcomp>z7BinaryMaxentFeatureEncoding.__init__.<locals>.<setcomp>2      @@@ 4ee@@@r   c                 ,    i | ]\  }}||j         z   S rE   r   rF   ri   r   r   s      r   r   z8BinaryMaxentFeatureEncoding.__init__.<locals>.<dictcomp>3  &    XXX
EE1t|#3XXXr   setvaluesrt   r   r   rs   r   _mappingr   	_alwayson_unseenrb   r   r"   mappingunseen_featuresalwayson_featuresfnamess   `     r   r   z$BinaryMaxentFeatureEncoding.__init__  H   " w~~  Cc'll(;(;$<$<<<8  
 F||(97||<,, 	0   :CF:K:K  DN LLC///LL 	(@@@@@FXXXXiPVFWFWXXXDLLLCKK'LLLL	( 	(r   c                    g }|                                 D ]\  }}|||f| j        v r&|                    | j        |||f         df           7| j        rC| j        D ]}|||f| j        v r n,|| j        v r"|                    | j        |         df           | j        r+|| j        v r"|                    | j        |         df           |S NrT   )itemsr   appendr   r   r   r   r,   r4   r   r   r   label2s          r   r2   z"BinaryMaxentFeatureEncoding.encode6  s    &++-- 	B 	BKE4tU#t}44udE/A!BA FGGGG  B"l B BFtV,== > ,, e)<a(@AAA > 	8et~55OOT^E2A6777r   c                    t          |t                    st          d          	 | j         nV# t          $ rI dgt          | j                  z  | _        | j                                        D ]\  }}|| j        |<   Y nw xY w|t          | j                  k     r| j        |         \  }}}| d|d|S | j        rI|| j        	                                v r.| j                                        D ]\  }}||k    rd|z  c S d S | j
        rI|| j
        	                                v r.| j
                                        D ]\  }}||k    rd|z  c S d S t          d          Nzdescribe() expected an intz==rP   zlabel is %rz%s is unseenzBad feature id
isinstancera   r   _inv_mappingAttributeErrorr   r   r   r   r   r   r   r   r7   infori   r   r   r4   f_id2s           r   rd   z$BinaryMaxentFeatureEncoding.describeQ     $$$ 	:8999	, 	, 	, 	,!#s4='9'9 9D=..00 , ,a'+!!$$, ,	,
 #dm$$$$#'#4T#: UD%>>t>>U>>>^ 		/(=(=(?(? ? ? $ 4 4 6 6 1 1u5==(50000 !1 1 \ 	/ddl&9&9&;&;;; $ 2 2 4 4 2 2u5==)E1111 !2 2 -...   . AB Bc                     | j         S r!   r   r#   s    r   r"   z"BinaryMaxentFeatureEncoding.labelsj  
    |r   c                     | j         S r!   r   r#   s    r   r   z"BinaryMaxentFeatureEncoding.lengthn  r   r   r   Nc                 |   i }t                      }t          t                    }|D ]\  }}	|r|	|vrt          d|	z            |                    |	           |                                D ]A\  }
}||
|fxx         dz  cc<   ||
|f         |k    r|
||	f|vrt          |          ||
||	f<   B||} | ||fi |S )a  
        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``.
        Unexpected label %srT   )r   r   ra   r   addr   r   r   r   r   r"   optionsr   seen_labelscounttokr4   r   r   s               r   r   z!BinaryMaxentFeatureEncoding.trainr  s   8 eeC  $ 	C 	CJC @%v-- !6!>???OOE"""  #yy{{ C Ct eTk"""a'"""%55tU+7::69'lltU 23C > Fs67..g...r   FFr   Nr   r   r   r   r   r2   rd   r"   r   r   r   rE   r   r   r   r     s        & &P/( /( /( /(b  6/ / /2     0/ 0/ 0/ [0/ 0/ 0/r   r   c                   D    e Zd ZdZ	 d	dZed             Zd Zd Zd Z	dS )
GISEncodinga  
    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.
    FNc                     t                               | ||||           |t          d |D                       dz   }|| _        dS )a	  
        :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__``
        Nc                     h | ]\  }}}|	S rE   rE   r   s       r   r   z'GISEncoding.__init__.<locals>.<setcomp>  s    ???3tUU???r   rT   )r   r   r   _C)r   r"   r   r   r   Cs         r   r   zGISEncoding.__init__  s[     	$,,&'?4E	
 	
 	
 9??w???@@1DAr   c                     | j         S )zOThe non-negative constant that all encoded feature vectors
        will sum to.)r
  r#   s    r   r  zGISEncoding.C  s     wr   c                    t                               | ||          }t                               |           }t          d |D                       }|| j        k    rt          d          |                    || j        |z
  f           |S )Nc              3       K   | ]	\  }}|V  
d S r!   rE   )rF   fvs      r   rH   z%GISEncoding.encode.<locals>.<genexpr>  s&      --&1aA------r   z&Correction feature is not high enough!)r   r2   r   sumr
  r   r   )r   r,   r4   r   base_lengthr6   s         r   r2   zGISEncoding.encode  s    .55dJNN188>> --H-----DGEFFFdgo6777 r   c                 <    t                               |           dz   S r   )r   r   r#   s    r   r   zGISEncoding.length  s    *11$77!;;r   c                     |t                               |           k    r
d| j        z  S t                               | |          S )NzCorrection feature (%s))r   r   r
  rd   )r   r7   s     r   rd   zGISEncoding.describe  s?    .55d;;;;,tw66.77dCCCr   )FFN)
r   r   r   r   r   propertyr  r2   r   rd   rE   r   r   r  r    s          RV       X
  < < <D D D D Dr   r  c                   F    e Zd Zd
dZd Zd Zd Zd Zedd	            Z	dS )TadmEventMaxentFeatureEncodingFc                     t          |          | _        t                      | _        t                              | || j        ||           d S r!   )r   r   _label_mappingr   r   )r   r"   r   r   r   s        r   r   z'TadmEventMaxentFeatureEncoding.__init__  sM    #G,,)mm#,,&$-:K	
 	
 	
 	
 	
r   c                 x   g }|                                 D ]\  }}||f| j        vrt          | j                  | j        ||f<   || j        vr<t	          |t
                    st          | j                  | j        |<   n
|| j        |<   |                    | j        ||f         | j        |         f           |S r!   )r   r   r   r  r   ra   r   )r   r,   r4   r   featurevalues         r   r2   z%TadmEventMaxentFeatureEncoding.encode  s    (..00 
	 
	NGUt}4425dm2D2Dw./D///!%-- 714T5H1I1ID'..16D'.OO/0$2Ee2LM    r   c                     | j         S r!   r   r#   s    r   r"   z%TadmEventMaxentFeatureEncoding.labels  r   r   c                 R    | j         D ]\  }}| j         ||f         |k    r||fc S d S r!   )r   )r   rq   r  r4   s       r   rd   z'TadmEventMaxentFeatureEncoding.describe  sN    "m 	( 	(NGU}gu-.#55'''' 6	( 	(r   c                 *    t          | j                  S r!   )r   r   r#   s    r   r   z%TadmEventMaxentFeatureEncoding.length  s    4=!!!r   r   Nc                     t                      }|sg }t          |          }|D ]\  }}||vr|                    |           |D ])\  }}|D ]!}|D ]}||f|vrt          |          |||f<   "* | ||fi |S r!   )r   rs   r   r   )	r   r   r   r"   r   r   r,   r4   r  s	            r   r   z$TadmEventMaxentFeatureEncoding.train  s    -- 	F *%%
!+ 	% 	%JF""e$$$!+ 	A 	AJ A A) A AG'w6647LL% 01AA
 s67..g...r   r  r  )
r   r   r   r   r2   r"   rd   r   r   r   rE   r   r   r  r    s        
 
 
 
    ( ( (
" " " / / / [/ / /r   r  c                   J    e Zd ZdZddZd Zd Zd Zd Ze	dd
            Z
d	S )TypedMaxentFeatureEncodingaZ  
    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.
    Fc                 d    t          |                                          t          t          t          |                              k    rt	          d          t          |           _        	 | _        	 t          |           _        	 d _	        	 d _
        	 |rB fdt          |          D              _	         xj        t           j	                  z  c_        |rKd |D             } fdt          |          D              _
         xj        t          |          z  c_        dS dS )a  
        :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.
        r   Nc                 ,    i | ]\  }}||j         z   S rE   r   r   s      r   r   z7TypedMaxentFeatureEncoding.__init__.<locals>.<dictcomp>{  r   r   c                     h | ]\  }}}|	S rE   rE   r   s       r   r   z6TypedMaxentFeatureEncoding.__init__.<locals>.<setcomp>  r   r   c                 ,    i | ]\  }}||j         z   S rE   r   r   s      r   r   z7TypedMaxentFeatureEncoding.__init__.<locals>.<dictcomp>  r   r   r   r   s   `     r   r   z#TypedMaxentFeatureEncoding.__init__T  r   r   c                 j   g }|                                 D ]\  }}t          |t          t          f          rL|t	          |          |f| j        v r2|                    | j        |t	          |          |f         |f           m|||f| j        v r&|                    | j        |||f         df           | j        rC| j        D ]}|||f| j        v r n,|| j        v r"|                    | j        |         df           | j	        r+|| j	        v r"|                    | j	        |         df           |S r   )
r   r   ra   floattyper   r   r   r   r   r   s          r   r2   z!TypedMaxentFeatureEncoding.encode  sm    &++-- 	F 	FKE4$e-- F4::u->>OOT]5$t**e3K%Ld$STTT 4'4=88OOT]5$3E%F$JKKKK \ F"&, F F!40DMAA!E B !DL00$OOT\%-@!,DEEE > 	8et~55OOT^E2A6777r   c                    t          |t                    st          d          	 | j         nV# t          $ rI dgt          | j                  z  | _        | j                                        D ]\  }}|| j        |<   Y nw xY w|t          | j                  k     r| j        |         \  }}}| d|d|S | j        rI|| j        	                                v r.| j                                        D ]\  }}||k    rd|z  c S d S | j
        rI|| j
        	                                v r.| j
                                        D ]\  }}||k    rd|z  c S d S t          d          r   r   r   s           r   rd   z#TypedMaxentFeatureEncoding.describe  r   r   c                     | j         S r!   r   r#   s    r   r"   z!TypedMaxentFeatureEncoding.labels  r   r   c                     | j         S r!   r   r#   s    r   r   z!TypedMaxentFeatureEncoding.length  r   r   r   Nc                    i }t                      }t          t                    }|D ]\  }}	|r|	|vrt          d|	z            |                    |	           |                                D ]m\  }
}t          |          t          t          fv rt          |          }||
|fxx         dz  cc<   ||
|f         |k    r|
||	f|vrt          |          ||
||	f<   n||} | ||fi |S )a)  
        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``.
        r   rT   )	r   r   ra   r   r   r   r)  r(  r   r   s               r   r   z TypedMaxentFeatureEncoding.train  s8   > eeC  $ 	C 	CJC @%v-- !6!>???OOE"""  #yy{{ 	C 	Ct::#u--::D eTk"""a'"""%55tU+7::69'lltU 23	C > Fs67..g...r   r  r  r  rE   r   r   r"  r"    s        6 6p/( /( /( /(b  @/ / /2     5/ 5/ 5/ [5/ 5/ 5/r   r"  r   c                    |                     dd           t          |          }|t                              | |          }t	          |d          st          d          d|j        z  }t          | |          }t          t          j
        |dk              d                   }t          j        t          |          d	          }	|D ]}
t          j        |	|
<   t          ||	          }t          j        |          }~|dk    rt!          d
|d         z             |dk    r,t!                       t!          d           t!          d           	 	 |dk    rJ|j        pt%          ||           }|j        pt)          ||           }|j        }t!          d|||fz             t-          || |          }|D ]}
||
xx         dz  cc<   t          j        |          }~|                                }	|	||z
  |z  z  }	|                    |	           |                    ||           rnn # t4          $ r t!          d           Y n  xY w|dk    r7t%          ||           }t)          ||           }t!          d|dd|d           |S )a  
    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.
    r   d   Nr"   r  zJThe GIS algorithm requires an encoding that defines C (e.g., GISEncoding).r/   r   d  ==> Training (%d iterations)r=   -      Iteration    Log Likelihood    Accuracy-      ---------------------------------------T     %9d    %14.5f    %9.3frT   *      Training stopped: keyboard interrupt         Final    14.5f    9.3f)
setdefaultr   r  r   rr   r   r  calculate_empirical_fcountr   numpynonzerozerosr   NINF ConditionalExponentialClassifierlog2r^   llr   accr   itercalculate_estimated_fcountr   r&   checkKeyboardInterrupt)r   r   r   r"   r   cutoffcheckerCinvempirical_fcount
unattestedr   rq   
classifierlog_empirical_fcountrC  rD  iternumestimated_fcountlog_estimated_fcounts                     r   r   r     s    z3'''!'**M $$Z$??8S!! 
-
 
 	
 D 2*hGG U]#3q#899!<==J k#.//55G " "z1(GDDJ !:&677qyy.1DDEEEqyy=>>>=>>> 	qyy"%O
J)O)O#'K8J
+K+K',3wC6HHIII  :J   
 " + + %%%*%%%%#(:.>#?#?   !((**G,/CCtKKG""7+++ "":z:: 5	4  < < <:;;;;;qyyJ
33z:..;2;;;;;;<<< s   CH- -I
I
c                     t          j        |                                d          }| D ]1\  }}|                    ||          D ]\  }}||xx         |z  cc<   2|S Nr1  )r=  r?  r   r2   )r   r   fcountr  r4   indexvals          r   r<  r<  f  su    [**C00F  ! !
U"//#u55 	! 	!JE35MMMS MMMM	! Mr   c                 D   t          j        |                                d          }|D ]u\  }}|                     |          }|                                D ]F}|                    |          }|                    ||          D ]\  }}	||xx         ||	z  z  cc<   Gv|S rS  )r=  r?  r   r*   r]   rY   r2   )
rM  r   r   rT  r  r4   rZ   rY   rq   r   s
             r   rF  rF  p  s    [**C00F  + +
U((--]]__ 	+ 	+E::e$$D%__S%88 + +	Tstd{*+	+
 Mr   c           
          |                     dd           t          |          }|t                              | |          }t	          | |          t          |           z  }t          | |          }t          j        t          ||j
                  d          }t          j        |t          |          df          }	t          t          j        |dk              d                   }
t          j        t          |          d          }|
D ]}t          j        ||<   t!          ||          }|dk    rt#          d	|d         z             |d
k    r,t#                       t#          d           t#          d           	 	 |d
k    rJ|j        pt'          ||           }|j        pt+          ||           }|j        }t#          d|||fz             t/          | ||
||||	|          }|                                }||z  }|                    |           |                    ||           rnn # t6          $ r t#          d           Y n  xY w|d
k    r7t'          ||           }t+          ||           }t#          d|dd|d           |S )a  
    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.
    r   r/  Nr0  )r?   r1  rT   r   r2  r=   r3  r4  Tr5  r6  r7  r8  r9  r:  )r;  r   r   r   r<  r   calculate_nfmapr=  arrayr\   __getitem__reshaper   r>  r?  r@  rA  r^   rC  r   rD  r   rE  calculate_deltasr   r&   rG  rH  )r   r   r   r"   r   rI  empirical_ffreqnfmapnfarraynftransposerL  r   rq   rM  rC  rD  rO  deltass                     r   r   r     s    z3'''!'**M .44Z4OO 1XFFZXO J11Ek&E,=>>>DDG-#g,,):;;K U]?a#788;<<J k#o..44G " "z1(GDDJqyy.1DDEEEqyy=>>>=>>> 	qyy"%O
J)O)O#'K8J
+K+K',3wC6HHIII &	 	F !((**GvG""7+++ "":z:: 5	4  < < <:;;;;;qyyJ
33z:..;2;;;;;;<<< s   B-H1 1IIc                    t                      }| D ]\\  }}|                                D ]B}|                    t          d |                    ||          D                                  C]d t          |          D             S )a  
    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              3       K   | ]	\  }}|V  
d S r!   rE   rF   idrV  s      r   rH   z"calculate_nfmap.<locals>.<genexpr>  s&      KK)2s#KKKKKKr   c                     i | ]\  }}||	S rE   rE   )rF   ri   nfs      r   r   z#calculate_nfmap.<locals>.<dictcomp>  s    222gq"B222r   )r   r"   r   r  r2   rb   )r   r   nfsetr  _r4   s         r   rY  rY    s    * EEE M MQ__&& 	M 	MEIIcKKxsE/J/JKKKKKLLLL	M225!1!12222r   c           	         d}d}	t          j        |                                d          }
t          j        t	          |          |                                fd          }| D ]\  }}|                    |          }|                                D ]g}|                    ||          }t          d |D                       }|D ]3\  }}|||         |fxx         |	                    |          |z  z  cc<   4h|t	          |           z  }t          |	          D ]}t          j        ||
          }d|z  }||z  }t          j        ||z  d          }t          j        ||z  d          }|D ]}||xx         dz  cc<   |
||z
  | z  z  }
t          j        t          ||z
                      t          j        t          |
                    z  }||k     r|
c S |
S )	a
  
    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,  r1  c              3       K   | ]	\  }}|V  
d S r!   rE   re  s      r   rH   z#calculate_deltas.<locals>.<genexpr>T  s&      99Yb#S999999r   r=   r   )axisrT   )r=  onesr   r?  r   r*   r"   r2   r  rY   rt   outerrM   )r   rM  rL  ffreq_empiricalr_  r`  ra  r   NEWTON_CONVERGE
MAX_NEWTONrb  Ar  r4   distr5   rh  rf  rV  rangenumnf_deltaexp_nf_deltanf_exp_nf_deltasum1sum2rq   n_errors                              r   r]  r]    s)   R OJZ))3//F
 	SZZ!2!23S99A  
; 
;
U'',,__&& 	; 	;E%__S%88N99.99999B) ; ;C%)R-   DIIe$4$4s$::    ;	; ZA *%%  ;w//({%4y)222y1,1555  	 	CIIINIIII 	?T)dU22 )C$ 6778859S[[;Q;QQ_$$MMM % Mr   c                 ^   d}d}d|v r|d         }d|v r|d         }|5|                     dd          }t                              | ||d          }n|t          d          	 t	          j        d	
          \  }	}
t          |
d          5 }t          | ||||           ddd           n# 1 swxY w Y   t          j	        |	           n,# t          t          f$ r}t          d|z            |d}~ww xY wg }|g dz  }|r|dgz  }|s|dgz  }|r	d|dz  z  }nd}|dd|z  dgz  }|dk     r|dgz  }d|v r|dd|d         z  gz  }d|v r|ddt          |d                   z  gz  }t          |d          r|dgz  }|d|
gz  }t          |          }	 t          j        |
           n,# t          $ r}t          d |
 d!|            Y d}~nd}~ww xY wt!          ||                                |          }|t%          j        t$          j                  z  }t+          ||          S )"a  
    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``
    Tr   r   Nr   r   )r"   r   z$Specify encoding or labels, not bothznltk-prefixw)r   r   z,Error while creating megam training file: %s)z-nobiasz-repeat10z	-explicitz-fvalsr/   r=   z-lambdaz%.2fz-tuner   z-quietr   z-maxirC   ll_deltaz-dppcostz-multilabel
multiclasszWarning: unable to delete z: )getr   r   r   tempfilemkstempopenr   oscloseOSErrorrM   rr   r   remover^   r   r   r=  rB  er   )r   r   r   r"   r   r   r   r   r   fdtrainfile_name	trainfiler  r   inv_variancestdoutr   s                    r   r   r     sL    HIV*%f;'	  zz.!44.44Vt 5 
 
 
	?@@@T%-W===N.#&& 	)Hi(i   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	Z  T T TG!KLLRSST G++++G !K=  H:  1144	6L0'::GqyyH:VGTF:$6677V 	FD3vj'9#:#::;;x   #M?"n--G  FB
	.!!!! B B B@>@@Q@@AAAAAAAAB "&(//*;*;XFFG uz%'"""G Hg...sT   &(C B/#C /B33C 6B37C C8 C33C8F/ /
G9GGc                   $    e Zd Zed             ZdS )r   c                    |                     dd          }|                     dd          }|                     dd           }|                     dd           }|                     dd          }|                     d	d          }|                     d
          }	|                     d          }
|st                              |||          }t          j        dd          \  }}t          j        d          \  }}t          |d          }t          |||           |                                 g }|                    dg           |                    d|g           |r|                    dd|dz  z  g           |	r|                    dd|	z  g           |
r'|                    ddt          |
          z  g           |                    d|g           |                    d|g           |dk     r|                    dg           n|                    dg           t          |           t          |          5 }t          |          }d d d            n# 1 swxY w Y   t          j        |           t          j        |           |t          j        t          j                  z  } | ||          S )Nr   tao_lmvmr   r   r   r"   r   r   r   r   r   r0  znltk-tadm-events-z.gz)r~  suffixznltk-tadm-weights-r}  r  z-monitorz-methodz-l2z%.6fr=   z-max_itz%dz-fatolz
-events_inz-params_outz2>&1z-summary)r  r  r   r  r  r   r
   r  extendrM   r   r  r	   r  r  r=  rB  r  )r   r   r   r   r   r   r"   sigmar   r   r  trainfile_fdr  weightfile_fdweightfile_namer  r   
weightfiler   s                      r   r   zTadmMaxentClassifier.train  s   JJ{J77	

7A&&::j$//Hd++

1155zz.!44::j))::m,,  	5;;L <  H (0'7&u(
 (
 (
$n *2)9AU)V)V)V&%nc::	
Hi888
|$$$	9-... 	7NNE6E1H#45666 	9NNIth7888 	?NNHfs8}}&<=>>>n56667888199NNF8$$$$NNJ<((('/"" 	5j(44G	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 		.!!!
	/""" 	5:eg&&& s8W%%%s   I""I&)I&N)r   r   r   r   r   rE   r   r   r   r     s-        5& 5& [5& 5& 5&r   r   c                    dd l }ddlm}  |            }t          |  d          5 } |j        t          t          |j        |                    |                                        }d d d            n# 1 swxY w Y   t          |  d          5 }|	                    |          }d d d            n# 1 swxY w Y   t          |  d          5 }|                    |          }d d d            n# 1 swxY w Y   t          |  d          5 }|
                    |          }d d d            n# 1 swxY w Y   ||||fS )Nr   )MaxentDecoder/weights.txt/mapping.tab/labels.txt/alwayson.tab)r=  nltk.tabdatar  r  rZ  rs   mapfloat64txt2listtupkey2dict
tab2ivdict)	tab_dirr=  r  mdecr  wgtmpglabaons	            r   load_maxent_paramsr    sY   LLL******=??D	&&&	'	' F1ek$s5=$--2B2BCCDDEEF F F F F F F F F F F F F F F 
&&&	'	' "1q!!" " " " " " " " " " " " " " " 
%%%	&	& !mmA               
'''	(	( !Aooa  ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! S#sH   AA77A;>A;B66B:=B:C55C9<C9D44D8;D8/tmpc           
      z   ddl m} ddlm} ddlm}  |            } ||          s ||           t          d|            t          | dd          5 }	|	                    |	                    t          t          |                                                                 d d d            n# 1 swxY w Y   t          | dd          5 }	|	                    |                    |                      d d d            n# 1 swxY w Y   t          | d	d          5 }	|	                    |	                    |                      d d d            n# 1 swxY w Y   t          | d
d          5 }	|	                    |                    |                      d d d            d S # 1 swxY w Y   d S )Nr   )mkdir)isdir)MaxentEncoderzSaving Maxent parameters in r  r  r  r  r  )r  r  os.pathr  r  r  r^   r  writelist2txtr  reprtolisttupdict2tab
ivdict2tab)
r  r  r  r  r  r  r  r  mencr  s
             r   save_maxent_paramsr  2  s   ******=??D5>> g	
2
2
2333	&&&	,	, =	4==T3::<<!8!899;<<<= = = = = = = = = = = = = = =	&&&	,	, ,	4##C((*+++, , , , , , , , , , , , , , ,	%%%s	+	+ )q	4==%%'((() ) ) ) ) ) ) ) ) ) ) ) ) ) )	'''	-	- +	4??3'')***+ + + + + + + + + + + + + + + + + +sI   AB33B7:B7*DDD%*EE"E9*F00F47F4c                      ddl m}  ddlm}  | d          }t	          |          \  }}}}t          t          |||          |          } ||          S )Nr   )find)ClassifierBasedPOSTaggerz.taggers/maxent_treebank_pos_tagger_tab/english)r   )rM  )	nltk.datar  nltk.tag.sequentialr  r  r   r   )r  r  r  r  r  r  r  mcs           r   maxent_pos_taggerr  I  s    <<<<<<dCDDG+G44Cc3	#CDDDc
 
B $#r2222r   c                  <    ddl m}   | t          j                  }d S )Nr   )
names_demo)nltk.classify.utilr  r   r   )r  rM  s     r   demor  X  s+    ------,233JJJr   __main__)r   NN)r   NNr   )r  )1r   r=  ImportErrorr  r  collectionsr   nltk.classify.apir   nltk.classify.megamr   r   r   nltk.classify.tadmr   r	   r
   r  r   r   r   r  r   nltk.probabilityr   	nltk.utilr   __docformat__r   rA  r   r   r   r  r  r"  r   r<  rF  r   rY  r]  r   r   r  r  r  r  r   rE   r   r   <module>r     s  , ,Z	LLLL 	 	 	D	 
			  # # # # # # ) ) ) ) ) ) Q Q Q Q Q Q Q Q Q Q M M M M M M M M M M F F F F F F F F F F ' ' ' ' ' ' / / / / / / ! ! ! ! ! !JA JA JA JA JA{ JA JA JA\ $4  F$ F$ F$ F$ F$ F$ F$ F$R,* ,* ,* ,* ,**@ ,* ,* ,*^G/ G/ G/ G/ G/"8 G/ G/ G/T:D :D :D :D :D- :D :D :Dz5/ 5/ 5/ 5/ 5/%@ 5/ 5/ 5/pa/ a/ a/ a/ a/!7 a/ a/ a/T 04_ _ _ _D  
 
 
& 04Y Y Y Yx3 3 38{ { {P KLT/ T/ T/ T/x7& 7& 7& 7& 7&+ 7& 7& 7&~  ,+ + + +.	3 	3 	34 4 4 zDFFFFF s   	 