
    g                        d Z 	 ddlZ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/        yy# e$ r Y w xY w)+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                   x    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y)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))
    c                 j    || _         || _        || _        |j                         t	        |      k(  sJ y)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       I/var/www/openai/venv/lib/python3.12/site-packages/nltk/classify/maxent.py__init__zMaxentClassifier.__init__a   s3    " "' CL000    c                 6    | j                   j                         S N)r   labelsr   s    r   r"   zMaxentClassifier.labelsx   s    ~~$$&&r   c                 b    || _         | j                  j                         t        |      k(  sJ y)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{   s+     $~~$$&#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                 @    | j                  |      j                         S r!   )prob_classifymax)r   
featuresets     r   classifyzMaxentClassifier.classify   s    !!*-1133r   c                 ^   i }| j                   j                         D ]w  }| j                   j                  ||      }| j                  r'd}|D ]  \  }}|| j                  |   |z  z  } |||<   Rd}|D ]  \  }}|| j                  |   |z  z  } |||<   y t        || 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EN  #1KD%T]]40588E $2#(	%  #1KD%DMM$/588D $2#'	%  -  ")1B1BdSSr   c           	      t    d}dt        |dz
        z   dz   } j                  |      t        j                         j                  d      }|d| }t        dj                  |      d	j                  d
 |D              z          t        dd|dz
  dt        |      z  z   z  z          t        t              t        |      D ]  \  }} j                  j                  ||      }|j                   fdd       |D ]  \  }	}
 j                  r j                   |	   |
z  }n j                   |	   |
z  } j                  j#                  |	      }|j%                  d      d   }|d|
z  z  }t        |      dkD  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j                  |      d	j                  fd|D              z          t        dj                  |      d	j                  fd|D              z          y)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   2   K   | ]  }d d|z  dd z    yw)z%8s%sN    ).0ls     r   	<genexpr>z+MaxentClassifier.explain.<locals>.<genexpr>   s      ?1eq"1~.s   z  -   c                 :    t        j                  | d            S )Nr   absr   )fid__r   s    r   <lambda>z*MaxentClassifier.explain.<locals>.<lambda>   s    #dmmE!H&=">r    and label is r   z (%s)/   ,   z...    z  TOTAL:c              3   .   K   | ]  }d |   z    ywz%8.3fNrC   )rD   rE   sumss     r   rF   z+MaxentClassifier.explain.<locals>.<genexpr>   s     3Vv!Gd1g4Evs   z  PROBS:c              3   F   K   | ]  }d j                  |      z    ywrT   )prob)rD   rE   pdists     r   rF   z+MaxentClassifier.explain.<locals>.<genexpr>   s     >v!g

1-vs   !)strr)   sortedsamplesrW   printljustjoinr   r   int	enumerater   r1   sortr   r   describesplit)r   r+   columnsdescr_widthTEMPLATEr"   ir3   r4   r6   r7   scoredescrrX   rU   s   `            @@r   explainzMaxentClassifier.explain   s,    Ca00:="":.UZZF!k*gg???@	
 	dSK!Oa#f+o=>>?3!&)HAu!^^22:uEN>     .e$$ MM$/%7E MM$/58E//5$45a85(u:?!#2J.Eh%Qe!<<=Uu$  . *" 	dSK!Oa#f+o=>>?[)BGG3Vv3V,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                 4    t        j                  |          S r!   rJ   )fidr   s    r   rM   z<MaxentClassifier.most_informative_features.<locals>.<lambda>   s    DMM#$6 7r   Tr<   )hasattrrl   rZ   listranger   r   )r   ns   ` r   most_informative_featuresz*MaxentClassifier.most_informative_features   sa     456222A66.4U3t}}-./7/D+
 222A66r   c                 Z   | j                  d      }|dk(  r#|D cg c]  }| j                  |   dkD  s| }}n'|dk(  r"|D cg c]  }| j                  |   dk  s| }}|d| D ]9  }t        | j                  |   dd| j                  j	                  |              ; yc c}w c c}w )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posr   negz8.3frQ   )rs   r   r\   r   rb   )r   rr   showfidsrn   s        r   show_most_informative_featuresz/MaxentClassifier.show_most_informative_features   s     --d35=#'B4C4==+=+AC4DBU]#'B4C4==+=+AC4DB8CT]]3'-Qt~~/F/Fs/K.LMN  CBs   B#B#B(B(c                     dt        | j                  j                               | j                  j                         fz  S )Nz:<ConditionalExponentialClassifier: %d labels, %d features>)r   r   r"   r   r#   s    r   __repr__zMaxentClassifier.__repr__   s:    K%%'(NN!!#O
 
 	
r   )GISIISMEGAMTADMNc                 T   |d}|D ]  }|dvst        d|z         |j                         }|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``.
        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   s   | IC 
 
   ;c ABB  OO%	3E8V7>  %3E8V7>  '!5E8V5IMT  & F#F7O!)F:%F8-AF)*'--jCFCC3i?@@r   )T)   )
   )r   all)N   NNr   )__name__
__module____qualname____doc__r   r"   r&   r   r,   r)   rj   rs   ry   r{   
ALGORITHMSclassmethodr   rC   r   r   r   r   N   sj    $1.';4T(*
X7O 
 1J aA aAr   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)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+   r3   s      r   r1   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   rn   s     r   rb   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   r1   r   r"   rb   r   rC   r   r   r   r   d  s     ,
$$$$
$r   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)#FunctionBackedMaxentFeatureEncodingz
    A feature encoding that calls a user-supplied function to map a
    given featureset/label pair to a sparse joint-feature vector.
    c                 .    || _         || _        || _        y)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                 &    | j                  ||      S r!   )r   r   s      r   r1   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                      y)Nzno description availablerC   r   s     r   rb   z,FunctionBackedMaxentFeatureEncoding.describe  s    )r   N)	r   r   r   r   r   r1   r   r"   rb   rC   r   r   r   r     s     
8-*r   r   c                   B    e Zd ZdZd	dZd Zd Zd Zd Ze	d
d       Z
y)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.
    c                    t        |j                               t        t        t        |                  k7  rt	        d      t        |      | _        	 || _        	 t        |      | _        	 d| _	        	 d| _
        	 |rYt        |      D ci c]  \  }}||| j                  z    c}}| _	        | xj                  t        | j                        z  c_        |rg|D ch c]  \  }}}|
 }	}}}t        |	      D ci c]  \  }}||| j                  z    c}}| _
        | xj                  t        |	      z  c_        yyc c}}w c c}}}w c c}}w )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)Nsetvaluesrq   r   r   rp   r   _mappingr   	_alwayson_unseenr`   
r   r"   mappingunseen_featuresalwayson_featuresrg   r3   fnamefvalfnamess
             r   r   z$BinaryMaxentFeatureEncoding.__init__  6   " w~~ Cc'l(;$<<8 
 F|(97|<,,:CF:K:KJQq4<<'':KDN LLC//L8?@ 4eeF@FOPVFWXFW
EE1t||#33FWXDLLLCK'L  AX   E E?Ec                    g }|j                         D ]  \  }}|||f| j                  v r$|j                  | j                  |||f   df       ;| j                  sH| j                  D ]  }|||f| j                  v s k || j                  v s{|j                  | j                  |   df        | j
                  r.|| j
                  v r |j                  | j
                  |   df       |S NrR   )itemsr   appendr   r   r   r   r+   r3   r   r   r   label2s          r   r1   z"BinaryMaxentFeatureEncoding.encode6  s     &++-KE4tU#t}}4udE/A!BA FG "llFtV,= +
 , e)<a(@A ." >>et~~5OOT^^E2A67r   c                    t        |t              st        d      	 | j                   |t        | j                        k  r| j                  |   \  }}}| d|d|S | j                  rK|| j                  j                         v r/| j                  j                         D ]  \  }}||k(  sd|z  c S  y | j                  rK|| j                  j                         v r/| j                  j                         D ]  \  }}||k(  sd|z  c S  y t        d      # t        $ rS dgt        | j                        z  | _        | j                  j                         D ]  \  }}|| j                  |<    Y Iw xY wNzdescribe() expected an intz==rN   zlabel is %rz%s is unseenzBad feature id
isinstancer_   r   _inv_mappingAttributeErrorr   r   r   r   r   r   r   r   r6   inforg   r   r   r3   f_id2s           r   rb   z$BinaryMaxentFeatureEncoding.describeQ  [   $$899	, #dmm$$#'#4#4T#: UD%WBthnUI>>^^(=(=(? ? $ 4 4 6u5=(500 !7 \\ddll&9&9&;; $ 2 2 4u5=)E11 !5 -..#  	,!#s4=='9 9D==..0a'+!!!$ 1	,   D AE32E3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   Nc                 H   i }t               }t        t              }|D ]u  \  }}	|r|	|vrt        d|	z        |j	                  |	       |j                         D ]8  \  }
}||
|fxx   dz  cc<   ||
|f   |k\  s |
||	f|vs(t        |      ||
||	f<   : w ||} | ||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 %srR   )r   r   r_   r   addr   r   r   r   r   r"   optionsr   seen_labelscounttokr3   r   r   s               r   r   z!BinaryMaxentFeatureEncoding.trainr  s    8 eC $JC%v- !6!>??OOE"  #yy{t eTk"a'"%5tU+7:69'ltU 23  + % > F67.g..r   FFr   Nr   r   r   r   r   r1   rb   r"   r   r   r   rC   r   r   r   r     s6    &P/(b6/2 0/ 0/r   r   c                   <    e Zd ZdZ	 ddZed        Zd Zd Zd Z	y)	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.
    Nc           
          t         j                  | ||||       |$t        |D ch c]  \  }}}|
 c}}}      dz   }|| _        yc c}}}w )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__``
        NrR   )r   r   r   _C)	r   r"   r   r   r   Cr   r   r3   s	            r   r   zGISEncoding.__init__  sW     	$,,&'?4E	
 9w?w3tUUw?@1DA @s   Ac                     | j                   S )zOThe non-negative constant that all encoded feature vectors
        will sum to.)r   r#   s    r   r   zGISEncoding.C  s     wwr   c                     t         j                  | ||      }t         j                  |       }t        d |D              }|| j                  k\  rt        d      |j                  || j                  |z
  f       |S )Nc              3   &   K   | ]	  \  }}|  y wr!   rC   )rD   fvs      r   rF   z%GISEncoding.encode.<locals>.<genexpr>  s     -H&1aAH   z&Correction feature is not high enough!)r   r1   r   sumr   r   r   )r   r+   r3   r   base_lengthr5   s         r   r1   zGISEncoding.encode  sp    .55dJN188> -H--DGGEFFdggo67 r   c                 2    t         j                  |       dz   S r   )r   r   r#   s    r   r   zGISEncoding.length  s    *11$7!;;r   c                 |    |t         j                  |       k(  rd| j                  z  S t         j                  | |      S )NzCorrection feature (%s))r   r   r   rb   )r   r6   s     r   rb   zGISEncoding.describe  s8    .55d;;,tww66.77dCCr   )FFN)
r   r   r   r   r   propertyr   r1   r   rb   rC   r   r   r   r     s7     RV   
<Dr   r   c                   >    e Zd ZddZd Zd Zd Zd Zed	d       Z	y)
TadmEventMaxentFeatureEncodingc                     t        |      | _        t               | _        t        j	                  | || j                  ||       y r!   )r   r   _label_mappingr   r   )r   r"   r   r   r   s        r   r   z'TadmEventMaxentFeatureEncoding.__init__  s6    #G,)m#,,&$--:K	
r   c                    g }|j                         D ]  \  }}||f| j                  vr$t        | j                        | j                  ||f<   || j                  vrBt	        |t
              s#t        | j                        | j                  |<   n|| j                  |<   |j                  | j                  ||f   | j                  |   f        |S r!   )r   r   r   r  r   r_   r   )r   r+   r3   r   featurevalues         r   r1   z%TadmEventMaxentFeatureEncoding.encode  s    (..0NGUt}}425dmm2Dw./D///!%-14T5H5H1ID''.16D''.OO/0$2E2Ee2LM 1 r   c                     | j                   S r!   r   r#   s    r   r"   z%TadmEventMaxentFeatureEncoding.labels  r   r   c                 `    | j                   D ]  \  }}| j                   ||f   |k(  s||fc S  y r!   )r   )r   rn   r
  r3   s       r   rb   z'TadmEventMaxentFeatureEncoding.describe  s5    "mmNGU}}gu-.#5'' ,r   c                 ,    t        | j                        S r!   )r   r   r#   s    r   r   z%TadmEventMaxentFeatureEncoding.length  s    4==!!r   Nc                     t               }|sg }t        |      }|D ]  \  }}||vs|j                  |        |D ]*  \  }}|D ]   }|D ]  }||f|vs
t        |      |||f<    " ,  | ||fi |S r!   )r   rp   r   r   )	r   r   r   r"   r   r   r+   r3   r
  s	            r   r   z$TadmEventMaxentFeatureEncoding.train  s    -F *%
!+JF"e$ ", ",J)G'w647L% 01  *   ", 67.g..r   r   r   )
r   r   r   r   r1   r"   rb   r   r   r   rC   r   r   r  r    s/    
(
" / /r   r  c                   B    e Zd ZdZd	dZd Zd Zd Zd Ze	d
d       Z
y)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.
    c                    t        |j                               t        t        t        |                  k7  rt	        d      t        |      | _        	 || _        	 t        |      | _        	 d| _	        	 d| _
        	 |rYt        |      D ci c]  \  }}||| j                  z    c}}| _	        | xj                  t        | j                        z  c_        |rg|D ch c]  \  }}}|
 }	}}}t        |	      D ci c]  \  }}||| j                  z    c}}| _
        | xj                  t        |	      z  c_        yyc c}}w c c}}}w c c}}w )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   Nr   r   s
             r   r   z#TypedMaxentFeatureEncoding.__init__T  r   r   c                    g }|j                         D ]  \  }}t        |t        t        f      rH|t	        |      |f| j
                  v s7|j                  | j
                  |t	        |      |f   |f       d|||f| j
                  v r$|j                  | j
                  |||f   df       | j                  s| j                  D ]  }|||f| j
                  v s  || j                  v s|j                  | j                  |   df        | j                  r.|| j                  v r |j                  | j                  |   df       |S r   )
r   r   r_   floattyper   r   r   r   r   r   s          r   r1   z!TypedMaxentFeatureEncoding.encode  s(    &++-KE4$e-4:u->OOT]]5$t*e3K%Ld$ST 4'4==8OOT]]5$3E%F$JK \\"&,,!40DMMA! #/
 !DLL0$OOT\\%-@!,DE' ., >>et~~5OOT^^E2A67r   c                    t        |t              st        d      	 | j                   |t        | j                        k  r| j                  |   \  }}}| d|d|S | j                  rK|| j                  j                         v r/| j                  j                         D ]  \  }}||k(  sd|z  c S  y | j                  rK|| j                  j                         v r/| j                  j                         D ]  \  }}||k(  sd|z  c S  y t        d      # t        $ rS dgt        | j                        z  | _        | j                  j                         D ]  \  }}|| j                  |<    Y Iw xY wr   r   r   s           r   rb   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   Nc                    i }t               }t        t              }|D ]  \  }}	|r|	|vrt        d|	z        |j	                  |	       |j                         D ]Z  \  }
}t        |      t        t        fv rt        |      }||
|fxx   dz  cc<   ||
|f   |k\  sB|
||	f|vsJt        |      ||
||	f<   \  ||} | ||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   rR   )	r   r   r_   r   r   r   r  r  r   r   s               r   r   z TypedMaxentFeatureEncoding.train  s    > eC $JC%v- !6!>??OOE"  #yy{t:#u-:D eTk"a'"%5tU+7:69'ltU 23  + %" > F67.g..r   r   r   r   rC   r   r   r  r    s7    6p/(b@/2 5/ 5/r   r  c                    |j                  dd       t        |      }|t        j                  | |      }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kD  rt!        d	|d   z         |d
kD  r t!                t!        d       t!        d       	 	 |d
kD  rQ|j"                  xs t%        ||       }|j&                  xs t)        ||       }|j*                  }t!        d|||fz         t-        || |      }|D ]  }
||
xx   dz  cc<    t        j                  |      }~|j/                         }	|	||z
  |z  z  }	|j1                  |	       |j3                  ||       rn	 |d
kD  r+t%        ||       }t)        ||       }t!        d|dd|d       |S # t4        $ r t!        d       Y H  xY w)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   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-      ---------------------------------------     %9d    %14.5f    %9.3frR   *      Training stopped: keyboard interrupt         Final    14.5f    9.3f)
setdefaultr   r   r   ro   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   rn   
classifierlog_empirical_fcountr/  r0  iternumestimated_fcountlog_estimated_fcounts                     r   r   r     sq    z3'!'*M $$Z$?8S!-
 	
 D 2*hG U]]#3q#89!<=J kk#./5Gzz 1(GDJ !::&67qy.1DDEqy=>=> qy"%%O
J)O#''K8J
+K',,3wC6HHI  :J 
 " %*% "#(::.>#?   !((*G,/CCtKKG""7+ "":z:5 4  qyJ
3z:."2e*DT
;<   <:;s   $CH% %H=;H=c                     t        j                  |j                         d      }| D ],  \  }}|j                  ||      D ]  \  }}||xx   |z  cc<    . |S Nr  )r)  r+  r   r1   )r   r   fcountr   r3   indexvals          r   r(  r(  f  sU    [[*C0F 
U"//#u5JE35MS M 6 ! Mr   c                 $   t        j                  |j                         d      }|D ]f  \  }}| j                  |      }|j	                         D ]=  }|j                  |      }|j                  ||      D ]  \  }}	||xx   ||	z  z  cc<    ? h |S r?  )r)  r+  r   r)   r[   rW   r1   )
r9  r   r   r@  r   r3   rX   rW   rn   r   s
             r   r2  r2  p  s    [[*C0F 
U((-]]_E::e$D%__S%8	Tstd{* 9 % ! Mr   c           
      z   |j                  dd       t        |      }|t        j                  | |      }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kD  rt#        d|d   z         |d	kD  r t#                t#        d
       t#        d       	 	 |d	kD  rQ|j$                  xs t'        ||       }|j(                  xs t+        ||       }|j,                  }t#        d|||fz         t/        | ||
||||	|      }|j1                         }||z  }|j3                  |       |j5                  ||       rn	 |d	kD  r+t'        ||       }t+        ||       }t#        d|dd|d       |S # t6        $ r t#        d       Y H  xY w)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  r  )r=   r  rR   r   r  r;   r  r   r!  r"  r#  r$  r%  r&  )r'  r   r   r   r(  r   calculate_nfmapr)  arrayrZ   __getitem__reshaper   r*  r+  r,  r-  r\   r/  r   r0  r   r1  calculate_deltasr   r&   r3  r4  )r   r   r   r"   r   r5  empirical_ffreqnfmapnfarraynftransposer8  r   rn   r9  r/  r0  r;  deltass                     r   r   r     sO    z3'!'*M .44Z4O 1XFZXO J1Ekk&E,=,=>DG--#g,):;K U]]?a#78;<J kk#o.4Gzz 1(GDJqy.1DDEqy=>=> qy"%%O
J)O#''K8J
+K',,3wC6HHI &	F !((*GvG""7+ "":z:5 4  qyJ
3z:."2e*DT
;<   <:;s   B#H" "H:8H:c                     t               }| D ]K  \  }}|j                         D ]3  }|j                  t        d |j	                  ||      D                     5 M t        |      D ci c]  \  }}||
 c}}S c c}}w )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   | ]	  \  }}|  y wr!   rC   rD   idrB  s      r   rF   z"calculate_nfmap.<locals>.<genexpr>  s     K/J)2s#/Jr   )r   r"   r   r   r1   r`   )r   r   nfsetr   _r3   rg   nfs           r   rE  rE    su    * EEQ__&EIIcKxsE/JKKL '  "+5!12!1gq"BE!1222s   )A:c           	      n   d}d}	t        j                  |j                         d      }
t        j                  t	        |      |j                         fd      }| D ]}  \  }}|j                  |      }|j                         D ]T  }|j                  ||      }t        d |D              }|D ])  \  }}|||   |fxx   |j                  |      |z  z  cc<   + V  |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  s|
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,  r  c              3   &   K   | ]	  \  }}|  y wr!   rC   rQ  s      r   rF   z#calculate_deltas.<locals>.<genexpr>T  s     9.Yb#S.r   r;   r   )axisrR   )r)  onesr   r+  r   r)   r"   r1   r   rW   rq   outerrK   )r   r9  r8  ffreq_empiricalrK  rL  rM  r   NEWTON_CONVERGE
MAX_NEWTONrN  Ar   r3   distr4   rU  rR  rB  rangenumnf_deltaexp_nf_deltanf_exp_nf_deltasum1sum2rn   n_errors                              r   rI  rI    s   R OJZZ)3/F
 	SZ!23S9A 
U'',__&E%__S%8N9.99B)C%)R- DIIe$4s$::  * ' ! ZA *%;;w/({%4yy)2yy1,15 CINI  	?T)dU22 ))C$ 678599S[;QQ_$M# && Mr   c                    d}d}d|v r|d   }d|v r|d   }|,|j                  dd      }t        j                  | ||d      }n|t        d      	 t	        j
                  d	
      \  }	}
t        |
d      5 }t        | ||||       ddd       t        j                  |	       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                  |
       t!        ||j#                         |      }|t%        j&                  t$        j(                        z  }t+        ||      S # 1 sw Y   xY w# t        t        f$ r}t        d|z        |d}~ww xY w# t        $ r}t        d |
 d!|        Y d}~d}~ww xY w)"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-maxirA   ll_deltaz-dppcostz-multilabel
multiclasszWarning: unable to delete z: )getr   r   r   tempfilemkstempopenr   oscloseOSErrorrK   ro   r   remover\   r   r   r)  r.  er   )r   r   r   r"   r   r   r   r   r   fdtrainfile_name	trainfilerw  r   inv_variancestdoutr   s                    r   r   r     sj    HIV*%f;'	  zz.!4.44Vt 5 
 
	?@@T%--W=N.#&)Hi(i ' 	
 G++GK= H: 1144	6L0'::GqyH:VGTF:$6677V 	FD3vj'9#::;;x M?"n--G FB
		.!
 "&(//*;XFG uzz%''""G Hg..c '&
 Z  TG!KLRSSTD  B*>*:"QC@AABsH   %F! 8F	F! 2G FF! !G0F??G	G+G&&G+c                       e Zd Zed        Zy)r   c                    |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                  |||      }t        j                  dd      \  }}t        j                  d      \  }}t        |d      }t        |||       |j                          g }|j                  dg       |j                  d|g       |r|j                  dd|dz  z  g       |	r|j                  dd|	z  g       |
r|j                  ddt        |
      z  g       |j                  d|g       |j                  d|g       |dk  r|j                  dg       n|j                  dg       t        |       t        |      5 }t        |      }d d d        t        j                  |       t        j                  |       t        j                   t        j"                        z  } | ||      S # 1 sw Y   bxY w)Nr   tao_lmvmr   r   r   r"   r   r   r   r   r   r  znltk-tadm-events-z.gz)ri  suffixznltk-tadm-weights-rh  rj  z-monitorz-methodz-l2z%.6fr;   z-max_itz%dz-fatolz
-events_inz-params_outz2>&1z-summary)ro  r  r   rp  rq  r   r
   rt  extendrK   r   rr  r	   rs  rv  r)  r.  rw  )r   r   r   r   r   r   r"   sigmar   r   rl  trainfile_fdry  weightfile_fdweightfile_namerz  r   
weightfiler   s                      r   r   zTadmMaxentClassifier.train  s   JJ{J7	

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

115zz.!4::j)::m, 5;;L < H (0'7'7&u(
$n *2)9)9AU)V&%nc:	
Hi8
|$	9-.NNE6E1H#456NNIth78NNHfs8}&<=>n567819NNF8$NNJ<('/"j(4G # 			.!
		/" 	5::egg&& 8W%% #"s   &IIN)r   r   r   r   r   rC   r   r   r   r     s    5& 5&r   r   c                 *   dd l }ddlm}  |       }t        |  d      5 } |j                  t        t        |j                  |j                  |                        }d d d        t        |  d      5 }|j                  |      }d d d        t        |  d      5 }|j                  |      }d d d        t        |  d      5 }|j                  |      }d d d        fS # 1 sw Y   xY w# 1 sw Y   kxY w# 1 sw Y   OxY w# 1 sw Y   3xY w)Nr   )MaxentDecoder/weights.txt/mapping.tab/labels.txt/alwayson.tab)r)  nltk.tabdatar  rr  rF  rp   mapfloat64txt2listtupkey2dict
tab2ivdict)	tab_dirr)  r  mdecr   wgtmpglabaons	            r   load_maxent_paramsr    s    *?D		&	'1ekk$s5==$--2BCDE 
( 
	&	'1q! 
( 
	%	&!mmA 
' 
	'	(Aooa  
) S# 
(	' 
(	' 
'	& 
)	(s/   ?C%5C1C=D	%C.1C:=D	Dc           
         ddl m} ddlm} ddlm}  |       } ||      s ||       t        d|        t        | dd      5 }	|	j                  |j                  t        t        | j                                             d d d        t        | dd      5 }	|	j                  |j                  |              d d d        t        | d	d      5 }	|	j                  |j                  |              d d d        t        | d
d      5 }	|	j                  |j                  |              d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   [xY w# 1 sw Y   y xY w)Nr   )mkdir)isdir)MaxentEncoderzSaving Maxent parameters in r  rj  r  r  r  )rs  r  os.pathr  r  r  r\   rr  writelist2txtr  reprtolisttupdict2tab
ivdict2tab)
r  r  r  r  r  r  r  r  mencr   s
             r   save_maxent_paramsr  2  s    *?D>g	(	
23		&	,	4==T3::<!89:< 
-		&	,	4##C()+ 
-		%s	+q	4==%&( 
,		'	-	4??3'(* 
.	- 
-	,	,	,	+	+	-	-s0   >D9"E"E"E9EEEE&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   )r9  )	nltk.datar  nltk.tag.sequentialr  r  r   r   )r  r  r  r  r  r  r  mcs           r   maxent_pos_taggerr  I  sK    <CDG+G4Cc3	#CDc
B $r22r   c                  <    ddl m}   | t        j                        }y )Nr   )
names_demo)nltk.classify.utilr  r   r   )r  r9  s     r   demor  X  s    -,223Jr   __main__)r   NN)r   NNr   )z/tmp)1r   r)  ImportErrorrs  rp  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   r-  r   r   r   r   r  r  r   r(  r2  r   rE  rI  r   r   r  r  r  r  r   rC   r   r   <module>r     s:  ,Z	 
  # ) Q Q M M F F ' / !JA{ JA\ $4  F$ F$R,**@ ,*^G/"8 G/T:D- :Dz5/%@ 5/pa/!7 a/T 04_D
& 04Yx38{P KLT/x7&+ 7&~,+.	34 zF M1  		s   C C%$C%