B
    P?ð[;(  ã               @   sÄ   d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZmZ eeƒeG dd„ deƒƒƒZeG d	d
„ d
eƒƒZeeG dd„ deƒƒƒZeG dd„ deƒƒZeG dd„ deƒƒZdS )é    )Úunicode_literals)Útotal_ordering)ÚABCMetaÚabstractmethod)Úadd_metaclass)Úraise_unorderable_types)Úpython_2_unicode_compatibleÚunicode_reprc               @   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   ú+lib/python3.7/site-packages/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_unify1   s    zAbstractCCGCategory.can_unifyc             C   s   d S )Nr   )r   r   r   r   Ú__str__:   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__D   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__G   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__O   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
   c               @   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 )z×Initialize 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__a   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&   l   s    zCCGVar.new_idc             C   s
   d| _ d S )Nr   )r+   )r,   r   r   r   Úreset_idt   s    zCCGVar.reset_idc             C   s   dS )NFr   )r   r   r   r   r   x   s    zCCGVar.is_primitivec             C   s   dS )NFr   )r   r   r   r   r   {   s    zCCGVar.is_functionc             C   s   dS )NTr   )r   r   r   r   r   ~   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   |  ¡ s| js| |fgS dS )zX If 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   r0   r   r   r   r   r   r%   W   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ú/)r4   )r   r   r   r   Ú
is_forward¨   s    zDirection.is_forwardc             C   s
   | j dkS )Nú\)r4   )r   r   r   r   Úis_backward«   s    zDirection.is_backwardc             C   s   | j S )N)r4   )r   r   r   r   r7   ®   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)
        )r5   )r   r   r   r   Úrestrs±   s    zDirection.restrsc             C   s
   | j dkS )NÚ_)r5   )r   r   r   r   Úis_variableº   s    zDirection.is_variablec             C   sD   |  ¡ rd|  ¡ fgS |   ¡ r,d| ¡ fgS |  ¡ | ¡ kr@g S d S )Nr>   )r?   r=   )r   r   r   r   r   r   À   s    zDirection.can_unifyc             C   s6   |   ¡ s| S x$|D ]\}}|dkrt| j|ƒS qW | S )Nr>   )r?   r3   r4   )r   Úsubsr.   r=   r   r   r   r   Ê   s    zDirection.substitutec             C   s
   d| j kS )Nú,)r5   )r   r   r   r   Úcan_composeÔ   s    zDirection.can_composec             C   s
   d| j kS )NÚ.)r5   )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   r3   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 d| j|f S )NÚ z%sz%s%s)r5   r4   )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 )Nr9   r;   )r4   r3   r5   )r   r   r   r   Ú__neg__ù   s    
zDirection.__neg__N)r   r"   r#   r$   r)   r:   r<   r7   r=   r?   r   r   rB   rD   r   r   r   r!   r   rG   r   r   r   r   r3   ™   s"   	

r3   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)Ú_categr5   r6   r   )r   Úcategr8   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)r5   )r   r   r   r   r=     s    zPrimitiveCategory.restrsc             C   s   | j S )N)rI   )r   r   r   r   rJ     s    zPrimitiveCategory.categc             C   s   | S )Nr   )r   r@   r   r   r   r     s    zPrimitiveCategory.substitutec             C   sV   |  ¡ sd S | ¡ r|| fgS | ¡ |  ¡ krRx| jD ]}|| ¡ kr6d S q6W g S d S )N)r   r   rJ   r5   r=   )r   r   Zrestrr   r   r   r   #  s    
zPrimitiveCategory.can_unifyc             C   s<   | j g krd| j S dd dd„ | j D ƒ¡ }d| j|f S )Nz%sz[%s]rA   c             s   s   | ]}t |ƒV  qd S )N)r	   )Ú.0rF   r   r   r   ú	<genexpr>2  s    z,PrimitiveCategory.__str__.<locals>.<genexpr>z%s%s)r5   rI   Újoin)r   r8   r   r   r   r   /  s    

zPrimitiveCategory.__str__N)r   r"   r#   r$   r)   r   r   r   r=   rJ   r   r   r   r   r   r   r   rH      s   rH   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Ú_argr4   r   )r   ÚresÚargr7   r   r   r   r)   >  s    zFunctionalCategory.__init__c             C   s   dS )NFr   )r   r   r   r   r   D  s    zFunctionalCategory.is_primitivec             C   s   dS )NTr   )r   r   r   r   r   G  s    zFunctionalCategory.is_functionc             C   s   dS )NFr   )r   r   r   r   r   J  s    zFunctionalCategory.is_varc             C   s2   | j  |¡}| j |¡}| j |¡}t||| jƒS )N)rO   r   r4   rP   rN   )r   r@   Zsub_resZsub_dirZsub_argr   r   r   r   O  s    zFunctionalCategory.substitutec             C   sz   |  ¡ r|| fgS | ¡ rv| j | ¡ ¡}| j | ¡ ¡}|d k	rv|d k	rv| j |¡ | 	¡  |¡¡}|d k	rv|| S d S )N)
r   r   rO   r   rQ   r4   r7   rP   r   rR   )r   r   ZsaZsdZsbr   r   r   r   W  s    
zFunctionalCategory.can_unifyc             C   s   | j S )N)rP   )r   r   r   r   rR   d  s    zFunctionalCategory.argc             C   s   | j S )N)rO   )r   r   r   r   rQ   g  s    zFunctionalCategory.resc             C   s   | j S )N)r4   )r   r   r   r   r7   j  s    zFunctionalCategory.dirc             C   s   d| j | j| jf S )Nz(%s%s%s))rO   r4   rP   )r   r   r   r   r   m  s    zFunctionalCategory.__str__N)r   r"   r#   r$   r)   r   r   r   r   r   rR   rQ   r7   r   r   r   r   r   rN   6  s   rN   N)Z
__future__r   Ú	functoolsr   Úabcr   r   Zsixr   Znltk.internalsr   Znltk.compatr   r	   Úobjectr
   r%   r3   rH   rN   r   r   r   r   Ú<module>   s"   
DA
e5