
4Ic           @   s  d  d k  Z  d  d k Td  d k Z yD d  d k l Z d  d k l Z d  d k l Z d  d k	 l
 Z
 Wn e j
 o n Xd e f d     YZ d	 e f d
     YZ d e e e i f d     YZ d   Z d e e e i f d     YZ d e e e i f d     YZ d e e e i f d     YZ d e e e i f d     YZ d e e e i f d     YZ d e e e i f d     YZ d e e e i f d     YZ  d e e! f d     YZ" d e" e# e i# f d     YZ$ d  e" e% e i% f d!     YZ& d" e" e' e i' f d#     YZ( d$ e e) e i) f d%     YZ* d& e" e i+ f d'     YZ+ d( e e, e i, f d)     YZ- d* e f d+     YZ. d, e/ f d-     YZ0 d.   Z1 d/   Z2 e3 d0 j o e1   n d S(1   iN(   t   *(   t   Canvas(   t   Tk(   t   Font(   t   in_idlet	   DrtTokensc           B   sV   e  Z d  Z d Z d Z d Z d Z e e e g Z e i	 e Z	 e i
 e g e Z
 RS(   t   DRSt   +t   PROt   [t   ](   t   __name__t
   __module__R   t   DRS_CONCt   PRONOUNt   OPEN_BRACKETt   CLOSE_BRACKETt   PUNCTt   Tokenst   SYMBOLSt   TOKENS(    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR      s   t   AbstractDrsc           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z e	 d  Z
 d   Z e e  Z e	 d	  Z d
   Z e d  Z d   Z d   Z d   Z d   Z RS(   si   
    This is the base abstract abstract DRT Expression from which every DRT 
    Expression extends.
    c         C   s   t  |  |  S(   N(   t   DrtApplicationExpression(   t   selft   other(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   applyto+   s    c         C   s
   t  |   S(   N(   t   DrtNegatedExpression(   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   __neg__.   s    c         C   s   t     d  S(   N(   t   NotImplementedError(   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   __and__1   s    c         C   s$   t  | t  p t  t |  |  S(   N(   t
   isinstanceR   t   AssertionErrort   DrtOrExpression(   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   __or__4   s    c         C   s$   t  | t  p t  t |  |  S(   N(   R   R   R   t   DrtImpExpression(   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   __gt__8   s    c         C   s$   t  | t  p t  t |  |  S(   N(   R   R   R   t   DrtIffExpression(   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   __lt__<   s    c         C   sK   t  | t  p t  |  i   i   } | i   i   } | i | |  S(   s   Pass the expression (self <-> other) to the theorem prover.   
        If the prover says it is valid, then the self and other are equal.(   R   R   R   t   simplifyt   toFolt	   tp_equals(   R   R   t   provert   f1t   f2(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR(   @   s    c         C   s   t  d |  i i   d  S(   Ns#   '%s' object has no attribute 'type'(   t   AttributeErrort	   __class__R   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt	   _get_typeI   s    c         C   s   t     d  S(   N(   R   (   R   t	   signature(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt	   typecheckN   s    c         C   s   t  |  |  S(   N(   t   ConcatenationDRS(   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   __add__Q   s    c         C   s   t     d S(   s   
        Return the set of discourse referents in this DRS.
        @param recursive: C{boolean} Also find discourse referents in subterms?
        @return: C{list} of C{Variable}s 
        N(   R   (   R   t	   recursive(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   get_refsT   s    c         C   sL   t  |  t  o< t  |  i t  o) |  i i i t i j o t  |  i t	  S(   s    Is self of the form "PRO(x)"? (
   R   R   t   functiont   DrtAbstractVariableExpressiont   variablet   nameR   R   t   argumentt   DrtIndividualVariableExpression(   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   is_pronoun_function\   s    c         C   s   t  | |  S(   N(   t   DrtEqualityExpression(   R   t   firstt   second(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   make_EqualityExpressionc   s    c         C   s
   t  |  S(   N(   t   DrtVariableExpression(   R   R7   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   make_VariableExpressionf   s    c         C   s   t  |   i   d  S(   N(   t	   DrsDrawert   draw(   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRC   i   s    (   R   R   t   __doc__R   R   R   R!   R#   R%   t   NoneR(   R.   t   propertyt   typeR0   R2   t   FalseR4   R;   R?   RA   RC   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR   &   s"   												R   c           B   sw   e  Z d  Z d   Z e d  Z d   Z e d  Z e d  Z	 d   Z
 d   Z d   Z d	   Z e i d
  Z RS(   s%   A Discourse Representation Structure.c         C   s   | |  _  | |  _ d S(   s   
        @param refs: C{list} of C{DrtIndividualVariableExpression} for the 
        discourse referents
        @param conds: C{list} of C{Expression} for the conditions
        N(   t   refst   conds(   R   RI   RJ   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   __init__o   s    	c         C   s}  y |  i  i |  } | p |  Sn[ t |  i  |  | i g |  i  | d g  } |  i D] } | | i | | t  qX ~  SWn t j
 o x t |  i   | i	   @D] } t
 |  } t |  }	 |  i  i |  } t |  i  |  | g |  i  | d g  }
 |  i D] } |
 | i | |	 t  q~
  }  q Wt |  i  g  } |  i D] } | | i | | |  qN~  Sn Xd S(   s_   Replace all instances of variable v with expression E in self,
        where v is free in self.i   N(   RI   t   indexR   R7   RJ   t   replacet   Truet
   ValueErrort   sett   freet   unique_variableR@   (   R   R7   t
   expressiont   replace_boundt   it   _[1]t   condt   reft   newvart   newvarext   _[2]t   _[3](    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRM   x   s$    &8 #3	c         C   sM   t  t i g  } |  i D] } | | i   q ~ t    } | t |  i  S(   s   @see: Expression.variables()(   t   reducet   operatort   or_RJ   t	   variablesRP   RI   (   R   RV   t   ct
   conds_vars(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR`      s    	3c         C   sP   t  t i g  } |  i D] } | | i |  q ~ t    } | t |  i  S(   s   @see: Expression.free()(   R]   R^   R_   RJ   RQ   RP   RI   (   R   t   indvar_onlyRV   Ra   t
   conds_free(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRQ      s    	6c         C   s]   | oK t  t i g  } |  i D] } | | i t  q ~ g   } |  i | Sn |  i Sd S(   s#   @see: AbstractExpression.get_refs()N(   R]   R^   t   addRJ   R4   RN   RI   (   R   R3   RV   Ra   t	   cond_refs(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR4      s
    	3c         C   s;   t  | g  } |  i |  i D] } | | |  q ~ |  S(   s   @see: Expression.visit()(   R]   RI   RJ   (   R   R5   t
   combinatort   defaultRV   t   e(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   visit   s    c         C   s4   t  |  i g  } |  i D] } | | i   q ~  S(   N(   R   RI   RJ   R&   (   R   RV   RW   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR&      s    c         C   s   |  i  p t d   n t t g  } |  i  D] } | | i   q. ~  } x- |  i d  d  d  D] } t | |  } qd W| S(   Ns-   Cannot convert DRS with no conditions to FOL.i(   RJ   t	   ExceptionR]   t   AndExpressionR'   RI   t   ExistsExpression(   R   RV   Ra   t   accumRX   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR'      s    
3 c         C   s   t  | t  o t |  i  t | i  j od | } xG t |  i | i  D]0 \ } } |  i |  } | i | | t  } qK W|  i | i j Sq n t	 S(   s}   Defines equality modulo alphabetic variance.
        If we are comparing \x.M  and \y.N, then check equality of M and N[x/y].(
   R   R   t   lenRI   t   zipRA   RM   RN   RJ   RH   (   R   R   t   converted_othert   r1t   r2t   varex(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   __eq__   s     c      
   C   s   | t  i j o |  i   i |  Snl d d i g  } |  i D] } | t |  q> ~  d i g  } |  i D] } | | i |  qn ~  f Sd  S(   Ns   ([%s],[%s])t   ,s   , (   R   t   PROVER9R'   t   strt   joinRI   RJ   (   R   t   syntaxRV   t   rR[   Ra   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRx      s    3(   R   R   RD   RK   RH   RM   R`   RN   RQ   R4   Rj   R&   R'   Ru   R   t   NLTKRx   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR   m   s   								c         C   sh   t  |  i  o t |   SnG t |  i  o t |   Sn) t |  i  o t |   Sn t |   Sd S(   s   
    This is a factory method that instantiates and returns a subtype of 
    C{DrtAbstractVariableExpression} appropriate for the given variable.
    N(   t	   is_indvarR8   R:   t
   is_funcvart   DrtFunctionVariableExpressiont   is_eventvart   DrtEventVariableExpressiont   DrtConstantExpression(   R7   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR@      s    R6   c           B   s   e  Z d    Z e d  Z RS(   c         C   s   |  S(   N(    (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR'      s    c         C   s   g  S(   s#   @see: AbstractExpression.get_refs()(    (   R   R3   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR4      s    (   R   R   R'   RH   R4   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR6      s   	R:   c           B   s   e  Z RS(    (   R   R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR:      s   R   c           B   s   e  Z RS(    (   R   R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR      s   R   c           B   s   e  Z RS(    (   R   R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR      s   R   c           B   s   e  Z RS(    (   R   R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR      s   R   c           B   s   e  Z d    Z RS(   c         C   s   t  |  i i    S(   N(   t   NegatedExpressiont   termR'   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR'      s    (   R   R   R'   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR      s   t   DrtLambdaExpressionc           B   s   e  Z d    Z d   Z RS(   c         C   s+   |  i  | |  i i |  i t |  t   S(   s   Rename all occurrences of the variable introduced by this variable
        binder in the expression to @C{newvar}.
        @param newvar: C{Variable}, for the new variable
        (   R-   R   RM   R7   R@   RN   (   R   RY   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   alpha_convert  s    c         C   s   t  |  i |  i i    S(   N(   t   LambdaExpressionR7   R   R'   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR'   	  s    (   R   R   R   R'   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR      s   	t   DrtBooleanExpressionc           B   s   e  Z e d   Z RS(   c         C   s3   | o$ |  i  i t  |  i i t  Sn g  Sd S(   s#   @see: AbstractExpression.get_refs()N(   R=   R4   RN   R>   (   R   R3   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR4     s    $(   R   R   RH   R4   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s   R    c           B   s   e  Z d    Z RS(   c         C   s   t  |  i i   |  i i    S(   N(   t   OrExpressionR=   R'   R>   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR'     s    (   R   R   R'   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR      s   R"   c           B   s   e  Z d    Z RS(   c         C   s   |  i  } |  i } d  } | i o7 t t g  } | i D] } | | i   q6 ~  } n | o t | | i    } n | i   } x- | i d  d  d  D] } t	 | |  } q W| S(   Ni(
   R=   R>   RE   RJ   R]   Rl   R'   t   ImpExpressionRI   t   AllExpression(   R   t	   first_drst
   second_drsRn   RV   Ra   RX   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR'     s    		
1 (   R   R   R'   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR"     s   R$   c           B   s   e  Z d    Z RS(   c         C   s   t  |  i i   |  i i    S(   N(   t   IffExpressionR=   R'   R>   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR'   .  s    (   R   R   R'   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR$   ,  s   R<   c           B   s   e  Z d    Z e d  Z RS(   c         C   s   t  |  i i   |  i i    S(   N(   t   EqualityExpressionR=   R'   R>   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR'   3  s    c         C   s3   | o$ |  i  i t  |  i i t  Sn g  Sd S(   s#   @see: AbstractExpression.get_refs()N(   R=   R4   RN   R>   (   R   R3   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR4   6  s    $(   R   R   R'   RH   R4   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR<   1  s   	R1   c           B   sP   e  Z d  Z e d  Z d   Z e d  Z e i d  Z	 d   Z
 d   Z RS(   s   DRS of the form '(DRS + DRS)'c         C   s  |  i  } |  i } t | t  om t | t  o] | t | i t   t | i t   @j o. | i | | t  } | i | | t  } nCt | t  oI | | i j o9 | o. | i | | |  } | i | | |  } qn t | t  oI | | i j o9 | o. | i | | |  } | i | | |  } qn xc t |  i t   | i	   @D]B } t
 t |   } | i | | t  } | i | | t  } qaW| i | | |  } | i | | |  } |  i | |  S(   s_   Replace all instances of variable v with expression E in self,
        where v is free in self.(   R=   R>   R   R   RP   R4   RN   RM   RI   RQ   R@   RR   R-   (   R   R7   RS   RT   R=   R>   RX   t   v(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRM   ?  s,    		 /    c         C   s   |  i  i   } |  i i   } t | t  o t | t  o xW t | i t   t | i t   @D]- } t t	 |   } | i
 | | t  } qg Wt | i | i | i | i  Sn |  i | |  Sd  S(   N(   R=   R&   R>   R   R   RP   R4   RN   R@   RR   RM   RI   RJ   R-   (   R   R=   R>   RX   RY   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR&   c  s     ) %c         C   s    |  i  i |  |  i i |  S(   s#   @see: AbstractExpression.get_refs()(   R=   R4   R>   (   R   R3   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR4   r  s    c         C   s   t  i S(   N(   R   R   (   R   Rz   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   getOpv  s    c         C   s   t  | t  o |  i   } | i   } t |  t |  j oq | } xA t | |  D]0 \ } } |  i |  } | i | | t  } qW W|  i | i j o |  i	 | i	 j Sq n t
 S(   s}   Defines equality modulo alphabetic variance.
        If we are comparing \x.M  and \y.N, then check equality of M and N[x/y].(   R   R1   R4   Ro   Rp   RA   RM   RN   R=   R>   RH   (   R   R   t	   self_refst
   other_refsRq   Rr   Rs   Rt   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRu   y  s     c         C   s   t  |  i i   |  i i    S(   N(   Rl   R=   R'   R>   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR'     s    (   R   R   RD   RH   RM   R&   R4   R   R|   R   Ru   R'   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR1   =  s   $		R   c           B   s   e  Z d    Z e d  Z RS(   c         C   s   t  |  i i   |  i i    S(   N(   t   ApplicationExpressionR5   R'   R9   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR'     s    c         C   s3   | o$ |  i  i t  |  i i t  Sn g  Sd S(   s#   @see: AbstractExpression.get_refs()N(   R5   R4   RN   R9   (   R   R3   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR4     s    $(   R   R   R'   RH   R4   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s   	RB   c           B   s   e  Z d  Z d Z d Z e e d  Z d   Z e e d  Z	 d   Z
 d   Z d   Z d	 d	 d
  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   i   i
   i   c   
      C   s#  t  i |  _ d } | p t   } | i d  t d d d d  } | o t | d d d d } | | _ | |  _	 |  i
 | |  i |  i  \ } } t | |  i d	  } | |  i }	 t | d | d |	 } n t | d d
 d d
 } | i   | | _ n | |  _	 | |  _ | |  _ d S(   s  
        @param drs: C{AbstractDrs}, The DRS to be drawn
        @param size_canvas: C{boolean}, True if the canvas size should be the exact size of the DRS
        @param canvas: C{Canvas} The canvas on which to draw the DRS.  If none is given, create a new canvas. 
        t   DRTt   familyt	   helveticat   sizei   t   widthi    t   heightid   i,  N(   R   R|   Rz   RE   R   t   titleR   R   t   fontt   canvast   _visitt
   OUTERSPACEt   TOPSPACEt   maxt   packt   drst   master(
   R   R   t   size_canvasR   R   R   t   rightt   bottomR   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRK     s(    			!
		c         C   s   |  i  i i d  S(   s    Get the height of a line of textt	   linespace(   R   R   t   metrics(   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   _get_text_height  s    c         C   s\   |  i  |  i |  i | |  |  i o t   o |  i i   n |  i |  i | |  Sd S(   s   Draw the DRSN(   t   _handleR   t   _draw_commandR   R   t   mainloopR   (   R   t   xt   y(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRC     s    c         C   s   |  i  | |  i | |  S(   s)  
        Return the bottom-rightmost point without actually drawing the item
        
        @param expression: the item to visit
        @param x: the top of the current drawing area
        @param y: the left side of the current drawing area
        @return: the bottom-rightmost point
        (   R   t   _visit_command(   R   RS   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s    	c      	   C   s   t  | t  o/ |  i i | | d d d |  i i d | nn t  | t  o] | \ } } |  i i | | | |  | |  i   |  i d } |  i i	 | | | |  n |  i
 | | |  S(   s	  
        Draw the given item at the given location
        
        @param item: the item to draw
        @param x: the top of the current drawing area
        @param y: the left side of the current drawing area
        @return: the bottom-rightmost point 
        t   anchort   nwR   t   texti   (   R   Rx   R   t   create_textR   t   tuplet   create_rectangleR   t   BUFFERt   create_lineR   (   R   t   itemR   R   R   R   t   horiz_line_y(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s    	/c         C   sW   t  | t  o+ | |  i i i |  | |  i   f Sn t  | t  o | Sn d S(   s#  
        Return the bottom-rightmost point without actually drawing the item
        
        @param item: the item to visit
        @param x: the top of the current drawing area
        @param y: the left side of the current drawing area
        @return: the bottom-rightmost point
        N(   R   Rx   R   R   t   measureR   R   (   R   R   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s    	+i    c         C   sn  | |  i  j oB y( | i | } | i | } | | f SWqR t j
 o qR Xn t | t  o |  i } n t | t  o |  i } n t | t	  o |  i
 } n t | t  o |  i } nj t | t  o |  i } nM t | t  o |  i } n0 t | t i  o |  i } n t | i i  | | | | |  \ } } | | | _ | | | _ | | f S(   s1  
        @param expression: the expression to handle
        @param command: the function to apply, either _draw_command or _visit_command
        @param x: the top of the current drawing area
        @param y: the left side of the current drawing area
        @return: the bottom-rightmost point
        (   R   t   _drawing_widtht   _drawing_heightR,   R   R6   t   _handle_VariableExpressionR   t   _handle_DRSR   t   _handle_NegatedExpressionR   t   _handle_LambdaExpressiont   BinaryExpressiont   _handle_BinaryExpressionR   t   _handle_ApplicationExpressiont   RAt   PossibleAntecedentsRk   R-   R   (   R   RS   t   commandR   R   R   R   t   factory(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s4    	c         C   s   | | i  |  i  | |  S(   N(   Rx   Rz   (   R   RS   R   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR   %  s    c         C   s   |  i  t i |  i | |  d } |  i | i | | |  \ } } | t i |  i | |  i | | | |  i     | | f S(   Ni    (   R   R   t   NOTRz   R   R   t   _get_centered_topR   (   R   RS   R   R   R   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR   (  s    #!3c         C   s   | |  i  } | |  i  } | i o7 d i g  } | i D] } | t |  q8 ~  }	 n d }	 | |	 | |  \ }
 } | |  i  d 7} | i oR xf | i D]@ } |  i | | | |  \ } } t |
 |  }
 | |  i  7} q Wn | |  i   |  i  7} |
 |  i  7}
 | |
 | f | |  S(   Nt    s        i   (   R   RI   Ry   Rx   RJ   R   R   R   (   R   RS   R   R   R   t   leftR   RV   RX   RI   t	   max_rightRW   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR   4  s     
7

 c      	   C   s  | i    \ } } t | t  p | i } | i g } n |  i | | |  d } t | g g  } | D]  }	 | |  i |	 | |  d qh ~  }
 |
 | } |  i | | | i  } |  i	 | | | |  d } |  i | | |  i
    } | t i | |  d } x t |  D]x \ } }	 |  i | | |	 i  } |  i	 |	 | | |  d } | d t |  j  o! | t i d | |  d } qqW| t i | |  d } | |
 f S(   Ni   i    R   (   t   uncurryR   R6   R5   R9   R   R   R   R   R   R   R   t   OPENt	   enumerateRo   t   COMMAt   CLOSE(   R   RS   R   R   R   R5   t   argst   function_bottomRV   t   argt
   max_bottomt   line_heightt   function_drawing_topR   t   centred_string_topRU   t   arg_drawing_top(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR   M  s&    	A
 %c         C   s   t  i |  i t | i  t  i |  i } |  i | | |  d } |  i | i | | |  \ } } | | | |  i	 | | | |  i
     | | f S(   Ni    (   R   t   LAMBDARz   Rx   R7   t   DOTR   R   R   R   R   (   R   RS   R   R   R   R`   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR   p  s
    +!)c         C   sE  |  i  | i d d  d } |  i  | i d d  d } t | |  } |  i | | |  i    } | t i | |  d }	 | i i } |  i	 | i | |	 |  i | | |   \ }	 }
 | d | i
 |  i  |	 |  d }	 | i i } |  i	 | i | |	 |  i | | |   \ }	 } | t i |	 |  d }	 |	 t |
 |  f S(   Ni    i   s    %s (   R   R=   R>   R   R   R   R   R   R   R   R   Rz   R   (   R   RS   R   R   R   t   first_heightt   second_heightR   R   R   t   first_bottomt   second_bottom(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR   }  s    0&0c         C   s   | | | d S(   s   Get the y-coordinate of the point that a figure should start at if
        its height is 'item_height' and it needs to be centered in an area that
        starts at 'top' and is 'full_height' tall.i   (    (   R   t   topt   full_heightt   item_height(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s    (   R   R   R   R   R   RN   RE   RK   R   RC   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRB     s"   "					,				#		t	   DrtParserc           B   sz   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z RS(   s$   A lambda calculus expression parser.c         C   s   t  i |   d  S(   N(   t   LogicParserRK   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRK     s    c         C   s   t  i S(   s#   This method exists to be overridden(   R   R   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   get_all_symbols  s    c         C   s   | t  i j S(   N(   R   R   (   R   t   tok(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt
   isvariable  s    c         C   s   | t  i j o |  i   Sn | t  i j o |  i |  Sn | t  i j o8 |  i d  t  i j o |  i   Sq |  i	 |  Sn_ | i
   t  i j o' |  i |  i   t  i  |  i   Sn" |  i |  o |  i |  Sn d S(   sh   This method is intended to be overridden for logics that 
        use different operators or expressionsi    N(   R   R   t   handle_negationR   t   handle_lambdaR   t   tokenR   t
   handle_DRSt   handle_opent   upperR   t   assertTokenR   t   handle_variable(   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   handle  s    c         C   s
   t  |  S(   N(   R   (   R   RS   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   make_NegatedExpression  s    c         C   s[  |  i  |  i   t i  g  } x^ |  i d  t i j oD |  i d  t i j o |  i   q" | i t |  i     q" W|  i   |  i d  t i j o |  i   n |  i  |  i   t i  g  } xX |  i d  t i j o> |  i d  t i j o |  i   q | i |  i    q W|  i   |  i  |  i   t i	  t
 | |  S(   Ni    (   R   R   R   R   R   R   t   appendt   Variablet   parse_ExpressionR   R   (   R   RI   RJ   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s$    

c         C   s   t  | |  S(   sl   This method serves as a hook for other logic parsers that
        have different equality expression classes(   R<   (   R   R=   R>   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR?     s    c         C   sh   | t  i j o t SnM | t  i j o t Sn5 | t  i j o t Sn | t  i j o t Sn d Sd S(   sb   This method serves as a hook for other logic parsers that
        have different boolean operatorsN(
   R   R   R1   t   ORR    t   IMPR"   t   IFFR$   RE   (   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   get_BooleanExpression_factory  s    c         C   s   | | |  S(   N(    (   R   R   R=   R>   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   make_BooleanExpression  s    c         C   s   t  | |  S(   N(   R   (   R   R5   R9   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   make_ApplicationExpression  s    c         C   s   t  t |   S(   N(   R@   R   (   R   R8   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRA     s    c         C   s   t  | |  S(   N(   R   (   R   R`   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   make_LambdaExpression  s    (   R   R   RD   RK   R   R   R   R   R   R?   R   R   R   RA   R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s   											c          C   sH  d d d GHt    }  |  i d  GH|  i d  GH|  i d  GH|  i d  GH|  i d  GH|  i d	  GH|  i d
  GH|  i d  GH|  i d  GHd d d GH|  i d  i   GHd d d GH|  i d  } | GH| i t d   } | GH| | j GHd d d GH|  i d  i   GH|  i d  i   GH|  i d  i   GHd  S(   Nt   =i   s
   TEST PARSEs   ([x,y],[sees(x,y)])s   ([x],[man(x), walks(x)])s   \x.\y.([],[sees(x,y)])s   \x.([],[walks(x)])(john)s$   (([x],[walks(x)]) + ([y],[runs(y)]))s#   (([],[walks(x)]) -> ([],[runs(x)]))s   ([x],[PRO(x), sees(John,x)])s    ([x],[man(x), -([],[walks(x)])])s*   ([],[(([x],[man(x)]) -> ([],[walks(x)]))])s   Test toFol()s4   Test alpha conversion and lambda expression equalitys   \x.([],[P(x)])t   zs   Test resolve_anaphora()s,   ([x,y,z],[dog(x), cat(y), walks(z), PRO(z)])s3   ([],[(([x],[dog(x)]) -> ([y],[walks(y), PRO(y)]))])s   (([x,y],[]) + ([],[PRO(x)]))s   ====================s   ====================s   ====================s   ====================s   ====================s   ====================s   ====================s   ====================(   R   t   parseR'   R   R   t   resolve_anaphora(   t   parsert   e1t   e2(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   demo  s.    	c          C   sX   d d d d d d d d d	 d
 d g }  x* |  D]" } t    i |  } | i   q. Wd  S(   NR   s   ([],[])s   ([x],[])s   ([x],[man(x)])s   ([x,y],[sees(x,y)])s   ([x],[man(x), walks(x)])s   \x.([],[man(x), walks(x)])s   \x y.([],[sees(x,y)])s)   ([],[(([],[walks(x)]) + ([],[runs(x)]))])s    ([x],[man(x), -([],[walks(x)])])s*   ([],[(([x],[man(x)]) -> ([],[walks(x)]))])(   R   R   RC   (   t   expressionsRi   t   d(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt	   test_draw  s    	 t   __main__(4   R^   t   logict   drt_resolve_anaphoraR   t   TkinterR   R   t   tkFontR   t   nltkR   t   ImportErrorR   R   t   objectR   t
   ExpressionR   R@   t   AbstractVariableExpressionR6   t   IndividualVariableExpressionR:   t   FunctionVariableExpressionR   t   EventVariableExpressionR   t   ConstantExpressionR   R   R   R   R   t   BooleanExpressionR   R   R    R   R"   R   R$   R   R<   R1   R   R   RB   R   R   R  R  R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pys   <module>   sb   
G`					N	 a		