U
    e                     @   s  d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZmZmZmZmZmZ ddlmZmZmZmZ G dd	 d	ZG d
d deZG dd deZG dd dZG dd deZG dd deZ G dd deZ!G dd de!Z"G dd de!Z#G dd de!Z$G dd de!Z%G dd de!Z&G d d! d!e!Z'G d"d# d#e!Z(G d$d% d%e!Z)G d&d' d'Z*G d(d) d)Z+G d*d+ d+e,Z-e.d,kre	d-Z/e/j0d.d/d0d1d2d3d4 e/j0d5d6d7d1d2d8d4 e/j0d9d:d;d1d2d<d4 e/j0d=d>d?d1d2d@d4 e/1 \Z2Z3e4e3dAkr:e/5dB e+e2j6dCZ7ee7j8e3d 9dDe2j:e2j;dEZ<e<dkr|e=d n.e<> ? Z<e2j@re=e<@ A  ne<B  dS )Fa  
An interface to Boxer.

This interface relies on the latest version of the development (subversion) version of
C&C and Boxer.

Usage
=====

Set the environment variable CANDC to the bin directory of your CandC installation.
The models directory should be in the CandC root directory.
For example::

    /path/to/candc/
    bin/
        candc
        boxer
    models/
        boxer/
    N)reduce)OptionParserfind_binary)	DRSDrtApplicationExpressionDrtEqualityExpressionDrtNegatedExpressionDrtOrExpression	DrtParserDrtProposition	DrtTokensDrtVariableExpression)ExpectedMoreTokensExceptionLogicalExpressionExceptionUnexpectedTokenExceptionVariablec                   @   s   e Zd ZdZdddZdddZdd	d
Zd ddZd!ddZd"ddZ	d#ddZ
d$ddZd%ddZg dfddZdd Zdd ZdS )&Boxerz
    This class is an interface to Johan Bos's program Boxer, a wide-coverage
    semantic parser that produces Discourse Representation Structures (DRSs).
    NFTc                 C   s0   |dkrt  }|| _|| _|| _| || dS )a  
        :param boxer_drs_interpreter: A class that converts from the
            ``AbstractBoxerDrs`` object hierarchy to a different object.  The
            default is ``NltkDrtBoxerDrsInterpreter``, which converts to the NLTK
            DRT hierarchy.
        :param elimeq: When set to true, Boxer removes all equalities from the
            DRSs and discourse referents standing in the equality relation are
            unified, but only if this can be done in a meaning-preserving manner.
        :param resolve: When set to true, Boxer will resolve all anaphoric DRSs and perform merge-reduction.
            Resolution follows Van der Sandt's theory of binding and accommodation.
        N)NltkDrtBoxerDrsInterpreter_boxer_drs_interpreter_resolve_elimeqset_bin_dir)selfZboxer_drs_interpreterZelimeqbin_dirverboseresolve r   O/var/www/html/assets/scripts/venv/lib/python3.8/site-packages/nltk/sem/boxer.py__init__A   s    zBoxer.__init__c                 C   sF   |  d||| _tjtj| jd d d| _|  d||| _d S )NZcandcz	../modelsboxer)_find_binary
_candc_binospathnormpathjoin_candc_models_path
_boxer_bin)r   r   r   r   r   r   r   ]   s
    zBoxer.set_bin_dirc                 C   s@   |dk	r|gnd}|  |gg|||\}|s<td| d|S )aI  
        Use Boxer to give a first order representation.

        :param input: str Input sentence to parse
        :param occur_index: bool Should predicates be occurrence indexed?
        :param discourse_id: str An identifier to be inserted to each occurrence-indexed predicate.
        :return: ``drt.DrtExpression``
        NUnable to interpret: ""interpret_multi_sents	Exceptionr   inputdiscourse_idquestionr   discourse_idsdr   r   r   	interpretd   s
    	zBoxer.interpretc                 C   s>   |dk	r|gnd}|  |g|||\}|s:td| d|S )ah  
        Use Boxer to give a first order representation.

        :param input: list of str Input sentences to parse as a single discourse
        :param occur_index: bool Should predicates be occurrence indexed?
        :param discourse_id: str An identifier to be inserted to each occurrence-indexed predicate.
        :return: ``drt.DrtExpression``
        Nr*   r+   r,   r/   r   r   r   interpret_multis   s
    	zBoxer.interpret_multic                 C   s   |  dd |D |||S )a{  
        Use Boxer to give a first order representation.

        :param inputs: list of str Input sentences to parse as individual discourses
        :param occur_index: bool Should predicates be occurrence indexed?
        :param discourse_ids: list of str Identifiers to be inserted to each occurrence-indexed predicate.
        :return: list of ``drt.DrtExpression``
        c                 S   s   g | ]
}|gqS r   r   ).0r0   r   r   r   
<listcomp>   s     z)Boxer.interpret_sents.<locals>.<listcomp>)r-   )r   inputsr3   r2   r   r   r   r   interpret_sents   s       zBoxer.interpret_sentsc                    s   |dk	r<t |t |kstttjdd |D s6td}nttttt |}d}| j	||||d}| j
||d}| ||  fdd|D S )	ac  
        Use Boxer to give a first order representation.

        :param inputs: list of list of str Input discourses to parse
        :param occur_index: bool Should predicates be occurrence indexed?
        :param discourse_ids: list of str Identifiers to be inserted to each occurrence-indexed predicate.
        :return: ``drt.DrtExpression``
        Nc                 s   s   | ]}|d k	V  qd S Nr   r7   idr   r   r   	<genexpr>   s     z.Boxer.interpret_multi_sents.<locals>.<genexpr>TF)r   c                    s   g | ]}  |d qS r;   )getr<   drs_dictr   r   r8      s     z/Boxer.interpret_multi_sents.<locals>.<listcomp>)lenAssertionErrorr   operatorand_listmapstrrange_call_candc_call_boxer_parse_to_drs_dict)r   r9   r3   r2   r   use_disc_id	candc_out	boxer_outr   r@   r   r-      s    zBoxer.interpret_multi_sentsc              	   C   sN   dt j| jddg| ddg}| dtdd t||D g | j||S )aF  
        Call the ``candc`` binary with the given input.

        :param inputs: list of list of str Input discourses to parse
        :param discourse_ids: list of str Identifiers to be inserted to each occurrence-indexed predicate.
        :param filename: str A filename for the output file
        :return: stdout
        z--modelsr!   Z	questionsz--candc-printer
c                 s   s$   | ]\}}d | dg| V  qdS )z<META>''Nr   )r7   r4   r=   r   r   r   r>      s     z$Boxer._call_candc.<locals>.<genexpr>)r$   r%   r'   r(   _callsumzipr#   )r   r9   r3   r2   r   argsr   r   r   rJ      s     
zBoxer._call_candcc                 C   s   d}z4tjdddd\}}t|d}||d W 5 |rF|   X dd	d
ddd	dg| j dd	dg| j ddddd|g}| 	d| j
||}t| |S )z
        Call the ``boxer`` binary with the given input.

        :param candc_out: str output from C&C parser
        :return: stdout
        Nzboxer-z.inT)prefixsuffixtextwutf-8z--boxfalsez--semanticsdrsz	--resolvetruez--elimeqz--formatprologz--instantiatez--input)closetempfilemkstempr$   fdopenwritedecoder   r   rR   r)   remove)r   rN   r   ffdZtemp_filenamerU   stdoutr   r   r   rK      s:      


zBoxer._call_boxerc                 C   s   t ||dgd||d g|dS )NZCANDCz)http://svn.ask.it.usyd.edu.au/trac/candc/z.exe)Zpath_to_binZenv_varsurlZbinary_namesr   r   )r   namer   r   r   r   r   r"      s    
zBoxer._find_binaryc           	      C   s   |r:t d| t d| t d| t d|d d|  |dkrb|g| }tj|tjtjd}n*d||d|}tj|tjtjd	d
}| \}}|rt d|j |rt d|d |rt d|d |jdkrtd|d||j||S )a  
        Call the binary with the given input.

        :param input_str: A string whose contents are used as stdin.
        :param binary: The location of the binary to call
        :param args: A list of command-line arguments.
        :return: stdout
        zCalling:zArgs:zInput:zCommand: N)rh   stderrzecho "{}" | {} {}T)rh   rl   shellzReturn code:zstdout:
rP   zstderr:
r   z&ERROR CALLING: {} {}
Returncode: {}
{})	printr'   
subprocessPopenPIPEformatcommunicate
returncoder.   )	r   Z	input_strbinaryrU   r   cmdprh   rl   r   r   r   rR      s@    	



   
   zBoxer._callc                 C   s  | dd}i }d}|t|k r|| }|dr|d}|d| }|d dkrt|d dkrt|d	d }||d	 |d
 }	|d	7 }|| }|d|	 dst|dd  dkr|d d d }|dstd| td|	 d}
d	}d}t||
d  D ]r\}}|dkr*|d	7 }|dkr|d	8 }|dkr|
| d	 }|||d  dkrr|d }n|d	 } qq|dkst||d  }| 	|||}| j
|||< |d	7 }q|S )NrZ   rP   r   zid(,   rQ      )zsem(z').'z).zcan't parse line: z,[[]z',')rd   splitrB   
startswithindexrC   endswith	enumeratestrip
_parse_drsr   r5   )r   rO   rM   linesrA   ilineZ	comma_idxr1   Zdrs_idZsearch_startZbrace_countZ	drs_startjcZ	drs_inputparsedr   r   r   rL   "  sJ    






zBoxer._parse_to_drs_dictc                 C   s   t d |g| |S r;   )BoxerOutputDrsParserparse)r   Z
drs_stringr1   rM   r   r   r   r   L  s    zBoxer._parse_drs)NFNFT)F)NFF)NFF)NFF)NFF)F)F)F)__name__
__module____qualname____doc__r   r   r5   r6   r:   r-   rJ   rK   r"   rR   rL   r   r   r   r   r   r   ;   s0        



     
     


'

)*r   c                   @   s  e Zd Zd@ddZdAddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!dS )Br   Nc                 C   s"   t |  || _d| _dg| _dS )zz
        This class is used to parse the Prolog DRS output from Boxer into a
        hierarchy of python objects.
        N)rQ   rQ   \F)r   r   r1   Zsentence_id_offsetZquote_charsr   r1   r   r   r   r   Q  s    
zBoxerOutputDrsParser.__init__c                 C   s   t | ||S r;   )r   r   )r   data	signaturer   r   r   r   [  s    zBoxerOutputDrsParser.parsec                 C   s   ddddddgS )N(r|   rx   r~   r   :r   r   r   r   r   get_all_symbols^  s    z$BoxerOutputDrsParser.get_all_symbolsc                 C   s
   |  |S r;   )
handle_drs)r   tokcontextr   r   r   handlea  s    zBoxerOutputDrsParser.handlec                 C   s   |S r;   r   r   Z
expressionr   r   r   r   attempt_adjunctsd  s    z%BoxerOutputDrsParser.attempt_adjunctsc                 C   s(   |   }| ||}|dkr$t||S )zS
        Parse a DRS condition

        :return: list of ``DrtExpression``
        N)tokenhandle_conditionr   )r   indicesr   accumr   r   r   parse_conditiong  s
    z$BoxerOutputDrsParser.parse_conditionc                 C   sH   |dkr|   S |dkr*| | jd g S |dkrD| | jd g S d S )Nr\   )mergeZsmerge)Zalfa)	parse_drs_handle_binary_expression_make_merge_expression_handle_alfa)r   r   r   r   r   r   s  s    zBoxerOutputDrsParser.handle_drsc                    s  |dkr|   gS |dkr*| | jg n|dkrB| | jg n|dkrV|  g n|dkrj|  g n|dkr~|  g nz|dkr|  g nf|dkr|  g nR|d	kr| 	  n@|d
kr| 
 g n,|dkr|  g n|dkr|  g ng  t fdd| |D g S )zx
        Handle a DRS condition

        :param indices: list of int
        :return: list of ``DrtExpression``
        notorimpeqpropprednamedrelZtimexcardwhqZduplexc                 3   s&   | ]\  fd dD V  qdS )c                    s   g | ]}| qS r   r   r7   cond
sent_indexword_indicesr   r   r8     s     zCBoxerOutputDrsParser.handle_condition.<locals>.<genexpr>.<listcomp>Nr   r7   condsr   r   r>     s   z8BoxerOutputDrsParser.handle_condition.<locals>.<genexpr>)_handle_notr   _make_or_expression_make_imp_expression
_handle_eq_handle_prop_handle_pred_handle_named_handle_rel_handle_timex_handle_card_handle_whq_handle_duplexrS   _sent_and_word_indices)r   r   r   r   r   r   r   {  s>    


z%BoxerOutputDrsParser.handle_conditionc                 C   s2   |  |  d | d }|  |  d t|S )Nr   r|   )assertTokenr   process_next_expressionBoxerNotr   r\   r   r   r   r     s    
z BoxerOutputDrsParser._handle_notc                    s     d    d     d    d t   d  fdd}|S )Nr   rx   r|   c                    s   t j| | S r;   	BoxerPredr1   r   rj   posr   sensevariabler   r   _handle_pred_f  s          z9BoxerOutputDrsParser._handle_pred.<locals>._handle_pred_fr   r   parse_variableint)r   r   r   r   r   r     s    z!BoxerOutputDrsParser._handle_predc                    s     d g    d   d d   d    d d   d  fddS )Nr   r   rx   r|   c                    s   t j| | S r;   BoxerWhqr1   r   	ans_typesZd1Zd2refr   r   r   <lambda>  s         z5BoxerOutputDrsParser._handle_duplex.<locals>.<lambda>)r   r   r   r   r   r   r   r   r     s    

z#BoxerOutputDrsParser._handle_duplexc                    s     d    d     d    d    d  fddS )Nr   rx   r|   c                    s   t j| | S r;   )
BoxerNamedr1   r   rj   r   r   typer   r   r   r     s         z4BoxerOutputDrsParser._handle_named.<locals>.<lambda>r   r   r   r   r   r   r   r     s    z"BoxerOutputDrsParser._handle_namedc                    s     d    d    d     d t   d  fddS )Nr   rx   r|   c                    s   t j| | S r;   )BoxerRelr1   r   r   r   r   var1var2r   r   r     s         z2BoxerOutputDrsParser._handle_rel.<locals>.<lambda>r   r   r   r   r   r     s    z BoxerOutputDrsParser._handle_relc                 C   sF   |  |  d |  }|  |  d | |}|  |  d |S )Nr   rx   r|   )r   r   r   _handle_time_expression)r   argZ	new_condsr   r   r   r     s    
z"BoxerOutputDrsParser._handle_timexc                    st       d dkr, }ndkr@ }nd S   d  fddgdd |D  S )	Nr   datetimer|   c                    s   t j| | ddS )Nnr   r   r   r   r   r   r   r   r     s         z>BoxerOutputDrsParser._handle_time_expression.<locals>.<lambda>c                    s   g | ]  fd dqS )c                    s    S r;   r   r   r   r   r   r         zIBoxerOutputDrsParser._handle_time_expression.<locals>.<listcomp>.<lambda>r   r   r   r   r   r8     s     z@BoxerOutputDrsParser._handle_time_expression.<locals>.<listcomp>)r   r   _handle_date_handle_time)r   r   r   r   r   r   r   
  s    z,BoxerOutputDrsParser._handle_time_expressionc           	   
   C   sx  g }|  |  \\}}| |  d |  }| |  d |t| j|||d| dd | |  d |  |  \\}}|  }|dkr|dd	}|t| j|||d
| dd | |  d |  |  \\}}|  }|dkr|t| j|||d| dd | |  d |  |  \\}}|  }|dkrt|t| j|||d| dd |S )Nr   r|   Z	date_pol_ar   rx   ZXXXXr   _Z
date_year_XXZdate_month_Z	date_day_)r   _parse_index_listr   r   appendr   r1   replace)	r   r   r   r   r   Zpolyearmonthdayr   r   r   r     s    





z!BoxerOutputDrsParser._handle_datec                 C   s   g }|    |  }|dkr0|| d|| | |  d |    |  }|dkrl|| d|| | |  d |    |  }|dkr|| d|| |S )Nr   Zr_hour_2rx   Zr_min_2Zr_sec_2)r   r   r   
_make_atomr   )r   r   r   hourminsecr   r   r   r   i  s     z!BoxerOutputDrsParser._handle_timec                    sj       d       d       d       d  fddS )Nr   rx   r|   c                    s   t  j| |S r;   )	BoxerCardr1   r   r   r   valuer   r   r   r     s        z3BoxerOutputDrsParser._handle_card.<locals>.<lambda>r   r   r   r   r   r     s    z!BoxerOutputDrsParser._handle_cardc                    sR     d    d d    d  fddS )Nr   rx   r|   c                    s   t j| | S r;   )	BoxerPropr1   r   r\   r   r   r   r   r     s       z3BoxerOutputDrsParser._handle_prop.<locals>.<lambda>)r   r   r   r   r   r   r  r   r     s    
z!BoxerOutputDrsParser._handle_propc                 C   sd   g }|  |  d | ddkrH||   | ddkr|   q|   |  |  d |S )Nr~   r   r   rx   r   )r   r   r   parse_index)r   r   r   r   r   r     s    
z&BoxerOutputDrsParser._parse_index_listc                 C   s   |  |  d |  |  d t }| ddkrb|  }||   | ddkr&|   q&|   |  |  d |  |  d g }| ddkr|  }|| | | ddkr|   q|   |  |  d tt	||S )Nr   r~   r   r   rx   r|   )
r   r   setr   addr   extendr   BoxerDrsrF   )r   refsr   r   r   r   r   r     s(    

zBoxerOutputDrsParser.parse_drsc                    sT   |  |  d | d  |  |  d | d |  |  d  fddS )Nr   rx   r|   c                    s   | | S r;   r   r   drs1drs2make_callbackr   r   r     s
      z@BoxerOutputDrsParser._handle_binary_expression.<locals>.<lambda>r   r   r   )r   r  r   r	  r   r     s    

z.BoxerOutputDrsParser._handle_binary_expressionc                    sl   |  |  d |  }|  |  d | d  |  |  d | d |  |  d  fddS )Nr   rx   r|   c                    s   | | S r;   r   r   r	  r   r   r     s
      z3BoxerOutputDrsParser._handle_alfa.<locals>.<lambda>r  )r   r  r   r   r	  r   r     s    

z!BoxerOutputDrsParser._handle_alfac                    sP       d       d       d  fddS )Nr   rx   r|   c                    s   t  j| |S r;   )BoxerEqr1   r   r   r   r   r   r   r     s       z1BoxerOutputDrsParser._handle_eq.<locals>.<lambda>r   r   r   r  r   r     s    zBoxerOutputDrsParser._handle_eqc                    s$    d   d g  ddkr }  d |dkrb   q$|dkr d  }|d	kr d
 q | q$   q$    d d   d    d d   d  fddS )Nr   r~   r   r   r   ZdesnumnumberZcoucountrx   r|   c                    s   t j| | S r;   r   r   r   r   r   r     s         z2BoxerOutputDrsParser._handle_whq.<locals>.<lambda>)r   r   r   r   r   )r   cattypr   r   r   r     s0    


z BoxerOutputDrsParser._handle_whqc                 C   s   t |j|j |j|j S r;   r  r  r   r   r   r   r
  r  r   r   r   r     s    z+BoxerOutputDrsParser._make_merge_expressionc                 C   s   t | j||||S r;   )BoxerOrr1   r  r   r   r   r     s    z(BoxerOutputDrsParser._make_or_expressionc                 C   s   t |j|j|S r;   r  r  r   r   r   r     s    z)BoxerOutputDrsParser._make_imp_expressionc                 C   s    |   }td|st||S )Nz^[exps]\d+$)r   rematchrC   r   varr   r   r   r     s    z#BoxerOutputDrsParser.parse_variablec                 C   s   t |  S r;   )r   r   r   r   r   r   r  	  s    z BoxerOutputDrsParser.parse_indexc                    s`   dd |D }|rDg }|D ]$  fdd|D }|  |f q|S dd |D }d|fgS dS )zD
        :return: list of (sent_index, word_indices) tuples
        c                 S   s    h | ]}|d kr|d d qS )r     r{   r   r7   r   r   r   r   	<setcomp>  s      z>BoxerOutputDrsParser._sent_and_word_indices.<locals>.<setcomp>c                    s(   g | ] } |d  d kr|d  d qS r  r{   r   r  r   r   r   r8     s     z?BoxerOutputDrsParser._sent_and_word_indices.<locals>.<listcomp>c                 S   s   g | ]}|d  d qS r  r   r  r   r   r   r8     s     N)r   )r   r   Zsent_indicespairsr   r   r   r   r     s    
z+BoxerOutputDrsParser._sent_and_word_indices)N)N)"r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r   r   P  s>   


.!	N

r   c                   @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )BoxerDrsParserzD
    Reparse the str form of subclasses of ``AbstractBoxerDrs``
    Nc                 C   s   t |  || _d S r;   )r   r   r1   r   r   r   r   r   #  s    
zBoxerDrsParser.__init__c                 C   s   t jt jt jt jt jgS r;   )r   OPENCLOSECOMMAZOPEN_BRACKETZCLOSE_BRACKETr   r   r   r   r   '  s    zBoxerDrsParser.get_all_symbolsc                 C   s   |S r;   r   r   r   r   r   r   0  s    zBoxerDrsParser.attempt_adjunctsc              
   C   s  z|dkr|  tj | jd k	r(| jn|  }|  tj |  }|  tj ttt	| 
 }|  tj t	|  }|  tj |  }|  tj |  }|  tj t	|  }	|  tj t|||||||	W S |dkr|  tj | jd k	r| jn|  }|  tj t	|  }|  tj tt	| 
 }|  tj t	|  }|  tj |  }|  tj |  }
|  tj t	|  }	|  tj t||||||
|	W S |dkr|  tj | jd k	r| jn|  }|  tj |  }|  tj ttt	| 
 }|  tj t	|  }|  tj t	|  }|  tj |  }|  tj t	|  }	|  tj t|||||||	W S |dkrJ|  tj | jd k	r| jn|  }|  tj t	|  }|  tj ttt	| 
 }|  tj t	|  }|  tj | d }|  tj t|||||W S |dkr|  tj | d }|  tj t|W S |dkr|  tj | d }|  tj | d }|  tj t|j|j|W S |dkr||  tj | jd k	r| jn|  }|  tj |  }|  tj tt	| 
 }|  tj | d }|  tj | d }|  tj t|||||W S |dkr,|  tj | jd k	r| jn|  }|  tj |  }|  tj ttt	| 
 }|  tj t	|  }|  tj t	|  }|  tj t|||||W S |d	kr|  tj | jd k	rT| jn|  }|  tj |  }|  tj tt	| 
 }|  tj t	|  }|  tj |  }|  tj |  }
|  tj t||||||
W S |d
kr|  tj | jd k	r| jn|  }|  tj |  }|  tj ttt	| 
 }|  tj | 
 }|  tj | d }|  tj t	|  }|  tj | d }|  tj t|||||||W S W n6 tk
r } zt| jt||W 5 d }~X Y nX dstt|d S )Nr   r   r   r   r   r   r   r   r   r   F)ZassertNextTokenr   r#  r1   r   r%  nullableIntTokenrF   rG   r   Zhandle_refsr$  r   r   r   r   r  r   r  r  r   r  r  r   r   r.   r   Z_currentIndexrH   rC   repr)r   r   r   Zdisc_idZsent_idZword_idsr   rj   r   r   r   r   r   r   r\   r
  r  r  r   r   er   r   r   r   3  s4   

      
















$zBoxerDrsParser.handlec                 C   s   |   }|dkrt|S d S )NNone)r   r   )r   tr   r   r   r&    s    zBoxerDrsParser.nullableIntTokenc              
   C   s@   z
|   W S  tk
r: } zt|jd|W 5 d }~X Y nX d S )NzVariable expected.)r   r   r   )r   descriptionr(  r   r   r   get_next_token_variable  s    
z&BoxerDrsParser.get_next_token_variable)N)
r   r   r   r   r   r   r   r   r&  r,  r   r   r   r   r"    s   
	 (r"  c                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )AbstractBoxerDrsc                 C   s$   |   \}}}|||B  ||| fS zK
        :return: (set<variables>, set<events>, set<propositions>)
        )
_variables)r   	variableseventsZpropositionsr   r   r   r0    s    zAbstractBoxerDrs.variablesc                 C   s2   i }t d|  D ]\}}|D ]}|||< qq|S )N)zr(  rw   )rT   r0  )r   Zvartypesr*  varsvr   r   r   variable_types  s
    zAbstractBoxerDrs.variable_typesc                 C   s   t  t  t  fS r.  r  r   r   r   r   r/    s    zAbstractBoxerDrs._variablesc                 C   s   t  S r;   r6  r   r   r   r   atoms  s    zAbstractBoxerDrs.atomsc                 C   s   | S r;   r   r   r   r   r   clean  s    zAbstractBoxerDrs.cleanc                 C   s   | dd ddS )N-r   rQ   )r   )r   rj   r   r   r   _clean_name   s    zAbstractBoxerDrs._clean_namec                 C   s   | S r;   r   r   rf   r   r   r   renumber_sentences  s    z#AbstractBoxerDrs.renumber_sentencesc                 C   s
   t |  S r;   )hashr   r   r   r   __hash__  s    zAbstractBoxerDrs.__hash__N)r   r   r   r0  r5  r/  r7  r8  r:  r<  r>  r   r   r   r   r-    s   r-  c                   @   sT   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
ejZdS )r  Nc                 C   s    t |  || _|| _|| _d S r;   )r-  r   r  r   
consequent)r   r  r   r?  r   r   r   r     s    
zBoxerDrs.__init__c                 C   sp   t  t  t  f}| jD ]&}t|| D ]\}}|| q(q| jd k	rlt|| j D ]\}}|| qX|S r;   )r  r   rT   r/  updater?  )r   r0  r   sr4  r   r   r   r/    s    

zBoxerDrs._variablesc                 C   s:   t tjdd | jD t }| jd k	r6|| j  |S )Nc                 s   s   | ]}|  V  qd S r;   )r7  r   r   r   r   r>     s     z!BoxerDrs.atoms.<locals>.<genexpr>)r   rD   or_r   r  r?  r@  r7  )r   r7  r   r   r   r7    s    
zBoxerDrs.atomsc                 C   s.   | j r| j  nd }t| jdd | jD |S )Nc                 S   s   g | ]}|  qS r   )r8  r7   r   r   r   r   r8   #  s     z"BoxerDrs.clean.<locals>.<listcomp>)r?  r8  r  r  r   )r   r?  r   r   r   r8  !  s    zBoxerDrs.cleanc                    s4   | j r| j  nd }t| j fdd| jD |S )Nc                    s   g | ]}|  qS r   )r<  rC  rf   r   r   r8   (  s     z/BoxerDrs.renumber_sentences.<locals>.<listcomp>)r?  r<  r  r  r   )r   rf   r?  r   rD  r   r<  %  s      zBoxerDrs.renumber_sentencesc                 C   sR   d ddd | jD ddd | jD }| jd k	rNd| d| j d}|S )Nzdrs([{}], [{}]), c                 s   s   | ]}d | V  qdS %sNr   r7   rr   r   r   r>   -  s     z$BoxerDrs.__repr__.<locals>.<genexpr>c                 s   s   | ]}d | V  qdS rF  r   rC  r   r   r   r>   .  s     zimp(r|   )rr   r'   r  r   r?  )r   rA  r   r   r   __repr__+  s    
zBoxerDrs.__repr__c                 C   sX   | j |j koV| j|jkoVt| jt|jkoVttjdd t| j|jD oV| j|jkS )Nc                 s   s   | ]\}}||kV  qd S r;   r   )r7   c1c2r   r   r   r>   :  s     z"BoxerDrs.__eq__.<locals>.<genexpr>)		__class__r  rB   r   r   rD   rE   rT   r?  r   otherr   r   r   __eq__4  s    
 
zBoxerDrs.__eq__c                 C   s
   | |k S r;   r   rN  r   r   r   __ne__?  s    zBoxerDrs.__ne__)Nr   r   r   r   r/  r7  r8  r<  rJ  rP  rQ  r-  r>  r   r   r   r   r  
  s   

	r  c                   @   sR   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
ejZdS )r   c                 C   s   t |  || _d S r;   )r-  r   r\   r   r   r   r   r   F  s    
zBoxerNot.__init__c                 C   s
   | j  S r;   )r\   r/  r   r   r   r   r/  J  s    zBoxerNot._variablesc                 C   s
   | j  S r;   r\   r7  r   r   r   r   r7  M  s    zBoxerNot.atomsc                 C   s   t | j S r;   )r   r\   r8  r   r   r   r   r8  P  s    zBoxerNot.cleanc                 C   s   t | j|S r;   )r   r\   r<  r;  r   r   r   r<  S  s    zBoxerNot.renumber_sentencesc                 C   s
   d| j  S )Nznot(%s)r\   r   r   r   r   rJ  V  s    zBoxerNot.__repr__c                 C   s   | j |j ko| j|jkS r;   )rM  r\   rN  r   r   r   rP  Y  s    zBoxerNot.__eq__c                 C   s
   | |k S r;   r   rN  r   r   r   rQ  \  s    zBoxerNot.__ne__NrR  r   r   r   r   r   E  s   r   c                   @   s:   e Zd Zdd Zdd Zdd Zdd ZejZd	d
 Z	dS )BoxerIndexedc                 C   s    t |  || _|| _|| _d S r;   )r-  r   r1   r   r   )r   r1   r   r   r   r   r   r   c  s    
zBoxerIndexed.__init__c                 C   s   | hS r;   r   r   r   r   r   r7  i  s    zBoxerIndexed.atomsc                 C   sL   | j |j koJ| j|jkoJ| j|jkoJ| j|jkoJttjdd t| |D S )Nc                 s   s   | ]\}}||kV  qd S r;   r   )r7   rA  or   r   r   r>   r  s     z&BoxerIndexed.__eq__.<locals>.<genexpr>)rM  r1   r   r   r   rD   rE   rT   rN  r   r   r   rP  l  s    


zBoxerIndexed.__eq__c                 C   s
   | |k S r;   r   rN  r   r   r   rQ  u  s    zBoxerIndexed.__ne__c              	   C   sH   d |  | j| jddd | jD }| D ]}|d| 7 }q.|d S )Nz{}({}, {}, [{}]rE  c                 s   s   | ]}d | V  qdS rF  r   )r7   Zwir   r   r   r>     s     z(BoxerIndexed.__repr__.<locals>.<genexpr>z, %sr|   )rr   _predr1   r   r'   r   )r   rA  r4  r   r   r   rJ  z  s    zBoxerIndexed.__repr__N)
r   r   r   r   r7  rP  rQ  r-  r>  rJ  r   r   r   r   rU  b  s   	rU  c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )r   c                 C   s,   t | ||| || _|| _|| _|| _d S r;   )rU  r   r  rj   r   r   )r   r1   r   r   r  rj   r   r   r   r   r   r     s
    zBoxerPred.__init__c                 C   s   | j ht t fS r;   r  r  r   r   r   r   r/    s    zBoxerPred._variablesc                 C   s    t | j| j| j|| j| j| jS r;   )r   r1   r   r   rj   r   r   r  r   r   r   
change_var  s    zBoxerPred.change_varc                 C   s(   t | j| j| j| j| | j| j| jS r;   )	r   r1   r   r   r  r:  rj   r   r   r   r   r   r   r8    s    
zBoxerPred.cleanc                 C   s*   || j }t| j|| j| j| j| j| jS r;   )r   r   r1   r   r  rj   r   r   )r   rf   Znew_sent_indexr   r   r   r<    s    
zBoxerPred.renumber_sentencesc                 C   s   t | j| j| j| jfS r;   )iterr  rj   r   r   r   r   r   r   __iter__  s    zBoxerPred.__iter__c                 C   s   dS )Nr   r   r   r   r   r   rW    s    zBoxerPred._predN
r   r   r   r   r/  rY  r8  r<  r[  rW  r   r   r   r   r     s   r   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )r   c                 C   s,   t | ||| || _|| _|| _|| _d S r;   )rU  r   r  rj   r   r   )r   r1   r   r   r  rj   r   r   r   r   r   r     s
    zBoxerNamed.__init__c                 C   s   | j ht t fS r;   rX  r   r   r   r   r/    s    zBoxerNamed._variablesc                 C   s    t | j| j| j|| j| j| jS r;   )r   r1   r   r   rj   r   r   r  r   r   r   rY    s    zBoxerNamed.change_varc                 C   s(   t | j| j| j| j| | j| j| jS r;   )	r   r1   r   r   r  r:  rj   r   r   r   r   r   r   r8    s    
zBoxerNamed.cleanc                 C   s&   t | j|| j| j| j| j| j| jS r;   )r   r1   r   r   r  rj   r   r   r;  r   r   r   r<    s    zBoxerNamed.renumber_sentencesc                 C   s   t | j| j| j| jfS r;   )rZ  r  rj   r   r   r   r   r   r   r[    s    zBoxerNamed.__iter__c                 C   s   dS )Nr   r   r   r   r   r   rW    s    zBoxerNamed._predNr\  r   r   r   r   r     s   r   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )r   c                 C   s,   t | ||| || _|| _|| _|| _d S r;   )rU  r   r   r   r   r   )r   r1   r   r   r   r   r   r   r   r   r   r     s
    zBoxerRel.__init__c                 C   s   | j | jht t fS r;   r   r   r  r   r   r   r   r/    s    zBoxerRel._variablesc              	   C   s(   t | j| j| j| j| j| | j| jS r;   )	r   r1   r   r   r   r   r:  r   r   r   r   r   r   r8    s    
zBoxerRel.cleanc                 C   s&   t | j|| j| j| j| j| j| jS r;   )r   r1   r   r   r   r   r   r   r;  r   r   r   r<    s    zBoxerRel.renumber_sentencesc                 C   s   t | j| j| j| jfS r;   )rZ  r   r   r   r   r   r   r   r   r[    s    zBoxerRel.__iter__c                 C   s   dS )Nr   r   r   r   r   r   rW    s    zBoxerRel._predN)	r   r   r   r   r/  r8  r<  r[  rW  r   r   r   r   r     s   r   c                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )r  c                 C   s    t | ||| || _|| _d S r;   )rU  r   r  r\   )r   r1   r   r   r  r\   r   r   r   r     s    zBoxerProp.__init__c                 C   s&   t ttjt t | jhf| j S r;   )tuplerG   rD   rB  r  r  r\   r/  r   r   r   r   r/    s     zBoxerProp._variablesc                 C   s   | j hS r;   rT  r   r   r   r   referenced_labels   s    zBoxerProp.referenced_labelsc                 C   s
   | j  S r;   rS  r   r   r   r   r7  #  s    zBoxerProp.atomsc                 C   s   t | j| j| j| j| j S r;   )r  r1   r   r   r  r\   r8  r   r   r   r   r8  &  s    zBoxerProp.cleanc                 C   s$   t | j|| j| j| j| j|S r;   )r  r1   r   r   r  r\   r<  r;  r   r   r   r<  /  s    
zBoxerProp.renumber_sentencesc                 C   s   t | j| jfS r;   )rZ  r  r\   r   r   r   r   r[  8  s    zBoxerProp.__iter__c                 C   s   dS )Nr   r   r   r   r   r   rW  ;  s    zBoxerProp._predN)r   r   r   r   r/  r_  r7  r8  r<  r[  rW  r   r   r   r   r    s   		r  c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )r  c                 C   s    t | ||| || _|| _d S r;   )rU  r   r   r   )r   r1   r   r   r   r   r   r   r   r   @  s    zBoxerEq.__init__c                 C   s   | j | jht t fS r;   r]  r   r   r   r   r/  E  s    zBoxerEq._variablesc                 C   s   t  S r;   r6  r   r   r   r   r7  H  s    zBoxerEq.atomsc                 C   s   t | j|| j| j| j| jS r;   )r  r1   r   r   r   r   r;  r   r   r   r<  K  s    zBoxerEq.renumber_sentencesc                 C   s   t | j| jfS r;   )rZ  r   r   r   r   r   r   r[  T  s    zBoxerEq.__iter__c                 C   s   dS )Nr   r   r   r   r   r   rW  W  s    zBoxerEq._predN)	r   r   r   r   r/  r7  r<  r[  rW  r   r   r   r   r  ?  s   	r  c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )r   c                 C   s&   t | ||| || _|| _|| _d S r;   )rU  r   r  r   r   )r   r1   r   r   r  r   r   r   r   r   r   \  s    zBoxerCard.__init__c                 C   s   | j ht t fS r;   rX  r   r   r   r   r/  b  s    zBoxerCard._variablesc                 C   s"   t | j|| j| j| j| j| jS r;   )r   r1   r   r   r  r   r   r;  r   r   r   r<  e  s    zBoxerCard.renumber_sentencesc                 C   s   t | j| j| jfS r;   )rZ  r  r   r   r   r   r   r   r[  o  s    zBoxerCard.__iter__c                 C   s   dS )Nr   r   r   r   r   r   rW  r  s    zBoxerCard._predN)r   r   r   r   r/  r<  r[  rW  r   r   r   r   r   [  s
   
r   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )r  c                 C   s    t | ||| || _|| _d S r;   )rU  r   r
  r  )r   r1   r   r   r
  r  r   r   r   r   w  s    zBoxerOr.__init__c                 C   s   t ttj| j | j S r;   )r^  rG   rD   rB  r
  r/  r  r   r   r   r   r/  |  s    zBoxerOr._variablesc                 C   s   | j  | j B S r;   r
  r7  r  r   r   r   r   r7    s    zBoxerOr.atomsc                 C   s"   t | j| j| j| j | j S r;   )r  r1   r   r   r
  r8  r  r   r   r   r   r8    s    zBoxerOr.cleanc                 C   s   t | j|| j| j| j| jS r;   )r  r1   r   r   r
  r  r;  r   r   r   r<    s    zBoxerOr.renumber_sentencesc                 C   s   t | j| jfS r;   )rZ  r
  r  r   r   r   r   r[    s    zBoxerOr.__iter__c                 C   s   dS )Nr   r   r   r   r   r   rW    s    zBoxerOr._predN
r   r   r   r   r/  r7  r8  r<  r[  rW  r   r   r   r   r  v  s   		r  c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )r   c                 C   s,   t | ||| || _|| _|| _|| _d S r;   )rU  r   r   r
  r   r  )r   r1   r   r   r   r
  r   r  r   r   r   r     s
    zBoxerWhq.__init__c                 C   s.   t ttj| jht t f| j | j S r;   )	r^  rG   rD   rB  r   r  r
  r/  r  r   r   r   r   r/    s    zBoxerWhq._variablesc                 C   s   | j  | j B S r;   r`  r   r   r   r   r7    s    zBoxerWhq.atomsc              	   C   s*   t | j| j| j| j| j | j| j S r;   )	r   r1   r   r   r   r
  r8  r   r  r   r   r   r   r8    s    zBoxerWhq.cleanc                 C   s&   t | j|| j| j| j| j| j| jS r;   )r   r1   r   r   r   r
  r   r  r;  r   r   r   r<    s    zBoxerWhq.renumber_sentencesc                 C   s&   t dd| j d | j| j| jfS )Nr~   rx   r   )rZ  r'   r   r
  r   r  r   r   r   r   r[    s     zBoxerWhq.__iter__c                 C   s   dS )Nr   r   r   r   r   r   rW    s    zBoxerWhq._predNra  r   r   r   r   r     s   	
r   c                   @   s   e Zd Zdd ZdS )PassthroughBoxerDrsInterpreterc                 C   s   |S r;   r   )r   exr   r   r   r5     s    z(PassthroughBoxerDrsInterpreter.interpretN)r   r   r   r5   r   r   r   r   rb    s   rb  c                   @   s.   e Zd ZdddZdd Zdd Zdd	 Zd
S )r   Fc                 C   s
   || _ d S r;   )_occur_index)r   occur_indexr   r   r   r     s    z#NltkDrtBoxerDrsInterpreter.__init__c                 C   s  t |trJtdd |jD tt| j|j}|jdk	rF| |j|_|S t |t	rdt
| |jS t |tr| |j d|j |}| ||jS t |tr| d|j d|j |}| ||jS t |t r| d|j |}| ||j|jS t |trtt|j| |jS t |trHttt|jtt|jS t |tr~| d|j d|j |}| ||jS t |t rt!| |j"| |j#S t |t$r| |j"}| |j#}t|j|j |j|j S dst%|j&j' d	| dS )
zT
        :param ex: ``AbstractBoxerDrs``
        :return: ``DrtExpression``
        c                 S   s   g | ]}t |qS r   )r   rH  r   r   r   r8     s     z8NltkDrtBoxerDrsInterpreter.interpret.<locals>.<listcomp>Nr   Zne_rG  Zcard_Fz: )(
isinstancer  r   r  rF   rG   r5   r   r?  r   r	   r\   r   _add_occur_indexingr   rj   r   r  r   r   r   r   r   r   r  r   r   r  r   r   r   r   r  r
   r
  r  r   rC   rM  r   )r   rc  r\   r   r
  r  r   r   r   r5     sH    
 



z$NltkDrtBoxerDrsInterpreter.interpretc                 G   s,   t t|}|D ]}t|t t|}q|S r;   )r   r   r   )r   r   rU   r   r   r   r   r   r     s     
z%NltkDrtBoxerDrsInterpreter._make_atomc                 C   sL   | j rH|jd k	rH|jr$|d|j 7 }|d|j 7 }|dt|jd  7 }|S )Nz_%sz_s%sz_w%sr   )rd  r   r1   sortedr   )r   baserc  r   r   r   rg    s    z.NltkDrtBoxerDrsInterpreter._add_occur_indexingN)F)r   r   r   r   r5   r   rg  r   r   r   r   r     s   
)r   c                   @   s   e Zd ZdS )UnparseableInputExceptionN)r   r   r   r   r   r   r   rj    s   rj  __main__zusage: %prog TEXT [options]z	--verbosez-vzdisplay verbose logs
store_trueFr   )helpactiondefaultdestz--folz-fz
output FOLfolz
--questionz-qzinput is a questionr2   z--occurz-ozoccurrence indexre  r{   zincorrect number of arguments)re  z\n)r2   r   )Cr   rD   r$   r  ro   r`   	functoolsr   optparser   Znltk.internalsr   Znltk.sem.drtr   r   r   r	   r
   r   r   r   r   Znltk.sem.logicr   r   r   r   r   r   r"  r-  r  r   rU  r   r   r   r  r  r   r  r   rb  r   r.   rj  r   opts
add_option
parse_argsoptionsrU   rB   errorre  interpreterr6   r   r2   r   r\   rn   simplifyZeliminate_equalityrq  	normalizeZpretty_printr   r   r   r   <module>
   s   ,     Q H%;$43(*%6>
     
  

