
    /hB                     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    K/var/www/auris/envauris/lib/python3.13/site-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&     R6aX]]:ua@AA6s   ),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+4V+<=+<Za+<H= (
E8V$$LLR6RR LL/0 !(//*e"DfX
 T"%hooj!&Df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58GGIOOD!Ekk.#.G::<<**,KC %fGC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
 	
 	qyUS[) L  
 LL1SE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__5A}}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