a
     ˜ëj¯î  ã                   @   s¦  d Z zddlZW n ey"   Y n0 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„ deƒZeZG dd„ dƒZG dd„ deƒZG dd„ deƒZ G dd„ de ƒZ!G dd„ de ƒZ"G dd„ deƒZ#d5dd„Z$dd„ Z%dd „ Z&d6d!d"„Z'd#d$„ Z(d%d&„ Z)d7d'd(„Z*G d)d*„ d*eƒZ+d+d,„ Z,d8d.d/„Z-d0d1„ Z.d2d3„ Z/e0d4k�r¢e/ƒ  dS )9aä  
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                   @   s~   e Zd ZdZd dd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	d!dd„Z
d"dd„Zd#dd„Zdd„ Zg d¢Zed$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                 C   s*   || _ || _|| _| ¡ 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ÚweightsZlogarithmic© r   úS/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/classify/maxent.pyÚ__init__a   s    zMaxentClassifier.__init__c                 C   s
   | j  ¡ S ©N)r   Úlabels©r   r   r   r   r   x   s    zMaxentClassifier.labelsc                 C   s    || _ | 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   Znew_weightsr   r   r   Úset_weights{   s    zMaxentClassifier.set_weightsc                 C   s   | j S )zg
        :return: The feature weight vector for this classifier.
        :rtype: list of float
        ©r   r   r   r   r   r   „   s    zMaxentClassifier.weightsc                 C   s   |   |¡ ¡ S r   )Úprob_classifyÚmax)r   Ú
featuresetr   r   r   Úclassify‹   s    zMaxentClassifier.classifyc           	      C   s’   i }| j  ¡ D ]r}| j  ||¡}| jrTd}|D ]\}}|| j| | 7 }q.|||< qd}|D ]\}}|| j| | 9 }q\|||< qt|| jdd�S )Ng        ç      ð?T)ÚlogÚ	normalize)r   r   Úencoder   r   r   )	r   r#   Z	prob_dictÚlabelÚfeature_vectorÚtotalÚf_idÚf_valÚprodr   r   r   r!   Ž   s    

zMaxentClassifier.prob_classifyé   c                    sÒ  d}dt |d ƒ d }ˆ |¡‰ tˆ  ¡ ˆ jdd�}|d|… }td |¡d	 d
d„ |D ƒ¡ ƒ tdd|d dt|ƒ    ƒ t	t
ƒ‰t|ƒD ]Ì\}}ˆj ||¡}|j‡fdd„dd� |D ]š\}	}
ˆjrâˆj|	 |
 }nˆj|	 |
 }ˆj |	¡}| d¡d }|d|
 7 }t|ƒdk�r4|dd… d }t|||d d |f ƒ ˆ|  |7  < qÄq”tdd|d dt|ƒ    ƒ td |¡d	 ‡fdd„|D ƒ¡ ƒ td |¡d	 ‡ fdd„|D ƒ¡ ƒ 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                 s   s"   | ]}d d| dd…  V  qdS )z%8sú%sNé   r   ©Ú.0Úlr   r   r   Ú	<genexpr>°   ó    z+MaxentClassifier.explain.<locals>.<genexpr>z  ú-é   c                    s   t ˆ j| d  ƒS )Nr   ©Úabsr   )Zfid__r   r   r   Ú<lambda>·   r<   z*MaxentClassifier.explain.<locals>.<lambda>ú and label is r   z (%s)é/   é,   z...ú é   z  TOTAL:c                 3   s   | ]}d ˆ |  V  qdS ©z%8.3fNr   r8   )Úsumsr   r   r;   Ç   r<   z  PROBS:c                 3   s   | ]}d ˆ   |¡ V  qdS rG   )Úprobr8   )Úpdistr   r   r;   Ë   r<   )Ústrr!   ÚsortedÚsamplesrI   ÚprintÚljustÚjoinr   r   ÚintÚ	enumerater   r(   Úsortr   r   ÚdescribeÚsplit)r   r#   ÚcolumnsZdescr_widthÚTEMPLATEr   Úir)   r*   r,   r-   ZscoreÚdescrr   )rJ   r   rH   r   Úexplain¢   sJ    
ÿÿ ÿ  ÿÿÿzMaxentClassifier.explainé
   c                    sP   t ˆ dƒrˆ jd|… S ttttˆ jƒƒƒ‡ fdd„dd�ˆ _ˆ jd|… S dS )zW
        Generates the ranked list of informative features from most to least.
        Ú_most_informative_featuresNc                    s   t ˆ j|  ƒS r   r?   )Úfidr   r   r   rA   ×   r<   z<MaxentClassifier.most_informative_features.<locals>.<lambda>Tr2   )Úhasattrr\   rL   ÚlistÚranger   r   )r   Únr   r   r   Úmost_informative_featuresÎ   s    

ýz*MaxentClassifier.most_informative_featuresÚallc                    sx   ˆ   d¡}|dkr&‡ fdd„|D ƒ}n|dkr@‡ fdd„|D ƒ}|d|… D ]&}tˆ j| d›dˆ j |¡› �ƒ qLd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                    s   g | ]}ˆ j | d kr|‘qS ©r   r    ©r9   r]   r   r   r   Ú
<listcomp>æ   r<   zCMaxentClassifier.show_most_informative_features.<locals>.<listcomp>Únegc                    s   g | ]}ˆ j | d k r|‘qS re   r    rf   r   r   r   rg   è   r<   z8.3frE   )rb   rN   r   r   rT   )r   ra   ÚshowZfidsr]   r   r   r   Úshow_most_informative_featuresÜ   s    
z/MaxentClassifier.show_most_informative_featuresc                 C   s   dt | j ¡ ƒ| j ¡ f S )Nz:<ConditionalExponentialClassifier: %d labels, %d features>)r   r   r   r   r   r   r   r   Ú__repr__ì   s    þzMaxentClassifier.__repr__)ZGISZIISZMEGAMZTADMNé   r   c           
      K   sÜ   |du rd}|D ]}|dvrt d| ƒ‚q| ¡ }|dkrPt||||fi |¤ŽS |dkrnt||||fi |¤ŽS |dkrŽt|||||fi |¤ŽS |dkrÌ|}	||	d< ||	d	< ||	d
< ||	d< tj|fi |	¤ŽS td| ƒ‚dS )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``.
        NZiis)	Úmax_iterZmin_llÚmin_lldeltaZmax_accZmin_accdeltaÚcount_cutoffZnormÚexplicitÚ	bernoullizUnexpected keyword arg %rZgisZmegamZ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Ú	algorithmrr   r   r   rs   Úcutoffsr3   Úkwargsr   r   r   rz   ö   s@    >ÿÿÿÿ
ÿÿzMaxentClassifier.train)T)r/   )r[   )r[   rc   )Nrl   NNr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r$   r!   rZ   rb   rj   rk   Z
ALGORITHMSÚclassmethodrz   r   r   r   r   r   N   s&   
	
,

     ùr   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
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                 C   s
   t ƒ ‚dS )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))
        N©ÚNotImplementedError©r   r#   r)   r   r   r   r(   {  s    
zMaxentFeatureEncodingI.encodec                 C   s
   t ƒ ‚dS )z’
        :return: The size of the fixed-length joint-feature vectors
            that are generated by this encoding.
        :rtype: int
        Nr‡   r   r   r   r   r   ‡  s    zMaxentFeatureEncodingI.lengthc                 C   s
   t ƒ ‚dS )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
        Nr‡   r   r   r   r   r   �  s    zMaxentFeatureEncodingI.labelsc                 C   s
   t ƒ ‚dS )z¦
        :return: A string describing the value of the joint-feature
            whose index in the generated feature vectors is ``fid``.
        :rtype: str
        Nr‡   ©r   r]   r   r   r   rT   ˜  s    zMaxentFeatureEncodingI.describec                 C   s
   t ƒ ‚dS )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.
        Nr‡   )r|   r}   r   r   r   rz      s    
zMaxentFeatureEncodingI.trainN)	r�   r‚   rƒ   r„   r(   r   r   rT   rz   r   r   r   r   r†   d  s   	r†   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
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                 C   s   || _ || _|| _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   r   r   r   r   ³  s    z,FunctionBackedMaxentFeatureEncoding.__init__c                 C   s   |   ||¡S r   )r�   r‰   r   r   r   r(   Ï  s    z*FunctionBackedMaxentFeatureEncoding.encodec                 C   s   | j S r   ©rŒ   r   r   r   r   r   Ò  s    z*FunctionBackedMaxentFeatureEncoding.lengthc                 C   s   | j S r   ©rŽ   r   r   r   r   r   Õ  s    z*FunctionBackedMaxentFeatureEncoding.labelsc                 C   s   dS )Nzno description availabler   rŠ   r   r   r   rT   Ø  s    z,FunctionBackedMaxentFeatureEncoding.describeN)	r�   r‚   rƒ   r„   r   r(   r   r   rT   r   r   r   r   r‹   ­  s   r‹   c                   @   sH   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Ze	d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                    sº   t | ¡ ƒt tt|ƒƒƒkr$tdƒ‚t|ƒˆ _|ˆ _t|ƒˆ _dˆ _	dˆ _
|rz‡ fdd„t|ƒD ƒˆ _	ˆ  jtˆ j	ƒ7  _|r¶dd„ |D ƒ}‡ fdd„t|ƒD ƒˆ _
ˆ  jt|ƒ7  _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                    s   i | ]\}}||ˆ j  “qS r   r�   ©r9   rX   r)   r   r   r   Ú
<dictcomp>,  s   z8BinaryMaxentFeatureEncoding.__init__.<locals>.<dictcomp>c                 S   s   h | ]\}}}|’qS r   r   ©r9   ÚfnameÚfvalr)   r   r   r   Ú	<setcomp>2  r<   z7BinaryMaxentFeatureEncoding.__init__.<locals>.<setcomp>c                    s   i | ]\}}||ˆ j  “qS r   r�   ©r9   rX   r—   r   r   r   r•   3  r<   ©ÚsetÚvaluesr`   r   r{   r_   rŽ   Ú_mappingrŒ   Ú	_alwaysonÚ_unseenrR   ©r   r   ÚmappingÚunseen_featuresÚalwayson_featuresÚfnamesr   r   r   r     s$    ÿ


ÿz$BinaryMaxentFeatureEncoding.__init__c                 C   s®   g }|  ¡ D ]x\}}|||f| jv r@| | j|||f df¡ q| jr| jD ]}|||f| jv rL qqL|| jv r| | j| df¡ q| jrª|| jv rª| | j| df¡ |S ©NrF   )Úitemsrž   Úappendr    rŽ   rŸ   ©r   r#   r)   r   r—   r˜   Úlabel2r   r   r   r(   6  s    

z"BinaryMaxentFeatureEncoding.encodec                 C   s  t |tƒstdƒ‚z
| j W nB ty^   dgt| jƒ | _| j ¡ D ]\}}|| j|< qFY n0 |t| jƒk r’| j| \}}}|› d|›d|›�S | jrÐ|| j 	¡ v rÐ| j ¡ D ]\}}||kr°d|   S q°nJ| j
�r|| j
 	¡ v �r| j
 ¡ D ]\}}||kròd|   S qòntdƒ‚d S ©Nzdescribe() expected an intéÿÿÿÿz==rB   zlabel is %rz%s is unseenzBad feature id©Ú
isinstancerQ   rt   Z_inv_mappingÚAttributeErrorr   rž   r§   rŸ   r�   r    r{   ©r   r,   ÚinforX   r—   r˜   r)   Zf_id2r   r   r   rT   Q  s(    

z$BinaryMaxentFeatureEncoding.describec                 C   s   | j S r   r‘   r   r   r   r   r   j  s    z"BinaryMaxentFeatureEncoding.labelsc                 C   s   | j S r   r�   r   r   r   r   r   n  s    z"BinaryMaxentFeatureEncoding.lengthr   Nc                 K   s¶   i }t ƒ }ttƒ}|D ]€\}}	|r6|	|vr6td|	 ƒ‚| |	¡ | ¡ D ]L\}
}||
|f  d7  < ||
|f |krH|
||	f|vrHt|ƒ||
||	f< qHq|du r¤|}| ||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 %srF   N)rœ   r   rQ   r{   Úaddr§   r   ©r|   r}   ro   r   Úoptionsr¢   Zseen_labelsÚcountÚtokr)   r—   r˜   r   r   r   rz   r  s    
z!BinaryMaxentFeatureEncoding.train)FF)r   N©r�   r‚   rƒ   r„   r   r(   rT   r   r   r…   rz   r   r   r   r   r’   Ü  s   (
1r’   c                   @   s>   e Zd ZdZddd„Zedd„ ƒZdd	„ Zd
d„ Zd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                 C   s:   t  | ||||¡ |du r0tdd„ |D ƒƒd }|| _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                 S   s   h | ]\}}}|’qS r   r   r–   r   r   r   r™   Â  r<   z'GISEncoding.__init__.<locals>.<setcomp>rF   )r’   r   r   Ú_C)r   r   r¢   r£   r¤   ÚCr   r   r   r   µ  s    	
ÿzGISEncoding.__init__c                 C   s   | j S )zOThe non-negative constant that all encoded feature vectors
        will sum to.)rº   r   r   r   r   r»   Å  s    zGISEncoding.Cc                 C   sT   t  | ||¡}t  | ¡}tdd„ |D ƒƒ}|| jkr<tdƒ‚| || j| f¡ |S )Nc                 s   s   | ]\}}|V  qd S r   r   )r9   ÚfÚvr   r   r   r;   Ñ  r<   z%GISEncoding.encode.<locals>.<genexpr>z&Correction feature is not high enough!)r’   r(   r   Úsumrº   r{   r¨   )r   r#   r)   r   Zbase_lengthr+   r   r   r   r(   Ë  s    

zGISEncoding.encodec                 C   s   t  | ¡d S r¦   )r’   r   r   r   r   r   r   Ù  s    zGISEncoding.lengthc                 C   s(   |t  | ¡krd| j S t  | |¡S d S )NzCorrection feature (%s))r’   r   rº   rT   )r   r,   r   r   r   rT   Ü  s    
zGISEncoding.describe)FFN)
r�   r‚   rƒ   r„   r   Úpropertyr»   r(   r   rT   r   r   r   r   r¹   ¦  s    ÿ

r¹   c                   @   sD   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zeddd„ƒZ	dS )ÚTadmEventMaxentFeatureEncodingFc                 C   s*   t |ƒ| _t ƒ | _t | || j||¡ d S r   )r   rž   Ú_label_mappingr’   r   )r   r   r¢   r£   r¤   r   r   r   r   ä  s
    
ÿz'TadmEventMaxentFeatureEncoding.__init__c                 C   sŠ   g }|  ¡ D ]x\}}||f| jvr6t| jƒ| j||f< || jvrft|tƒs\t| jƒ| j|< n
|| j|< | | j||f | j| f¡ q|S r   )r§   rž   r   rÁ   r®   rQ   r¨   )r   r#   r)   r   ÚfeatureÚvaluer   r   r   r(   ë  s    


ÿz%TadmEventMaxentFeatureEncoding.encodec                 C   s   | j S r   r‘   r   r   r   r   r   ú  s    z%TadmEventMaxentFeatureEncoding.labelsc                 C   s2   | j D ]&\}}| j ||f |kr||f  S qd S r   )rž   )r   r]   rÂ   r)   r   r   r   rT   ý  s    z'TadmEventMaxentFeatureEncoding.describec                 C   s
   t | jƒS r   )r   rž   r   r   r   r   r     s    z%TadmEventMaxentFeatureEncoding.lengthr   Nc           	      K   s†   t ƒ }|sg }t|ƒ}|D ]\}}||vr| |¡ q|D ]8\}}|D ]*}|D ] }||f|vrNt|ƒ|||f< qNqFq:| ||fi |¤ŽS r   )r   r_   r¨   r   )	r|   r}   ro   r   rµ   r¢   r#   r)   rÂ   r   r   r   rz     s    z$TadmEventMaxentFeatureEncoding.train)FF)r   N)
r�   r‚   rƒ   r   r(   r   rT   r   r…   rz   r   r   r   r   rÀ   ã  s   
rÀ   c                   @   sH   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Ze	d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                    sº   t | ¡ ƒt tt|ƒƒƒkr$tdƒ‚t|ƒˆ _|ˆ _t|ƒˆ _dˆ _	dˆ _
|rz‡ fdd„t|ƒD ƒˆ _	ˆ  jtˆ j	ƒ7  _|r¶dd„ |D ƒ}‡ fdd„t|ƒD ƒˆ _
ˆ  jt|ƒ7  _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                    s   i | ]\}}||ˆ j  “qS r   r�   r”   r   r   r   r•   {  s   z7TypedMaxentFeatureEncoding.__init__.<locals>.<dictcomp>c                 S   s   h | ]\}}}|’qS r   r   r–   r   r   r   r™   �  r<   z6TypedMaxentFeatureEncoding.__init__.<locals>.<setcomp>c                    s   i | ]\}}||ˆ j  “qS r   r�   rš   r   r   r   r•   ‚  r<   r›   r¡   r   r   r   r   T  s$    ÿ


ÿz#TypedMaxentFeatureEncoding.__init__c                 C   sð   g }|  ¡ D ]º\}}t|ttfƒrV|t|ƒ|f| jv rÆ| | j|t|ƒ|f |f¡ q|||f| jv r‚| | j|||f df¡ q| jr| jD ]}|||f| jv rŽ qqŽ|| jv r| | j| df¡ q| j	rì|| j	v rì| | j	| df¡ |S r¦   )
r§   r®   rQ   ÚfloatÚtyperž   r¨   r    rŽ   rŸ   r©   r   r   r   r(   …  s      

z!TypedMaxentFeatureEncoding.encodec                 C   s  t |tƒstdƒ‚z
| j W nB ty^   dgt| jƒ | _| j ¡ D ]\}}|| j|< qFY n0 |t| jƒk r’| j| \}}}|› d|›d|›�S | jrÐ|| j 	¡ v rÐ| j ¡ D ]\}}||kr°d|   S q°nJ| j
�r|| j
 	¡ v �r| j
 ¡ D ]\}}||kròd|   S qòntdƒ‚d S r«   r­   r°   r   r   r   rT   ¥  s(    

z#TypedMaxentFeatureEncoding.describec                 C   s   | j S r   r‘   r   r   r   r   r   ¾  s    z!TypedMaxentFeatureEncoding.labelsc                 C   s   | j S r   r�   r   r   r   r   r   Â  s    z!TypedMaxentFeatureEncoding.lengthr   Nc                 K   sÎ   i }t ƒ }ttƒ}|D ]˜\}}	|r6|	|vr6td|	 ƒ‚| |	¡ | ¡ D ]d\}
}t|ƒttfv rht|ƒ}||
|f  d7  < ||
|f |krH|
||	f|vrHt|ƒ||
||	f< qHq|du r¼|}| ||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²   rF   N)	rœ   r   rQ   r{   r³   r§   rÆ   rÅ   r   r´   r   r   r   rz   Æ  s"    
z TypedMaxentFeatureEncoding.train)FF)r   Nr¸   r   r   r   r   rÄ     s   8
1 rÄ   rl   c                 K   sê  |  dd¡ t|ƒ}|du r*tj| |d�}t|dƒs<tdƒ‚d|j }t| |ƒ}tt	 
|dk¡d ƒ}t	 t|ƒd	¡}	|D ]}
t	j|	|
< qzt||	ƒ}t	 |¡}~|dkr¸td
|d  ƒ |dkrÖtƒ  tdƒ tdƒ z°|dk�r|jpðt|| ƒ}|j�pt|| ƒ}|j}td|||f ƒ t|| |ƒ}|D ]}
||
  d7  < �q,t	 |¡}~| ¡ }	|	|| | 7 }	| |	¡ | || ¡rØ�q„qØW n( t�y¢   tdƒ Y n   ‚ Y n0 |dk�ræt|| ƒ}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.
    rm   éd   N©r   r»   zJThe GIS algorithm requires an encoding that defines C (e.g., GISEncoding).r%   r   Údú  ==> Training (%d iterations)r1   ú-      Iteration    Log Likelihood    Accuracyú-      ---------------------------------------ú     %9d    %14.5f    %9.3frF   ú*      Training stopped: keyboard interruptú         Final    ú14.5fú    ú9.3f)Ú
setdefaultr
   r¹   rz   r^   rt   r»   Úcalculate_empirical_fcountrœ   ÚnumpyÚnonzeroÚzerosr   ÚNINFÚ ConditionalExponentialClassifierÚlog2rN   Úllr   Úaccr   ÚiterÚcalculate_estimated_fcountr   r   ÚcheckÚKeyboardInterrupt)r}   rr   r   r   r   ÚcutoffcheckerZCinvZempirical_fcountÚ
unattestedr   r]   Ú
classifierZlog_empirical_fcountrÛ   rÜ   ÚiternumZestimated_fcountZlog_estimated_fcountr   r   r   rw     sd    
ÿ




ÿ





rw   c                 C   sH   t  | ¡ d¡}| D ].\}}| ||¡D ]\}}||  |7  < q(q|S ©NrÉ   )rÕ   r×   r   r(   )r}   r   Úfcountr·   r)   ÚindexÚvalr   r   r   rÔ   f  s
    rÔ   c           
      C   sn   t  | ¡ d¡}|D ]T\}}|  |¡}| ¡ D ]8}| |¡}| ||¡D ]\}}	||  ||	 7  < qHq.q|S rå   )rÕ   r×   r   r!   rM   rI   r(   )
rã   r}   r   ræ   r·   r)   rJ   rI   r]   r˜   r   r   r   rÞ   p  s    

rÞ   c              	   K   sÚ  |  dd¡ t|ƒ}|du r*tj| |d�}t| |ƒt| ƒ }t| |ƒ}t t	||j
d�d¡}t |t|ƒdf¡}	tt |dk¡d ƒ}
t t|ƒd¡}|
D ]}tj||< qšt||ƒ}|dkrÌtd	|d  ƒ |d
krêtƒ  tdƒ tdƒ zŒ|d
k�r2|j�pt|| ƒ}|j�pt|| ƒ}|j}td|||f ƒ t| ||
||||	|ƒ}| ¡ }||7 }| |¡ | || ¡rì�qtqìW n( t�y’   tdƒ Y n   ‚ Y n0 |d
k�rÖt|| ƒ}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.
    rm   rÇ   NrÈ   )r3   rÉ   rF   r   rÊ   r1   rË   rÌ   rÍ   rÎ   rÏ   rÐ   rÑ   rÒ   )rÓ   r
   r’   rz   rÔ   r   Úcalculate_nfmaprÕ   ÚarrayrL   Ú__getitem__Zreshaperœ   rÖ   r×   rØ   rÙ   rN   rÛ   r   rÜ   r   rÝ   Úcalculate_deltasr   r   rß   rà   )r}   rr   r   r   r   rá   Zempirical_ffreqÚnfmapÚnfarrayÚnftransposerâ   r   r]   rã   rÛ   rÜ   rä   Údeltasr   r   r   rv   ‚  sb    


ø




rv   c              
   C   sT   t ƒ }| D ]6\}}| ¡ D ]$}| tdd„ | ||¡D ƒƒ¡ qq
d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                 s   s   | ]\}}|V  qd S r   r   ©r9   Úidrè   r   r   r   r;   ö  r<   z"calculate_nfmap.<locals>.<genexpr>c                 S   s   i | ]\}}||“qS r   r   )r9   rX   Únfr   r   r   r•   ÷  r<   z#calculate_nfmap.<locals>.<dictcomp>)rœ   r   r³   r¾   r(   rR   )r}   r   Znfsetr·   Ú_r)   r   r   r   ré   Þ  s
    $ré   c              	   C   s`  d}d}	t  | ¡ d¡}
t  t|ƒ| ¡ fd¡}| D ]n\}}| |¡}| ¡ D ]R}| ||¡}tdd„ |D ƒƒ}|D ]*\}}||| |f  | 	|¡| 7  < qtqNq4|t| ƒ }t
|	ƒD ]¢}t  ||
¡}d| }|| }t j|| dd�}t j|| dd�}|D ]}||  d	7  < �q |
|| |  8 }
t  t|| ƒ¡t  t|
ƒ¡ }||k r¸|
  S q¸|
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                 s   s   | ]\}}|V  qd S r   r   rñ   r   r   r   r;   T  r<   z#calculate_deltas.<locals>.<genexpr>r1   r   )ZaxisrF   )rÕ   Zonesr   r×   r   r!   r   r(   r¾   rI   r`   Úouterr@   )r}   rã   râ   Zffreq_empiricalrí   rî   rï   r   ZNEWTON_CONVERGEZ
MAX_NEWTONrð   ÚAr·   r)   Údistr*   ró   rò   rè   ZrangenumZnf_deltaZexp_nf_deltaZnf_exp_nf_deltaZsum1Zsum2r]   Zn_errorr   r   r   rì   ú  s2    I
(
 
rì   c              
   K   s>  d}d}d|v r|d }d|v r(|d }|du rP|  dd¡}tj| ||dd�}n|dur`tdƒ‚zZtjd	d
�\}	}
t|
dƒ�"}t| ||||d� W d  ƒ n1 s¤0    Y  t 	|	¡ W n6 t
tfyð } ztd| ƒ|‚W Y d}~n
d}~0 0 g }|g d¢7 }|�r|dg7 }|�s"|dg7 }|�r6d|d  }nd}|dd| dg7 }|dk �r`|dg7 }d|v �r~|dd|d  g7 }d|v �r |ddt|d ƒ g7 }t|dƒ�r¶|dg7 }|d|
g7 }t|ƒ}zt |
¡ W n: t
�y } z td |
› d!|› �ƒ W Y d}~n
d}~0 0 t|| ¡ |ƒ}|t tj¡9 }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``
    Trp   rq   Nro   r   )r   r¤   z$Specify encoding or labels, not bothznltk-©ÚprefixÚw)rp   rq   z,Error while creating megam training file: %s)z-nobiasz-repeatÚ10z	-explicitz-fvalsr%   r1   z-lambdaz%.2fz-tunerl   z-quietrm   z-maxir6   Úll_deltaz-dppZcostz-multilabelZ
multiclasszWarning: unable to delete z: )Úgetr’   rz   r{   ÚtempfileÚmkstempÚopenr   ÚosÚcloseÚOSErrorr@   r^   r   ÚremoverN   r   r   rÕ   rÚ   Úer   )r}   rr   r   r   rs   r€   rp   rq   ro   ÚfdÚtrainfile_nameÚ	trainfiler  rµ   Zinv_varianceÚstdoutr   r   r   r   rx   �  sb    ÿ
ÿ$$






*rx   c                   @   s   e Zd Zedd„ ƒZdS )ry   c                 K   sÂ  |  dd¡}|  dd¡}|  dd ¡}|  dd ¡}|  dd¡}|  d	d¡}|  d
¡}	|  d¡}
|sptj|||d�}tjddd�\}}tjdd�\}}t|dƒ}t|||ƒ | ¡  g }| dg¡ | d|g¡ |rè| dd|d  g¡ |	rþ| dd|	 g¡ |
�r| ddt	|
ƒ g¡ | d|g¡ | d|g¡ |dk �rN| dg¡ n| dg¡ t
|ƒ t|ƒ�}t|ƒ}W d   ƒ n1 �sŠ0    Y  t |¡ t |¡ |t tj¡9 }| ||ƒS )Nr~   Ztao_lmvmrr   rl   r   r   rs   r   ro   rm   rn   rÈ   znltk-tadm-events-z.gz)rù   Úsuffixznltk-tadm-weights-rø   rú   z-monitorz-methodz-l2z%.6fr1   z-max_itz%dz-fatolz
-events_inz-params_outz2>&1z-summary)rý   rÀ   rz   rþ   rÿ   r   r	   r  Úextendr@   r   r   r   r  r  rÕ   rÚ   r  )r|   r}   r€   r~   rr   r   r   Úsigmaro   rm   rü   Ztrainfile_fdr  Zweightfile_fdZweightfile_namer  rµ   Z
weightfiler   r   r   r   rz   Þ  sP    

ÿÿ



(

zTadmMaxentClassifier.trainN)r�   r‚   rƒ   r…   rz   r   r   r   r   ry   Ý  s   ry   c           	   	   C   s  dd l }ddlm} |ƒ }t| › d�ƒ�,}| tt|j| |¡ƒƒ¡}W d   ƒ n1 sZ0    Y  t| › d�ƒ�}| 	|¡}W d   ƒ n1 s’0    Y  t| › d�ƒ�}| |¡}W d   ƒ n1 sÊ0    Y  t| › d�ƒ�}| 
|¡}W d   ƒ n1 �s0    Y  ||||fS )Nr   )ÚMaxentDecoderú/weights.txtú/mapping.tabú/labels.txtú/alwayson.tab)rÕ   Únltk.tabdatar  r   rê   r_   ÚmapÚfloat64Ztxt2listZtupkey2dictZ
tab2ivdict)	Útab_dirrÕ   r  Zmdecr¼   ÚwgtÚmpgÚlabÚaonr   r   r   Úload_maxent_params  s    :((*r  ú/tmpc           
   	   C   sb  ddl m} ddlm} ddlm} |ƒ }||ƒs:||ƒ td|› �ƒ t|› d�dƒ�,}	|	 | 	t
t|  ¡ ƒ¡› ¡ W d   ƒ n1 sŠ0    Y  t|› d�dƒ�"}	|	 | |¡› ¡ W d   ƒ n1 sÌ0    Y  t|› d	�dƒ�"}	|	 | 	|¡› ¡ W d   ƒ n1 �s0    Y  t|› d
�dƒ�"}	|	 | |¡› ¡ W d   ƒ n1 �sT0    Y  d S )Nr   )Úmkdir)Úisdir)ÚMaxentEncoderzSaving Maxent parameters in r  rú   r  r  r  )r  r  Úos.pathr  r  r  rN   r   ÚwriteZlist2txtr  ÚreprÚtolistZtupdict2tabZ
ivdict2tab)
r  r  r  r  r  r  r  r  Zmencr¼   r   r   r   Úsave_maxent_params2  s    :02r#  c                  C   sN   ddl m}  ddlm} | dƒ}t|ƒ\}}}}tt|||d�|ƒ}||d�S )Nr   )Úfind)ÚClassifierBasedPOSTaggerz.taggers/maxent_treebank_pos_tagger_tab/english)r¤   )rã   )Ú	nltk.datar$  Znltk.tag.sequentialr%  r  r   r’   )r$  r%  r  r  r  r  r  Zmcr   r   r   Úmaxent_pos_taggerI  s    ÿr'  c                  C   s   ddl m}  | tjƒ}d S )Nr   )Ú
names_demo)Únltk.classify.utilr(  r   rz   )r(  rã   r   r   r   ÚdemoX  s    r*  Ú__main__)rl   NN)rl   NN)rl   NNr   )r  )1r„   rÕ   ÚImportErrorr  rþ   Úcollectionsr   Znltk.classify.apir   Znltk.classify.megamr   r   r   Znltk.classify.tadmr   r   r	   r)  r
   r   r   r&  r   Znltk.probabilityr   Z	nltk.utilr   Z__docformat__r   rÙ   r†   r‹   r’   r¹   rÀ   rÄ   rw   rÔ   rÞ   rv   ré   rì   rx   ry   r  r#  r'  r*  r�   r   r   r   r   Ú<module>	   sZ   -  I/ K=8 k ÿ
b
 ÿ
\ 	 ÿ
\?

