
4Ic           @   s{  d  d k  Z  d  d k l Z d  d k l Z d  d k Td  d k l Z d  d k l	 Z	 d  d k
 Td  d k Z d  d k Z d  d k Z h  d d <d d	 <d
 d <d d <d d <d d <Z d d d g Z d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d  d   Z e d! j o e   n d S("   iN(   t   data(   t   Counter(   t   *(   t
   MaltParser(   t   brownt   ex_quantt   at   ant
   univ_quantt   everyt   def_artt   thet   no_quantt   not   defaultt   nmodt   vmodt   punctt   GlueFormulac           B   sh   e  Z e d   Z d   Z d   Z d   Z d   Z e d  Z d   Z	 d   Z
 d   Z d	   Z RS(
   c         C   s   | p t    } n t | t  o t i   i |  |  _ n7 t | t i  o | |  _ n t d | | i	 f  t | t  o t
 i   i |  |  _ n7 t | t
 i  o | |  _ n t d | | i	 f  | |  _ d  S(   Ns1   Meaning term neither string or expression: %s, %ss.   Glue term neither string or expression: %s, %s(   t   sett
   isinstancet   strt   logict   LogicParsert   parset   meaningt
   Expressiont   RuntimeErrort	   __class__t   linearlogict   LinearLogicParsert   gluet   indices(   t   selfR   R   R    (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyt   __init__   s    c      	   C   s  |  i  | i  @o t i d |  | f  n |  i  | i  B} y" t i |  i | i | i   } Wn8 t i j
 o) t i d |  i   | i   f  n X| i } | oP xM |  i i   i i d d d  D]% } |  i	 t
 i d |  |  } q Wn |  i i |  } |  i | | |  S(   s    self = (\x.(walk x), (subj -o f))
            arg  = (john        ,  subj)
            returns ((walk john),          f)
        s0   '%s' applied to '%s'.  Indices are not disjoint.s   '%s' applied to '%s'Nis   v%s(   R    R   t   LinearLogicApplicationExceptiont   ApplicationExpressionR   t   simplifyR   t
   antecedentt   dependenciest   make_LambdaExpressionR   t   Variablet   applytoR   (   R!   t   argt   return_indicest   return_gluet   arg_meaning_abstractedt   dept   return_meaning(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR*   2   s    "'	# c         C   s   t  i |  S(   N(   R   t   VariableExpression(   R!   t   name(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyt   make_VariableExpressionJ   s    c         C   s   t  i | |  S(   N(   R   t   LambdaExpression(   R!   t   variablet   term(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR(   M   s    c         C   sk   t  | t  p t  t  | i t i  p t  |  i |  i | i i |  i  t	 i
 | i |  i   S(   N(   R   R   t   AssertionErrorR   R   t   AbstractVariableExpressionR   R(   R5   R   t   ImpExpressionR   (   R!   t   other(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyt   lambda_abstractP   s
    	c         C   sd   | p t    } n |  i i   i | |  i  \ } } | |  i |  i | t | i   g   g S(   s)   From Iddo Lev's PhD Dissertation p108-109(   R   R   R%   t   compile_posR   R   R   t   get(   R!   t   countert   compiled_gluet	   new_forms(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyt   compileW   s    $c         C   s(   |  i  |  i i   |  i i   |  i  S(   N(   R   R   R%   R   R    (   R!   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR%   ^   s    c         C   s6   |  i  | i  j o# |  i | i j o |  i | i j S(   N(   R   R   R   (   R!   R:   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyt   __eq__a   s    c         C   s   t  |  i t  p t  d |  i |  i f } |  i oC | d d i g  } |  i D] } | t |  qT ~  d 7} n | S(   Ns   %s : %ss    : {s   , t   }(   R   R    R   R7   R   R   t   joinR   (   R!   t   accumt   _[1]t   index(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyt   __str__d   s
    
Cc         C   s
   t  |   S(   N(   R   (   R!   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyt   __repr__k   s    (   t   __name__t
   __module__t   NoneR"   R*   R3   R(   R;   RA   R%   RB   RH   RI   (    (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR      s   							t   GlueDictc           B   s   e  Z d    Z e d  Z d   Z e e e d  Z d   Z	 d   Z
 d   Z d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z RS(   c         C   s   | |  _  |  i   d  S(   N(   t   filenamet	   read_file(   R!   RN   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR"   o   s    	c         C   s  | o |  i    n y. t t i t i i d d |  i    } WnD t j
 o8 } y t |  i  } Wq t j
 o |  q Xn X| i	   } | i
   x| D]} | i   } t |  p q n | d d j o q n | i d d  } g  } d } d }	 d }
 d  } t |  d j oxt | d  D] \ } } | d j o) | d j o | d }	 n | d 7} q9| d	 j oX | d 8} | d j o= | d |	 |
 !} | d |
 d | !} | i | | g  q8q9| d
 j o | d j o
 | }
 q8q9| d j o# | d j o t d |  n Pq9q9Wn t |  d j o | d i d  d } | d i d  } | | j o t   } qt g  } | d | | !i d
  D] } | | i   q~  } n yS | d i d  } | d i d	  } | d |  i   } | d | d | !} Wn | d i   } d  } n X| |  j o h  |  | <n | d  j o | or x |  | i   D]Y \ } } | |  | j o g  |  | | <n |  | | i |  |  | | i |  qWqd  |  | j o g  |  | d  <n |  | d  i |  q | |  | j o g  |  | | <n | o! |  | | i |  | |  n |  | | i |  q Wd  S(   Nt   grammarst   sample_grammarsi    t   #s    : i   i   t   (t   )t   ,s&   Formula syntax is incorrect for entry t   [t   ](   t   cleart   openR    t   findt   ost   pathRD   RN   t   LookupErrort	   readlinest   closet   stript   lent   splitRL   t	   enumeratet   appendR   RG   t	   frozensett	   iteritemst   extend(   R!   t   empty_firstt   ft   et   linest   linet   partst   glue_formulast
   parenCountt   tuple_startt   tuple_commat   relationshipst   it   ct   meaning_termt	   glue_termt   relStartt   relEndRF   t   rt   startInheritancet   endInheritancet   semt	   supertypet   relsR   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyRO   s   s    .
  
E
 !c         C   s   d } x |  D] } x |  | D] } d } x |  | | D] } | d j o | t  |  d 7} n | d t t  |   d 7} | t  |  7} | o3 | t |  | |  j o | d t  |  7} n | d 7} | d 7} q9 Wq Wq W| S(   Nt    i   s   : t    i   s    : s   
(   R   Ra   (   R!   RE   t   post   relsetRs   t   gf(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyRH      s"       "
c   
      C   s   | d  j o? | i d } | i | d d } |  i | | t   |  Sn |  i | | |  } x> | d D]2 } | i | }	 | i |  i | |	 | |   ql W| S(   Ni    t   deps(   RL   t   nodelistt   to_glueformula_listR   t   lookupRg   (
   R!   t   depgrapht   nodeR>   t   verboset   topt   roott   glueformulast   dep_idxR/   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR      s     #c         C   s   |  i  |  } d  } x* | D]" } | |  j o |  | } Pq q W| d  j o g  Sn |  i | |  |  i | | |  } t |  p& t d | d | d | d f  n |  i | | d | | |  S(   NsK   There is no GlueDict entry for sem type of '%s' with tag '%s', and rel '%s't   wordt   tagt   rel(   t   get_semtypesRL   t   add_missing_dependenciest   _lookup_semtype_optionRa   t   KeyErrort#   get_glueformulas_from_semtype_entry(   R!   R   R   R>   t   semtype_namest   semtypeR2   R   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR      s     
	 c         C   s`   | d i    } | d j o? | i | d } |  i d | |  } | d i | d  n d  S(   NR   t   maint   headt   subjR   t   address(   t   lowerR   t   lookup_uniqueRd   (   R!   R   R   R   t   headnodeR   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR      s
    c   
      C   s  t  g  } | d D]@ } | i | d i   t j o | | i | d i   q q ~  } y | | } Wn t j
 o t    } xQ t |  t d  g  D]6 }	 t |	  t |  j o |	 | j  o
 |	 } q q W| p d  | j o
 d  } q d  Sn | | } n X| S(   NR   R   (   Re   R   R   t   OPTIONAL_RELATIONSHIPSR   R   RL   Ra   (
   R!   R   R   R   RF   R/   Rr   R   t
   best_matcht   relset_option(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR     s&    
2	 
c         C   s   d } | d i   } | d i   } | d j o+ | t j o t | g Sq t d g Sn+ | d	 j o | d | g Sn | d g Sd S(
   sj   
        Based on the node, return a list of plausible semtypes in order of
        plausibility.
        R   R   t   specR   R   R   R   N(   R   R   (   RL   R   t   SPEC_SEMTYPES(   R!   R   t   semtype_nameR   R   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR   #  s    c         C   s   g  } |  i    } x | D] \ } }	 | |  i | |  |	  }
 t |  p | |
 _ n d | t |  d f |
 _ |  i |
 i | | | i    |
 _ | i |
  q W| S(   Ns   %s%si   (   t   get_GlueFormula_factoryt   get_meaning_formulaRa   R   t   initialize_labelsR   R=   Rd   (   R!   R   R   R   R   R>   R   t   glueFormulaFactoryR   R   R   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR   7  s     $c         C   s"   | i  d d  } | i  d |  S(   s   
        @param generic: A meaning formula string containing the 
        parameter "<word>"
        @param word: The actual word to be replace "<word>"
        t   .R   s   <word>(   t   replace(   R!   t   genericR   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR   G  s    c         C   s   t  | t i  oT |  i | i i   | | |  } | d i   o t i |  Sq t i |  Sn; t i	 |  i
 | i | | |  |  i
 | i | | |   Sd  S(   Ni    (   R   R   t   AtomicExpressiont   find_label_nameR2   R   t   isupperR1   t   ConstantExpressionR9   R   R&   t
   consequent(   R!   t   exprR   R   t   unique_indexR2   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR   P  s    !c   	      C   s  y | i  d  } | |  } | | d } | d j o% |  i | | i | d | |  Sn& |  i | |  i | | |  | |  SWn-t j
 o!|  i |  } | d j o | Sq| d j o d | Sq| d j o d	 | Sq| d j o |  i | i | d  Sq| d
 j o d | i   | f Sq| d j o  |  i |  i d | |   Sq| d j o  |  i |  i d | |   Sq|  i |  i | | |   Sn Xd  S(   NR   i   t   superR   Ri   t   vs   %svRy   s   %srt   vars   %s%sR   t   conjat   bt   conjb(   RG   R   R   R   t
   ValueErrort	   get_labelt   upper(	   R!   R2   R   R   R   t   dott
   before_dott	   after_dott   lbl(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR   \  s"    
%))%--c         C   s   | d } d d d d d d d d	 d
 d d d d d d d d d d d d d d d d d g | d } t  |  d } | d j o | t |  Sn | Sd S(    s   
        Pick an alphabetic character as identifier for an entity in the model.
        
        @parameter value: where to index into the list of characters
        @type value: C{int}
        R   Ri   t   gt   hRs   t   jt   kt   lt   mt   nt   ot   pt   qRy   t   st   tt   uR   t   wt   xt   yt   zR   R   Rt   t   dRj   i   i   i    N(   t   intR   (   R!   R   t   valuet   lettert   num(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR   q  s    
*2c         C   s   g  } | d D]< } | i  | d i   | i   j o | | i  | q q ~ } t |  d j o t d | d | f  n7 t |  d j o t d | d | f  n	 | d Sd S(	   s\   
        Lookup 'key'. There should be exactly one item in the associated relation.
        R   R   i    s#   '%s' doesn't contain a feature '%s'R   i   s&   '%s' should only have one feature '%s'N(   R   R   Ra   R   (   R!   R   R   R   RF   R/   R   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR     s    ?c         C   s   t  S(   N(   R   (   R!   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR     s    (   RJ   RK   R"   t   TrueRO   RH   RL   t   FalseR   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyRM   n   s   	Y												t   Gluec           B   s   e  Z e e e e d   Z e d  Z d   Z d   Z d   Z d d  Z	 d d  Z
 d	   Z d
   Z d   Z d   Z RS(   c         C   s<   | |  _  | |  _ | |  _ | o | |  _ n
 d |  _ d  S(   Ns   glue.semtype(   R   t   remove_duplicatest	   depparsert   semtype_file(   R!   R   R   R   R   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR"     s    			c         C   sJ   | o |  i  i |  n, |  i  i t i t i i d d d    d  S(   NRP   RQ   s   glue_train.conll(   R   t   traint   train_from_fileR    RZ   R[   R\   RD   (   R!   t	   depgraphs(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyt   train_depparser  s    c         C   s:   g  } x- |  i  |  D] } | i |  i |   q W| S(   N(   t   parse_to_compiledRg   t   get_readings(   R!   t   sentencet   readingst   agenda(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyt   parse_to_meaning  s
     c         C   s  g  } t  |  } t   } t   } xQ| oI| i   } | i i   } t | t i  ox | D] } y t | i t i  o | i i	 }	 n t i
   }	 | i i | |	  xZ | | D]N }
 | i |
 i @p7 y | i | i |
   Wq
t i j
 o q
Xq q WWqc t i j
 o qc Xqc Wy | | i i |  Wqst j
 o | g | | i <qsXq' x | D] } x | | D] } y t | i t i  o | i i	 }	 n t i
   }	 | i | |	  | i | i @p7 y | i | i |   Wqt i j
 o qXn Wqt i j
 o qXqWquWy | | i |  Wq' t j
 o | g | | <q' Xq' WxM | D]E } x< | | D]0 } t  | i  | j o |  i | |  qqWqWxM | D]E } x< | | D]0 } t  | i  | j o |  i | |  qqWqW| S(   N(   Ra   t   dictt   popR   R%   R   R   R9   R$   t   bindingst   BindingDictR&   t   unifyR    Rd   R*   R#   t   UnificationExceptionR   t   _add_to_reading_list(   R!   R   R   t   agenda_lengtht   atomicst
   nonatomicst   curt	   glue_simpt   keyR   t   atomict	   nonatomict   entryR   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR     sz    		
  	      c         C   ss   t  } |  i oD xA | D]5 } y% | i | i d  o t } Pn Wq q Xq Wn | o | i | i  n d  S(   Nt   Prover9(   R   R   t	   tp_equalsR   R   Rd   (   R!   t   glueformulat   reading_listt   add_readingt   reading(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR     s    
 	s   a man sees Maryc         C   s[   g  } |  i  |  D] } | |  i |  q ~ } g  } | D] } | |  i |  q> ~ S(   N(   t	   dep_parset   depgraph_to_gluet   gfl_to_compiled(   R!   R   RF   t   dgt   gflst   _[2]t   gfl(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR     s    3s   every cat leavesc         C   sc   |  i  d  j o t d |  i    |  _  n |  i  i p |  i   n |  i  i | d |  i g S(   Nt   taggerR   (   R   RL   R   t   get_pos_taggert   _trainedR   R   R   (   R!   R   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR     s
    c         C   s   |  i    i |  S(   N(   t   get_glue_dictR   (   R!   R   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR     s    c         C   s   t  |  i  S(   N(   RM   R   (   R!   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR    s    c         C   sc   t    } g  } x$ | D] } | i | i |   q W|  i o d GHx | D] } | GHqL Wn | S(   Ns   Compiled Glue Premises:(   R   Rg   RA   R   (   R!   R  t   index_countert   return_listR   t   cgf(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR   
  s    	 
 c         C   s   t  d d d d d d d d  d! g	  } t i d d  } t | d | } t | d | } t | d | } t  d" d# g d | } | S($   Ns   ^-?[0-9]+(.[0-9]+)?$t   CDs   (The|the|A|a|An|an)$t   ATs   .*able$t   JJs   .*ness$t   NNs   .*ly$t   RBs   .*s$t   NNSs   .*ing$t   VBGs   .*ed$t   VBDs   .*t
   categoriest   newst   backoffs   (A|a|An|an)$R   s   (Every|every|All|all)$R   (   s   ^-?[0-9]+(.[0-9]+)?$s   CD(   s   (The|the|A|a|An|an)$s   AT(   s   .*able$s   JJ(   s   .*ness$s   NN(   s   .*ly$s   RB(   s   .*s$s   NNS(   s   .*ing$s   VBG(   s   .*ed$s   VBD(   s   .*s   NN(   s   (A|a|An|an)$s   ex_quant(   s   (Every|every|All|all)$s
   univ_quant(   t   RegexpTaggerR   t   tagged_sentst   UnigramTaggert   BigramTaggert   TrigramTagger(   R!   t   regexp_taggert   brown_traint   unigram_taggert   bigram_taggert   trigram_taggert   main_tagger(    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR    s&    		(   RJ   RK   RL   R   R"   R   R   R   R   R   R   R   R  R   R  (    (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR     s   
		<					t   DrtGlueFormulac           B   s&   e  Z e d   Z d   Z d   Z RS(   c         C   s   | p t    } n t | t  o t i   i |  |  _ n7 t | t i  o | |  _ n t d | | i	 f  t | t  o t
 i   i |  |  _ n7 t | t
 i  o | |  _ n t d | | i	 f  | |  _ d  S(   Ns1   Meaning term neither string or expression: %s, %ss.   Glue term neither string or expression: %s, %s(   R   R   R   t   drtt	   DrtParserR   R   t   AbstractDrsR   R   R   R   R   R   R    (   R!   R   R   R    (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR"   2  s    c         C   s   t  i |  S(   N(   R"  t   DrtVariableExpression(   R!   R2   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR3   F  s    c         C   s   t  i | |  S(   N(   R"  t   DrtLambdaExpression(   R!   R5   R6   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR(   I  s    (   RJ   RK   RL   R"   R3   R(   (    (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR!  1  s   	t   DrtGlueDictc           B   s   e  Z d    Z RS(   c         C   s   t  S(   N(   R!  (   R!   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR   M  s    (   RJ   RK   R   (    (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR'  L  s   t   DrtGluec           B   s&   e  Z e e e e d   Z d   Z RS(   c         C   s.   | p
 d } n t  i |  | | | |  d  S(   Ns   drt_glue.semtype(   R   R"   (   R!   R   R   R   R   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR"   Q  s    
c         C   s   t  |  i  S(   N(   R'  R   (   R!   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR  W  s    (   RJ   RK   RL   R   R"   R  (    (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyR(  P  s   c      
   C   s   d d d d d d d d d	 d
 g
 } d GHt  d! d" d# d$ d% d& d' d( g  } t d |  } t d | d t  } xq t |  D]c \ } } | |  j p |  d j o= d | | f GHx" | i |  D] } | i   GHq Wd  GHq~ q~ Wd  S()   Ns   David sees Marys   David eats a sandwichs   every man chases a dogs   every man believes a dog sleepss   John gives David a sandwichs   John chases himselfs   every big cat leavess   every gray cat leavess   every big gray cat leavess   a former senator leavess"   ============== DEMO ==============s   ^(David|Mary|John)$t   NNPsN   ^(sees|eats|chases|believes|gives|sleeps|chases|persuades|tries|seems|leaves)$t   VBs!   ^(go|order|vanish|find|approach)$s   ^(a)$R   s	   ^(every)$R   s.   ^(sandwich|man|dog|pizza|unicorn|cat|senator)$R  s   ^(big|gray|former)$R  s   ^(him|himself)$t   PRPR  R   R   is   [[[Example %s]]]  %sR   (   s   ^(David|Mary|John)$R)  (   sN   ^(sees|eats|chases|believes|gives|sleeps|chases|persuades|tries|seems|leaves)$s   VB(   s!   ^(go|order|vanish|find|approach)$s   VB(   s   ^(a)$s   ex_quant(   s	   ^(every)$s
   univ_quant(   s.   ^(sandwich|man|dog|pizza|unicorn|cat|senator)$s   NN(   s   ^(big|gray|former)$s   JJ(   s   ^(him|himself)$s   PRP(   R  R   R   R   Rc   R   R%   (   t   show_examplet   examplesR  R   R   Rs   R   R   (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pyt   demo[  s<    	  t   __main__(   R[   t   nltkR    t   nltk.internalsR   t
   nltk.parset   nltk.parse.maltR   t   nltk.corpusR   t   nltk.tagR   R"  R   R   R   t   objectR   R   RM   R   R!  R'  R(  R.  RJ   (    (    (    s#   /p/zhu/06/nlp/nltk/nltk/sem/glue.pys   <module>   s4   

				Q &)