3
dx(                 @   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   s   dS )z<
        Returns true if the category is primitive.
        N )selfr   r   ,/tmp/pip-build-v9q4h5k9/nltk/nltk/ccg/api.pyis_primitive   s    z AbstractCCGCategory.is_primitivec             C   s   dS )zI
        Returns true if the category is a function application.
        Nr   )r   r   r   r	   is_function   s    zAbstractCCGCategory.is_functionc             C   s   dS )z=
        Returns true if the category is a variable.
        Nr   )r   r   r   r	   is_var    s    zAbstractCCGCategory.is_varc             C   s   dS )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&   s    zAbstractCCGCategory.substitutec             C   s   dS )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-   s    zAbstractCCGCategory.can_unifyc             C   s   d S )Nr   )r   r   r   r	   __str__6   s    zAbstractCCGCategory.__str__c             C   s   | j |j ko| j|jkS )N)	__class___comparison_key)r   r   r   r   r	   __eq__:   s    zAbstractCCGCategory.__eq__c             C   s
   | |k S )Nr   )r   r   r   r   r	   __ne__@   s    zAbstractCCGCategory.__ne__c             C   sB   t |tstd| | | j|jkr.| j|jk S | jj|jjk S d S )N<)
isinstancer   r   r   r   __name__)r   r   r   r   r	   __lt__C   s
    
zAbstractCCGCategory.__lt__c             C   s.   y| j S  tk
r(   t| j| _ | j S X d S )N)_hashAttributeErrorhashr   )r   r   r   r	   __hash__K   s
    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  | _|| _| 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	   r#   g   s    zCCGVar.new_idc             C   s
   d| _ d S )Nr   )r(   )r)   r   r   r	   reset_ido   s    zCCGVar.reset_idc             C   s   dS )NFr   )r   r   r   r	   r
   s   s    zCCGVar.is_primitivec             C   s   dS )NFr   )r   r   r   r	   r   v   s    zCCGVar.is_functionc             C   s   dS )NTr   )r   r   r   r	   r   y   s    zCCGVar.is_varc             C   s"   x|D ]\}}|| kr|S qW | 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   |j  s| j r| |fgS dS )zWIf the variable can be replaced with other
        a substitution is returned.
        N)r
   r%   )r   r   r   r   r	   r      s    
zCCGVar.can_unifyc             C   s   | j S )N)r$   )r   r   r   r	   id   s    z	CCGVar.idc             C   s   dt | j S )NZ_var)strr$   )r   r   r   r	   r      s    zCCGVar.__str__N)F)r   r   r   r    r(   r&   classmethodr#   r*   r
   r   r   r   r   r-   r   r   r   r   r	   r"   S   s   
	r"   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   s   || _ || _|t|f| _d S )N)_dir_restrstupler   )r   dirrestrictionsr   r   r	   r&      s    zDirection.__init__c             C   s
   | j dkS )N/)r1   )r   r   r   r	   
is_forward   s    zDirection.is_forwardc             C   s
   | j dkS )N\)r1   )r   r   r   r	   is_backward   s    zDirection.is_backwardc             C   s   | j S )N)r1   )r   r   r   r	   r4      s    zDirection.dirc             C   s   | j S )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)
        )r2   )r   r   r   r	   restrs   s    zDirection.restrsc             C   s
   | j dkS )N_)r2   )r   r   r   r	   is_variable   s    zDirection.is_variablec             C   sD   |j  rd| j fgS | j  r,d|j fgS | j |j kr@g S d S )Nr;   )r<   r:   )r   r   r   r   r	   r      s    zDirection.can_unifyc             C   s6   | j  s| S x$|D ]\}}|dkrt| j|S qW | S )Nr;   )r<   r0   r1   )r   subsr+   r:   r   r   r	   r      s    zDirection.substitutec             C   s
   d| j kS )N,)r2   )r   r   r   r	   can_compose   s    zDirection.can_composec             C   s
   d| j kS )N.)r2   )r   r   r   r	   	can_cross   s    zDirection.can_crossc             C   s   | j |j ko| j|jkS )N)r   r   )r   r   r   r   r	   r      s    zDirection.__eq__c             C   s
   | |k S )Nr   )r   r   r   r   r	   r      s    zDirection.__ne__c             C   sB   t |tstd| | | j|jkr.| j|jk S | jj|jjk S d S )Nr   )r   r0   r   r   r   r   )r   r   r   r   r	   r      s
    
zDirection.__lt__c             C   s.   y| j S  tk
r(   t| j| _ | j S X d S )N)r   r   r   r   )r   r   r   r	   r      s
    zDirection.__hash__c             C   s.   d}x| j D ]}|d|  }qW | j | S )N z%s)r2   r1   )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 d S )Nr6   r8   )r1   r0   r2   )r   r   r   r	   __neg__   s    
zDirection.__neg__N)r   r   r   r    r&   r7   r9   r4   r:   r<   r   r   r?   rA   r   r   r   r   r   rD   r   r   r   r	   r0      s"   	

r0   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   s   || _ || _|t|f| _d S )N)_categr2   r3   r   )r   categr5   r   r   r	   r&     s    zPrimitiveCategory.__init__c             C   s   dS )NTr   )r   r   r   r	   r
     s    zPrimitiveCategory.is_primitivec             C   s   dS )NFr   )r   r   r   r	   r   	  s    zPrimitiveCategory.is_functionc             C   s   dS )NFr   )r   r   r   r	   r     s    zPrimitiveCategory.is_varc             C   s   | j S )N)r2   )r   r   r   r	   r:     s    zPrimitiveCategory.restrsc             C   s   | j S )N)rF   )r   r   r   r	   rG     s    zPrimitiveCategory.categc             C   s   | S )Nr   )r   r=   r   r   r	   r     s    zPrimitiveCategory.substitutec             C   sV   |j  sd S |j r|| fgS |j | j krRx| jD ]}||j kr6d S q6W g S d S )N)r
   r   rG   r2   r:   )r   r   restrr   r   r	   r     s    
zPrimitiveCategory.can_unifyc             C   s<   | j g krd| j S ddjdd | j D  }| j | S )Nz%sz[%s]r>   c             s   s   | ]}t |V  qd S )N)repr).0rC   r   r   r	   	<genexpr>+  s    z,PrimitiveCategory.__str__.<locals>.<genexpr>)r2   rF   join)r   r5   r   r   r	   r   (  s    

zPrimitiveCategory.__str__N)r   r   r   r    r&   r
   r   r   r:   rG   r   r   r   r   r   r   r	   rE      s   rE   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 )N)_res_argr1   r   )r   resargr4   r   r   r	   r&   6  s    zFunctionalCategory.__init__c             C   s   dS )NFr   )r   r   r   r	   r
   <  s    zFunctionalCategory.is_primitivec             C   s   dS )NTr   )r   r   r   r	   r   ?  s    zFunctionalCategory.is_functionc             C   s   dS )NFr   )r   r   r   r	   r   B  s    zFunctionalCategory.is_varc             C   s2   | j j|}| jj|}| jj|}t||| jS )N)rN   r   r1   rO   rM   )r   r=   Zsub_resZsub_dirZsub_argr   r   r	   r   G  s    zFunctionalCategory.substitutec             C   sz   |j  r|| fgS |j rv| jj|j }| jj|j }|d k	rv|d k	rv| jj|j|j	 j|}|d k	rv|| S d S )N)
r   r   rN   r   rP   r1   r4   rO   r   rQ   )r   r   sasdZsbr   r   r	   r   O  s    
zFunctionalCategory.can_unifyc             C   s   | j S )N)rO   )r   r   r   r	   rQ   \  s    zFunctionalCategory.argc             C   s   | j S )N)rN   )r   r   r   r	   rP   _  s    zFunctionalCategory.resc             C   s   | j S )N)r1   )r   r   r   r	   r4   b  s    zFunctionalCategory.dirc             C   s   d| j  | j | j dS )N())rN   r1   rO   )r   r   r   r	   r   e  s    zFunctionalCategory.__str__N)r   r   r   r    r&   r
   r   r   r   r   rQ   rP   r4   r   r   r   r   r	   rM   /  s   rM   N)abcr   r   	functoolsr   Znltk.internalsr   r   r"   r0   rE   rM   r   r   r   r	   <module>   s   
DAf5