o
    rZh                     @   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rYe	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u r?e=d dS e<> ? Z<e2j@rSe=e<@ A  dS e<B  dS 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u 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   =/var/www/auris/lib/python3.10/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u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u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urt |t |ksJ ttjdd |D sJ 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 uV  qd S Nr   r7   idr   r   r   	<genexpr>       z.Boxer.interpret_multi_sents.<locals>.<genexpr>TF)r   c                    s   g | ]}  |d qS r;   )getr<   drs_dictr   r   r8          z/Boxer.interpret_multi_sents.<locals>.<listcomp>)lenr   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   rA   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   rK      s    
zBoxer._call_candcc                 C   s   d}z t jdddd\}}t|d}||d W |r"|  n|r*|  w w 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)tempfilemkstempr$   fdopenwritedecodecloser   r   rS   r)   remove)r   rO   r   ffdZtemp_filenamerV   stdoutr   r   r   rL      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u r1|g| }tj|tjtjd}nd||d|}tj|tjtjd	d
}| \}}|rdt d|j |r\t d|d |rdt d|d |jdkrwt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)ri   stderrzecho "{}" | {} {}T)ri   rm   shellzReturn code:zstdout:
rQ   zstderr:
r   z&ERROR CALLING: {} {}
Returncode: {}
{})	printr'   
subprocessPopenPIPEformatcommunicate
returncoder.   )	r   Z	input_strbinaryrV   r   cmdpri   rm   r   r   r   rS      s4   	




zBoxer._callc                 C   s  | dd}i }d}|t|k r|| }|dr|d}|d| }|d dkr8|d dkr8|d	d }||d	 |d
 }	|d	7 }|| }|d|	 dsVJ |dd  dkrf|d d d }|dsrJ d| td|	 d}
d	}d}t||
d  D ]3\}}|dkr|d	7 }|dkr|d	8 }|dkr|
| d	 }|||d  dkr|d }n|d	 } nq|dksJ ||d  }| |||}| j	
|||< |d	7 }|t|k s|S )Nr[   rQ   r   zid(,   rR      )zsem(z').'z).zcan't parse line: z,[[]z',')rd   splitrD   
startswithindexendswith	enumeratestrip
_parse_drsr   r5   )r   rP   rN   linesrB   ilineZ	comma_idxr1   Zdrs_idZsearch_startZbrace_countZ	drs_startjcZ	drs_inputparsedr   r   r   rM   "  sN   


$zBoxer._parse_to_drs_dictc                 C   s   t d |g| |S r;   )BoxerOutputDrsParserparse)r   Z
drs_stringr1   rN   r   r   r   r   L     zBoxer._parse_drs)NFNFTF)NFF)__name__
__module____qualname____doc__r   r   r5   r6   r:   r-   rK   rL   r"   rS   rM   r   r   r   r   r   r   ;   s*    








'
)*r   c                   @   s  e Zd Zd@d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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 )Ar   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)rR   rR   \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   [     zBoxerOutputDrsParser.parsec                 C   s   g dS )N)(r}   ry   r   r   :r   r   r   r   r   get_all_symbols^     z$BoxerOutputDrsParser.get_all_symbolsc                 C   s
   |  |S r;   )
handle_drs)r   tokcontextr   r   r   handlea     
zBoxerOutputDrsParser.handlec                 C      |S r;   r   r   Z
expressionr   r   r   r   attempt_adjunctsd     z%BoxerOutputDrsParser.attempt_adjunctsc                 C   s(   |   }| ||}|du 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v r| | jd g S |dv r"| | 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 ng|dkr!| | jg n[|dkr+|  g nQ|dkr5|  g nG|dkr?|  g n=|dkrI|  g n3|dkrS|  g n)|d	kr\| 	  n |d
krf| 
 g n|dkrp|  g n|dkrz|  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         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_duplexrT   _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   ry   r}   c                    s   t j| | S r;   	BoxerPredr1   r   rk   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   ry   r}   c                       t j| | S r;   BoxerWhqr1   r   	ans_typesZd1Zd2refr   r   r   <lambda>      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   ry   r}   c                    s   t j| | S r;   )
BoxerNamedr1   r   rk   r   r   typer   r   r   r     r   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   ry   r}   c                    s   t j| | S r;   )BoxerRelr1   r   r   r   r   var1var2r   r   r     r   z2BoxerOutputDrsParser._handle_rel.<locals>.<lambda>r   r   r   r   r   r     s   z BoxerOutputDrsParser._handle_relc                 C   sF   |  |  d |  }|  |  d | |}|  |  d |S )Nr   ry   r}   )r   r   r   _handle_time_expression)r   argZ	new_condsr   r   r   r     s   
z"BoxerOutputDrsParser._handle_timexc                    s       d dkr }ndkr  }nd S   d dd  fddgfd	d
|D  S )Nr   datetimer}   c                    s    fddS )Nc                    s    S r;   r   r   xr   r   r     s    zPBoxerOutputDrsParser._handle_time_expression.<locals>.func_gen.<locals>.<lambda>r   r   r   r   r   func_gen     z>BoxerOutputDrsParser._handle_time_expression.<locals>.func_genc                    s   t j| | ddS )Nnr   r   r   )r   r   r   r   r   r     r   z>BoxerOutputDrsParser._handle_time_expression.<locals>.<lambda>c                    s   g | ]} |qS r   r   r   )r   r   r   r8         z@BoxerOutputDrsParser._handle_time_expression.<locals>.<listcomp>)r   r   _handle_date_handle_time)r   r   r   r   )r   r   r   r   r   r   
  s   z,BoxerOutputDrsParser._handle_time_expressionc           	   
   C   st  g }|  |  \\}}| |  d |  }| |  d |t| j|||d| dd | |  d |  |  \\}}|  }|dkrb|dd	}|t| j|||d
| dd | |  d |  |  \\}}|  }|dkr|t| j|||d| dd | |  d |  |  \\}}|  }|dkr|t| j|||d| dd |S )Nr   r}   Z	date_pol_ar   ry   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r|| d|| | |  d |    |  }|dkr6|| d|| | |  d |    |  }|dkrT|| d|| |S )Nr   Zr_hour_2ry   Zr_min_2Zr_sec_2)r   r   r  
_make_atomr   )r   r   r   hourminsecr   r   r   r   m  s    z!BoxerOutputDrsParser._handle_timec                    sj       d       d       d       d  fddS )Nr   ry   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   ry   r}   c                    s   t j| | S r;   )	BoxerPropr1   r   r]   r   r   r   r   r         z3BoxerOutputDrsParser._handle_prop.<locals>.<lambda>)r   r   r   r   r   r   r  r   r     s   
z!BoxerOutputDrsParser._handle_propc                 C   sp   g }|  |  d | ddkr*||   | ddkr#|   | ddks|   |  |  d |S )Nr   r   r   ry   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r7|  }||   | ddkr0|   | ddks|   |  |  d |  |  d g }| ddkrr|  }|| | | ddkrk|   | ddksT|   |  |  d tt	||S )Nr   r   r   r   ry   r}   )
r   r   setr   addr   extendr   BoxerDrsrG   )r   refsr   r   r   r   r   r     s,   zBoxerOutputDrsParser.parse_drsc                    sT   |  |  d | d  |  |  d | d |  |  d  fddS )Nr   ry   r}   c                       | | S r;   r   r   drs1drs2make_callbackr   r   r         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   ry   r}   c                    r  r;   r   r   r  r   r   r     r  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   ry   r}   c                    s   t  j| |S r;   )BoxerEqr1   r   r   r   r   r   r   r     r  z1BoxerOutputDrsParser._handle_eq.<locals>.<lambda>r   r   r   r  r   r     s   zBoxerOutputDrsParser._handle_eqc                    s0    d   d g  ddkr\ }  d |dkr1   n$|dkrN d  }|d	krH d
 n | n   ddks    d d   d    d d   d  fddS )Nr   r   r   r   r   ZdesnumnumberZcoucountry   r}   c                    r   r;   r   r   r   r   r   r     r   z2BoxerOutputDrsParser._handle_whq.<locals>.<lambda>)r   r   r  r   r   )r   cattypr   r   r   r     s2   


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        z(BoxerOutputDrsParser._make_or_expressionc                 C   s   t |j|j|S r;   r$  r%  r   r   r   r        z)BoxerOutputDrsParser._make_imp_expressionc                 C   s    |   }td|sJ ||S )Nz^[exps]\d+$)r   rematchr   varr   r   r   r     s   z#BoxerOutputDrsParser.parse_variablec                 C   s   t |  S r;   )r   r   r   r   r   r   r    r   z BoxerOutputDrsParser.parse_indexc                    s\   dd |D }|r"g }|D ]  fdd|D }|  |f q|S dd |D }d|fg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 r0  r   r.  r   r   r   r8     rC   N)r  )r   r   Zsent_indicespairsr   r   r1  r   r     s   

z+BoxerOutputDrsParser._sent_and_word_indicesr;   )"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      t |  || _d S r;   )r   r   r1   r   r   r   r   r   '     

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   r   r;   r   r   r   r   r   r   4  r   zBoxerDrsParser.attempt_adjunctsc              
   C   s  z`|dkrn|  tj | jd u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 u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rI|  tj | jd u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r|  tj | jd u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 u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 urO| 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 ur| 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 u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 n tyw } z	t| jt||d }~ww J t|)Nr   r   r   r   r   r   r   r   r   r   )ZassertNextTokenr   r6  r1   r   r8  nullableIntTokenrG   rH   r   Zhandle_refsr7  r   r   r   r   r  r   r  r  r   r&  r  r
  r   r.   r   Z_currentIndexrI   repr)r   r   r   Zdisc_idZsent_idZword_idsr   rk   r   r   r   r   r   r   r]   r  r  r,  r  r   er   r   r   r   7  s.  

















zBoxerDrsParser.handlec                 C   s   |   }|dkrt|S d S )NNone)r   r   )r   tr   r   r   r9    s   zBoxerDrsParser.nullableIntTokenc              
   C   s2   z|   W S  ty } zt|jd|d }~ww )NzVariable expected.)r   r   r   )r   descriptionr;  r   r   r   get_next_token_variable  s   
z&BoxerDrsParser.get_next_token_variabler;   )
r   r   r   r   r   r   r   r   r9  r?  r   r   r   r   r3  "  s    
	 (r3  c                   @   L   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   rD    s   zAbstractBoxerDrs.variablesc                 C   s2   i }t d|  D ]\}}|D ]}|||< qq	|S )N)zr;  rx   )rU   rD  )r   Zvartypesr=  varsvr   r   r   variable_types  s   
zAbstractBoxerDrs.variable_typesc                 C   s   t  t  t  fS rB  r  r   r   r   r   rC    s   zAbstractBoxerDrs._variablesc                 C      t  S r;   rJ  r   r   r   r   atoms     zAbstractBoxerDrs.atomsc                 C      | S r;   r   r   r   r   r   clean  r   zAbstractBoxerDrs.cleanc                 C   s   | dd ddS )N-r   rR   )r  )r   rk   r   r   r   _clean_name     zAbstractBoxerDrs._clean_namec                 C   rN  r;   r   r   rg   r   r   r   renumber_sentences  r   z#AbstractBoxerDrs.renumber_sentencesc                 C   s
   t |  S r;   )hashr   r   r   r   __hash__
  r   zAbstractBoxerDrs.__hash__N)r   r   r   rD  rI  rC  rL  rO  rQ  rT  rV  r   r   r   r   rA    s    rA  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       t |  || _|| _|| _d S r;   )rA  r   r  r   
consequent)r   r  r   rX  r   r   r   r        

zBoxerDrs.__init__c                 C   sp   t  t  t  f}| jD ]}t|| D ]	\}}|| qq| jd ur6t|| j D ]	\}}|| q,|S r;   )r  r   rU   rC  updaterX  )r   rD  r   srH  r   r   r   rC    s   

zBoxerDrs._variablesc                 C   s:   t tjdd | jD t }| jd ur|| j  |S )Nc                 s   s    | ]}|  V  qd S r;   )rL  r   r   r   r   r>      r?   z!BoxerDrs.atoms.<locals>.<genexpr>)r   rE   or_r   r  rX  rZ  rL  )r   rL  r   r   r   rL    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   )rO  r7   r   r   r   r   r8   '  r   z"BoxerDrs.clean.<locals>.<listcomp>)rX  rO  r  r  r   )r   rX  r   r   r   rO  %  s   zBoxerDrs.cleanc                    s4   | j r	| j  nd }t| j fdd| jD |S )Nc                    s   g | ]}|  qS r   )rT  r]  rg   r   r   r8   ,  r   z/BoxerDrs.renumber_sentences.<locals>.<listcomp>)rX  rT  r  r  r   )r   rg   rX  r   r^  r   rT  )  s   zBoxerDrs.renumber_sentencesc                 C   sR   d ddd | jD ddd | jD }| jd ur'd| d| j d}|S )Nzdrs([{}], [{}]), c                 s       | ]}d | V  qdS %sNr   r7   rr   r   r   r>   1  r?   z$BoxerDrs.__repr__.<locals>.<genexpr>c                 s   r`  ra  r   r]  r   r   r   r>   2  r?   zimp(r}   )rs   r'   r  r   rX  )r   r[  r   r   r   __repr__/  s   
zBoxerDrs.__repr__c                 C   sX   | j |j ko+| j|jko+t| jt|jko+ttjdd t| j|jD o+| j|jkS )Nc                 s       | ]	\}}||kV  qd S r;   r   )r7   c1c2r   r   r   r>   >      z"BoxerDrs.__eq__.<locals>.<genexpr>)		__class__r  rD   r   r   rE   rF   rU   rX  r   otherr   r   r   __eq__8  s   

zBoxerDrs.__eq__c                 C   
   | |k S r;   r   rk  r   r   r   __ne__C  r   zBoxerDrs.__ne__r;   r   r   r   r   rC  rL  rO  rT  re  rm  ro  rA  rV  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   r4  r;   )rA  r   r]   r   r   r   r   r   J  r5  zBoxerNot.__init__c                 C   
   | j  S r;   )r]   rC  r   r   r   r   rC  N  r   zBoxerNot._variablesc                 C   rq  r;   r]   rL  r   r   r   r   rL  Q  r   zBoxerNot.atomsc                 C   s   t | j S r;   )r   r]   rO  r   r   r   r   rO  T  r   zBoxerNot.cleanc                 C   s   t | j|S r;   )r   r]   rT  rS  r   r   r   rT  W  r(  zBoxerNot.renumber_sentencesc                 C   s
   d| j  S )Nznot(%s)r]   r   r   r   r   re  Z  r   zBoxerNot.__repr__c                 C   s   | j |j ko| j|jkS r;   )rj  r]   rk  r   r   r   rm  ]     zBoxerNot.__eq__c                 C   rn  r;   r   rk  r   r   r   ro  `  r   zBoxerNot.__ne__Nrp  r   r   r   r   r   I  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   rW  r;   )rA  r   r1   r   r   )r   r1   r   r   r   r   r   r   g  rY  zBoxerIndexed.__init__c                 C   s   | hS r;   r   r   r   r   r   rL  m  rM  zBoxerIndexed.atomsc                 C   sL   | j |j ko%| j|jko%| j|jko%| j|jko%ttjdd t| |D S )Nc                 s   rf  r;   r   )r7   r[  or   r   r   r>   v  ri  z&BoxerIndexed.__eq__.<locals>.<genexpr>)rj  r1   r   r   r   rE   rF   rU   rk  r   r   r   rm  p  s   


zBoxerIndexed.__eq__c                 C   rn  r;   r   rk  r   r   r   ro  y  r   zBoxerIndexed.__ne__c              	   C   sH   d |  | j| jddd | jD }| D ]}|d| 7 }q|d S )Nz{}({}, {}, [{}]r_  c                 s   r`  ra  r   )r7   Zwir   r   r   r>     r?   z(BoxerIndexed.__repr__.<locals>.<genexpr>z, %sr}   )rs   _predr1   r   r'   r   )r   r[  rH  r   r   r   re  ~  s   zBoxerIndexed.__repr__N)
r   r   r   r   rL  rm  ro  rA  rV  re  r   r   r   r   ru  f  s    	ru  c                   @   D   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   ,   t | ||| || _|| _|| _|| _d S r;   )ru  r   r,  rk   r   r   )r   r1   r   r   r,  rk   r   r   r   r   r   r     
   
zBoxerPred.__init__c                 C      | j ht t fS r;   r,  r  r   r   r   r   rC    r'  zBoxerPred._variablesc                 C       t | j| j| j|| j| j| jS r;   )r   r1   r   r   rk   r   r   r+  r   r   r   
change_var     zBoxerPred.change_varc                 C   (   t | j| j| j| j| | j| j| jS r;   )	r   r1   r   r   r,  rQ  rk   r   r   r   r   r   r   rO       
zBoxerPred.cleanc                 C   s*   || j }t| j|| j| j| j| j| jS r;   )r   r   r1   r   r,  rk   r   r   )r   rg   Znew_sent_indexr   r   r   rT    s   
zBoxerPred.renumber_sentencesc                 C      t | j| j| j| jfS r;   )iterr,  rk   r   r   r   r   r   r   __iter__  rt  zBoxerPred.__iter__c                 C      dS )Nr   r   r   r   r   r   rw    r   zBoxerPred._predN
r   r   r   r   rC  r~  rO  rT  r  rw  r   r   r   r   r     s    r   c                   @   rx  )r   c                 C   ry  r;   )ru  r   r,  rk   r   r   )r   r1   r   r   r,  rk   r   r   r   r   r   r     rz  zBoxerNamed.__init__c                 C   r{  r;   r|  r   r   r   r   rC    r'  zBoxerNamed._variablesc                 C   r}  r;   )r   r1   r   r   rk   r   r   r+  r   r   r   r~    r  zBoxerNamed.change_varc                 C   r  r;   )	r   r1   r   r   r,  rQ  rk   r   r   r   r   r   r   rO    r  zBoxerNamed.cleanc                 C   &   t | j|| j| j| j| j| j| jS r;   )r   r1   r   r   r,  rk   r   r   rS  r   r   r   rT       zBoxerNamed.renumber_sentencesc                 C   r  r;   )r  r,  rk   r   r   r   r   r   r   r    rt  zBoxerNamed.__iter__c                 C   r  )Nr   r   r   r   r   r   rw    r   zBoxerNamed._predNr  r   r   r   r   r     s    r   c                   @   <   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   ry  r;   )ru  r   r   r   r   r   )r   r1   r   r   r   r   r   r   r   r   r   r     rz  zBoxerRel.__init__c                 C      | j | jht t fS r;   r   r   r  r   r   r   r   rC    r   zBoxerRel._variablesc              	   C   s(   t | j| j| j| j| j| | j| jS r;   )	r   r1   r   r   r   r   rQ  r   r   r   r   r   r   rO    s   
zBoxerRel.cleanc                 C   r  r;   )r   r1   r   r   r   r   r   r   rS  r   r   r   rT    r  zBoxerRel.renumber_sentencesc                 C   r  r;   )r  r   r   r   r   r   r   r   r   r    rt  zBoxerRel.__iter__c                 C   r  )Nr   r   r   r   r   r   rw    r   zBoxerRel._predN)	r   r   r   r   rC  rO  rT  r  rw  r   r   r   r   r     s    r   c                   @   r@  )r  c                 C       t | ||| || _|| _d S r;   )ru  r   r,  r]   )r   r1   r   r   r,  r]   r   r   r   r        
zBoxerProp.__init__c                 C   s&   t ttjt t | jhf| j S r;   )tuplerH   rE   r\  r  r,  r]   rC  r   r   r   r   rC        zBoxerProp._variablesc                 C   s   | j hS r;   rs  r   r   r   r   referenced_labels$  r   zBoxerProp.referenced_labelsc                 C   rq  r;   rr  r   r   r   r   rL  '  r   zBoxerProp.atomsc                 C   s   t | j| j| j| j| j S r;   )r  r1   r   r   r,  r]   rO  r   r   r   r   rO  *  s   zBoxerProp.cleanc                 C   s$   t | j|| j| j| j| j|S r;   )r  r1   r   r   r,  r]   rT  rS  r   r   r   rT  3  s   
zBoxerProp.renumber_sentencesc                 C      t | j| jfS r;   )r  r,  r]   r   r   r   r   r  <  r(  zBoxerProp.__iter__c                 C   r  )Nr   r   r   r   r   r   rw  ?  r   zBoxerProp._predN)r   r   r   r   rC  r  rL  rO  rT  r  rw  r   r   r   r   r    s    		r  c                   @   r  )r  c                 C   r  r;   )ru  r   r   r   )r   r1   r   r   r   r   r   r   r   r   D  r  zBoxerEq.__init__c                 C   r  r;   r  r   r   r   r   rC  I  r   zBoxerEq._variablesc                 C   rK  r;   rJ  r   r   r   r   rL  L  rM  zBoxerEq.atomsc                 C      t | j|| j| j| j| jS r;   )r  r1   r   r   r   r   rS  r   r   r   rT  O     zBoxerEq.renumber_sentencesc                 C   r  r;   )r  r   r   r   r   r   r   r  X  r(  zBoxerEq.__iter__c                 C   r  )Nr   r   r   r   r   r   rw  [  r   zBoxerEq._predN)	r   r   r   r   rC  rL  rT  r  rw  r   r   r   r   r  C  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   r{  r;   r|  r   r   r   r   rC  f  r'  zBoxerCard._variablesc                 C   s"   t | j|| j| j| j| j| jS r;   )r
  r1   r   r   r,  r  r   rS  r   r   r   rT  i  s   zBoxerCard.renumber_sentencesc                 C   s   t | j| j| jfS r;   )r  r,  r  r   r   r   r   r   r  s  rR  zBoxerCard.__iter__c                 C   r  )Nr   r   r   r   r   r   rw  v  r   zBoxerCard._predN)r   r   r   r   rC  rT  r  rw  r   r   r   r   r
  _  s    
r
  c                   @   rx  )r&  c                 C   r  r;   )ru  r   r  r  )r   r1   r   r   r  r  r   r   r   r   {  r  zBoxerOr.__init__c                 C   s   t ttj| j | j S r;   )r  rH   rE   r\  r  rC  r  r   r   r   r   rC    s   zBoxerOr._variablesc                 C      | j  | j B S r;   r  rL  r  r   r   r   r   rL    rR  zBoxerOr.atomsc                 C   s"   t | j| j| j| j | j S r;   )r&  r1   r   r   r  rO  r  r   r   r   r   rO    s   zBoxerOr.cleanc                 C   r  r;   )r&  r1   r   r   r  r  rS  r   r   r   rT    r  zBoxerOr.renumber_sentencesc                 C   r  r;   )r  r  r  r   r   r   r   r    r(  zBoxerOr.__iter__c                 C   r  )Nr   r   r   r   r   r   rw    r   zBoxerOr._predN
r   r   r   r   rC  rL  rO  rT  r  rw  r   r   r   r   r&  z  s    		r&  c                   @   rx  )r   c                 C   ry  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  rH   rE   r\  r   r  r  rC  r  r   r   r   r   rC    s   zBoxerWhq._variablesc                 C   r  r;   r  r   r   r   r   rL    rR  zBoxerWhq.atomsc              	   C   s*   t | j| j| j| j| j | j| j S r;   )	r   r1   r   r   r   r  rO  r   r  r   r   r   r   rO    s   zBoxerWhq.cleanc                 C   r  r;   )r   r1   r   r   r   r  r   r  rS  r   r   r   rT    r  zBoxerWhq.renumber_sentencesc                 C   s&   t dd| j d | j| j| jfS )Nr   ry   r   )r  r'   r   r  r   r  r   r   r   r   r    r  zBoxerWhq.__iter__c                 C   r  )Nr   r   r   r   r   r   rw    r   zBoxerWhq._predNr  r   r   r   r   r     s    	
r   c                   @   s   e Zd Zdd ZdS )PassthroughBoxerDrsInterpreterc                 C   r   r;   r   )r   exr   r   r   r5     r   z(PassthroughBoxerDrsInterpreter.interpretN)r   r   r   r5   r   r   r   r   r    s    r  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     r   z#NltkDrtBoxerDrsInterpreter.__init__c                 C   s  t |tr%tdd |jD tt| j|j}|jdur#| |j|_|S t |t	r2t
| |jS t |trK| |j d|j |}| ||jS t |tre| d|j d|j |}| ||jS t |tr|| d|j |}| ||j|jS t |trtt|j| |jS t |trt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 J |j%j& d	| )
zT
        :param ex: ``AbstractBoxerDrs``
        :return: ``DrtExpression``
        c                 S   s   g | ]}t |qS r   )r   rc  r   r   r   r8     r   z8NltkDrtBoxerDrsInterpreter.interpret.<locals>.<listcomp>Nr   Zne_rb  Zcard_Fz: )'
isinstancer  r   r  rG   rH   r5   r   rX  r   r	   r]   r   _add_occur_indexingr   rk   r  r,  r   r   r   r   r   r   r  r   r   r  r   r   r
  r  r&  r
   r  r  r   rj  r   )r   r  r]   r   r  r  r   r   r   r5     sF   










z$NltkDrtBoxerDrsInterpreter.interpretc                 G   s,   t t|}|D ]}t|t t|}q|S r;   )r   r   r   )r   r   rV   r   r   r   r   r   r    s   z%NltkDrtBoxerDrsInterpreter._make_atomc                 C   sL   | j r$|jd ur$|jr|d|j 7 }|d|j 7 }|dt|jd  7 }|S )Nz_%sz_s%sz_w%sr   )r  r   r1   sortedr   )r   baser  r   r   r   r    s   z.NltkDrtBoxerDrsInterpreter._add_occur_indexingNr   )r   r   r   r   r5   r  r  r   r   r   r   r     s
    
)r   c                   @   s   e Zd ZdS )UnparseableInputExceptionN)r   r   r   r   r   r   r   r    s    r  __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 indexr  r|   zincorrect number of arguments)r  z\n)r2   r   )Cr   rE   r$   r)  rp   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   r3  rA  r  r   ru  r   r   r   r  r  r
  r&  r   r  r   r.   r  r   opts
add_option
parse_argsoptionsrV   rD   errorr  interpreterr6   r   r2   r   r]   ro   simplifyZeliminate_equalityr  	normalizeZpretty_printr   r   r   r   <module>   s   	,     U H%;$43(*%6>


