3
d                 @   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/j1 \Z2Z3e4e3dAkr:e/j5dB e+e2j6dCZ7ee7j8e3d j9dDe2j:e2j;dEZ<e<dkr|e=d n.e<j> j? Z<e2j@re=e<j@ jA  ne<jB  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  }|| _|| _|| _| j|| 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   ./tmp/pip-build-v9q4h5k9/nltk/nltk/sem/boxer.py__init__A   s    zBoxer.__init__c             C   sF   | j d||| _tjjtjj| jd d d| _| j d||| _d S )NZcandc   z	../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}| j |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``
        Nz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}| j |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``
        NzUnable to interpret: "r*   )r+   r,   )r   r-   r.   r/   r   r0   r1   r   r   r   interpret_multis   s
    	zBoxer.interpret_multic             C   s   | j 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   ).0r-   r   r   r   
<listcomp>   s    z)Boxer.interpret_sents.<locals>.<listcomp>)r+   )r   inputsr0   r/   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}| j||  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   )r4   idr   r   r   	<genexpr>   s    z.Boxer.interpret_multi_sents.<locals>.<genexpr>TF)r   c                s   g | ]} j |d qS )N)get)r4   r8   )drs_dictr   r   r5      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   r6   r0   r/   r   use_disc_id	candc_out	boxer_outr   )r;   r   r+      s    zBoxer.interpret_multi_sentsc             C   sN   dt jj| jddg| ddg}| jdj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   )r4   r1   r8   r   r   r   r9      s    z$Boxer._call_candc.<locals>.<genexpr>)r$   r%   r'   r(   _callsumzipr#   )r   r6   r0   r/   r   argsr   r   r   rD      s    
zBoxer._call_candcc             C   s   d}z.t jdddd\}}tj|d}|j| W d|r@|j  X ddd	d
dddg| j dddg| j ddddd|g}| jd| j	||}tj
| |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wz--boxfalsez--semanticsdrsz	--resolvetruez--elimeqz--formatZprologz--instantiatez--input)tempfilemkstempr$   fdopenwritecloser   r   rL   r)   remove)r   rH   r   ffdZtemp_filenamerO   stdoutr   r   r   rE      s2    

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j|  |dkrb|g| }tj|tjtjd}n*dj||dj|}tj|tjtjd	d
}|j \}}|rt d|j |rt d|d |rt d|d |jdkrtdj|dj||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)r_   stderrzecho "{}" | {} {}T)r_   rc   shellzReturn code:zstdout:
rJ   zstderr:
r   z&ERROR CALLING: {} {}
Returncode: {}
{})	printr'   
subprocessPopenPIPEformatcommunicate
returncoder,   )	r   Z	input_strbinaryrO   r   cmdpr_   rc   r   r   r   rL      s.    	




zBoxer._callc             C   s  |j d}i }d}x|t|k r|| }|jdr|jd}|d| }|d dkrr|d dkrr|dd }||d |jd }	|d7 }|| }|jd	|	 dst|dd  dkr|d d d }|jdstd| td	|	 d}
d}d}xt||
d  D ]n\}}|dkr*|d7 }|dkr|d8 }|dkr|
| d }|||d  dkrr|d }n|d }P qW |dkst||d j }| j|||}| j	j
|||< |d7 }qW |S )NrJ   r   zid(,   rK      )zsem(   z').'z).zcan't parse line: z,[[]z','   rw   rx   rw   rw   )splitr<   
startswithindexr=   endswith	enumeratestrip
_parse_drsr   r2   )r   rI   rG   linesr;   ilineZ	comma_idxr.   Zdrs_idZsearch_startZbrace_countZ	drs_startjcZ	drs_inputparsedr   r   r   rF   "  sJ    





zBoxer._parse_to_drs_dictc             C   s   t d |g| j|S )N)BoxerOutputDrsParserparse)r   Z
drs_stringr.   rG   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   r2   r3   r7   r+   rD   rE   r"   rL   rF   r   r   r   r   r   r   ;   s"       







'

)*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 j|  || _d| _dg| _dS )zz
        This class is used to parse the Prolog DRS output from Boxer into a
        hierarchy of python objects.
        NrK   \F)rK   rK   r   F)r
   r   r.   Zsentence_id_offsetZquote_chars)r   r.   r   r   r   r   Q  s    
zBoxerOutputDrsParser.__init__c             C   s   t j| ||S )N)r
   r   )r   data	signaturer   r   r   r   [  s    zBoxerOutputDrsParser.parsec             C   s   ddddddgS )N(rr   ro   rt   ru   :r   )r   r   r   r   get_all_symbols^  s    z$BoxerOutputDrsParser.get_all_symbolsc             C   s
   | j |S )N)
handle_drs)r   tokcontextr   r   r   handlea  s    zBoxerOutputDrsParser.handlec             C   s   |S )Nr   )r   
expressionr   r   r   r   attempt_adjunctsd  s    z%BoxerOutputDrsParser.attempt_adjunctsc             C   s(   | j  }| j||}|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| j  S |dkr*| j| jd g S |dkrD| j| jd g S d S )NrU   mergesmergealfa)r   r   )r   )	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| j  gS |dkr*| j| jg n|dkrB| j| jg n|dkrV| j g n|dkrj| j g n|dkr~| j g nz|dkr| j g nf|dkr| j g nR|d	kr| j	  n@|d
kr| j
 g n,|dkr| j g n|dkr| j g ng  t fdd| j|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   )r4   cond)
sent_indexword_indicesr   r   r5     s    zCBoxerOutputDrsParser.handle_condition.<locals>.<genexpr>.<listcomp>Nr   )r4   )conds)r   r   r   r9     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_duplexrM   _sent_and_word_indices)r   r   r   r   )r   r   r   {  s:    


z%BoxerOutputDrsParser.handle_conditionc             C   s2   | j | j d | jd }| j | j d t|S )Nr   rr   )assertTokenr   process_next_expressionBoxerNot)r   rU   r   r   r   r     s    
z BoxerOutputDrsParser._handle_notc                s   j j d j j j d j  j j d j j j d tj j j d  fdd}|S )Nr   ro   rr   c                s   t j| | S )N)	BoxerPredr.   )r   r   )ra   posr   sensevariabler   r   _handle_pred_f  s    z9BoxerOutputDrsParser._handle_pred.<locals>._handle_pred_f)r   r   parse_variableint)r   r   r   )ra   r   r   r   r   r   r     s    z!BoxerOutputDrsParser._handle_predc                s   j j d g  j j d j j d jd j j d j j j d jd j j d  fddS )Nr   r   ro   rr   c                s   t j| | S )N)BoxerWhqr.   )r   r   )	ans_typesd1d2refr   r   r   <lambda>  s   z5BoxerOutputDrsParser._handle_duplex.<locals>.<lambda>)r   r   r   r   )r   r   )r   r   r   r   r   r   r     s    

z#BoxerOutputDrsParser._handle_duplexc                s   j j d j j j d j  j j d j j j d j j j d  fddS )Nr   ro   rr   c                s   t j| | S )N)
BoxerNamedr.   )r   r   )ra   r   r   typer   r   r   r     s   z4BoxerOutputDrsParser._handle_named.<locals>.<lambda>)r   r   r   )r   r   )ra   r   r   r   r   r   r     s    z"BoxerOutputDrsParser._handle_namedc                s   j j d j j j d j j j d j  j j d tj j j d  fddS )Nr   ro   rr   c                s   t j| | S )N)BoxerRelr.   )r   r   )r   r   r   var1var2r   r   r     s   z2BoxerOutputDrsParser._handle_rel.<locals>.<lambda>)r   r   r   r   )r   r   )r   r   r   r   r   r   r     s    z BoxerOutputDrsParser._handle_relc             C   sF   | j | j d | j }| j | j d | j|}| j | j d |S )Nr   ro   rr   )r   r   r   _handle_time_expression)r   argZ	new_condsr   r   r   r     s    
z"BoxerOutputDrsParser._handle_timexc                st   j  jj  d dkr,j }ndkr@j }nd S jj  d  fddgdd |D  S )	Nr   datetimerr   c                s   t j| | ddS )Nnr   )r   r.   )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 )Nr   )r   r   )r   r   r   r     s    zIBoxerOutputDrsParser._handle_time_expression.<locals>.<listcomp>.<lambda>r   )r4   r   )r   r   r5     s    z@BoxerOutputDrsParser._handle_time_expression.<locals>.<listcomp>)r   r   _handle_date_handle_time)r   r   r   r   )r   r   r   r   r   
  s    z,BoxerOutputDrsParser._handle_time_expressionc       	   	   C   sx  g }| j | j \\}}| j| j d | j }| j| j d |jt| j|||d| dd | j| j d | j | j \\}}| j }|dkr|jdd	}|jt| j|||d
| dd | j| j d | j | j \\}}| j }|dkr|jt| j|||d| dd | j| j d | j | j \\}}| j }|dkrt|jt| j|||d| dd |S )Nr   rr   Z	date_pol_ar   ro   ZXXXXr   _Z
date_year_XXZdate_month_Z	date_day_)r   _parse_index_listr   r   appendr   r.   replace)	r   r   r   r   r   Zpolyearmonthdayr   r   r   r     sv    

z!BoxerOutputDrsParser._handle_datec             C   s   g }| j   | j }|dkr0|j| jd|| | j| j d | j   | j }|dkrl|j| jd|| | j| j d | j   | j }|dkr|j| jd|| |S )Nr   Zr_hour_2ro   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    j  j d  j  j  j d  j  j  j d  j  j  j d  fddS )Nr   ro   rr   c                s   t  j| |S )N)	BoxerCardr.   )r   r   )r   r   valuer   r   r   r     s   z3BoxerOutputDrsParser._handle_card.<locals>.<lambda>)r   r   r   )r   r   )r   r   r   r   r   r     s    z!BoxerOutputDrsParser._handle_cardc                sR   j j d j j j d jd  j j d  fddS )Nr   ro   rr   c                s   t j| | S )N)	BoxerPropr.   )r   r   )rU   r   r   r   r   r     s   z3BoxerOutputDrsParser._handle_prop.<locals>.<lambda>)r   r   r   r   )r   r   )rU   r   r   r   r     s    
z!BoxerOutputDrsParser._handle_propc             C   sh   g }| j | j d x6| jddkrJ|j| j  | jddkr| j  qW | j  | j | j d |S )Nrt   r   ru   ro   r   )r   r   r   parse_index)r   r   r   r   r   r     s    z&BoxerOutputDrsParser._parse_index_listc             C   s   | j | j d | j | j d t }x>| jddkrd| j }|j| j  | jddkr(| j  q(W | j  | j | j d | j | j d g }x@| jddkr| j }|j| j| | jddkr| j  qW | j  | j | j d tt	||S )Nr   rt   r   ru   ro   rr   )
r   r   setr   addr   extendr   BoxerDrsr@   )r   refsr   r   r   r   r   r     s(    zBoxerOutputDrsParser.parse_drsc                sT   | j | j d | jd  | j | j d | jd | j | j d  fddS )Nr   ro   rr   c                s   | | S )Nr   )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   r   r     s    

z.BoxerOutputDrsParser._handle_binary_expressionc                sl   | j | j d | j }| j | j d | jd  | j | j d | jd | j | j d  fddS )Nr   ro   rr   c                s   | | S )Nr   )r   r   )r   r   r   r   r   r     s   z3BoxerOutputDrsParser._handle_alfa.<locals>.<lambda>)r   r   r   )r   r   r   r   )r   r   r   r   r     s    

z!BoxerOutputDrsParser._handle_alfac                sP    j  j d  j  j  j d  j  j  j d  fddS )Nr   ro   rr   c                s   t  j| |S )N)BoxerEqr.   )r   r   )r   r   r   r   r   r     s   z1BoxerOutputDrsParser._handle_eq.<locals>.<lambda>)r   r   r   )r   r   )r   r   r   r   r     s    zBoxerOutputDrsParser._handle_eqc                s(  j j d j j d g  xjddkrj }j j d |dkrd jj  q&|dkr jd j }|d	kr jd
 q j| q& jj  q&W j  j j d jd j j d j j j d jd j j d  fddS )Nr   rt   r   ru   r   ZdesnumnumberZcoucountro   rr   c                s   t j| | S )N)r   r.   )r   r   )r   r   r   r   r   r   r   r     s   z2BoxerOutputDrsParser._handle_whq.<locals>.<lambda>)r   r   r   r   r   )r   cattypr   )r   r   r   r   r   r   r     s0    


z BoxerOutputDrsParser._handle_whqc             C   s   t |j|j |j|j S )N)r   r   r   )r   r   r   r   r   r   r   r   r     s    z+BoxerOutputDrsParser._make_merge_expressionc             C   s   t | j||||S )N)BoxerOrr.   )r   r   r   r   r   r   r   r   r     s    z(BoxerOutputDrsParser._make_or_expressionc             C   s   t |j|j|S )N)r   r   r   )r   r   r   r   r   r   r   r   r     s    z)BoxerOutputDrsParser._make_imp_expressionc             C   s    | j  }tjd|st||S )Nz^[exps]\d+$)r   rematchr=   )r   varr   r   r   r     s    z#BoxerOutputDrsParser.parse_variablec             C   s   t | j S )N)r   r   )r   r   r   r   r   	  s    z BoxerOutputDrsParser.parse_indexc                sd   dd |D }|rHg }x,|D ]$  fdd|D }|j  |f qW |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   i  rq   r   )r4   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 )i  rq   r   )r4   r   )r   r   r   r5     s    z?BoxerOutputDrsParser._sent_and_word_indices.<locals>.<listcomp>c             S   s   g | ]}|d  d qS )i  rq   r   )r4   r   r   r   r   r5     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 j|  || _d S )N)r
   r   r.   )r   r.   r   r   r   r   #  s    
zBoxerDrsParser.__init__c             C   s   t jt jt jt jt jgS )N)r   OPENCLOSECOMMAZOPEN_BRACKETZCLOSE_BRACKET)r   r   r   r   r   '  s
    zBoxerDrsParser.get_all_symbolsc             C   s   |S )Nr   )r   r   r   r   r   r   r   0  s    zBoxerDrsParser.attempt_adjunctsc             C   s  y|dkr| j tj | jd k	r(| jn| j }| j tj | j }| j tj ttt	| j
 }| j tj t	| j }| j tj | j }| j tj | j }| j tj t	| j }	| j tj t|||||||	S |dkr| j tj | jd k	r| jn| j }| j tj t	| j }| j tj tt	| j
 }| j tj t	| j }| j tj | j }| j tj | j }
| j tj t	| j }	| j tj t||||||
|	S |dkr| j tj | jd k	r| jn| j }| j tj | j }| j tj ttt	| j
 }| j tj t	| j }| j tj t	| j }| j tj | j }| j tj t	| j }	| j tj t|||||||	S |dkrB| j tj | jd k	r| jn| j }| j tj t	| j }| j tj ttt	| j
 }| j tj t	| j }| j tj | jd }| j tj t|||||S |dkrv| j tj | jd }| j tj t|S |dkr| j tj | jd }| j tj | jd }| j tj t|j|j|S |dkrn| j tj | jd k	r| jn| j }| j tj | j }| j tj tt	| j
 }| j tj | jd }| j tj | jd }| j tj t|||||S |dkr| j tj | jd k	r| jn| j }| j tj | j }| j tj ttt	| j
 }| j tj t	| j }| j tj t	| j }| j tj t|||||S |d	kr| j tj | jd k	rD| jn| j }| j tj | j }| j tj tt	| j
 }| j tj t	| j }| j tj | j }| j tj | j }
| j tj t||||||
S |d
kr| j tj | jd k	r | jn| j }| j tj | j }| j tj ttt	| j
 }| j tj | j
 }| j tj | jd }| j tj t	| j }| j tj | jd }| j tj t|||||||S W n6 tk
r } zt| jt||W Y d d }~X nX dstt|d S )Nr   r   r   r   r   r   r   r   r   r   F)ZassertNextTokenr   r  r.   r   r  nullableIntTokenr@   rA   r   Zhandle_refsr  r   r   r   r   r   r   r   r   r   r   r   r   r   r,   r   Z_currentIndexrB   r=   repr)r   r   r   Zdisc_idZsent_idZword_idsr   ra   r   r   r   r   r   r   rU   r   r   r   r   r   er   r   r   r   3  s   

















$zBoxerDrsParser.handlec             C   s   | j  }|dkrt|S d S )NNone)r   r   )r   tr   r   r   r    s    zBoxerDrsParser.nullableIntTokenc             C   s>   y| j  S  tk
r8 } zt|jd|W Y d d }~X 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$   | j  \}}}|||B  ||| fS )zK
        :return: (set<variables>, set<events>, set<propositions>)
        )
_variables)r   	variableseventsZpropositionsr   r   r   r    s    zAbstractBoxerDrs.variablesc             C   s:   i }x0t d| j D ]\}}x|D ]}|||< q"W qW |S )Nzr  rn   )r  r  rn   )rN   r  )r   Zvartypesr	  varsvr   r   r   variable_types  s
    
zAbstractBoxerDrs.variable_typesc             C   s   t  t  t  fS )zK
        :return: (set<variables>, set<events>, set<propositions>)
        )r   )r   r   r   r   r    s    zAbstractBoxerDrs._variablesc             C   s   t  S )N)r   )r   r   r   r   atoms  s    zAbstractBoxerDrs.atomsc             C   s   | S )Nr   )r   r   r   r   clean  s    zAbstractBoxerDrs.cleanc             C   s   |j ddj ddS )N-r   rK   )r   )r   ra   r   r   r   _clean_name   s    zAbstractBoxerDrs._clean_namec             C   s   | S )Nr   )r   r]   r   r   r   renumber_sentences  s    z#AbstractBoxerDrs.renumber_sentencesc             C   s
   t |  S )N)hash)r   r   r   r   __hash__  s    zAbstractBoxerDrs.__hash__N)r   r   r   r  r  r  r  r  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 j|  || _|| _|| _d S )N)r  r   r   r   
consequent)r   r   r   r  r   r   r   r     s    
zBoxerDrs.__init__c             C   s|   t  t  t  f}x4| jD ]*}x$t||j D ]\}}|j| q,W qW | jd k	rxx&t|| jj D ]\}}|j| qbW |S )N)r   r   rN   r  updater  )r   r  r   sr  r   r   r   r    s    
zBoxerDrs._variablesc             C   s:   t tjdd | jD t }| jd k	r6|j| jj  |S )Nc             s   s   | ]}|j  V  qd S )N)r  )r4   r   r   r   r   r9     s    z!BoxerDrs.atoms.<locals>.<genexpr>)r   r>   or_r   r   r  r  r  )r   r  r   r   r   r    s    
zBoxerDrs.atomsc             C   s.   | j r| j j nd }t| jdd | jD |S )Nc             S   s   g | ]}|j  qS r   )r  )r4   r   r   r   r   r5   #  s    z"BoxerDrs.clean.<locals>.<listcomp>)r  r  r   r   r   )r   r  r   r   r   r  !  s    zBoxerDrs.cleanc                s4   | j r| j j nd }t| j fdd| jD |S )Nc                s   g | ]}|j  qS r   )r  )r4   r   )r]   r   r   r5   (  s    z/BoxerDrs.renumber_sentences.<locals>.<listcomp>)r  r  r   r   r   )r   r]   r  r   )r]   r   r  %  s    zBoxerDrs.renumber_sentencesc             C   sR   dj djdd | jD djdd | jD }| jd k	rNd| d| j d}|S )Nzdrs([{}], [{}])z, c             s   s   | ]}d | V  qdS )z%sNr   )r4   rr   r   r   r9   -  s    z$BoxerDrs.__repr__.<locals>.<genexpr>c             s   s   | ]}d | V  qdS )z%sNr   )r4   r   r   r   r   r9   .  s    zimp(rr   )ri   r'   r   r   r  )r   r  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 )Nr   )r4   c1c2r   r   r   r9   :  s    z"BoxerDrs.__eq__.<locals>.<genexpr>)		__class__r   r<   r   r   r>   r?   rN   r  )r   otherr   r   r   __eq__4  s    zBoxerDrs.__eq__c             C   s
   | |k S )Nr   )r   r$  r   r   r   __ne__?  s    zBoxerDrs.__ne__)N)r   r   r   r   r  r  r  r  r   r%  r&  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 j|  || _d S )N)r  r   rU   )r   rU   r   r   r   r   F  s    
zBoxerNot.__init__c             C   s
   | j j S )N)rU   r  )r   r   r   r   r  J  s    zBoxerNot._variablesc             C   s
   | j j S )N)rU   r  )r   r   r   r   r  M  s    zBoxerNot.atomsc             C   s   t | jj S )N)r   rU   r  )r   r   r   r   r  P  s    zBoxerNot.cleanc             C   s   t | jj|S )N)r   rU   r  )r   r]   r   r   r   r  S  s    zBoxerNot.renumber_sentencesc             C   s
   d| j  S )Nznot(%s))rU   )r   r   r   r   r   V  s    zBoxerNot.__repr__c             C   s   | j |j ko| j|jkS )N)r#  rU   )r   r$  r   r   r   r%  Y  s    zBoxerNot.__eq__c             C   s
   | |k S )Nr   )r   r$  r   r   r   r&  \  s    zBoxerNot.__ne__N)r   r   r   r   r  r  r  r  r   r%  r&  r  r  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 j|  || _|| _|| _d S )N)r  r   r.   r   r   )r   r.   r   r   r   r   r   r   c  s    
zBoxerIndexed.__init__c             C   s   | hS )Nr   )r   r   r   r   r  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 )Nr   )r4   r  or   r   r   r9   r  s    z&BoxerIndexed.__eq__.<locals>.<genexpr>)r#  r.   r   r   r   r>   r?   rN   )r   r$  r   r   r   r%  l  s
    zBoxerIndexed.__eq__c             C   s
   | |k S )Nr   )r   r$  r   r   r   r&  u  s    zBoxerIndexed.__ne__c             C   sL   dj | j | j| jdjdd | jD }x| D ]}|d| 7 }q0W |d S )Nz{}({}, {}, [{}]z, c             s   s   | ]}d | V  qdS )z%sNr   )r4   Zwir   r   r   r9     s    z(BoxerIndexed.__repr__.<locals>.<genexpr>z, %srr   )ri   _predr.   r   r'   r   )r   r  r  r   r   r   r   z  s    
zBoxerIndexed.__repr__N)
r   r   r   r   r  r%  r&  r  r  r   r   r   r   r   r'  b  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 j| ||| || _|| _|| _|| _d S )N)r'  r   r   ra   r   r   )r   r.   r   r   r   ra   r   r   r   r   r   r     s
    zBoxerPred.__init__c             C   s   | j ht t fS )N)r   r   )r   r   r   r   r    s    zBoxerPred._variablesc             C   s    t | j| j| j|| j| j| jS )N)r   r.   r   r   ra   r   r   )r   r   r   r   r   
change_var  s    zBoxerPred.change_varc             C   s(   t | j| j| j| j| j| j| j| jS )N)	r   r.   r   r   r   r  ra   r   r   )r   r   r   r   r    s    
zBoxerPred.cleanc             C   s*   || j }t| j|| j| j| j| j| jS )N)r   r   r.   r   r   ra   r   r   )r   r]   Znew_sent_indexr   r   r   r    s    
zBoxerPred.renumber_sentencesc             C   s   t | j| j| j| jfS )N)iterr   ra   r   r   )r   r   r   r   __iter__  s    zBoxerPred.__iter__c             C   s   dS )Nr   r   )r   r   r   r   r)    s    zBoxerPred._predN)
r   r   r   r   r  r*  r  r  r,  r)  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 j| ||| || _|| _|| _|| _d S )N)r'  r   r   ra   r   r   )r   r.   r   r   r   ra   r   r   r   r   r   r     s
    zBoxerNamed.__init__c             C   s   | j ht t fS )N)r   r   )r   r   r   r   r    s    zBoxerNamed._variablesc             C   s    t | j| j| j|| j| j| jS )N)r   r.   r   r   ra   r   r   )r   r   r   r   r   r*    s    zBoxerNamed.change_varc             C   s(   t | j| j| j| j| j| j| j| jS )N)	r   r.   r   r   r   r  ra   r   r   )r   r   r   r   r    s    
zBoxerNamed.cleanc             C   s&   t | j|| j| j| j| j| j| jS )N)r   r.   r   r   r   ra   r   r   )r   r]   r   r   r   r    s    zBoxerNamed.renumber_sentencesc             C   s   t | j| j| j| jfS )N)r+  r   ra   r   r   )r   r   r   r   r,    s    zBoxerNamed.__iter__c             C   s   dS )Nr   r   )r   r   r   r   r)    s    zBoxerNamed._predN)
r   r   r   r   r  r*  r  r  r,  r)  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 j| ||| || _|| _|| _|| _d S )N)r'  r   r   r   r   r   )r   r.   r   r   r   r   r   r   r   r   r   r     s
    zBoxerRel.__init__c             C   s   | j | jht t fS )N)r   r   r   )r   r   r   r   r    s    zBoxerRel._variablesc             C   s(   t | j| j| j| j| j| j| j| jS )N)	r   r.   r   r   r   r   r  r   r   )r   r   r   r   r    s    
zBoxerRel.cleanc             C   s&   t | j|| j| j| j| j| j| jS )N)r   r.   r   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 )N)r+  r   r   r   r   )r   r   r   r   r,    s    zBoxerRel.__iter__c             C   s   dS )Nr   r   )r   r   r   r   r)    s    zBoxerRel._predN)	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 )r   c             C   s    t j| ||| || _|| _d S )N)r'  r   r   rU   )r   r.   r   r   r   rU   r   r   r   r     s    zBoxerProp.__init__c             C   s&   t ttjt t | jhf| jj S )N)tuplerA   r>   r  r   r   rU   r  )r   r   r   r   r    s    zBoxerProp._variablesc             C   s   | j hS )N)rU   )r   r   r   r   referenced_labels   s    zBoxerProp.referenced_labelsc             C   s
   | j j S )N)rU   r  )r   r   r   r   r  #  s    zBoxerProp.atomsc             C   s   t | j| j| j| j| jj S )N)r   r.   r   r   r   rU   r  )r   r   r   r   r  &  s    zBoxerProp.cleanc             C   s$   t | j|| j| j| j| jj|S )N)r   r.   r   r   r   rU   r  )r   r]   r   r   r   r  /  s    zBoxerProp.renumber_sentencesc             C   s   t | j| jfS )N)r+  r   rU   )r   r   r   r   r,  8  s    zBoxerProp.__iter__c             C   s   dS )Nr   r   )r   r   r   r   r)  ;  s    zBoxerProp._predN)r   r   r   r   r  r.  r  r  r  r,  r)  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 j| ||| || _|| _d S )N)r'  r   r   r   )r   r.   r   r   r   r   r   r   r   r   @  s    zBoxerEq.__init__c             C   s   | j | jht t fS )N)r   r   r   )r   r   r   r   r  E  s    zBoxerEq._variablesc             C   s   t  S )N)r   )r   r   r   r   r  H  s    zBoxerEq.atomsc             C   s   t | j|| j| j| j| jS )N)r   r.   r   r   r   r   )r   r]   r   r   r   r  K  s    zBoxerEq.renumber_sentencesc             C   s   t | j| jfS )N)r+  r   r   )r   r   r   r   r,  T  s    zBoxerEq.__iter__c             C   s   dS )Nr   r   )r   r   r   r   r)  W  s    zBoxerEq._predN)	r   r   r   r   r  r  r  r,  r)  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 j| ||| || _|| _|| _d S )N)r'  r   r   r   r   )r   r.   r   r   r   r   r   r   r   r   r   \  s    zBoxerCard.__init__c             C   s   | j ht t fS )N)r   r   )r   r   r   r   r  b  s    zBoxerCard._variablesc             C   s"   t | j|| j| j| j| j| jS )N)r   r.   r   r   r   r   r   )r   r]   r   r   r   r  e  s    zBoxerCard.renumber_sentencesc             C   s   t | j| j| jfS )N)r+  r   r   r   )r   r   r   r   r,  o  s    zBoxerCard.__iter__c             C   s   dS )Nr   r   )r   r   r   r   r)  r  s    zBoxerCard._predN)r   r   r   r   r  r  r,  r)  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 j| ||| || _|| _d S )N)r'  r   r   r   )r   r.   r   r   r   r   r   r   r   r   w  s    zBoxerOr.__init__c             C   s   t ttj| jj | jj S )N)r-  rA   r>   r  r   r  r   )r   r   r   r   r  |  s    zBoxerOr._variablesc             C   s   | j j | jj B S )N)r   r  r   )r   r   r   r   r    s    zBoxerOr.atomsc             C   s"   t | j| j| j| jj | jj S )N)r   r.   r   r   r   r  r   )r   r   r   r   r    s    zBoxerOr.cleanc             C   s   t | j|| j| j| j| jS )N)r   r.   r   r   r   r   )r   r]   r   r   r   r    s    zBoxerOr.renumber_sentencesc             C   s   t | j| jfS )N)r+  r   r   )r   r   r   r   r,    s    zBoxerOr.__iter__c             C   s   dS )Nr   r   )r   r   r   r   r)    s    zBoxerOr._predN)
r   r   r   r   r  r  r  r  r,  r)  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 j| ||| || _|| _|| _|| _d S )N)r'  r   r   r   r   r   )r   r.   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 | jj S )N)	r-  rA   r>   r  r   r   r   r  r   )r   r   r   r   r    s    zBoxerWhq._variablesc             C   s   | j j | jj B S )N)r   r  r   )r   r   r   r   r    s    zBoxerWhq.atomsc             C   s*   t | j| j| j| j| jj | j| jj S )N)	r   r.   r   r   r   r   r  r   r   )r   r   r   r   r    s    zBoxerWhq.cleanc             C   s&   t | j|| j| j| j| j| j| jS )N)r   r.   r   r   r   r   r   r   )r   r]   r   r   r   r    s    zBoxerWhq.renumber_sentencesc             C   s&   t ddj| j d | j| j| jfS )Nrt   ro   ru   )r+  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   r)    s    zBoxerWhq._predN)
r   r   r   r   r  r  r  r  r,  r)  r   r   r   r   r     s   	
r   c               @   s   e Zd Zdd ZdS )PassthroughBoxerDrsInterpreterc             C   s   |S )Nr   )r   exr   r   r   r2     s    z(PassthroughBoxerDrsInterpreter.interpretN)r   r   r   r2   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 )N)_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|j|_|S t |t	rdt
| j|jS t |tr| j|j d|j |}| j||jS t |tr| jd|j d|j |}| j||jS t |t r| jd|j |}| j||j|jS t |trtt|j| j|jS t |trHttt|jtt|jS t |tr~| jd|j d|j |}| j||jS t |t rt!| j|j"| j|j#S t |t$r| j|j"}| 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   )r4   r  r   r   r   r5     s    z8NltkDrtBoxerDrsInterpreter.interpret.<locals>.<listcomp>Nr   Zne_z%sZcard_Fz: )(
isinstancer   r   r   r@   rA   r2   r   r  r   r   rU   r   _add_occur_indexingr   ra   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   r0  rU   r   r   r   r   r   r   r2     sB    
"



z$NltkDrtBoxerDrsInterpreter.interpretc             G   s0   t t|}x|D ]}t|t t|}qW |S )N)r   r   r   )r   r   rO   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   )r1  r   r.   sortedr   )r   baser0  r   r   r   r4    s    z.NltkDrtBoxerDrsInterpreter._add_occur_indexingN)F)r   r   r   r   r2   r   r4  r   r   r   r   r     s   
)r   c               @   s   e Zd ZdS )UnparseableInputExceptionN)r   r   r   r   r   r   r   r7    s   r7  __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 questionr/   z--occurz-ozoccurrence indexr2  rq   zincorrect number of arguments)r2  z\n)r/   r   )Cr   r>   r$   r   rf   rW   	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   r'  r   r   r   r   r   r   r   r   r/  r   r,   r7  r   opts
add_option
parse_argsoptionsrO   r<   errorr2  Zinterpreterr3   rz   r/   r   rU   re   ZsimplifyZeliminate_equalityr>  	normalizeZpretty_printr   r   r   r   <module>   s   ,     Q H%;$43(*%6>



