o
    rZh'                     @   s   d dl mZmZ d dlmZ d dlmZ eG dd dedZG dd deZeG d	d
 d
Z	G dd deZ
G dd deZdS )    )ABCMetaabstractmethod)total_ordering)raise_unorderable_typesc                   @   sx   e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	edd Z
dd Zdd Zdd Zdd ZdS )AbstractCCGCategoryz;
    Interface for categories in combinatory grammars.
    c                 C      dS )z<
        Returns true if the category is primitive.
        N selfr   r   ;/var/www/auris/lib/python3.10/site-packages/nltk/ccg/api.pyis_primitive       z AbstractCCGCategory.is_primitivec                 C   r   )zI
        Returns true if the category is a function application.
        Nr   r	   r   r   r   is_function   r   zAbstractCCGCategory.is_functionc                 C   r   )z=
        Returns true if the category is a variable.
        Nr   r	   r   r   r   is_var    r   zAbstractCCGCategory.is_varc                 C   r   )z
        Takes a set of (var, category) substitutions, and replaces every
        occurrence of the variable with the corresponding category.
        Nr   )r
   substitutionsr   r   r   
substitute&   r   zAbstractCCGCategory.substitutec                 C   r   )z
        Determines whether two categories can be unified.
         - Returns None if they cannot be unified
         - Returns a list of necessary substitutions if they can.
        Nr   r
   otherr   r   r   	can_unify-   r   zAbstractCCGCategory.can_unifyc                 C   s   d S Nr   r	   r   r   r   __str__6   s   zAbstractCCGCategory.__str__c                 C      | j |j u o| j|jkS r   	__class___comparison_keyr   r   r   r   __eq__:      
zAbstractCCGCategory.__eq__c                 C   
   | |k S r   r   r   r   r   r   __ne__@      
zAbstractCCGCategory.__ne__c                 C   >   t |tstd| | | j|ju r| j|jk S | jj|jjk S N<)
isinstancer   r   r   r   __name__r   r   r   r   __lt__C   
   
zAbstractCCGCategory.__lt__c                 C   .   z| j W S  ty   t| j| _ | j  Y S w r   _hashAttributeErrorhashr   r	   r   r   r   __hash__K      
zAbstractCCGCategory.__hash__N)r$   
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r%   r,   r   r   r   r   r      s$    





r   )	metaclassc                   @   sn   e Zd ZdZdZdddZedd Ze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 )CCGVarz
    Class representing a variable CCG category.
    Used for conjunctions (and possibly type-raising, if implemented as a
    unary rule).
    r   Fc                 C   s   |   | _|| _| j| _dS )zInitialize a variable (selects a new identifier)

        :param prim_only: a boolean that determines whether the variable is
                          restricted to primitives
        :type prim_only: bool
        N)new_id_id
_prim_onlyr   )r
   Z	prim_onlyr   r   r   __init__\   s   
zCCGVar.__init__c                 C   s   | j d | _ | j d S )zT
        A class method allowing generation of unique variable identifiers.
           _maxIDclsr   r   r   r3   g   s   
zCCGVar.new_idc                 C   s
   d| _ d S )Nr   r8   r:   r   r   r   reset_ido   s   
zCCGVar.reset_idc                 C   r   NFr   r	   r   r   r   r   s      zCCGVar.is_primitivec                 C   r   r=   r   r	   r   r   r   r   v   r>   zCCGVar.is_functionc                 C   r   NTr   r	   r   r   r   r   y   r>   zCCGVar.is_varc                 C   s"   |D ]\}}|| kr|  S q| S )zlIf there is a substitution corresponding to this variable,
        return the substituted category.
        r   )r
   r   varcatr   r   r   r   |   s
   zCCGVar.substitutec                 C   s   |  s| js| |fgS dS )zWIf the variable can be replaced with other
        a substitution is returned.
        N)r   r5   r   r   r   r   r      s   
zCCGVar.can_unifyc                 C      | j S r   )r4   r	   r   r   r   id      z	CCGVar.idc                 C   s   dt | j S )NZ_var)strr4   r	   r   r   r   r      s   zCCGVar.__str__N)F)r$   r.   r/   r0   r9   r6   classmethodr3   r<   r   r   r   r   r   rC   r   r   r   r   r   r2   S   s    


	r2   c                   @   s   e Zd 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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 )#	Directionz
    Class representing the direction of a function application.
    Also contains maintains information as to which combinators
    may be used with the category.
    c                 C      || _ || _|t|f| _d S r   )_dir_restrstupler   )r
   dirrestrictionsr   r   r   r6         zDirection.__init__c                 C   
   | j dkS )N/rI   r	   r   r   r   
is_forward   r   zDirection.is_forwardc                 C   rO   )N\rQ   r	   r   r   r   is_backward   r   zDirection.is_backwardc                 C   rB   r   rQ   r	   r   r   r   rL      rD   zDirection.dirc                 C   rB   )a8  A list of restrictions on the combinators.
        '.' denotes that permuting operations are disallowed
        ',' denotes that function composition is disallowed
        '_' denotes that the direction has variable restrictions.
        (This is redundant in the current implementation of type-raising)
        rJ   r	   r   r   r   restrs   s   zDirection.restrsc                 C   rO   N_rU   r	   r   r   r   is_variable   r   zDirection.is_variablec                 C   sD   |  rd|  fgS |   rd| fgS |  | kr g S d S rW   )rY   rV   r   r   r   r   r      s   zDirection.can_unifyc                 C   s6   |   s| S |D ]\}}|dkrt| j|  S q| S rW   )rY   rG   rI   )r
   subsr@   rV   r   r   r   r      s   zDirection.substitutec                 C   
   d| j vS )N,rU   r	   r   r   r   can_compose   r   zDirection.can_composec                 C   r[   )N.rU   r	   r   r   r   	can_cross   r   zDirection.can_crossc                 C   r   r   r   r   r   r   r   r      r   zDirection.__eq__c                 C   r   r   r   r   r   r   r   r      r   zDirection.__ne__c                 C   r    r!   )r#   rG   r   r   r   r$   r   r   r   r   r%      r&   zDirection.__lt__c                 C   r'   r   r(   r	   r   r   r   r,      r-   zDirection.__hash__c                 C   s*   d}| j D ]}|d|  }q| j | S )N %s)rJ   rI   )r
   Zr_strrr   r   r   r      s   
zDirection.__str__c                 C   s"   | j dkrtd| jS td| jS )NrP   rS   )rI   rG   rJ   r	   r   r   r   __neg__   s   
zDirection.__neg__N)r$   r.   r/   r0   r6   rR   rT   rL   rV   rY   r   r   r]   r_   r   r   r%   r,   r   rc   r   r   r   r   rG      s$    	

rG   c                   @   s\   e Zd ZdZg f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S )PrimitiveCategoryz
    Class representing primitive categories.
    Takes a string representation of the category, and a
    list of strings specifying the morphological subcategories.
    c                 C   rH   r   )_categrJ   rK   r   )r
   categrM   r   r   r   r6     rN   zPrimitiveCategory.__init__c                 C   r   r?   r   r	   r   r   r   r     r>   zPrimitiveCategory.is_primitivec                 C   r   r=   r   r	   r   r   r   r   	  r>   zPrimitiveCategory.is_functionc                 C   r   r=   r   r	   r   r   r   r     r>   zPrimitiveCategory.is_varc                 C   rB   r   rU   r	   r   r   r   rV     rD   zPrimitiveCategory.restrsc                 C   rB   r   )re   r	   r   r   r   rf     rD   zPrimitiveCategory.categc                 C   s   | S r   r   )r
   rZ   r   r   r   r     r>   zPrimitiveCategory.substitutec                 C   sT   |  sd S | r|| fgS | |  kr(| jD ]}|| vr% d S qg S d S r   )r   r   rf   rJ   rV   )r
   r   restrr   r   r   r     s   

zPrimitiveCategory.can_unifyc                 C   s<   | j g kr
d| j S dddd | j D  }| j | S )Nra   z[%s]r\   c                 s   s    | ]}t |V  qd S r   )repr).0rb   r   r   r   	<genexpr>+  s    z,PrimitiveCategory.__str__.<locals>.<genexpr>)rJ   re   join)r
   rM   r   r   r   r   (  s   

zPrimitiveCategory.__str__N)r$   r.   r/   r0   r6   r   r   r   rV   rf   r   r   r   r   r   r   r   rd      s    rd   c                   @   s`   e Zd 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d Zdd ZdS )FunctionalCategoryz
    Class that represents a function application category.
    Consists of argument and result categories, together with
    an application direction.
    c                 C   s"   || _ || _|| _|||f| _d S r   )_res_argrI   r   )r
   resargrL   r   r   r   r6   6  s   zFunctionalCategory.__init__c                 C   r   r=   r   r	   r   r   r   r   <  r>   zFunctionalCategory.is_primitivec                 C   r   r?   r   r	   r   r   r   r   ?  r>   zFunctionalCategory.is_functionc                 C   r   r=   r   r	   r   r   r   r   B  r>   zFunctionalCategory.is_varc                 C   s2   | j |}| j|}| j|}t||| jS r   )rm   r   rI   rn   rl   )r
   rZ   Zsub_resZsub_dirZsub_argr   r   r   r   G  s   zFunctionalCategory.substitutec                 C   sz   |  r	|| fgS | r;| j| }| j| }|d ur;|d ur;| j||	 |}|d ur;|| S d S r   )
r   r   rm   r   ro   rI   rL   rn   r   rp   )r
   r   sasdsbr   r   r   r   O  s   
zFunctionalCategory.can_unifyc                 C   rB   r   )rn   r	   r   r   r   rp   \  rD   zFunctionalCategory.argc                 C   rB   r   )rm   r	   r   r   r   ro   _  rD   zFunctionalCategory.resc                 C   rB   r   rQ   r	   r   r   r   rL   b  rD   zFunctionalCategory.dirc                 C   s   d| j  | j | j dS )N())rm   rI   rn   r	   r   r   r   r   e  s   zFunctionalCategory.__str__N)r$   r.   r/   r0   r6   r   r   r   r   r   rp   ro   rL   r   r   r   r   r   rl   /  s    rl   N)abcr   r   	functoolsr   Znltk.internalsr   r   r2   rG   rd   rl   r   r   r   r   <module>   s   DAe5