ó
ù`]c           @  s‹   d  d l  m Z m Z m Z d  d l m Z d  d l m Z d  d l m	 Z	 e	 d e
 f d „  ƒ  Yƒ Z d „  Z e d k r‡ e ƒ  n  d	 S(
   iÿÿÿÿ(   t   print_functiont   divisiont   unicode_literals(   t   chain(   t   Counter(   t   python_2_unicode_compatiblet
   FStructurec           B  s›   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z	 e
 d „  ƒ Z e
 d d d „ ƒ Z e
 d	 „  ƒ Z d
 „  Z d „  Z d d „ Z RS(   c         C  s.   | |  k r g  |  | <n  |  | j  | ƒ d S(   uo   
        Append 'item' to the list at 'key'.  If no list exists for 'key', then
        construct one.
        N(   t   append(   t   selft   keyt   item(    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyt
   safeappend   s    c         C  s   t  j |  | j ƒ  | ƒ d  S(   N(   t   dictt   __setitem__t   lower(   R   R	   t   value(    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyR      s    c         C  s   t  j |  | j ƒ  ƒ S(   N(   R   t   __getitem__R   (   R   R	   (    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyR      s    c         C  s   t  j |  | j ƒ  ƒ S(   N(   R   t   __contains__R   (   R   R	   (    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyR   !   s    c         C  s   |  j  ƒ  } | j | ƒ S(   N(   t   to_depgrapht   to_glueformula_list(   R   t	   glue_dictt   depgraph(    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyR   $   s    c   	      C  sÎ   d d l  m } | ƒ  } | j } |  j | d d ƒ x… | j ƒ  D]w \ } } xh d „  | j ƒ  Dƒ D]P } | d | k re | d } | d j | g  ƒ | d | j | d	 ƒ qe qe WqB W| d
 | _ | S(   Niÿÿÿÿ(   t   DependencyGraphi    u   ROOTc         s  s%   |  ] } | d  d k r | Vq d S(   u   relu   TOPN(    (   t   .0t   n(    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pys	   <genexpr>2   s    u   headu   relu   depsu   addressi   (	   t   nltk.parse.dependencygraphR   t   nodest   _to_depgrapht   itemst   valuest
   setdefaultR   t   root(	   R   t   relR   R   R   t   addresst   nodet   n2t   relation(    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyR   (   s    		
$c   	      C  sI  t  | ƒ } | | j i | d 6|  j d d 6|  j d d 6| d 6| d 6ƒ xô t |  ƒ D]æ } xÝ t |  | ƒ D]Ë } t | t ƒ r | j | | | ƒ qr t | t ƒ r÷ t  | ƒ } | | j i | d 6| d d 6| d d 6| d 6| d 6ƒ qr t | t ƒ r-x4 | D] } | j | | | ƒ qWqr t	 d | ƒ ‚ qr Wq[ Wd  S(	   Nu   addressi    u   wordi   u   tagu   headu   relu0   feature %s is not an FStruct, a list, or a tuple(
   t   lent   updatet   predt   sortedt
   isinstanceR   R   t   tuplet   listt	   Exception(	   R   R   t   headR    t   indext   featureR
   t	   new_indexR   (    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyR   <   s4    

c         C  s   t  j |  j |  ƒ S(   N(   R   t   _read_depgraphR   (   R   (    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyt   read_depgraph`   s    c   
      C  sX  | s t  ƒ  } n  |  d j ƒ  d k r: |  d |  d f St ƒ  } d  | _ t j | j ƒ  ƒ | _ | | _ |  d |  d } } | d  d k rÎ | d d !d	 k r¸ | j	 d
 d ƒ n  | | d  f | _ n  | j sé | | f | _ n  g  t
 |  d j ƒ  Œ  D] } | j | ^ q } x4 | D], }	 | j	 |	 d t j |	 | | | ƒ ƒ q W| Sd  S(   Nu   relu   specu   punctu   wordu   tagi   u   VBi   u   Du   tenseu   PASTu   deps(   u   specu   punct(   u   PASTu   tense(   R   R   R   t   NoneR'   t   _make_labelt   gett   labelt   parentR   R   R   R   R1   (
   R"   R   t   label_counterR7   t   fstructt   wordt   tagt   idxt   childrent   child(    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyR1   d   s*    				0c         C  sŽ   d d d d d d d d d	 d
 d d d d d d d d d d d d d d d d g |  d } t  |  ƒ d } | d k r† | t | ƒ S| Sd S(   u³   
        Pick an alphabetic character as identifier for an entity in the model.

        :param value: where to index into the list of characters
        :type value: int
        u   fu   gu   hu   iu   ju   ku   lu   mu   nu   ou   pu   qu   ru   su   tu   uu   vu   wu   xu   yu   zu   au   bu   cu   du   ei   i   i    N(   t   intt   str(   R   t   lettert   num(    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyR4   †   s>    	c         C  s   |  j  ƒ  j d d ƒ S(   Nu   
u    (   t   __unicode__t   replace(   R   (    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyt   __repr__°   s    c         C  s
   |  j  ƒ  S(   N(   t   pretty_format(   R   (    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyt   __str__³   s    i   c         C  sp  y d |  j  } Wn t k
 r* d } n Xy | d |  j d 7} Wn t k
 rW n Xxt |  ƒ D]ÿ } xö |  | D]ê } t | t ƒ rÓ | t | ƒ d t |  j  ƒ } | d d | | | j | ƒ f 7} qv t | t ƒ r| d d | | | d f 7} qv t | t	 ƒ rP| d	 d | | d
 d | t | ƒ d j
 | ƒ f 7} qv t d | ƒ ‚ qv Wqe W| d S(   Nu   %s:[u   [u	   pred '%s'i    i   u   
%s%s %su    u
   
%s%s '%s'u
   
%s%s {%s}u   
%si   u0   feature %s is not an FStruct, a list, or a tupleu   ](   R6   t	   NameErrorR'   R(   R)   R   R%   RF   R*   R+   t   joinR,   (   R   t   indentt   accumR/   R
   t   next_indent(    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyRF   ¶   s4    
!"-N(   t   __name__t
   __module__R   R   R   R   R   R3   R   R   t   staticmethodR2   R1   R4   RE   RG   RF   (    (    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyR      s   							$!*		c          C  sz   d d l  m }  |  d ƒ } |  d ƒ } |  d ƒ } |  d ƒ } | | | | g } x! | D] } t t j | ƒ ƒ qY Wd  S(   Niÿÿÿÿ(   R   uý   Esso       NNP     2       SUB
said       VBD     0       ROOT
the        DT      5       NMOD
Whiting    NNP     5       NMOD
field      NN      6       SUB
started    VBD     2       VMOD
production NN      6       OBJ
Tuesday    NNP     6       VMOD
uU   John    NNP     2       SUB
sees    VBP     0       ROOT
Mary    NNP     2       OBJ
uW   a       DT      2       SPEC
man     NN      3       SUBJ
walks   VB      0       ROOT
u   every   DT      2       SPEC
girl    NN      3       SUBJ
chases  VB      0       ROOT
a       DT      5       SPEC
dog     NN      3       OBJ
(   R   R   t   printR   R2   (   R   t   dg1t   dg2t   dg3t   dg4t	   depgraphst   dg(    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyt   demo_read_depgraphØ   s    
				u   __main__N(   t
   __future__R    R   R   t	   itertoolsR   t   nltk.internalsR   t   nltk.compatR   R   R   RW   RM   (    (    (    s+   lib/python2.7/site-packages/nltk/sem/lfg.pyt   <module>   s   Ç	,