ó
ù`]c           @  s”   d  d l  m Z d  d l m Z m Z m Z d  d l m Z d  d l m	 Z	 d e
 f d „  ƒ  YZ d d d „ Z d	 „  Z e d
 k r e ƒ  n  d S(   iÿÿÿÿ(   t   print_function(   t   LambdaExpressiont   ApplicationExpressiont   Variable(   t   load_parser(   t   InstantiateVarsChartt   CooperStorec           B  s,   e  Z d  Z d „  Z d „  Z e d „ Z RS(   sK   
    A container for handling quantifier ambiguity via Cooper storage.
    c         C  sV   | |  _  g  |  _ y | d |  _ | d |  _ Wn t k
 rQ t d | ƒ n Xd S(   sÃ   
        :param featstruct: The value of the ``sem`` node in a tree from
            ``parse_with_bindops()``
        :type featstruct: FeatStruct (with features ``core`` and ``store``)

        t   COREt   STOREs$   %s is not a Cooper storage structureN(   t
   featstructt   readingst   coret   storet   KeyErrort   print(   t   selfR	   (    (    s6   lib/python2.7/site-packages/nltk/sem/cooper_storage.pyt   __init__   s    		c         c  sg   d „  } | r^ xQ t  | ƒ D]; \ } } x, |  j | | | ƒ ƒ D] } | f | VqA Wq Wn d Vd S(   s{   
        :return: An iterator over the permutations of the input list
        :type lst: list
        :rtype: iter
        c         S  s   |  |  |  | d S(   Ni   (    (   t   lst0t   index(    (    s6   lib/python2.7/site-packages/nltk/sem/cooper_storage.pyt   <lambda>(   t    N(    (   t	   enumeratet   _permute(   R   t   lstt   removeR   t   xt   y(    (    s6   lib/python2.7/site-packages/nltk/sem/cooper_storage.pyR   "   s    	c         C  sÄ   x½ t  |  j |  j ƒ ƒ D]£ \ } } | r@ t d | d ƒ n  |  j } x` | D]X } t | j ƒ \ } } t | t | j	 | ƒ ƒ } | rœ t d | ƒ n  | j
 ƒ  } qP W|  j j | ƒ q Wd S(   sI  
        Carry out S-Retrieval of binding operators in store. If hack=True,
        serialize the bindop and core as strings and reparse. Ugh.

        Each permutation of the store (i.e. list of binding operators) is
        taken to be a possible scoping of quantifiers. We iterate through the
        binding operators in each permutation, and successively apply them to
        the current term, starting with the core semantic representation,
        working from the inside out.

        Binding operators are of the form::

             bo(\P.all x.(man(x) -> P(x)),z1)
        s   Permutation %si   s     N(   R   R   R   R   R   t   tuplet   argsR   R   t   variablet   simplifyR
   t   append(   R   t   tracet   permt
   store_permt   termt   bindopt   quantt   varex(    (    s6   lib/python2.7/site-packages/nltk/sem/cooper_storage.pyt
   s_retrieve0   s    %	(   t   __name__t
   __module__t   __doc__R   R   t   FalseR'   (    (    (    s6   lib/python2.7/site-packages/nltk/sem/cooper_storage.pyR      s   		i    c         C  sF   | s d } n  t  | d | d t ƒ} |  j ƒ  } t | j | ƒ ƒ S(   sC   
    Use a grammar with Binding Operators to parse a sentence.
    s#   grammars/book_grammars/storage.fcfgR    t   chart_class(   R   R   t   splitt   listt   parse(   t   sentencet   grammarR    t   parsert   tokens(    (    s6   lib/python2.7/site-packages/nltk/sem/cooper_storage.pyt   parse_with_bindopsQ   s
    	c          C  sT  d d l  m }  d } t ƒ  t d | ƒ t d d ƒ |  j | d d ƒ} xÿ | D]÷ } |  j | j ƒ  d	 ƒ } t ƒ  t d
 ƒ t d ƒ x | j D] } t | ƒ q™ Wt ƒ  t d ƒ t d ƒ t | j ƒ t ƒ  t d ƒ t d ƒ | j d t	 ƒ t d ƒ t d ƒ x5 t
 | j ƒ D]$ \ } } t d | d | f ƒ q$WqU Wd  S(   Niÿÿÿÿ(   t   cooper_storages   every girl chases a dogs   Analyis of sentence '%s't   =i2   R    i    t   SEMs   Binding operators:t   -i   s   Core:s   S-Retrieval:s	   Readings:s   %s: %si   s   ---------------s   ---------------s   ---------------s   ---------------(   t   nltk.semR5   R   R4   R   t   labelR   R   R'   t   TrueR   R
   (   t   csR0   t   treest   treet   semrept   st   it   reading(    (    s6   lib/python2.7/site-packages/nltk/sem/cooper_storage.pyt   demo]   s2    







t   __main__N(   t
   __future__R    t   nltk.sem.logicR   R   R   t
   nltk.parseR   t   nltk.parse.featurechartR   t   objectR   t   NoneR4   RC   R(   (    (    (    s6   lib/python2.7/site-packages/nltk/sem/cooper_storage.pyt   <module>   s   C	