ó
ù`]c           @  s½   d  Z  d d l m Z d d l m Z m Z m Z d d l m Z m	 Z	 d d l
 m Z m Z d d l m Z d d l m Z d d l m Z d	 Z e d
 e f d „  ƒ  Yƒ Z d „  Z d S(   uV  
A general interface to the SENNA pipeline that supports any of the
operations specified in SUPPORTED_OPERATIONS.

Applying multiple operations at once has the speed advantage. For example,
Senna will automatically determine POS tags if you are extracting named
entities. Applying both of the operations will cost only the time of
extracting the named entities.

The SENNA pipeline has a fixed maximum size of the sentences that it can read.
By default it is 1024 token/sentence. If you have larger sentences, changing
the MAX_SENTENCE_SIZE value in SENNA_main.c should be considered and your
system specific binary should be rebuilt. Otherwise this could introduce
misalignment errors.

The input is:
- path to the directory that contains SENNA executables. If the path is incorrect,
   Senna will automatically search for executable file specified in SENNA environment variable
- List of the operations needed to be performed.
- (optionally) the encoding of the input data (default:utf-8)

Note: Unit tests for this module can be found in test/unit/test_senna.py

    >>> from __future__ import unicode_literals
    >>> from nltk.classify import Senna
    >>> pipeline = Senna('/usr/share/senna-v3.0', ['pos', 'chk', 'ner'])
    >>> sent = 'Dusseldorf is an international business center'.split()
    >>> [(token['word'], token['chk'], token['ner'], token['pos']) for token in pipeline.tag(sent)] # doctest: +SKIP
    [('Dusseldorf', 'B-NP', 'B-LOC', 'NNP'), ('is', 'B-VP', 'O', 'VBZ'), ('an', 'B-NP', 'O', 'DT'),
    ('international', 'I-NP', 'O', 'JJ'), ('business', 'I-NP', 'O', 'NN'), ('center', 'I-NP', 'O', 'NN')]
iÿÿÿÿ(   t   unicode_literals(   t   patht   sept   environ(   t   Popent   PIPE(   t   architecturet   system(   t	   text_type(   t   TaggerI(   t   python_2_unicode_compatibleu   http://ml.nec-labs.com/senna/t   Sennac           B  sG   e  Z d  d d g Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(	   u   posu   chku   neru   utf-8c         C  s³   | |  _  t j | ƒ t |  _ |  j |  j ƒ } t j | ƒ s¦ d t k r¦ t j t d ƒ t |  _ |  j |  j ƒ } t j | ƒ s£ t d | | f ƒ ‚ q£ q¦ n  | |  _	 d  S(   Nu   SENNAu3   Senna executable expected at %s or %s but not found(
   t	   _encodingR   t   normpathR   t   _patht
   executablet   isfileR   t   OSErrort
   operations(   t   selft
   senna_pathR   t   encodingt
   exe_file_1t
   exe_file_2(    (    s2   lib/python2.7/site-packages/nltk/classify/senna.pyt   __init__<   s    	c         C  s–   t  ƒ  } | d k rN t ƒ  d } | d k r> t j | d ƒ St j | d ƒ S| d k rj t j | d ƒ S| d k r† t j | d	 ƒ St j | d
 ƒ S(   uÆ   
        The function that determines the system specific binary that should be
        used in the pipeline. In case, the system is not known the default senna binary will
        be used.
        u   Linuxi    u   64bitu   senna-linux64u   senna-linux32u   Windowsu   senna-win32.exeu   Darwinu	   senna-osxu   senna(   R   R   R   t   join(   R   t	   base_patht   os_namet   bits(    (    s2   lib/python2.7/site-packages/nltk/classify/senna.pyR   Q   s    	c         C  sJ   i  } d } x7 t  j D], } | |  j k r | | | <| d 7} q q W| S(   u¨   
        A method that calculates the order of the columns that SENNA pipeline
        will output the tags into. This depends on the operations being ordered.
        i   (   R   t   SUPPORTED_OPERATIONSR   (   R   t   _mapt   it	   operation(    (    s2   lib/python2.7/site-packages/nltk/classify/senna.pyR   c   s    
c         C  s   |  j  | g ƒ d S(   uI   
        Applies the specified operation(s) on a list of tokens.
        i    (   t	   tag_sents(   R   t   tokens(    (    s2   lib/python2.7/site-packages/nltk/classify/senna.pyt   tagp   s    c         C  sI  |  j  } t j |  j |  j ƒ ƒ sC t d |  j |  j ƒ ƒ ‚ n  |  j |  j ƒ d |  j d d g } | j g  |  j D] } d | ^ qw ƒ d j d „  | Dƒ ƒ d } t	 | t
 ƒ rÒ | rÒ | j | ƒ } n  t | d t d	 t d
 t ƒ} | j d | ƒ \ } } | }	 | j d k r0t d | ƒ ‚ n  | rH| j | ƒ }	 n  |  j ƒ  }
 g  g } d } d } xÙ |	 j ƒ  j d ƒ D]Â } | s®| j g  ƒ | d 7} d } qn  | j d ƒ } i  } x& |
 D] } | |
 | j ƒ  | | <qÊWy | | | | d <Wn! t k
 r%t d | ƒ ‚ n X| d j | ƒ | d 7} qW| S(   uÍ   
        Applies the tag method over a list of sentences. This method will return a
        list of dictionaries. Every dictionary will contain a word with its
        calculated annotations/tags.
        u-   Senna executable expected at %s but not foundu   -pathu
   -usrtokensu   -iobtagsu   -u   
c         s  s   |  ] } d  j  | ƒ Vq d S(   u    N(   R   (   t   .0t   x(    (    s2   lib/python2.7/site-packages/nltk/classify/senna.pys	   <genexpr>   s    t   stdint   stdoutt   stderrt   inputi    u!   Senna command failed! Details: %si   u   	u   wordu´   Misalignment error occurred at sentence number %d. Possible reason is that the sentence size exceeded the maximum size. Check the documentation of Senna class for more information.iÿÿÿÿ(   R   R   R   R   R   R   t   extendR   R   t
   isinstanceR   t   encodeR   R   t   communicatet
   returncodet   RuntimeErrort   decodeR   t   stript   splitt   appendt
   IndexError(   R   t	   sentencesR   t
   _senna_cmdt   opt   _inputt   pR'   R(   t   senna_outputt   map_t   tagged_sentencest   sentence_indext   token_indext   tagged_wordt   tagst   resultR#   (    (    s2   lib/python2.7/site-packages/nltk/classify/senna.pyR!   v   sX    		'	
(   t   __name__t
   __module__R   R   R   R   R#   R!   (    (    (    s2   lib/python2.7/site-packages/nltk/classify/senna.pyR   7   s   			c         C  sP   d d l  m } y t d d d d g ƒ } Wn t k
 rK | d ƒ ‚ n Xd  S(   Niÿÿÿÿ(   t   SkipTestu   /usr/share/senna-v3.0u   posu   chku   neru   Senna executable not found(   t   noseRD   R   R   (   t   moduleRD   t   tagger(    (    s2   lib/python2.7/site-packages/nltk/classify/senna.pyt   setup_module½   s
    N(   t   __doc__t
   __future__R    t   osR   R   R   t
   subprocessR   R   t   platformR   R   t   sixR   t   nltk.tag.apiR	   t   nltk.compatR
   t
   _senna_urlR   RH   (    (    (    s2   lib/python2.7/site-packages/nltk/classify/senna.pyt   <module>'   s   …