a
     ˜ëj5%  ã                   @   s  d Z ddlZddlmZ ddlmZmZmZmZ ddl	m
Z
 ddlmZ e d¡Ze d¡Ze d	¡Ze d
ej¡Ze dej¡Ze dej¡Ze d¡ZG dd„ dƒZG dd„ dƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd%dd„Zd&dd „Ze
d!ƒd"d#„ ƒZed$ƒZ dS )'z
CCG Lexicons
é    N)Údefaultdict)ÚCCGVarÚ	DirectionÚFunctionalCategoryÚPrimitiveCategory)Ú
deprecated)Ú
Expressionz([A-Za-z]+)(\[[A-Za-z,]+\])?z"([A-Za-z]+(?:\[[A-Za-z,]+\])?)(.*)z([\\/])([.,]?)([.,]?)(.*)z([\S_]+)\s*(::|[-=]+>)\s*(.+)z([^{}]*[^ {}])\s*(\{[^}]+\})?z\{([^}]+)\}z([^#]*)(?:#.*)?c                   @   s:   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚTokenzÄ
    Class representing a token.

    token => category {semantics}
    e.g. eat => S\var[pl]/var {\x y.eat(x,y)}

    * `token` (string)
    * `categ` (string)
    * `semantics` (Expression)
    Nc                 C   s   || _ || _|| _d S ©N)Ú_tokenÚ_categÚ
_semantics)ÚselfÚtokenÚcategÚ	semantics© r   úO/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/ccg/lexicon.pyÚ__init__9   s    zToken.__init__c                 C   s   | j S r
   )r   ©r   r   r   r   r   >   s    zToken.categc                 C   s   | j S r
   )r   r   r   r   r   r   A   s    zToken.semanticsc                 C   s2   d}| j d ur dt| j ƒ d }dt| jƒ | S )NÚ z {Ú})r   Ústrr   )r   Úsemantics_strr   r   r   Ú__str__D   s    
zToken.__str__c                 C   s*   t |tƒsdS t| j| jf| ¡ | ¡ ƒS )Néÿÿÿÿ)Ú
isinstancer	   Úcmpr   r   r   r   )r   Úotherr   r   r   Ú__cmp__J   s    
zToken.__cmp__)N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r	   -   s   
r	   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú
CCGLexiconzâ
    Class representing a lexicon for CCG grammars.

    * `primitives`: The list of primitive categories for the lexicon
    * `families`: Families of categories
    * `entries`: A mapping of words to possible categories
    c                 C   s    t |ƒ| _|| _|| _|| _d S r
   )r   Ú_startZ_primitivesZ	_familiesÚ_entries)r   ÚstartÚ
primitivesÚfamiliesÚentriesr   r   r   r   Y   s    
zCCGLexicon.__init__c                 C   s
   | j | S )z@
        Returns all the possible categories for a word
        )r&   )r   Úwordr   r   r   Ú
categories_   s    zCCGLexicon.categoriesc                 C   s   | j S )z;
        Return the target category for the parser
        )r%   r   r   r   r   r'   e   s    zCCGLexicon.startc                 C   sf   d}d}t | jƒD ]N}|s"|d }|| d }d}| j| D ]"}|sN|d }nd}|d|  }q<q|S )zK
        String representation of the lexicon. Used for debugging.
        r   TÚ
z => z | Fz%s)Úsortedr&   )r   ÚstringÚfirstÚidentÚcatr   r   r   r   k   s    
zCCGLexicon.__str__N)r    r!   r"   r#   r   r,   r'   r   r   r   r   r   r$   P   s
   r$   c                 C   sŽ   | dd… }d}|dkr\|  d¡s\|  d¡rBt|ƒ\}}|| }q||d  }|dd… }q|  d¡rz|d |dd… fS td|  d ƒ‚dS )	zb
    Separate the contents matching the first set of brackets from the rest of
    the input.
    é   Nú(r   ú)r   zUnmatched bracket in string 'ú')Ú
startswithÚmatchBracketsÚAssertionError)r/   ÚrestZinsideÚpartr   r   r   r8   …   s    


r8   c                 C   s    |   d¡rt| ƒS t | ¡ ¡ S )zb
    Separate the string for the next portion of the category from the rest
    of the string
    r4   )r7   r8   ÚNEXTPRIM_REÚmatchÚgroups)r/   r   r   r   ÚnextCategory™   s    
r?   c                 C   s   t | d | dd… ƒS )z'
    Parse an application operator
    r   r3   N)r   )Úappr   r   r   ÚparseApplication£   s    rA   c                 C   s   | r| dd…   d¡S g S )z7
    Parse the subscripts for a primitive category
    r3   r   ú,)Úsplit)Zsubscrr   r   r   ÚparseSubscriptsª   s    rD   c                 C   s¦   | d dkr.| d du r.|du r&t ƒ }||fS | d }||v rp|| \}}|du rX|}n| ||fg¡}||fS ||v r’t| d ƒ}t||ƒ|fS td| d ƒ‚dS )zƒ
    Parse a primitive category

    If the primitive is the special category 'var', replace it with the
    correct `CCGVar`.
    r   Úvarr3   NzString 'z-' is neither a family nor primitive category.)r   Ú
substituterD   r   r9   )Úchunksr(   r)   rE   Úcatstrr2   ZcvarZsubscrsr   r   r   ÚparsePrimitiveCategory³   s$    
ÿrI   c           
      C   sà   t | ƒ\}}| d¡r2t|dd… |||ƒ\}}ntt |¡ ¡ |||ƒ\}}|dkrØt |¡ ¡ }t|dd… ƒ}|d }t |ƒ\}}| d¡r®t|dd… |||ƒ\}	}ntt |¡ ¡ |||ƒ\}	}t	||	|ƒ}qN||fS )z{
    Parse a string representing a category, and returns a tuple with
    (possibly) the CCG variable for the category
    r4   r3   r   r   r   é   )
r?   r7   ÚaugParseCategoryrI   ÚPRIM_REr=   r>   ÚAPP_RErA   r   )
Úliner(   r)   rE   Z
cat_stringr:   Úresr@   Ú	directionÚargr   r   r   rK   Ñ   s$    
ÿ
ÿrK   Fc                 C   s$  t  ¡  g }i }ttƒ}|  ¡ D ]ð}t |¡ ¡ d  ¡ }|dkrDq | 	d¡rt|dd„ |dd…  ¡  
d¡D ƒ }q t |¡ ¡ \}}}t |¡ ¡ \}	}
t|	||ƒ\}}|d	krÀ||f||< q d}|d
u rú|
du rât|d ƒ‚nt t |
¡ ¡ d ¡}||  t|||ƒ¡ q t|d |||ƒS )z@
    Convert string representation into a lexicon for CCGs.
    r   r   z:-c                 S   s   g | ]}|  ¡ ‘qS r   )Ústrip)Ú.0Zprimr   r   r   Ú
<listcomp>  s   zfromstring.<locals>.<listcomp>é   NrB   z::Tz@ must contain semantics because include_semantics is set to True)r   Zreset_idr   ÚlistÚ
splitlinesÚCOMMENTS_REr=   r>   rR   r7   rC   ÚLEX_REÚRHS_RErK   r9   r   Ú
fromstringÚSEMANTICS_REÚappendr	   r$   )Úlex_strZinclude_semanticsr(   r)   r*   rN   r1   ÚsepÚrhsrH   r   r2   rE   r   r   r   r   r[   ñ   s<    
ÿ
ÿÿÿr[   zUse fromstring() instead.c                 C   s   t | ƒS r
   )r[   )r^   r   r   r   ÚparseLexicon"  s    ra   a¡  
    # Rather minimal lexicon based on the openccg `tinytiny' grammar.
    # Only incorporates a subset of the morphological subcategories, however.
    :- S,NP,N                    # Primitive categories
    Det :: NP/N                  # Determiners
    Pro :: NP
    IntransVsg :: S\NP[sg]    # Tensed intransitive verbs (singular)
    IntransVpl :: S\NP[pl]    # Plural
    TransVsg :: S\NP[sg]/NP   # Tensed transitive verbs (singular)
    TransVpl :: S\NP[pl]/NP   # Plural

    the => NP[sg]/N[sg]
    the => NP[pl]/N[pl]

    I => Pro
    me => Pro
    we => Pro
    us => Pro

    book => N[sg]
    books => N[pl]

    peach => N[sg]
    peaches => N[pl]

    policeman => N[sg]
    policemen => N[pl]

    boy => N[sg]
    boys => N[pl]

    sleep => IntransVsg
    sleep => IntransVpl

    eat => IntransVpl
    eat => TransVpl
    eats => IntransVsg
    eats => TransVsg

    see => TransVpl
    sees => TransVsg
    )N)F)!r#   ÚreÚcollectionsr   Znltk.ccg.apir   r   r   r   Znltk.internalsr   Znltk.sem.logicr   ÚcompilerL   r<   rM   ÚUNICODErY   rZ   r\   rX   r	   r$   r8   r?   rA   rD   rI   rK   r[   ra   Zopenccg_tinytinyr   r   r   r   Ú<module>   s4   



#5
	
 
1
ÿ