³ò
4ÒÇIc           @   sº  d  d k  Z  d  d k l Z 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  d k l Z d  d k l Z d  d	 k l Z d  d
 k 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  e! d j ol e ƒ  e d d f d d f d d f d  d! f d" d# f g ƒ Z" e d$ e" ƒ Z# e e d% e$ d& e# ƒ ƒ n d S('   iÿÿÿÿN(   t   and_t   add(   t   parse(   t   show_cfg(   t   root_semrept
   Expression(   t   MaceCommand(   t   Prover9Command(   t   RegexpTagger(   t
   MaltParser(   t   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_threadD   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_readingsP   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 ƒ |  _ d S(   sn   
        @parameter gramfile: name of file where grammar can be loaded
        @type gramfile: C{str}
        s"   grammars/sample_grammars/sem4.fcfgN(   t   Nonet	   _gramfileR   t   load_earleyt   _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   f   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   l   s    (   R   R   R   R   R   R   (    (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   Z   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   r   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 | ƒ } | i ƒ  i ƒ  S(   s'   @see: ReadingCommand.combine_readings()(   R$   R   t   simplifyt   resolve_anaphora(   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%   q   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   sortedR3   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.   RB   R7   RC   t   expand_threadsR;   t   _get_readingsR   t   provet   strR0   t   appendR1   R2   R3   t   models(   R   R   t	   informchkt
   consistchkt   tidR"   t   ridt   readingRH   t   sent_readingt   tpt   _[2]R?   R@   (    (    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%sRF   s   Current sentences are N(
   R0   t   removet
   ValueErrorRE   R   R1   R2   R3   R   R.   (   R   R   RF   R"   R?   R@   (    (    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   R6   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}.
        (   R6   R   (   R   R   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyRK   ÷   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(   R5   R3   t	   iteritemsRK   R1   R2   R+   (   R   t   sidR   R   R"   RS   RT   (    (    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(   RB   R5   RC   t   multiplyR1   R2   R7   R8   t   _check_consistencyt   itemst   True(   R   t   thread_listR^   R"   RR   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 Wnr xn t |  i i ƒ  ƒ D]W } Hd | GHd d GHx; 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:t   -i   s   %s: %sN(   R   RK   RM   RB   R5   RC   (   R   R   R"   RT   t   rR^   RS   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} 
        Rh   i    s   : %ss   : INVALID: %st    s   %s:N(   R8   R7   RB   RC   R5   R   R6   R   t	   Exceptiont	   __class__R   (
   R   t   filtert   show_thread_readingst   threadsRR   R"   RS   R   R-   RA   (    (    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   Ro   Rp   N(   R_   Rg   Rc   Rk   Rr   (   R   R   t   threadedRF   Ro   Rp   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR   E  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}
        Rh   i   (   R   R7   R   R5   (   R   t	   thread_idRq   R"   RS   R^   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyRJ   ]  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(	   NRq   t   timeouti   iP   s   Model for Discourse Thread %st   formatt   cookeds   No model found!
(   RB   RC   RJ   R6   R   R;   R   R   t   build_modelR.   RN   t   spacert   model(   R   Rq   RI   RF   t   resultsRR   R"   RS   RT   RH   t   mbt
   modelfoundt   a(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyRa   q  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.
        RI   RF   s   Inconsistent discourse %s %s:s
       %s: %ss   Consistent discourse: %s %s:N(   R_   Rg   R   R7   Ra   RJ   RM   (   R   Rt   RI   RF   Rq   RR   R}   R"   RS   t   idlistRW   RT   t   _[3](    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyRO   Œ  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(   R2   R9   R   R:   R;   RN   R_   Rg   (   R   R>   RF   t   countRA   (    (    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;   RM   (   R   RA   (    (    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
        (   RN   (   t	   discourseR   t   resultt   sublistRi   t   new(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR`   Æ  s      
(   R   R   t   __doc__R   R   RE   R.   RX   Rc   R[   R\   RK   R_   Rg   Rk   Rr   R   RJ   Ra   RO   R‚   R>   t   staticmethodR`   (    (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyR/      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   #Rl   s   Unable to parse line %s: %s(
   t   nltk.semR‰   R2   t
   splitlinest   stript
   startswithRN   R   t   ErrorRZ   (   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 girlRs   t   d1s   John is a boxers   A student dancess   Every student is a persons   No person dancesRQ   RF   s   A person dancesRP   s   Vincent is a boxers   Fido is a boxers   Vincent is marrieds
   Fido barksRo   iÿÿÿÿNs)   /grammars/sample_grammars/background1.fol(   R/   RO   RE   R   Rc   RX   R[   t	   nltk.datat   datat   loadR‚   R.   R>   (   R=   t   dtt   nltkR>   (    (    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 runsRp   Ro   N(   R/   RO   RE   R   Rc   (   R=   Rš   (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyt   drt_discourse_demo0  s    	


i   c         C   s   d |  GHd  S(   NRh   (    (   t   num(    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pyRy   A  s    t   __main__s   ^(chases|runs)$t   VBs   ^(a)$t   ex_quants	   ^(every)$t
   univ_quants   ^(dog|boy)$t   NNs   ^(he)$t   PRPt   taggerR'   R(   (%   t   ost   operatorR    R   R›   R   R—   R   R‹   R   R   t   maceR   t   prover9R   t   nltk.tagR   t   nltk.parse.maltR	   t   nltk.sem.drt_resolve_anaphoraR
   t   nltk.sem.glueR   t   objectR   R   R%   R/   R•   R   Rœ   R   Ry   R   R¥   R(   R.   (    (    (    s.   /p/zhu/06/nlp/nltk/nltk/inference/discourse.pys   <module>	   s<   (ÿ S	4				