
8dhKc           @   s  d  d k  Z  d  d k TyD 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 f d     YZ d   Z d e e f d     YZ d e e f d     YZ d e e f d     YZ d e e f d     YZ d e e f d     YZ d e e f d     YZ d e e f d     YZ d e e f d     YZ  d e  e! f d     YZ" d  e  e# f d!     YZ$ d" e  e% f d#     YZ& d$ e e' f d%     YZ( d& e  f d'     YZ) d( e e* f d)     YZ+ d* e, e e f d+     YZ- d, e. f d-     YZ/ g  d.  Z0 d/ e f d0     YZ1 d1 e2 f d2     YZ3 d3   Z4 d4   Z5 e6 d5 j o e4   n d S(6   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 d   Z RS(   s`   
    This is the base 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__2   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__5   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__9   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.
        
        @param other: an C{AbstractDrs} to check equality against
        @param prover: a C{nltk.inference.api.Prover}
        (   R   R   R   t   simplifyt   folt	   tp_equals(   R   R   t   provert   f1t   f2(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR(   A   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_typeO   s    c         C   s   t     d  S(   N(   R   (   R   t	   signature(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt	   typecheckT   s    c         C   s   t  |  |  S(   N(   t   ConcatenationDRS(   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   __add__W   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_refsZ   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_functionb   s    c         C   s   t  | |  S(   N(   t   DrtEqualityExpression(   R   t   firstt   second(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   make_EqualityExpressioni   s    c         C   s
   t  |  S(   N(   t   DrtVariableExpression(   R   R7   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   make_VariableExpressionl   s    c         C   s
   t  |   S(   N(   t   resolve_anaphora(   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRB   o   s    c         C   s   t  |   i   d  S(   N(   t	   DrsDrawert   draw(   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRD   r   s    (   R   R   t   __doc__R   R   R   R!   R#   R%   t   NoneR(   R.   t   propertyt   typeR0   R2   t   FalseR4   R;   R?   RA   RB   RD   (    (    (    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   RJ   RK   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   __init__x   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(   RJ   t   indexR   R7   RK   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.pyRN      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_RK   t	   variablesRQ   RJ   (   R   RW   t   ct
   conds_vars(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRa      s    	3c         C   sP   t  t i g  } |  i D] } | | i |  q ~ t    } | t |  i  S(   s   @see: Expression.free()(   R^   R_   R`   RK   RR   RQ   RJ   (   R   t   indvar_onlyRW   Rb   t
   conds_free(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRR      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   addRK   R4   RO   RJ   (   R   R3   RW   Rb   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^   RJ   RK   (   R   R5   t
   combinatort   defaultRW   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   RJ   RK   R&   (   R   RW   RX   (    (    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(   RK   t	   ExceptionR^   t   AndExpressionR'   RJ   t   ExistsExpression(   R   RW   Rb   t   accumRY   (    (    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   lenRJ   t   zipRA   RN   RO   RK   RI   (   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   joinRJ   RK   (   R   t   syntaxRW   t   rR\   Rb   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRy      s    3(   R   R   RE   RL   RI   RN   Ra   RO   RR   R4   Rk   R&   R'   Rv   R   t   NLTKRy   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR   v   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'   RI   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 e 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    c         C   s   |  i  i |  S(   s#   @see: AbstractExpression.get_refs()(   R   R4   (   R   R3   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR4      s    (   R   R   R'   RI   R4   (    (    (    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   RN   R7   R@   RO   (   R   RZ   (    (    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   RO   R>   (   R   R3   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR4     s    $(   R   R   RI   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>   RF   RK   R^   Rm   R'   t   ImpExpressionRJ   t   AllExpression(   R   t	   first_drst
   second_drsRo   RW   Rb   RY   (    (    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'   2  s    c         C   s3   | o$ |  i  i t  |  i i t  Sn g  Sd S(   s#   @see: AbstractExpression.get_refs()N(   R=   R4   RO   R>   (   R   R3   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR4   5  s    $(   R   R   R'   RI   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   RQ   R4   RO   RN   RJ   RR   R@   RS   R-   (   R   R7   RT   RU   R=   R>   RY   t   v(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRN   >  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   RQ   R4   RO   R@   RS   RN   RJ   RK   R-   (   R   R=   R>   RY   RZ   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR&   b  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   q  s    c         C   s   t  i S(   N(   R   R   (   R   R{   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   getOpu  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   Rp   Rq   RA   RN   RO   R=   R>   RI   (   R   R   t	   self_refst
   other_refsRr   Rs   Rt   Ru   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRv   x  s     c         C   s   t  |  i i   |  i i    S(   N(   Rm   R=   R'   R>   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR'     s    (   R   R   RE   RI   RN   R&   R4   R   R}   R   Rv   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   RO   R9   (   R   R3   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR4     s    $(   R   R   R'   RI   R4   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s   	t   PossibleAntecedentsc           B   s/   e  Z e d   Z e d  Z e i d  Z RS(   c         C   s
   t  |   S(   s   Set of free variables.(   RQ   (   R   Rd   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRR     s    c         C   sI   t    } x9 |  D]1 } | | j o |  i |  q |  i |  q W| S(   s_   Replace all instances of variable v with expression E in self,
        where v is free in self.(   R   t   append(   R   R7   RT   RU   t   resultt   item(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRN     s    	 c         C   s   d d i  t t |    d S(   NR	   Rw   R
   (   Rz   t   mapRy   (   R   R{   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRy     s    (	   R   R   RO   RR   RI   RN   R   R}   Ry   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s   t   AnaphoraResolutionExceptionc           B   s   e  Z RS(    (   R   R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s   c         C   s"  t  |  t  o	|  i   o t   } xl | D]d } x[ | i   D]M } |  i |  } | i |  i i j o" | |  i j o | i |  q@ q@ Wq- Wt	 |  d j o | d } n | } |  i
 |  i |  Sqt |  i | |  g  } t |  i | |  g  } |  i | |  Snt  |  t  o g  }	 x |  i D] }
 t |
 | |  g  } t  | t  oq t  | i t  o" | i } | i | _ | | _ n t  | i t  o% | i p t d | i   qqn |	 i |  q9W|  i |  i |	  Sn!t  |  t  o |  Sn	t  |  t  o$ |  i t |  i | |  g   Sn t  |  t  o@ |  i t |  i | |  g  t |  i | |  |  i g   Sn t  |  t  o: |  i t |  i | |  g  t |  i | |  g   Sn; t  |  t  o* |  i |  i t |  i | |  g   Sn d  S(   Ni   i    s+   Variable '%s' does not resolve to anything.(   R   R   R;   R   R4   RA   R-   R9   R   Rp   R?   RB   R5   R   RK   R   R=   R>   R   RJ   t   AbstractVariableExpressionR   R   R   t   BinaryExpressionR   R7   (   RT   t   trailt   possible_antecedentst   ancestorRY   t   refext
   resolutiont
   r_functiont
   r_argumentt   r_condsRX   t   r_condt   temp(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRB     s\    	  
 	
$$RC   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}   R{   RF   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.pyRL     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.pyRD     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   RT   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   Ry   R   t   create_textR   t   tuplet   create_rectangleR   t   BUFFERt   create_lineR   (   R   R   R   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   Ry   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_LambdaExpressionR   t   _handle_BinaryExpressionR   t   _handle_ApplicationExpressiont   RAR   Rl   R-   R   (   R   RT   t   commandR   R   R   R   t   factory(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR   N  s4    	c         C   s   | | i  |  i  | |  S(   N(   Ry   R{   (   R   RT   R   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR   z  s    c         C   s   |  i  t i |  i | |  d } |  i | i | | |  \ } } | t i |  i | |  i | | | |  i     | | f S(   Ni    (   R   R   t   NOTR{   R   R   t   _get_centered_topR   (   R   RT   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   RJ   Rz   Ry   RK   R   R   R   (   R   RT   R   R   R   t   leftR   RW   RY   RJ   t	   max_rightRX   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     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	   enumerateRp   t   COMMAt   CLOSE(   R   RT   R   R   R   R5   t   argst   function_bottomRW   t   argt
   max_bottomt   line_heightt   function_drawing_topR   t   centred_string_topRV   t   arg_drawing_top(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     s&    	A
 %c         C   s   t  i |  i t | i  t  i |  i } |  i | | |  d } |  i | i | | |  \ } } | | | |  i	 | | | |  i
     | | f S(   Ni    (   R   t   LAMBDAR{   Ry   R7   t   DOTR   R   R   R   R   (   R   RT   R   R   R   Ra   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyR     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   R{   R   (   R   RT   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   RO   RF   RL   R   RD   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRC     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   LogicParserRL   (   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRL     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   RT   (    (    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   R   t   Variablet   parse_ExpressionR   R   (   R   RJ   RK   (    (    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?   4  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$   RF   (   R   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   get_BooleanExpression_factory9  s    c         C   s   | | |  S(   N(    (   R   R   R=   R>   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   make_BooleanExpressionG  s    c         C   s   t  | |  S(   N(   R   (   R   R5   R9   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   make_ApplicationExpressionJ  s    c         C   s   t  t |   S(   N(   R@   R  (   R   R8   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyRA   M  s    c         C   s   t  | |  S(   N(   R   (   R   Ra   R   (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   make_LambdaExpressionP  s    (   R   R   RE   RL   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 GHt |  i d   GHt |  i d   GHt |  i d   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 fol()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  RB   (   t   parsert   e1t   e2(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt   demoT  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  RD   (   t   expressionsRj   t   d(    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pyt	   test_drawq  s    	 t   __main__(7   R_   t   logict   TkinterR   R   t   tkFontR   t	   nltk.utilR   t   ImportErrorR   R   t   objectR   t
   ExpressionR   R@   R   R6   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   t   listR   Rl   R   RB   RC   R   R   R  R  R   (    (    (    s"   /p/zhu/06/nlp/nltk/nltk/sem/drt.pys   <module>	   sH   
P`	N? a		