
    Ng5%                        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j        d          Z ej        d          Z ej        d	          Z ej        d
ej                  Z ej        dej                  Z ej        dej                  Z ej        d          Z G d d          Z G d d          Zd Zd Zd Zd Zd ZddZddZ e
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                   2    e Zd ZdZddZd Zd Zd Z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                 0    || _         || _        || _        d S N)_token_categ
_semantics)selftokencateg	semanticss       L/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/ccg/lexicon.py__init__zToken.__init__9   s    #    c                     | j         S r   )r   r   s    r   r   zToken.categ>   s
    {r   c                     | j         S r   )r   r   s    r   r   zToken.semanticsA   s
    r   c                 |    d}| j         dt          | j                   z   dz   }dt          | j                  z   |z   S )N z {})r   strr   )r   semantics_strs     r   __str__zToken.__str__D   sB    ?& 3t#7#77#=MC$$$}44r   c                     t          |t                    sdS t          | j        | j        f|                                |                                          S )N)
isinstancer   cmpr   r   r   r   )r   others     r   __cmp__zToken.__cmp__J   sF    %'' 	2DK15;;==%//BSBSTTTr   r   )	__name__
__module____qualname____doc__r   r   r   r    r&    r   r   r   r   -   ss        	 	$ $ $ $
    5 5 5U U U U Ur   r   c                   *    e Zd ZdZd Zd Zd Z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                 X    t          |          | _        || _        || _        || _        d S r   )r   _start_primitives	_families_entries)r   start
primitivesfamiliesentriess        r   r   zCCGLexicon.__init__Y   s+    '..%!r   c                     | j         |         S )z@
        Returns all the possible categories for a word
        )r2   )r   words     r   
categorieszCCGLexicon.categories_   s     }T""r   c                     | j         S )z;
        Return the target category for the parser
        )r/   r   s    r   r3   zCCGLexicon.starte   s     {r   c                     d}d}t          | j                  D ]5}|s|dz   }||z   dz   }d}| j        |         D ]}|s|dz   }nd}|d|z  z   }6|S )zK
        String representation of the lexicon. Used for debugging.
        r   T
z => z | Fz%s)sortedr2   )r   stringfirstidentcats        r   r    zCCGLexicon.__str__k   s     DM** 	- 	-E '$e^f,FE}U+ - - "#e^FF!E$*,- r   N)r'   r(   r)   r*   r   r9   r3   r    r+   r   r   r-   r-   P   sZ              # # #      r   r-   c                 |   | dd         }d}|dk    rr|                     d          s]|                     d          rt          |          \  }}||z   }n||d         z   }|dd         }|dk    r|                     d          ]|                     d          r|dz   |dd         fS t          d| z   dz             )	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>   restinsideparts       r   rH   rH      s    
 !"":DF
"**T__S11*??3 	(..LT4d]FFd1g%F8D "**T__S11* s (d122h''
86ACG
H
HHr   c                     |                      d          rt          |           S t                              |                                           S )zb
    Separate the string for the next portion of the category from the rest
    of the string
    rD   )rG   rH   NEXTPRIM_REmatchgroups)r>   s    r   nextCategoryrQ      sH    
  %V$$$V$$++---r   c                 >    t          | d         | dd                   S )z'
    Parse an application operator
    r   rC   N)r   )apps    r   parseApplicationrT      s      SVSW%%%r   c                 D    | r| dd                              d          S g S )z7
    Parse the subscripts for a primitive category
    rC   r"   ,)split)subscrs    r   parseSubscriptsrY      s-      'ad|!!#&&&Ir   c                 B   | d         dk    r| d         |t                      }||fS | d         }||v r,||         \  }}||}n|                    ||fg          }||fS ||v r't          | d                   }t          ||          |fS t	          d|z   dz             )z
    Parse a primitive category

    If the primitive is the special category 'var', replace it with the
    correct `CCGVar`.
    r   varrC   NzString 'z-' is neither a family nor primitive category.)r   
substituterY   r   rI   )chunksr4   r5   r[   catstrrA   cvarsubscrss           r   parsePrimitiveCategoryra      s     ayE!9{hh:AYFv&d;CC..4+//CSz!&),,!&'22C88
VMM  r   c                    t          |           \  }}|                    d          rt          |dd         |||          \  }}n?t          t                              |                                          |||          \  }}|dk    rt                              |                                          }t          |dd                   }|d         }t          |          \  }}|                    d          rt          |dd         |||          \  }	}n?t          t                              |                                          |||          \  }	}t          ||	|          }|dk    ||fS )z{
    Parse a string representing a category, and returns a tuple with
    (possibly) the CCG variable for the category
    rD   rC   r"   r   r      )
rQ   rG   augParseCategoryra   PRIM_RErO   rP   APP_RErT   r   )
liner4   r5   r[   
cat_stringrJ   resrS   	directionargs
             r   rd   rd      ss   
 &d++ZS!! 
%j2&6
HcRR
cc ,MM*%%,,..
Hc
 

c "**ll4  ''))$S1X..	1v)$//T  %% 	)*QrT*:JRUVVJS##/j))0022J# JS# !c955 "** :r   Fc                    t          j                     g }i }t          t                    }|                                 D ]}t
                              |                                          d                                         }|dk    rN|	                    d          r=|d |dd                                         
                    d          D             z   }t                              |                                          \  }}}t                              |                                          \  }	}
t          |	||          \  }}|dk    r	||f||<   "d}|d	u rX|
t          |d
z             t          j        t"                              |
                                          d                   }||                             t'          |||                     t)          |d         |||          S )z@
    Convert string representation into a lexicon for CCGs.
    r   r   z:-c                 6    g | ]}|                                 S r+   )strip).0prims     r   
<listcomp>zfromstring.<locals>.<listcomp>  s-     ' ' '!%

' ' 'r      NrV   z::Tz@ must contain semantics because include_semantics is set to True)r   reset_idr   list
splitlinesCOMMENTS_RErO   rP   rn   rG   rW   LEX_RERHS_RErd   rI   r	   
fromstringSEMANTICS_REappendr   r-   )lex_strinclude_semanticsr4   r5   r6   rg   r@   seprhsr^   r   rA   r[   r   s                 r   ry   ry      s    OJH$G""$$ %D %D  &&--//288::2::??4   	D $ ' ')-abb)9)9)?)?)D)D' ' ' JJ
 !'T 2 2 9 9 ; ;UC&,ll3&7&7&>&>&@&@#V])&*hGGJS#d{{ $'* 	$,,$,, `a  
 %/$9(..}==DDFFqI% %	
 %%eE3	&B&BCCCCjmZ7CCCr   zUse fromstring() instead.c                      t          |           S r   )ry   )r|   s    r   parseLexiconr   "  s    gr   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
    r   )F)!r*   recollectionsr   nltk.ccg.apir   r   r   r   nltk.internalsr   nltk.sem.logicr	   compilere   rN   rf   UNICODErw   rx   rz   rv   r   r-   rH   rQ   rT   rY   ra   rd   ry   r   openccg_tinytinyr+   r   r   <module>r      s(    
			 # # # # # # Q Q Q Q Q Q Q Q Q Q Q Q % % % % % % % % % % % % "*8
9
9 bjBCC 
4	5	5 
8"*	E	E 
8"*	E	E rz,bj99 bj.// U  U  U  U  U  U  U  UF- - - - - - - -jI I I(. . .& & &    <   @.D .D .D .Db '((  )( :)+ +   r   