ó
    ´OÂfB  ã                   óp   • S r SSKrSSKJr   SSKrSqS	S jrS
S jrSS jr	S r
S rg! \ a    Sr N"f = f)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                 ó$   • [        SU S// SQSS9qg)a%  
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    Ú5/usr/lib/python3/dist-packages/nltk/classify/megam.pyÚconfig_megamr   )   s   € ô ØØØ�ÚJØ>ñ�Jó    c                 ó  ^^^
• TR                  5       n[        U5       VVs0 s H  u  pgXv_M	     nnnU  HÆ  u  m
m[        TS5      (       a-  UR                  SR	                  UU
U4S jU 5       5      5        OUR                  SUT   -  5        U(       d  [        TR                  T
T5      X#5        O6U H0  n	UR                  S5        [        TR                  T
U	5      X#5        M2     UR                  S5        MÈ     gs  snnf )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   ó\   >#   • U  H!  n[        TR                  TTU5      5      v •  M#     g 7f©N)Ústrr   )Ú.0ÚlÚencodingÚ
featuresetÚlabels     €€€r   Ú	<genexpr>Ú#write_megam_file.<locals>.<genexpr>i   s&   øé € ÐRÊ6Àaœ˜XŸ]™]¨:°u¸aÓ@×AÐAÊ6ù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�’Ñ+<€HÑ=ó (Ñˆ
�Eä�8˜V×$Ñ$Ø�L‰LØ—‘ÖRÉ6ÓRÓRõð �L‰L˜ ¨¡Ñ/Ô0ö Ü! (§/¡/°*¸eÓ"DÀfÕXó
 �Ø—‘˜TÔ"Ü% h§o¡o°jÀ!Ó&DÀfÖXñ ð
 	�‰�TÖò- (ùó >s   ¢D c                 óD  • [         c  [        S5      eU(       d   S5       eU R                  5       R                  S5      n[         R                  " US5      nU HC  nUR                  5       (       d  M  UR                  5       u  pg[        U5      U[        U5      '   ME     U$ )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                 óØ   • U (       d  [        S5      eU  HR  u  p4U(       a/  US:X  a  UR                  SU-  5        M(  US:w  a  [        S5      eM;  UR                  SU SU 35        MT     g )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'   r:   Úfvals        r   r#   r#   �   st   € ÞÜØKó
ð 	
ó ‰	ˆÞØ�q‹yØ—‘˜U S™[Ö)Ø˜“Ü ØLóð ñ ð
 �L‰L˜1˜S˜E  4 &Ð)Ö*ò r   c                 ó’  • [        U [        5      (       a  [        S5      e[        c
  [	        5         [        /U -   n[
        R                  " U[
        R                  S9nUR                  5       u  p4UR                  S:w  a   [        5         [        U5        [        S5      e[        U[        5      (       a  U$ UR                  S5      $ )z5
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ÚprC   Ústderrs        r   Ú
call_megamrR   ¡   s¡   € ô �$œ×ÑÜÐ:Ó;Ð;ÜÑÜŒô ˆ,˜Ñ
€CÜ×Ò˜¤Z§_¡_Ñ5€AØ—}‘}“Ñ€Vð 	‡|�|�qÓÜŒÜˆfŒÜÐ-Ó.Ð.ä�&œ#×ÑØˆà�}‰}˜WÓ%Ð%r   r   )TT)T)Ú__doc__rF   Únltk.internalsr   r.   ÚImportErrorr   r   r+   r<   r#   rR   © r   r   Ú<module>rW      sR   ðñó" å &ðÛð €
ôô29ôxò$+ó"&øðE ó Ø‚Eðús   Ž* ª5´5