Ñò
áê´Kc           @   sF  d  d k  Z  h d d 6d d 6d d 6d d 6d d	 6d d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6Z h d d 6d d 6d d 6d d 6d  d! g d" 6d# d 6d$ d% g d& 6d' d( 6d d 6d d 6d) d* 6d+ d, 6d- d. 6d/ d0 6d1 d2 6d3 d4 6d5 d 6d6 d7 g d8 6d9 d
 6Z e  i d: ƒ Z e  i i e ƒ Z e  i i	 i
 e ƒ Z e d; „  e i ƒ  Dƒ ƒ Z e d< „  e i ƒ  Dƒ ƒ Z d= GHx; e i ƒ  D]- \ Z Z e Gx e D] Z e Ge i GHqËWq´Wd> „  Z d? „  Z d@ „  Z dA „  Z dB „  Z e e dC „ Z dD „  Z e dE j o e ƒ  n d S(F   iÿÿÿÿNs	   <<e,t>,t>t   At   Bt   Cs   <<<e,t>,t>, <<e,t>, t>>t   Ds   <e,e>t   Ft   wifes	   <e,<e,t>>t   Lt   lovess   <e,t>t   Pt   Qt   girlt   bookt   boyt   smokest   et   xt   yt   zt   billt   johns   \P Q.all x.(P(x) & Q(x))t   everys
   \x.girl(x)s
   \x.book(x)s	   \x.boy(x)s+   \A P B.(A(\y.(B(\x.(using(x)(y) & P(x))))))s)   \A B P.(A(\y.(B(\x.(has(x)(y) & P(y))))))t   withs   \x.smokes(x)s(   \B C A.C(\x.A(\z.(B(\y.give(x)(y)(z)))))s"   \B C x.C(\z.(B(\y.give(x)(y)(z))))t   givess
   \P.P(mary)t   marys   \P Q.exists x.(P(x) & Q(x))t   as   \A x.A(\y.(hits(x)(y)))t   hitss   \A.At   doess   \E.Et   toos	   \x P.P(x)t   does_toos   \P x y.(P(x) & P(y))t   ands   \x y.loves(x,y)s   \F L x.L(x, F(x))s   \F L x y.L(y, F(x))t   hiss
   \x.wife(x)s4  
S -> NP VP | SCon S
SCon -> NP VPCon 
PP -> 'with' NP
NP -> 'every' 'girl' | 'a' 'book' | 'a' 'boy' | 'his' 'wife'
NP -> NP PP
VPCon -> 'smokes' CON | VP CON
VP -> 'hits' NP | 'loves' NP | 'does' 'too' | 'does_too'
VP -> VP PP | DVP NP | NP VP 
DVP -> 'gives' NP
CON -> 'and'
NP -> 'mary' | 'john' | 'bill'
c         c   sB   x; |  ]4 \ } } | t  | ƒ t j o | p | g f Vq Wd  S(   N(   t   typet   list(   t   .0t   wt   m(    (    s   lg.pys	   <genexpr>7   s   	 c         c   sL   xE |  ]> \ } } | g  } | D] } | t  i | t ƒ q  ~ f Vq Wd  S(   N(   t   lpt   parset   types(   R!   R"   R#   t   _[1]R   (    (    s   lg.pys	   <genexpr>8   s   	 s   the lexicon:c         c   st   t  |  ƒ d j o! xZ |  d D] } | f Vq Wn= x9 |  d D]- } x$ t |  d ƒ D] } | f | VqV Wq? Wd S(   s5    fold a sequence using cartesian product as operator i   i    N(   t   lent   cartpi(   t   seqR   t   b(    (    s   lg.pyR)   >   s         c         c   s‘   xŠ t  g  } |  i ƒ  D] } | t | q ~ ƒ D]W } |  i t ƒ } x: t | | i d ƒ ƒ D]  \ } } | | | f | | <q` W| Vq2 Wd  S(   Nt   leaves(   R)   R,   t   lexicont   copyt   Truet   zipt   treepositions(   t	   parsetreeR'   R+   R   R.   t   c(    (    s   lg.pyt   applylexiconF   s    2  c         C   s™  t  |  ƒ t i i j opt |  ƒ d j o) t i |  i |  d d f |  d g ƒ St |  ƒ d j ot |  d ƒ t |  d ƒ } } | i d | i d } } t  | i  ƒ t i i i	 j o/ | i  i
 i | i  ƒ o | | ƒ i ƒ  } nt t  | i  ƒ t i i i	 j o/ | i  i
 i | i  ƒ o | | ƒ i ƒ  } n& t i i i d | i  | i  f ƒ ‚ t i |  i | f | | g ƒ Sn t i |  g  ƒ S(   Ni   i    i   s   type mismatch: %s / %s(   R   t   nltkt   treet   TreeR(   t   nodet	   applytreet   semt   logict   ComplexTypet   firstt   matchest   simplifyt   TypeException(   R2   t   aat   bbR   R+   R8   (    (    s   lg.pyR9   O   s    )!%$c         C   s‚   d „  } t  |  ƒ t j o | |  ƒ St |  ƒ d j o | |  i ƒ St i | |  i ƒ g  } |  D] } | t | ƒ qe ~ ƒ S(   Nc         S   s   |  |  d i  f S(   Ni   (   R   (   R8   (    (    s   lg.pyt   addtypee   s    i    (   R   t   tupleR(   R8   R5   R7   t
   applytypes(   R2   RC   R'   R   (    (    s   lg.pyRE   c   s    	c      	   C   s½   t  |  ƒ t j o d i d „  |  Dƒ ƒ St  |  i ƒ t j o5 t i |  i g  } |  D] } | t | ƒ qX ~ ƒ St i d i d „  |  i Dƒ ƒ g  } |  D] } | t | ƒ q  ~ ƒ S(   Ns   
c         s   s   x |  ] } t  | ƒ Vq Wd  S(   N(   t   str(   R!   R   (    (    s   lg.pys	   <genexpr>o   s   	 c         s   s   x |  ] } t  | ƒ Vq Wd  S(   N(   RF   (   R!   R   (    (    s   lg.pys	   <genexpr>r   s   	 (   R   RD   t   joinR8   RF   R5   R7   t   tostring(   R2   R'   R   t   _[2](    (    s   lg.pyRH   l   s    5c      	   c   sÔ   t  i i | ƒ } x» | i |  i ƒ  ƒ D]¤ } x› t | ƒ D] } y[ t | ƒ } t | i d ƒ t	 t
 | ƒ ƒ i ƒ  f V| o t	 t
 | ƒ ƒ i ƒ  n Wq; t  i i i j
 o } | d f Vq; Xq; Wq( Wd  S(   Ni   t    (   R5   R%   t   EarleyChartParsert   nbest_parset   splitR4   R9   RF   R8   RH   RE   t   pprintt   drawR:   R;   R@   (   t   sentencet   grammarRO   t   srR   R+   R3   t   msg(    (    s   lg.pyt	   interpretu   s      * c          C   sP   x# t  d d t ƒD] }  |  d GHq Wx# t  d d t ƒD] }  |  d GHq9 Wd  S(   Ns%   john loves his wife and bill does_tooRO   i    s*   john john loves his wife and bill does_too(   RT   R/   (   R   (    (    s   lg.pyt   main‚   s        t   __main__(   R5   R&   R-   t	   parse_cfgRQ   R%   RK   RR   R:   R;   t   LogicParserR/   R$   t   dictt   itemsR"   R#   R   R   R)   R4   R9   RE   RH   t   FalseRT   RU   t   __name__(    (    (    s   lg.pyt   <module>   sd   &



	   									 