³ò
ˆdhKc           @   s_  d  Z  d d k Z d d k Z d d k Z d d k l Z d d k Td d „ Z e d ƒ d „  ƒ Z d	 d
 „ Z	 d	 d d „ Z
 e d ƒ d „  ƒ Z d d „ Z e 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 d „ Z 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
   deprecated(   t   *i    c         C   s‡   t  | t i i ƒ o t i i | ƒ } n t i i | d | ƒ} g  } x6 |  D]. } | i ƒ  } | i | ƒ } | i	 | ƒ qQ W| S(   s/  
    Convert input sentences into syntactic trees.
    
    @parameter inputs: sentences to be parsed
    @type inputs: C{list} of C{str}
    @parameter grammar: L{FeatureGrammar} or name of feature-based grammar
    @rtype: C{dict}
    @return: a mapping from input sentences to a list of L{Tree}s
    t   trace(
   t
   isinstancet   nltkt   grammart   FeatureGrammart   parset   FeatureChartParsert   load_parsert   splitt   nbest_parset   append(   t   inputsR   R   t   cpt   parsest   sentt   tokenst   syntrees(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   batch_parse   s    
 s   Use batch_parse() instead.c          O   s   t  |  | Ž  d  S(   N(   R   (   t   argst   kwargs(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt
   text_parse0   s    t   SEMc         C   s\   |  i  } t | t i i ƒ p t ‚ y | | SWn  t j
 o | Gd | GHn X‚  d S(   s#  
    Find the semantic representation at the root of a tree.
    
    @parameter syntree: a parse L{Tree}
    @parameter semkey: the feature label to use for the root semantics in the tree
    @return: the semantic representation at the root of a L{Tree}
    @rtype: L{logic.Expression}
    s'   has no specification for the feature %sN(   t   nodeR   R   R   t   FeatStructNonterminalt   AssertionErrort   KeyError(   t   syntreet   semkeyR   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   root_semrep4   s    		c      
   C   sX   g  } t  |  | d | ƒD]7 } | g  } | D] } | | t | | ƒ f q. ~ q ~ S(   sP  
    Add the semantic representation to each syntactic parse tree
    of each input sentence.
    
    @parameter inputs: a list of sentences
    @parameter grammar: L{FeatureGrammar} or name of 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   t   _[1]R   t   _[2]t   syn(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   batch_interpretF   s    
s   Use batch_interpret() instead.c          O   s   t  |  | Ž  d  S(   N(   R"   (   R   R   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   text_interpretS   s    c   
      C   sj   g  } t  |  | ƒ D]O } | g  } | D]4 \ } }	 | | |	 | i t |	 ƒ | d | ƒf q( ~ q ~ S(   s‡  
    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: L{FeatureGrammar} or name of 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   R   t   interpretationsR    R!   t   sem(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   batch_evaluateW   s    
s   Use batch_evaluate() instead.c          O   s   t  |  | Ž  d  S(   N(   R*   (   R   R   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   text_evaluatee   s    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_RER   t   set(   t   st   piecest   symbolt   valuet   tuple_stringst   set_elementst   tst   element(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   parse_valuation_liner   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.   R   R<   t
   ValueErrorR$   t	   Valuation(   R4   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@   RA   R.   R   R   t   ParseExceptionRB   (   R4   t   logic_parserRD   RE   RF   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   parse_logic§   s     $c         C   s'  | d j o t ƒ  } n | d j o t ƒ  } n t |  t ƒ o[ t |  i | t |  i g ƒ B| t |  i g ƒ Bƒ } | i |  i t t	 d | ƒ ƒ ƒ Sn…t |  t
 ƒ o* t |  i | | ƒ t |  i | | ƒ @SnKt |  t ƒ o/ t t |  i | | ƒ t |  i | | ƒ ƒ Snt |  t ƒ o0 t t |  i | | ƒ t |  i | | ƒ ƒ SnÌt |  t ƒ o\ t t |  i | | ƒ t |  i | | ƒ ƒ t t |  i | | ƒ t |  i | | ƒ ƒ @Sn`t |  t ƒ o |  SnHt |  t ƒ oŠ|  i } t | t ƒ ov t | i | | t |  i g ƒ Bƒ } | o | i | i t | ƒ ƒ Sqrt t	 d | ƒ ƒ } | i | i | ƒ Sq#t | t
 ƒ o1 t t | i | | ƒ t | i | | ƒ ƒ Sq#t | t ƒ o, t | i | | ƒ t | i | | ƒ @Sq#t | t ƒ o+ t | i | | ƒ t | i | | ƒ @Sq#t | t ƒ o\ t t | i | | ƒ t | i | | ƒ ƒ t t | i | | ƒ t | i | | ƒ ƒ @Sq#t | t ƒ o |  Sq#t | t ƒ o t | i | | ƒ Sq#t | t ƒ o\ t | i | t | i g ƒ B| t |  i g ƒ Bƒ } | i | i t t	 d | ƒ ƒ ƒ Sq#t | t ƒ o |  Sq#t d |  ƒ ‚ n® t |  t ƒ ou t |  i | | t |  i g ƒ Bƒ } | o | i |  i t | ƒ ƒ Sq#t t	 d | ƒ ƒ } | i |  i | ƒ Sn) t |  t ƒ o |  Sn t d |  ƒ ‚ d S(   sO   
    Skolemize the expression and convert to conjunctive normal form (CNF)
    t   ignores   '%s' cannot be skolemizedN(   RI   R3   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_scopet   used_variablesRQ   t   negatedt   skolem_constant(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyRP   À   s€    5&	&6&%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   RV   RZ   RW   RX   (   RW   RX   t   r_firstt   r_second(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyRZ   
  s    c          C   s-  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 }  t 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(   Rj   Rk   (   Rl   Rm   (   Rn   Ro   (   Rp   Rq   (   Rr   Rs   (   Rt   Ru   (   Rk   Rm   (   Rx   Rm   (   Rm   Rq   (   Ro   Rs   (   Rk   Rm   (   Rx   Rs   (   Rm   Rk   (   Rs   Rk   (   Ro   Ru   (   Rk   Ru   (   Rx   Ru   (   Rs   Ru   (   Rk   Rm   (   Rm   Rk   (   Rq   Rk   (   Rk   Rq   (   R3   R$   RC   t   domaint   Modelt   m0t
   Assignmentt   g0(   t   vRG   t   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   sentsR    t   _[3](    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt
   read_sents7  s    -86c          C   s^   t  i d ƒ }  d |  GHd GHx: t d g |  d d ƒD]  } | d \ } } Hd	 G| GHq6 Wd
 S(   s¦   
    Check that batch_interpret() is compatible with legacy grammars that use
    a lowercase 'sem' feature.
    
    Define 'test.fcfg' to be the following
    
    s1   
    % start S
    S[sem=<hello>] -> 'hello'
    s   Reading grammar: %sR   i   t   helloR   R)   i    s   output: Ns   ********************(   R   t
   parse_fcfgR"   (   t   gt   readingR!   R)   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyt   demo_legacy_grammar>  s    		 c          B   sG  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 t }
 t } | i o" e | | |
 | d< | i ƒ} n e | | d< | i ƒ} xÒ e | ƒ D]Ä \ } } d= } d> | GH| GH| i of x— | | D]S \ } } } e | e ƒ o e | i ƒ  ƒ } n d? | | 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 St   Ss   -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 modelR   i   s   
Sentence: %ss   %d:  %s(   t   syst   optparseR•   t   set_defaultst   Truet
   add_optiont
   parse_argsR‡   Rœ   R   R&   RI   R   R‚   R„   R$   R*   R™   R"   R˜   R?   R   t   dictR3   t   keys(   R¨   R•   R–   t   optst   optionsR   t   SPACERR   t   gramfilet	   sentsfileR&   R’   t   evaluationst   semrepst   iR   Ru   R   t   semrepR7   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pyRš   Q  s|    
	



" 	
  t   __main__(   t   __doc__R$   t   reR   t   nltk.internalsR    t   nltk.sem.logicR   R   R   R"   R#   R*   R+   t   compileR-   R2   t   VERBOSER/   R<   RH   RI   RM   RP   RZ   R‡   R   R”   Rš   t   __name__(    (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/util.pys   <module>   s6   
	"	J					U