ó
ù`]c           @  s³   d  Z  d d l m Z m Z d d l Z d d l m Z d d „ Z d d „ Z d d d	 „ Z	 d d
 „ Z
 d „  Z d d „ Z d „  Z d „  Z e d k r¯ e ƒ  e ƒ  n  d S(   uà   
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ÿÿÿÿ(   t   print_functiont   unicode_literalsN(   t   evaluatei    c         C  sŸ   d d l  m } d d l m } m } t | | ƒ rD | | ƒ } n | | d | ƒ} g  } x< |  D]4 } | j ƒ  }	 t | j |	 ƒ ƒ }
 | j	 |
 ƒ qc W| S(   ux  
    Convert input sentences into syntactic trees.

    :param inputs: sentences to be parsed
    :type inputs: list(str)
    :param grammar: ``FeatureGrammar`` or name of feature-based grammar
    :type grammar: nltk.grammar.FeatureGrammar
    :rtype: list(nltk.tree.Tree) or dict(list(str)): list(Tree)
    :return: a mapping from input sentences to a list of ``Tree``s
    iÿÿÿÿ(   t   FeatureGrammar(   t   FeatureChartParsert   load_parsert   trace(
   t   nltk.grammarR   t
   nltk.parseR   R   t
   isinstancet   splitt   listt   parset   append(   t   inputst   grammarR   R   R   R   t   cpt   parsest   sentt   tokenst   syntrees(    (    s,   lib/python2.7/site-packages/nltk/sem/util.pyt   parse_sents   s    u   SEMc         C  sv   d d l  m } |  j ƒ  } t | | ƒ s1 t ‚ y | | SWn/ t k
 rn t | d d ƒt d | ƒ n X‚  d S(   u  
    Find the semantic representation at the root of a tree.

    :param syntree: a parse ``Tree``
    :param semkey: the feature label to use for the root semantics in the tree
    :return: the semantic representation at the root of a ``Tree``
    :rtype: sem.Expression
    iÿÿÿÿ(   t   FeatStructNonterminalt   endu    u'   has no specification for the feature %sN(   R   R   t   labelR	   t   AssertionErrort   KeyErrort   print(   t   syntreet   semkeyR   t   node(    (    s,   lib/python2.7/site-packages/nltk/sem/util.pyt   root_semrep5   s    	c         C  sH   g  t  |  | d | ƒD]. } g  | D] } | t | | ƒ f ^ q# ^ q S(   uÍ  
    Add the semantic representation to each syntactic parse tree
    of each input sentence.

    :param inputs: a list of sentences
    :type inputs: list(str)
    :param grammar: ``FeatureGrammar`` or name of feature-based grammar
    :type grammar: nltk.grammar.FeatureGrammar
    :return: a mapping from sentences to lists of pairs (parse-tree, semantic-representations)
    :rtype: list(list(tuple(nltk.tree.Tree, nltk.sem.logic.ConstantExpression)))
    R   (   R   R   (   R   R   R   R   R   t   syn(    (    s,   lib/python2.7/site-packages/nltk/sem/util.pyt   interpret_sentsJ   s    c         C  sX   g  t  |  | ƒ D]D } g  | D]1 \ } } | | | j d | | d | ƒf ^ q ^ q S(   u  
    Add the truth-in-a-model value to each semantic representation
    for each syntactic parse of each input sentences.

    :param inputs: a list of sentences
    :type inputs: list(str)
    :param grammar: ``FeatureGrammar`` or name of feature-based grammar
    :type grammar: nltk.grammar.FeatureGrammar
    :return: a mapping from sentences to lists of triples (parse-tree, semantic-representations, evaluation-in-model)
    :rtype: list(list(tuple(nltk.tree.Tree, nltk.sem.logic.ConstantExpression, bool or dict(str): bool)))
    u   %sR   (   R!   R   (   R   R   t   modelt
   assignmentR   t   interpretationsR    t   sem(    (    s,   lib/python2.7/site-packages/nltk/sem/util.pyt   evaluate_sents\   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 j |  ƒ } | j } t j | | ƒ a t j | ƒ a d  S(-   Nu   johnu   b1u   maryu   g1u   suzieu   g2u   fidou   d1u   tessu   d2u   noosau   nu   girlu   boyu   b2u   dogu   barku   walku   chaseu   seeu   inu   with(   u   johnu   b1(   u   maryu   g1(   u   suzieu   g2(   u   fidou   d1(   u   tessu   d2(   u   noosau   n(   u   b1u   g1(   u   b2u   g1(   u   g1u   d1(   u   g2u   d2(   u   b1u   g1(   u   b2u   d2(   u   g1u   b1(   u   d2u   b1(   u   g2u   n(   u   b1u   n(   u   b2u   n(   u   d2u   n(   u   b1u   g1(   u   g1u   b1(   u   d1u   b1(   u   b1u   d1(   t   setR   t	   Valuationt   domaint   Modelt   m0t
   Assignmentt   g0(   t   vt   valt   dom(    (    s,   lib/python2.7/site-packages/nltk/sem/util.pyt   demo_model0q   s(    !	u   utf8c         C  s•   t  j |  d | ƒ & } g  | D] } | j ƒ  ^ q } Wd  QXg  | D] } t | ƒ d k rD | ^ qD } g  | D] } | d d k so | ^ qo } | S(   Nu   ri    u   #(   t   codecst   opent   rstript   len(   t   filenamet   encodingt   fpt   lt   sents(    (    s,   lib/python2.7/site-packages/nltk/sem/util.pyt
   read_sents’   s
    %+)c          C  s‚   d d l  m }  |  j d ƒ } t d | ƒ t d ƒ xD t d g | d d	 ƒD]* } | d
 \ } } t ƒ  t d | ƒ qP Wd S(   už   
    Check that interpret_sents() is compatible with legacy grammars that use
    a lowercase 'sem' feature.

    Define 'test.fcfg' to be the following

    iÿÿÿÿ(   R   u1   
    % start S
    S[sem=<hello>] -> 'hello'
    u   Reading grammar: %su   *i   u   helloR   u   semi    u   output: Nu   ********************(   R   R   t
   fromstringR   R!   (   R   t   gt   readingR    R%   (    (    s,   lib/python2.7/site-packages/nltk/sem/util.pyt   demo_legacy_grammarœ   s    	
c          B  sR  d d  l  }  d d l m } d } | d | ƒ } | j d e d e d d d	 d d
 d d d d d ƒ | j d d d d d d d d ƒ| j d d d d d d d d ƒ| j d d d d d d d d  ƒ| j d! d" d d# d d$ d d% ƒ| j d& d' d( d) d d* d d+ ƒ| j d, d- d( d) d d. d d/ ƒ| j d0 d1 d( d2 d d3 d d4 ƒ| j d5 d6 d( d2 d d7 d d8 ƒ| j ƒ  \ } } d9 d: } e ƒ  d; d< d= d> d? d@ g } dA } | j rË| j }	 n  | j	 rà| j	 } n  | j
 rûdB | j
 d  Un  | d  k re |	 ƒ } n  e }
 e } | j rLe | | |
 | dC | j ƒ} n e | | dC | j ƒ} xç e | ƒ D]Ù \ } } dD } e dE | ƒ e | ƒ | j rx£ | | D][ \ } } } e | e ƒ râe | j ƒ  ƒ } n  e dF | | f ƒ e | ƒ | dD 7} q¯Wqqx6 | | D]* \ } } e dF | | f ƒ | dD 7} qWqqWd  S(G   Niÿÿÿÿ(   t   OptionParseru,   
    Parse and evaluate some sentences.
    t   descriptionR   t   betat   syntracei    t   semtracet   demou   defaultR   u    t	   sentencesu   -du   --demot   destu   demot   helpuB   choose demo D; omit this for the default demo, or specify 'chat80't   metavaru   Du   -gu   --gramu   grammaru   read in grammar Gu   Gu   -mu   --modelu   modelu"   import model M (omit '.py' suffix)u   Mu   -su   --sentencesu	   sentencesu"   read in a file of test sentences Su   Su   -eu	   --no-evalt   actionu   store_falseu   evaluateu   just do a syntactic analysisu   -bu   --no-beta-reductionu   betau   don't carry out beta-reductionu   -tu
   --syntraceu   countu   syntraceu.   set syntactic tracing on; requires '-e' optionu   -Tu
   --semtraceu   semtraceu   set semantic tracing onu   -i   u   Fido sees a boy with Maryu   John sees Maryu   every girl chases a dogu   every boy chases a girlu   John walks with a girl in Noosau	   who walksu"   grammars/sample_grammars/sem2.fcfgu   import %s as modelR   i   u   
Sentence: %su   %d:  %s(   t   syst   optparseR@   t   set_defaultst   Truet
   add_optiont
   parse_argsR1   RF   R   R"   t   NoneR;   R+   R-   R   R&   RD   R!   RC   t	   enumerateR   R	   t   dictR'   t   keys(   RK   R@   RA   t   optst   optionst   argst   SPACERR:   t   gramfilet	   sentsfileR"   R=   t   evaluationst   semrepst   iR   t   nR   t   semrept   value(    (    s,   lib/python2.7/site-packages/nltk/sem/util.pyRE   ´   sº    	
					!
	
u   __main__(   t   __doc__t
   __future__R    R   R2   t   nltk.semR   R   R   R!   R&   R1   R;   R?   RE   t   __name__(    (    (    s,   lib/python2.7/site-packages/nltk/sem/util.pyt   <module>   s   	!
		€