§
    'ê[fü  ã                   ól   — d Z ddlZddlmZ 	 ddlZn# e$ r dZY nw xY wdad
d„Zdd„Zdd„Z	d„ Z
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                 ó4   — t          d| dgg 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
    ÚmegamÚMEGAM)z	megam.optr   Ú	megam_686zmegam_i686.optz0https://www.umiacs.umd.edu/~hal/megam/index.html)Úenv_varsÚbinary_namesÚurlN)r   Ú
_megam_bin)Úbins    úG/var/www/piapp/venv/lib/python3.11/site-packages/nltk/classify/megam.pyÚconfig_megamr   )   s3   € õ ØØØ�ØJÐJÐJØ>ðñ ô €J€J€Jó    Tc                 ó8  ‡‡‡	— ‰                      ¦   «         }d„ t          |¦  «        D ¦   «         }| D ]æ\  ŠŠ	t          ‰d¦  «        r7|                     d                     ˆˆˆ	fd„|D ¦   «         ¦  «        ¦  «         n|                     d|‰	         z  ¦  «         |s&t          ‰                     ‰‰	¦  «        ||¦  «         n?|D ]<}|                     d¦  «         t          ‰                     ‰|¦  «        ||¦  «         Œ=|                     d¦  «         Œç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                 ó   — i | ]\  }}||“Œ	S © r   )Ú.0ÚiÚlabels      r   ú
<dictcomp>z$write_megam_file.<locals>.<dictcomp>b   s   € Ð=Ð=Ð=™Z˜a ��qÐ=Ð=Ð=r   Úcostú:c              3   ó`   •K  — | ](}t          ‰                     ‰‰|¦  «        ¦  «        V — Œ)d S ©N)Ústrr   )r   ÚlÚencodingÚ
featuresetr   s     €€€r   ú	<genexpr>z#write_megam_file.<locals>.<genexpr>i   s;   øè è € ÐRÐRÀa�˜XŸ]š]¨:°u¸aÑ@Ô@ÑAÔAÐRÐRÐRÐRÐRÐRr   z%dz #Ú
N)ÚlabelsÚ	enumerateÚhasattrÚwriteÚjoinÚ_write_megam_featuresÚencode)
Ú
train_toksr   ÚstreamÚ	bernoulliÚexplicitr!   Úlabelnumr   r   r   s
    `      @@r   Úwrite_megam_filer-   B   sR  øøø€ ð> �_Š_ÑÔ€FØ=Ð=­9°VÑ+<Ô+<Ð=Ñ=Ô=€Hð (ð ð Ñˆ
�Eå�8˜VÑ$Ô$ð 	1Ø�LŠLØ—’ÐRÐRÐRÐRÐRÐRÈ6ÐRÑRÔRÑRÔRñô ð ð ð �LŠL˜ ¨¤Ñ/Ñ0Ô0Ð0ð ð 	YÝ! (§/¢/°*¸eÑ"DÔ"DÀfÈiÑXÔXÐXÐXð
 ð Yð Y�Ø—’˜TÑ"Ô"Ð"Ý% h§o¢o°jÀ!Ñ&DÔ&DÀfÈiÑXÔXÐXÐXð 	�Š�TÑÔÐÐð-ð r   c                 ó`  — t           €t          d¦  «        ‚|s
J d¦   «         ‚|                      ¦   «                              d¦  «        }t          j        |d¦  «        }|D ]L}|                     ¦   «         r6|                     ¦   «         \  }}t          |¦  «        |t          |¦  «        <   ŒM|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)ÚsÚfeatures_countr+   ÚlinesÚweightsÚlineÚfidÚweights           r   Úparse_megam_weightsr>   ~   s£   € õ €}ÝÐIÑJÔJÐJØÐ5Ð5Ð5Ñ5Ô5Ð5Ø�GŠG‰IŒI�OŠO˜DÑ!Ô!€EÝŒk˜.¨#Ñ.Ô.€GØð .ð .ˆØ�:Š:‰<Œ<ð 	.ØŸ*š*™,œ,‰KˆC�Ý % f¡¤ˆG•C˜‘H”HÑøØ€Nr   c                 óÜ   — | st          d¦  «        ‚| D ]W\  }}|r5|dk    r|                     d|z  ¦  «         Œ&|dk    rt          d¦  «        ‚Œ<|                     d|› d|› �¦  «         ŒXd S )Nz:MEGAM classifier requires the use of an always-on feature.é   z %sr   z3If bernoulli=True, then allfeatures must be binary.Ú )r1   r$   )Úvectorr)   r*   r<   Úfvals        r   r&   r&   �   s³   € Øð 
ÝØKñ
ô 
ð 	
ð ð 	+ð 	+‰ˆˆdØð 	+Ø�qŠyˆyØ—’˜U S™[Ñ)Ô)Ð)Ð)Ø˜’�Ý ØLñô ð ð ð
 �LŠLÐ)˜SÐ)Ð) 4Ð)Ð)Ñ*Ô*Ð*Ð*ð	+ð 	+r   c                 ó¾  — t          | t          ¦  «        rt          d¦  «        ‚t          €t	          ¦   «          t          g| z   }t          j        |t
          j        ¬¦  «        }|                     ¦   «         \  }}|j	        dk    r,t          ¦   «          t          |¦  «         t          d¦  «        ‚t          |t          ¦  «        r|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ÚprE   Ústderrs        r   Ú
call_megamrT   ¡   sÆ   € õ �$�ÑÔð <ÝÐ:Ñ;Ô;Ð;ÝÐÝ‰Œˆõ ˆ,˜Ñ
€CÝÔ˜¥Z¤_Ð5Ñ5Ô5€AØ—}’}‘”Ñ€VˆVð 	„|�qÒÐÝ‰ŒˆÝˆf‰ŒˆÝÐ-Ñ.Ô.Ð.å�&�#ÑÔð &Øˆà�}Š}˜WÑ%Ô%Ð%r   r   )TT)T)Ú__doc__rH   Únltk.internalsr   r0   ÚImportErrorr   r   r-   r>   r&   rT   r   r   r   ú<module>rX      sÏ   ððð ð" Ð Ð Ð à &Ð &Ð &Ð &Ð &Ð &ðØ€L€L€L€LøØð ð ð Ø€E€E€Eðøøøð €
ðð ð ð ð29ð 9ð 9ð 9ðxð ð ð ð$+ð +ð +ð"&ð &ð &ð &ð &s   Ž “œ