U
    er                     @   s^   d dl mZ d dlmZ d dlmZmZmZ G dd dZdddZ	d	d
 Z
edkrZe
  dS )    )load_parser)InstantiateVarsChart)ApplicationExpressionLambdaExpressionVariablec                   @   s*   e Zd ZdZdd Zdd Zd
ddZd	S )CooperStorezK
    A container for handling quantifier ambiguity via Cooper storage.
    c                 C   sJ   || _ g | _z|d | _|d | _W n  tk
rD   td|  Y nX dS )z
        :param featstruct: The value of the ``sem`` node in a tree from
            ``parse_with_bindops()``
        :type featstruct: FeatStruct (with features ``core`` and ``store``)

        ZCOREZSTOREz$%s is not a Cooper storage structureN)
featstructreadingscorestoreKeyErrorprint)selfr    r   X/var/www/html/assets/scripts/venv/lib/python3.8/site-packages/nltk/sem/cooper_storage.py__init__   s    
zCooperStore.__init__c                 c   sL   dd }|rBt |D ]*\}}| |||D ]}|f| V  q,qndV  dS )z{
        :return: An iterator over the permutations of the input list
        :type lst: list
        :rtype: iter
        c                 S   s   | d | | |d d   S )N   r   )Zlst0indexr   r   r   <lambda>'       z&CooperStore._permute.<locals>.<lambda>r   N)	enumerate_permute)r   lstremover   xyr   r   r   r   !   s    zCooperStore._permuteFc                 C   s   t | | jD ]n\}}|r,td|d   | j}|D ]:}t|j\}}t|t|j	|}|rhtd| |
 }q6| j| qdS )aI  
        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)
        zPermutation %sr   z  N)r   r   r   r   r
   tupleargsr   r   variablesimplifyr	   append)r   tracepermZ
store_permtermZbindopZquantZvarexr   r   r   
s_retrieve/   s     


zCooperStore.s_retrieveN)F)__name__
__module____qualname____doc__r   r   r$   r   r   r   r   r      s   r   Nc                 C   s,   |sd}t ||td}|  }t||S )zC
    Use a grammar with Binding Operators to parse a sentence.
    z#grammars/book_grammars/storage.fcfg)r!   Zchart_class)r   r   splitlistparse)sentenceZgrammarr!   parsertokensr   r   r   parse_with_bindopsP   s
    r/   c                  C   s   ddl m}  d}t  td|  td | j|dd}|D ]}| | d }t  td td	 |jD ]}t| qnt  td
 td	 t|j t  td td	 |jdd td td	 t	|j
D ]\}}t|d  d|  qq<d S )Nr   )cooper_storagezevery girl chases a dogzAnalysis of sentence '%s'z2==================================================)r!   ZSEMzBinding operators:z---------------zCore:zS-Retrieval:Tz	Readings:r   z: )Znltk.semr0   r   r/   r   labelr   r
   r$   r   r	   )csr,   ZtreestreeZsemrepsireadingr   r   r   demo\   s2    


r7   __main__)Nr   )Z
nltk.parser   Znltk.parse.featurechartr   Znltk.sem.logicr   r   r   r   r/   r7   r%   r   r   r   r   <module>   s   C
