Ë
    çÍ:jB  ã                   óh   — d Z ddlZddlmZ 	 ddlZdad	d„Zd
d„Zdd„Z	d„ Z
d„ Zy# e$ r dZY Œw xY w)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                 ó*   — t        d| dgg d¢d¬«      ay)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    úh/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/nltk/classify/megam.pyÚconfig_megamr   )   s   € ô ØØØ�ÚJØ>ô�Jó    c                 óð  ‡‡‡
— ‰j                  «       }t        |«      D ��ci c]  \  }}||“Œ
 }}}| D ]º  \  Š
Št        ‰d«      r,|j                  dj	                  ˆˆ
ˆfd„|D «       «      «       n|j                  d|‰   z  «       |st        ‰j                  ‰
‰«      ||«       n5|D ]0  }	|j                  d«       t        ‰j                  ‰
|	«      ||«       Œ2 |j                  d«       Œ¼ yc c}}w )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.
    Úcostú:c              3   óV   •K  — | ]   }t        ‰j                  ‰‰|«      «      –— Œ" y ­w©N)Ústrr   )Ú.0ÚlÚencodingÚ
featuresetÚlabels     €€€r   ú	<genexpr>z#write_megam_file.<locals>.<genexpr>i   s#   øè ø€ ÒRÀaœ˜XŸ]™]¨:°u¸aÓ@×AÑRùs   ƒ&)z%dz #ú
N)ÚlabelsÚ	enumerateÚhasattrÚwriteÚjoinÚ_write_megam_featuresÚencode)Ú
train_toksr   ÚstreamÚ	bernoulliÚexplicitr   Úir   Úlabelnumr   r   s    `     `  @r   Úwrite_megam_filer*   B   së   ú€ ð> �_‰_Ó€FÜ+4°VÓ+<×=™Z˜a ��q‘Ð=€HÑ=ð (ò Ñˆ
�Eä�8˜VÔ$Ø�L‰LØ—‘ÕRÈ6ÔRÓRõð �L‰L˜ ¨¡Ñ/Ô0ñ Ü! (§/¡/°*¸eÓ"DÀfÈiÕXð
 ò Y�Ø—‘˜TÔ"Ü% h§o¡o°jÀ!Ó&DÀfÈiÕXðYð
 	�‰�TÕñ-ùó >s   ¢C2c                 ó(  — t         €t        d«      ‚|sJ d«       ‚| j                  «       j                  d«      }t        j                  |d«      }|D ]=  }|j                  «       sŒ|j                  «       \  }}t        |«      |t        |«      <   Œ? |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.
    z.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ÙÐ5Ð5Ó5ˆ8Ø�G‰G‹I�O‰O˜DÓ!€EÜ�k‰k˜.¨#Ó.€GØò .ˆØ�:‰:�<ØŸ*™*›,‰KˆC�Ü % f£ˆG”C˜“HÒð.ð €Nr   c                 óº   — | st        d«      ‚| D ]I  \  }}|r+|dk(  r|j                  d|z  «       Œ"|dk7  sŒ(t        d«      ‚|j                  d|› d|› �«       ŒK y )Nz:MEGAM classifier requires the use of an always-on feature.é   z %sr   z3If bernoulli=True, then allfeatures must be binary.ú )r.   r    )Úvectorr%   r&   r9   Úfvals        r   r"   r"   �   sv   € ÙÜØKó
ð 	
ð ò 	+‰	ˆˆTÙØ�qŠyØ—‘˜U S™[Õ)Ø˜“Ü ØLóð ð �L‰L˜1˜S˜E  4 &Ð)Õ*ñ	+r   c                 ó€  — t        | t        «      rt        d«      ‚t        €
t	        «        t        g| z   }t        j                  |t
        j                  ¬«      }|j                  «       \  }}|j                  dk7  r t        «        t        |«       t        d«      ‚t        |t        «      r|S |j                  d«      S )z=
    Call the ``megam`` binary with the given arguments.
    z args should be a list of strings)Ústdoutr   zmegam command failed!zutf-8)Ú
isinstancer   Ú	TypeErrorr   r   Ú
subprocessÚPopenÚPIPEÚcommunicateÚ
returncodeÚprintÚOSErrorÚdecode)ÚargsÚcmdÚprB   Ústderrs        r   Ú
call_megamrQ   ¡   s�   € ô �$œÔÜÐ:Ó;Ð;ÜÐÜŒô ˆ,˜Ñ
€CÜ×Ñ˜¤Z§_¡_Ô5€AØ—}‘}“Ñ€VˆVð 	‡|�|�qÒÜŒÜˆfŒÜÐ-Ó.Ð.ä�&œ#ÔØˆà�}‰}˜WÓ%Ð%r   r   )TT)T)Ú__doc__rE   Únltk.internalsr   r-   ÚImportErrorr   r   r*   r;   r"   rQ   © r   r   ú<module>rV      sR   ðñó" å &ðÛð €
óó29óxò$+ó"&øðE ò Ø‚Eðús   Ž' §1°1