a
     ˜ëjB  ã                   @   sr   d Z ddlZddlmZ zddlZW n ey:   dZY n0 daddd„Zddd„Zdd	d
„Z	dd„ Z
dd„ ZdS )aP  
A set of functions used to interface with the external megam_ maxent
optimization package. Before megam can be used, you should tell NLTK where it
can find the megam binary, using the ``config_megam()`` function. Typical
usage:

    >>> from nltk.classify import megam
    >>> megam.config_megam() # pass path to megam if not found in PATH # doctest: +SKIP
    [Found megam: ...]

Use with MaxentClassifier. Example below, see MaxentClassifier documentation
for details.

    nltk.classify.MaxentClassifier.train(corpus, 'megam')

.. _megam: https://www.umiacs.umd.edu/~hal/megam/index.html
é    N)Úfind_binaryc                 C   s   t d| dgg d¢dd�adS )aA  
    Configure NLTK's interface to the ``megam`` maxent optimization
    package.

    :param bin: The full path to the ``megam`` binary.  If not specified,
        then nltk will search the system for a ``megam`` binary; and if
        one is not found, it will raise a ``LookupError`` exception.
    :type bin: str
    ÚmegamZMEGAM)z	megam.optr   Z	megam_686zmegam_i686.optz0https://www.umiacs.umd.edu/~hal/megam/index.html)Zenv_varsZbinary_namesÚurlN)r   Ú
_megam_bin)Úbin© r   úR/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/classify/megam.pyÚconfig_megam)   s    ûr	   Tc                    s¸   ˆ   ¡ }dd„ t|ƒD ƒ}| D ]”\‰‰tˆ dƒrT| d ‡ ‡‡fdd„|D ƒ¡¡ n| d|ˆ  ¡ |s€tˆ  ˆˆ¡||ƒ n(|D ]"}| d¡ tˆ  ˆ|¡||ƒ q„| d	¡ qd
S )aò  
    Generate an input file for ``megam`` based on the given corpus of
    classified tokens.

    :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 encoding: MaxentFeatureEncodingI
    :param encoding: A feature encoding, used to convert featuresets
        into feature vectors. May optionally implement a cost() method
        in order to assign different costs to different class predictions.

    :type stream: stream
    :param stream: The stream to which the megam input file should be
        written.

    :param bernoulli: If true, then use the 'bernoulli' format.  I.e.,
        all joint features have binary values, and are listed iff they
        are true.  Otherwise, list feature values explicitly.  If
        ``bernoulli=False``, then you must call ``megam`` with the
        ``-fvals`` option.

    :param explicit: If true, then use the 'explicit' format.  I.e.,
        list the features that would fire for any of the possible
        labels, for each token.  If ``explicit=True``, then you must
        call ``megam`` with the ``-explicit`` option.
    c                 S   s   i | ]\}}||“qS r   r   )Ú.0ÚiÚlabelr   r   r   Ú
<dictcomp>b   ó    z$write_megam_file.<locals>.<dictcomp>Úcostú:c                 3   s    | ]}t ˆ  ˆˆ|¡ƒV  qd S )N)Ústrr   )r
   Úl©ÚencodingZ
featuresetr   r   r   Ú	<genexpr>i   r   z#write_megam_file.<locals>.<genexpr>z%dz #Ú
N)ÚlabelsÚ	enumerateÚhasattrÚwriteÚjoinÚ_write_megam_featuresÚencode)Z
train_toksr   ÚstreamÚ	bernoulliÚexplicitr   Zlabelnumr   r   r   r   Úwrite_megam_fileB   s    
ÿ
r!   c                 C   sh   t du rtdƒ‚|sJ dƒ‚|  ¡  d¡}t  |d¡}|D ](}| ¡ r:| ¡ \}}t|ƒ|t|ƒ< q:|S )zÔ
    Given the stdout output generated by ``megam`` when training a
    model, return a ``numpy`` array containing the corresponding weight
    vector.  This function does not currently handle bias features.
    Nz.This function requires that numpy be installedznon-explicit not supported yetr   Úd)ÚnumpyÚ
ValueErrorÚstripÚsplitÚzerosÚfloatÚint)ÚsZfeatures_countr    ÚlinesÚweightsÚlineÚfidZweightr   r   r   Úparse_megam_weights~   s    r/   c                 C   sb   | st dƒ‚| D ]L\}}|rF|dkr4| d| ¡ q\|dkr\t dƒ‚q| d|› d|› �¡ qd S )Nz:MEGAM classifier requires the use of an always-on feature.é   z %sr   z3If bernoulli=True, then allfeatures must be binary.ú )r$   r   )Zvectorr   r   r.   Zfvalr   r   r   r   �   s    ÿÿr   c                 C   s‚   t | tƒrtdƒ‚tdu r tƒ  tg|  }tj|tjd�}| ¡ \}}|j	dkrft
ƒ  t
|ƒ tdƒ‚t |tƒrt|S | d¡S dS )z=
    Call the ``megam`` binary with the given arguments.
    z args should be a list of stringsN)Ústdoutr   zmegam command failed!zutf-8)Ú
isinstancer   Ú	TypeErrorr   r	   Ú
subprocessÚPopenÚPIPEÚcommunicateÚ
returncodeÚprintÚOSErrorÚdecode)ÚargsÚcmdÚpr2   Ústderrr   r   r   Ú
call_megam¡   s    



rA   )N)TT)T)Ú__doc__r5   Znltk.internalsr   r#   ÚImportErrorr   r	   r!   r/   r   rA   r   r   r   r   Ú<module>   s   


<
