³ò
4ÒÇIc           @   s  d  Z  d d k Z d d k Z d d k Z d d k Td d „ Z d „  Z d d „ Z d d d	 „ Z d d
 „ Z	 e i
 d ƒ Z e i
 d ƒ Z e i
 d e i ƒ Z d „  Z d „  Z d d „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z e d j o e ƒ  n d S(   sà   
Utility functions for batch-processing sentences: parsing and
extraction of the semantic representation of the root node of the the
syntax tree, followed by evaluation of the semantic representation in
a first-order model.
iÿÿÿÿN(   t   *i    c         C   sX   h  } t  i i | d | ƒ} x3 |  D]+ } | i ƒ  } | i | ƒ } | | | <q% W| S(   s,  
    Convert input sentences into syntactic trees.
    
    @parameter inputs: sentences to be parsed
    @type inputs: C{list} of C{str}
    @parameter grammar: name of feature-based grammar to use in parsing
    @rtype: C{dict}
    @return: a mapping from input sentences to a list of L{Tree}s
    t   trace(   t   nltkt   parset   load_earleyt   splitt   nbest_parse(   t   inputst   grammarR   t   parsest   cpt   sentt   tokenst   syntrees(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt
   text_parse   s    
 c         C   sQ   t  |  t i i ƒ p t ‚ y |  d } | SWn t j
 o d GHn X‚  d S(   s’   
    Find the semantic representation at a given tree node.
    
    @parameter node: node of a parse L{Tree}
    @rtype: L{logic.Expression}
    t   sems'   Node has no 'sem' feature specificationN(   t
   isinstanceR   R   t   FeatStructNonterminalt   AssertionErrort   KeyError(   t   nodet   semrep(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   _semrep)   s    

t   Sc         C   s   t  |  i ƒ S(   s8  
    Find the semantic representation at the root of a tree.
    
    @parameter syntree: a parse L{Tree}
    @parameter beta_reduce: if C{True}, carry out beta reduction on the logical forms that are returned
    @return: the semantic representation at the root of a L{Tree}
    @rtype: L{logic.Expression}
    (   R   R   (   t   syntreet   start(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   root_semrep8   s    	c      	   C   sw   t  |  | d | ƒ} h  } xU |  D]M } | | } g  } | D] }	 | |	 t |	 d | ƒf q= ~ }
 |
 | | <q" W| S(   s=  
    Add the semantic representation to each syntactic parse tree
    of each input sentence.
    
    @parameter inputs: a list of sentences
    @parameter grammar: name of a feature-based grammar
    @return: a mapping from sentences to lists of pairs (parse-tree, semantic-representations)
    @rtype: C{dict}
    R   R   (   R   R   (   R   R   R   R   R	   t   semrepsR   R   t   _[1]t   synt   syn_sem(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   text_interpretC   s    
 
,c         C   sŒ   | } | } t  |  | ƒ } h  } xd |  D]\ }	 g  }
 | |	 D]4 \ } } |
 | | | i t | ƒ | d | ƒf q= ~
 } | | |	 <q( W| S(   st  
    Add the truth-in-a-model value to each semantic representation
    for each syntactic parse of each input sentences.
    
    @parameter inputs: a list of sentences
    @parameter grammar: name of a feature-based grammar    
    @return: a mapping from sentences to lists of triples (parse-tree, semantic-representations, evaluation-in-model)
    @rtype: C{dict}
    R   (   R   t   evaluatet   str(   R   R   t   modelt
   assignmentR   t   gt   mR   t   evaluationsR   R   R   R   t   syn_sem_val(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   text_evaluateW   s    
 Ls	   \s*=+>\s*s   \s*,\s*sp   \s*         
                                (\([^)]+\))  # tuple-expression
                                \s*c         C   sÉ   t  i |  ƒ } | d } | d } | i d ƒ oŒ | d d !} t i | ƒ } | oJ g  } xP | D]5 } | d d !} t t i | ƒ ƒ } | i | ƒ qc Wn t i | ƒ } t | ƒ } n | | f S(   s/  
    Parse a line in a valuation file.
    
    Lines are expected to be of the form::
    
      noosa => n
      girl => {g1, g2}
      chase => {(b1, g1), (b2, g1), (g1, d1), (g2, d2)}
    
    @parameter s: input line
    @type s: C{str}
    @return: a pair (symbol, value)
    @rtype: C{tuple}
    i    i   t   {iÿÿÿÿ(	   t   _VAL_SPLIT_RER   t
   startswitht
   _TUPLES_REt   findallt   tuplet   _ELEMENT_SPLIT_REt   appendt   set(   t   st   piecest   symbolt   valuet   tuple_stringst   set_elementst   tst   element(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   parse_valuation_linet   s     

 c         C   s¬   g  } x t  |  i ƒ  ƒ D]| \ } } | i ƒ  } | i d ƒ p | d j o q n y | i t | ƒ ƒ Wq t j
 o t d | | f ‚ q Xq Wt i | ƒ } | S(   s¾   
    Convert a valuation file into a valuation.
    
    @parameter s: the contents of a valuation file
    @type s: C{str}
    @return: a L{nltk.sem} valuation
    @rtype: L{Valuation}
    t   #t    s   Unable to parse line %s: %s(	   t	   enumeratet
   splitlinest   stripR+   R0   R:   t
   ValueErrorR    t	   Valuation(   R2   t
   statementst   linenumt   linet   val(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   parse_valuation–   s    	 $c         C   sº   | t  j o t ƒ  } n g  } x“ t |  i ƒ  ƒ D] \ } } | i ƒ  } | i d ƒ p | d j o q3 n y | i | i | ƒ ƒ Wq3 t j
 o t	 d | | f ‚ q3 Xq3 W| S(   sQ  
    Convert a file of First Order Formulas into a list of {Expression}s.
    
    @param s: the contents of the file
    @type s: C{str}
    @param logic_parser: The parser to be used to parse the logical expression
    @type logic_parser: C{LogicParser}
    @return: a list of parsed formulas.
    @rtype: C{list} of L{Expression}
    R;   R<   s   Unable to parse line %s: %s(
   t   Nonet   LogicParserR=   R>   R?   R+   R0   R   t   ParseExceptionR@   (   R2   t   logic_parserRB   RC   RD   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   parse_logic©   s     $c         C   sj  | d j o t ƒ  } n t |  t ƒ oB t |  i | t |  i g ƒ Bƒ } | i |  i t t	 ƒ  ƒ ƒ Snût |  t
 ƒ o$ t |  i | ƒ t |  i | ƒ @SnÇt |  t ƒ o) t t |  i | ƒ t |  i | ƒ ƒ SnŽt |  t ƒ o* t t |  i | ƒ t |  i | ƒ ƒ SnTt |  t ƒ oP t t |  i | ƒ t |  i | ƒ ƒ t t |  i | ƒ t |  i | ƒ ƒ @Snôt |  t ƒ o |  SnÜt |  t ƒ o7|  i } t | t ƒ o] t | i | ƒ } | o | i | i t | ƒ ƒ SqÎt t	 ƒ  ƒ } | i | i | ƒ Sqft | t
 ƒ o+ t t | i | ƒ t | i | ƒ ƒ Sqft | t ƒ o& t | i | ƒ t | i | ƒ @Sqft | t ƒ o% t | i | ƒ t | i | ƒ @Sqft | t ƒ oP t t | i | ƒ t | i | ƒ ƒ t t | i | ƒ t | i | ƒ ƒ @Sqft | t ƒ o |  Sqft | t ƒ o t | i | ƒ Sqft | t ƒ oC t | i | t | i g ƒ Bƒ } | i | i t t	 ƒ  ƒ ƒ Sqft | t ƒ o |  Sqft d |  ƒ ‚ n• t |  t ƒ o\ t |  i | ƒ } | o | i |  i t | ƒ ƒ Sqft t	 ƒ  ƒ } | i |  i | ƒ Sn) t |  t ƒ o |  Sn t d |  ƒ ‚ d S(   sO   
    Skolemize the expression and convert to conjunctive normal form (CNF)
    s   '%s' cannot be skolemizedN(   RG   R1   R   t   AllExpressiont	   skolemizet   termt   variablet   replacet   VariableExpressiont   unique_variablet   AndExpressiont   firstt   secondt   OrExpressiont   to_cnft   ImpExpressiont   IffExpressiont   EqualityExpressiont   NegatedExpressiont   skolem_functiont   ExistsExpressiont   ApplicationExpressiont	   Exception(   t
   expressiont
   univ_scopeRN   t   negatedt   skolem_constant(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyRM   Â   s|    " 	# c         C   sŒ   t  |  t ƒ o0 t |  i | ƒ } t |  i | ƒ } | | @SnI t  | t ƒ o0 t |  | i ƒ } t |  | i ƒ } | | @Sn	 |  | BSd S(   sI   
    Convert this split disjunction to conjunctive normal form (CNF)
    N(   R   RS   RW   RT   RU   (   RT   RU   t   r_firstt   r_second(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyRW   
  s    c          C   s7  t  i ƒ  }  d d d d d d d t d d g ƒ f d t d d g ƒ f d t d d
 g ƒ f d t d d
 g ƒ f d t d d d g ƒ f d t d d d d  g ƒ f d t d! d" d# d$ d% g ƒ f d t d& d' d( g ƒ f d t d) d* d+ d, g ƒ f g } |  i | ƒ |  i } t  i | |  ƒ a t  i | ƒ a d  S(-   Nt   johnt   b1t   maryt   g1t   suziet   g2t   fidot   d1t   tesst   d2t   noosat   nt   girlt   boyt   b2t   dogt   barkt   walkt   chaset   seet   int   with(   Rf   Rg   (   Rh   Ri   (   Rj   Rk   (   Rl   Rm   (   Rn   Ro   (   Rp   Rq   (   Rg   Ri   (   Rt   Ri   (   Ri   Rm   (   Rk   Ro   (   Rg   Ri   (   Rt   Ro   (   Ri   Rg   (   Ro   Rg   (   Rk   Rq   (   Rg   Rq   (   Rt   Rq   (   Ro   Rq   (   Rg   Ri   (   Ri   Rg   (   Rm   Rg   (   Rg   Rm   (	   R    RA   R1   t   readt   domaint   Modelt   m0t
   Assignmentt   g0(   RE   t   vt   dom(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   demo_model0  s(    !	c         C   sŸ   g  } t  |  ƒ D] } | | i ƒ  q ~ } g  } | D]$ } t | ƒ d j o | | q8 q8 ~ } g  } | D]" } | d d j p | | qp qp ~ } | S(   Ni    R;   (   t   opent   rstript   len(   t   fileR   t   lt   sentst   _[2]t   _[3](    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt
   read_sents8  s    -86c          B   s\  d d  k  }  d d k l } d } | d | ƒ } | i d e d e d d d	 d d
 d d d d d ƒ | i d d d d
 d d d d ƒ| i d d d d d d d d ƒ| i d d d d d d d d ƒ| i d d  d d d d! d d" ƒ| i d# d$ d% d& d d d d' ƒ| i d( d) d% d& d d d d* ƒ| i d+ d, d% d- d d d d. ƒ| i d/ d0 d% d- d d	 d d1 ƒ| i ƒ  \ } } d2 d3 } e ƒ  d4 d5 d6 d7 d8 d9 g } d: } | i o | i }	 n | i	 o | i	 } n | i
 o d; | i
 d  Un | d  j o e |	 ƒ } n e i i | ƒ }
 t } t } | i o" e | |
 | | d	 | i ƒ} n" e | |
 d< | i d | i ƒ} xÌ | D]Ä } d= } d> | GH| GH| i ol x | | D]Y \ } } } e | e ƒ o e | i ƒ  ƒ } n d? | | i ƒ  f GH| GH| d= 7} q¿Wqx1 | | D]% \ } } d? | | f GH| d= 7} q+WqWd  S(@   Niÿÿÿÿ(   t   OptionParsers,   
    Parse and evaluate some sentences.
    t   descriptionR    t   betat   syntracei    t   semtracet   demot   defaultR   R<   t	   sentencess   -ds   --demot   destt   helpsB   choose demo D; omit this for the default demo, or specify 'chat80't   metavart   Ds   -gs   --grams   read in grammar Gt   Gs   -ms   --modelR"   s"   import model M (omit '.py' suffix)t   Ms   -ss   --sentencess"   read in a file of test sentences SR   s   -es	   --no-evalt   actiont   store_falses   just do a syntactic analysiss   -bs   --no-beta-reductions   don't carry out beta-reductions   -ts
   --syntracet   counts.   set syntactic tracing on; requires '-e' options   -Ts
   --semtraces   set semantic tracing ont   -i   s   Fido sees a boy with Marys   John sees Marys   every girl chases a dogs   every boy chases a girls   John walks with a girl in Noosas	   who walkss"   grammars/sample_grammars/sem2.fcfgs   import %s as modelt   beta_reducei   s   
Sentence: %ss   %d:  %s(   t   syst   optparseRŽ   t   set_defaultst   Truet
   add_optiont
   parse_argsR„   R•   R   R"   RG   R   R   t   datat   loadR   R   R    R(   R’   R   R   R‘   R   t   dictR1   t   keyst   infixify(   R¡   RŽ   R   t   optst   optionst   argst   SPACERRŠ   t   gramfilet	   sentsfilet   gramR"   R$   R&   R   R   Rq   R   R   R5   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyR“   ?  s~    
	



"! 	
  t   __main__(   t   __doc__R    t   reR   t   logicR   R   R   R   R(   t   compileR*   R/   t   VERBOSER,   R:   RF   RG   RK   RM   RW   R„   R   R“   t   __name__(    (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pys   <module>   s.   
		"	H				X