³ò
bKc           @   sd  d  d k  Z  d  d k l Z l Z d  d k Z d  d k l Z d  d k l Z l	 Z	 d  d k
 l Z d  d k l Z d  d k l Z d  d k l Z d  d	 k l Z l Z d  d
 k l Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z e d „ Z e d „ Z d d „ Z  d „  Z! e" d j o e! ƒ  n d S(   iÿÿÿÿN(   t   and_t   add(   t   show_cfg(   t   root_semrept
   Expression(   t   MaceCommand(   t   Prover9Command(   t   RegexpTagger(   t
   MaltParser(   t   resolve_anaphorat   AnaphoraResolutionException(   t   DrtGluet   ReadingCommandc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   t  ƒ  ‚ d S(   sV   
        @param sentence: the sentence to read
        @type sentence: C{str}
        N(   t   NotImplementedError(   t   selft   sentence(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   parse_to_readings>   s    c         C   s   | S(   sT  
        This method should be used to handle dependencies between readings such
        as resolving anaphora.
        
        @param sentence_readings: readings to process
        @type sentence_readings: C{list} of C{Expression}
        @return: the list of readings after processing
        @rtype: C{list} of C{Expression}   
        (    (   R   t   sentence_readings(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   process_threadE   s    
c         C   s   t  ƒ  ‚ d S(   s«   
        @param readings: readings to combine
        @type readings: C{list} of C{Expression}
        @return: one combined reading
        @rtype: C{Expression}
        N(   R   (   R   t   readings(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   combine_readingsQ   s    (   t   __name__t
   __module__R   R   R   (    (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   =   s   		t   CfgReadingCommandc           B   s&   e  Z e d  „ Z d „  Z d „  Z RS(   c         C   s?   | d j o d |  _ n
 | |  _ t i i |  i ƒ |  _ d S(   sn   
        @parameter gramfile: name of file where grammar can be loaded
        @type gramfile: C{str}
        s%   grammars/book_grammars/discourse.fcfgN(   t   Nonet	   _gramfilet   nltkt   parset   load_parsert   _parser(   R   t   gramfile(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   __init__\   s    	c         C   sC   | i  ƒ  } |  i i | ƒ } g  } | D] } | t | ƒ q) ~ S(   s(   @see: ReadingCommand.parse_to_readings()(   t   splitR   t   nbest_parseR   (   R   R   t   tokenst   treest   _[1]t   tree(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   g   s    c         C   s   t  t | ƒ S(   s'   @see: ReadingCommand.combine_readings()(   t   reduceR    (   R   R   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   m   s    (   R   R   R   R   R   R   (    (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   [   s   	t   DrtGlueReadingCommandc           B   s5   e  Z e e e d  „ Z d „  Z d „  Z d „  Z RS(   c         C   s9   | d j o
 d } n t d | d | d | ƒ |  _ d S(   s¿   
        @param semtype_file: name of file where grammar can be loaded
        @param remove_duplicates: should duplicates be removed?
        @param depparser: the dependency parser
        s   drt_glue.semtypet   semtype_filet   remove_duplicatest	   depparserN(   R   R   t   _glue(   R   R(   R)   R*   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   s   s
    
c         C   s   |  i  i | ƒ S(   s(   @see: ReadingCommand.parse_to_readings()(   R+   t   parse_to_meaning(   R   R   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   €   s    c         C   s2   y |  i  | ƒ g SWn t j
 o g  Sn Xd S(   s%   @see: ReadingCommand.process_thread()N(   R   R
   (   R   R   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   „   s    c         C   s   t  t | ƒ } t | i ƒ  ƒ S(   s'   @see: ReadingCommand.combine_readings()(   R&   R   R	   t   simplify(   R   R   t   thread_reading(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   ‹   s    (   R   R   R   t   FalseR   R   R   R   (    (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR'   r   s
   		t   DiscourseTesterc           B   sé   e  Z d  Z e e d „ Z d „  Z e e d „ Z e d „ Z	 d „  Z
 d „  Z d „  Z d „  Z e d	 „ Z e e d
 „ Z e e e e e d „ Z e d „ Z e e d „ Z e e e d „ Z e d „ Z d „  Z e d „  ƒ Z RS(   s3   
    Check properties of an ongoing discourse.
    c         C   sâ   | |  _  t g  } t | ƒ D] \ } } | d | | f q ~ ƒ |  _ d |  _ h  |  _ | d j o t ƒ  |  _ n
 | |  _ h  |  _	 h  |  _
 | d j	 o5 x% | D] } t | t ƒ p t ‚ q§ W| |  _ n
 g  |  _ d S(   s  
        Initialize a C{DiscourseTester}.
        
        @parameter input: the discourse sentences
        @type input: C{list} of C{str}
        @parameter background: Formulas which express background assumptions
        @type background: C{list} of L{logic.Expression}.
        s   s%sN(   t   _inputt   dictt	   enumeratet
   _sentencesR   t   _modelst	   _readingsR   t   _reading_commandt   _threadst   _filtered_threadst
   isinstanceR   t   AssertionErrort   _background(   R   t   inputt   reading_commandt
   backgroundR$   t   it   sentt   e(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   •   s    		@					 c         C   s:   x3 t  |  i i ƒ  ƒ D] } d | |  i | f GHq Wd S(   sI   
        Display the list of sentences in the current discourse.
        s   %s: %sN(   t   sortedR4   t   keys(   R   t   id(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt	   sentences³   s     c      	   C   sX  | oÏ |  i  d t ƒ x¼ t |  i i ƒ  ƒ D]¡ } g  } |  i | ƒ D] \ } } | | qG ~ } | |  i 7} x[ |  i | ƒ D]J }	 t d |	 d | ƒ }
 |
 i	 ƒ  o" d | t
 |	 ƒ f GHd | GHq€ q€ Wq- Wn |  i i | ƒ t g  } t |  i ƒ D] \ } } | d | | f qý ~ ƒ |  _ | o$ |  i  d t ƒ |  i d t ƒ n d S(	   sã  
        Add a sentence to the current discourse.
        
        Updates C{self._input} and C{self._sentences}.
        @parameter sentence: An input sentence
        @type sentence: C{str}
        @parameter informchk: if C{True}, check that the result of adding the sentence is thread-informative. Updates C{self._readings}.
        @parameter consistchk: if C{True}, check that the result of adding the sentence is thread-consistent. Updates C{self._readings}.
        
        t   verboset   goalt   assumptionss!   Sentence '%s' under reading '%s':s'   Not informative relative to thread '%s's   s%st   showN(   R   R/   RC   R8   RD   t   expand_threadsR<   t   _get_readingsR   t   provet   strR1   t   appendR2   R3   R4   t   models(   R   R   t	   informchkt
   consistchkt   tidR$   t   ridt   readingRI   t   sent_readingt   tpt   _[2]R@   RA   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   add_sentenceº   s"     0 Cc      	   C   s²   y |  i  i | ƒ Wn* t j
 o d | GH|  i ƒ  d Sn Xt g  } t |  i  ƒ D] \ } } | d | | f qX ~ ƒ |  _ |  i d t	 ƒ | o d GH|  i ƒ  n d S(   s2  
        Remove a sentence from the current discourse.
        
        Updates C{self._input}, C{self._sentences} and C{self._readings}.
        @parameter sentence: An input sentence
        @type sentence: C{str}
        @parameter verbose: If C{True},  report on the updated list of sentences.
        sJ   Retraction failed. The sentence '%s' is not part of the current discourse:s   s%sRG   s   Current sentences are N(
   R1   t   removet
   ValueErrorRF   R   R2   R3   R4   R   R/   (   R   R   RG   R$   R@   RA   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   retract_sentenceÙ   s    		
	Cc         C   s   t  |  i i ƒ d S(   sJ   
        Print out the grammar in use for parsing input sentences
        N(   R   R7   R   (   R   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   grammarî   s    c         C   s   |  i  i | ƒ S(   s}   
        Build a list of semantic readings for a sentence.
        
        @rtype: C{list} of  L{logic.Expression}.
        (   R7   R   (   R   R   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyRL   ø   s    c         C   sŒ   h  |  _  x| |  i i ƒ  D]k \ } } |  i | ƒ } t g  } t | ƒ D]) \ } } | d | | f | i ƒ  f qH ~ ƒ |  i  | <q Wd S(   sT   
        Use C{self._sentences} to construct a value for C{self._readings}.
        s   %s-r%sN(   R6   R4   t	   iteritemsRL   R2   R3   R-   (   R   t   sidR   R   R$   RT   RU   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   _construct_readings   s    	 
c         C   sñ   g  g } xB t  |  i i ƒ  ƒ D]+ } |  i | t  |  i | i ƒ  ƒ ƒ } q Wt g  } t | ƒ D] \ } } | d | | f qb ~ ƒ |  _ h  |  _ |  i |  i ƒ } xA |  i i	 ƒ  D]0 \ } } | t
 f | j o | |  i | <q¹ q¹ Wd S(   s¦   
        Use C{self._readings} to construct a value for C{self._threads}
        and use the model builder to construct a value for C{self._filtered_threads}
        s   d%sN(   RC   R6   RD   t   multiplyR2   R3   R8   R9   t   _check_consistencyt   itemst   True(   R   t   thread_listR_   R$   RS   t   threadt   consistency_checked(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   _construct_threads  s    	 )@	 c         C   sË   | d j	 oQ d | GHx® g  } |  i | ƒ D] } | t | ƒ q- ~ D] } d | GHqG Wnj xf t |  i i ƒ  ƒ D]O } Hd | GHHx; t |  i | ƒ D]& } |  i | | } d | | f GHq™ Wqt Wd S(   sS   
        Print out the readings for  the discourse (or a single sentence).
        s%   The sentence '%s' has these readings:s       %ss   %s readings:s   %s: %sN(   R   RL   RN   RC   R6   RD   (   R   R   R$   RU   t   rR_   RT   t   lf(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   _show_readings  s    	1  	 c   
   	   C   sé   | o |  i  } n
 |  i } xÅ t | i ƒ  ƒ D]± } | o† g  } |  i | D]% } | |  i | i d ƒ d | qO ~ } y d |  i i | ƒ } WqÉ t j
 o }	 d |	 i	 i
 } qÉ Xn d } d | G|  i | G| GHq0 Wd S(   sV   
        Print out the value of C{self._threads} or C{self._filtered_hreads} 
        t   -i    s   : %ss   : INVALID: %st    s   %s:N(   R9   R8   RC   RD   R6   R    R7   R   t	   Exceptiont	   __class__R   (
   R   t   filtert   show_thread_readingst   threadsRS   R$   RT   R   R.   RB   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   _show_threads/  s    	 9c         C   sl   |  i  ƒ  |  i ƒ  | p | o
 t } n | o5 | p |  i d | ƒ qh |  i d | d | ƒ n d S(   ss  
        Construct and show the readings of the discourse (or of a single sentence).
        
        @parameter sentence: test just this sentence
        @type sentence: C{str}
        @parameter threaded: if C{True}, print out each thread ID and the corresponding thread.
        @parameter filter: if C{True}, only print out consistent thread IDs and threads.
        R   Rp   Rq   N(   R`   Rh   Rd   Rk   Rs   (   R   R   t   threadedRG   Rp   Rq   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   F  s    



c         C   sh   | t  j o |  i } n g  } | | D]8 } | i d ƒ d  D] } | | |  i | | f q@ q) ~ S(   sÝ  
        Given a thread ID, find the list of L{logic.Expression}s corresponding to the reading IDs in that thread.
        
        @parameter thread_id: thread ID
        @type thread_id: C{str}
        @parameter threads: a mapping from thread IDs to lists of reading IDs
        @type threads: C{dict}
        @return: A list of pairs (C{rid}, I{reading}) where I{reading} is the L{logic.Expression} associated with a reading ID 
        @rtype: C{list} of C{tuple}
        Rl   i   (   R   R8   R    R6   (   R   t	   thread_idRr   R$   RT   R_   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyRK   ^  s    c         C   s5  g  } x(t  | i ƒ  ƒ D]} g  } |  i | d | ƒD] \ } } | | q9 ~ }	 |  i i |	 ƒ }	 |	 o2 |	 |  i 7}	 t d  |	 d d ƒ}
 |
 i ƒ  } n t	 } | i
 | | f ƒ | om t d ƒ d | GHt d ƒ | o$ x |	 D] } | GHqë Wt d ƒ n | o |
 i d d ƒ GHq-d GHq q W| S(	   NRr   t
   max_modelsi   iP   s   Model for Discourse Thread %st   formatt   cookeds   No model found!
(   RC   RD   RK   R7   R   R<   R   R   t   build_modelR/   RO   t   spacert   model(   R   Rr   RJ   RG   t   resultsRS   R$   RT   RU   RI   t   mbt
   modelfoundt   a(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyRb   r  s0     6
	
 	c      	   C   s|  |  i  ƒ  |  i ƒ  | d j o |  i } n h  |  i | | <} x1|  i | d | d | ƒD]\ } } g  } | | D] } | | q{ ~ }	 | pp d | |	 f GHxY g  }
 |  i | ƒ D] \ } } |
 | t | ƒ f q¾ ~
 D] \ } } d | | f GHqä WHq` d | |	 f GHxY g  } |  i | ƒ D] \ } } | | t | ƒ f q.~ D] \ } } d | | f GHqTWHq` Wd S(   sí   
        Call Mace4 to build a model for each current discourse thread.
        
        @parameter thread_id: thread ID
        @type thread_id: C{str}
        @parameter show: If C{True}, display the model that has been found.
        RJ   RG   s   Inconsistent discourse: %s %s:s
       %s: %ss   Consistent discourse: %s %s:N(   R`   Rh   R   R8   Rb   RK   RN   (   R   Ru   RJ   RG   Rr   RS   R~   R$   RT   t   idlistRX   RU   t   _[3](    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyRP     s&    

 %= = c         C   sp   xU t  | ƒ D]G \ } } t | t ƒ p t ‚ | o d | GHn |  i i | ƒ q W|  i ƒ  |  i ƒ  d S(   sK  
        Add a list of background assumptions for reasoning about the discourse.
        
        When called,  this method also updates the discourse model's set of readings and threads.
        @parameter background: Formulas which contain background information
        @type background: C{list} of L{logic.Expression}.
        s"   Adding assumption %s to backgroundN(   R3   R:   R   R;   R<   RO   R`   Rh   (   R   R?   RG   t   countRB   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   add_backgroundª  s     
c         C   s#   x |  i  D] } t | ƒ GHq
 Wd S(   s:   
        Show the current background assumptions.
        N(   R<   RN   (   R   RB   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR?   ¼  s    
 c         C   sV   g  } xI |  D]A } x8 | D]0 } g  } | | 7} | i  | ƒ | i  | ƒ q Wq W| S(   s  
        Multiply every thread in C{discourse} by every reading in C{readings}.
        
        Given discourse = [['A'], ['B']], readings = ['a', 'b', 'c'] , returns        
        [['A', 'a'], ['A', 'b'], ['A', 'c'], ['B', 'a'], ['B', 'b'], ['B', 'c']]
        
        @parameter discourse: the current list of readings
        @type discourse: C{list} of C{list}s
        @parameter readings: an additional list of readings
        @type readings: C{list} of C{logic.Expression}s
        @rtype: A C{list} of C{list}s
        (   RO   (   t	   discourseR   t   resultt   sublistRi   t   new(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyRa   Ç  s      
(   R   R   t   __doc__R   R   RF   R/   RY   Rd   R\   R]   RL   R`   Rh   Rk   Rs   R   RK   Rb   RP   Rƒ   R?   t   staticmethodRa   (    (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR0   ‘   s&   		
					c         C   s¹   d d k  l } g  } | ƒ  } x“ t |  i ƒ  ƒ D] \ } } | i ƒ  } | i d ƒ p | d j o q2 n y | i | i | ƒ ƒ Wq2 t j
 o t	 d | | f ‚ q2 Xq2 W| S(   s  
    Temporarily duplicated from L{nltk.sem.util}.
    Convert a  file of First Order Formulas into a list of C{Expression}s.
    
    @parameter s: the contents of the file
    @type s: C{str}
    @return: a list of parsed formulas.
    @rtype: C{list} of L{Expression}
    iÿÿÿÿ(   t   LogicParsert   #Rm   s   Unable to parse line %s: %s(
   t   nltk.semRŠ   R3   t
   splitlinest   stript
   startswithRO   R   t   ErrorR[   (   t   sRŠ   t
   statementst   lpt   linenumt   line(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt	   parse_folã  s    
	 $c         C   s{  t  d d g |  ƒ } | i ƒ  HH| i ƒ  H| i ƒ  H| i d t ƒ H| i d ƒ | i d ƒ H| i ƒ  H| i d t ƒ Ht  d d g |  ƒ } H| i d d	 t ƒH| i ƒ  H| i d d
 t ƒH| i ƒ  H| i d ƒ H| i d d t ƒt  d d d d g |  ƒ } | i d t ƒ d d k } | i i	 d ƒ } H| i
 | d
 t ƒ| i ƒ  H| i d t ƒ H| i ƒ  d S(   s>   
    Illustrate the various methods of C{DiscourseTester}
    s   A boxer walkss   Every boxer chases a girlRt   t   d1s   John is a boxers   A student dancess   Every student is a persons   No person dancesRR   RG   s   A person dancesRQ   s   Vincent is a boxers   Fido is a boxers   Vincent is marrieds
   Fido barksRp   iÿÿÿÿNs&   /grammars/book_grammars/background.fol(   R0   RP   RF   R   Rd   RY   R\   t	   nltk.datat   datat   loadRƒ   R/   R?   (   R>   t   dtR   R?   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   discourse_demoý  sZ    	



	

			
c         C   sa   t  d d g |  ƒ } | i ƒ  H| i ƒ  H| i ƒ  H| i d t ƒ H| i d t d t ƒ d S(   s>   
    Illustrate the various methods of C{DiscourseTester}
    s   every dog chases a boys   he runsRq   Rp   N(   R0   RP   RF   R   Rd   (   R>   R›   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   drt_discourse_demo1  s    	


i   c         C   s   d |  GHd  S(   NRl   (    (   t   num(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyRz   B  s    c          C   sN   t  ƒ  t d d d d d g ƒ }  t d |  ƒ } t t d t d | ƒ ƒ d  S(   Ns   ^(chases|runs)$t   VBs   ^(a)$t   ex_quants	   ^(every)$t
   univ_quants   ^(dog|boy)$t   NNs   ^(he)$t   PRPt   taggerR)   R*   (   s   ^(chases|runs)$RŸ   (   s   ^(a)$R    (   s	   ^(every)$R¡   (   s   ^(dog|boy)$s   NN(   s   ^(he)$R£   (   Rœ   R   R   R   R'   R/   (   R¤   R*   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   demoE  s    t   __main__(#   t   ost   operatorR    R   R   R˜   R   RŒ   R   R   t   maceR   t   prover9R   t   nltk.tagR   t   nltk.parse.maltR   t   nltk.sem.drtR	   R
   t   nltk.sem.glueR   t   objectR   R   R'   R0   R–   R   Rœ   R   Rz   R¥   R   (    (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pys   <module>
   s,   (ÿ S	4	