
    g7(                        d Z ddlmZmZ ddlmZ  G d de      Z G d de      Z G d	 d
e      Z G d de      Z	 G d de      Z
d Zd Z e e
       e      Z e	 e
       e      Z G d de      Zd Zd Zd Zd Z e e       e      Z e	 e       e      Z e	 e       ed      Z G d de      Zd Zd Z e e       e      Z e	 e       ed      Zd Z G d de      Zd  Zd! Z e e       e      Z  e	 e       e      Z!y")#z
CCG Combinators
    )ABCMetaabstractmethod)FunctionalCategoryc                   0    e Zd ZdZed        Zed        Zy)UndirectedBinaryCombinatora  
    Abstract class for representing a binary combinator.
    Merely defines functions for checking if the function and argument
    are able to be combined, and what the resulting category is.

    Note that as no assumptions are made as to direction, the unrestricted
    combinators can perform all backward, forward and crossed variations
    of the combinators; these restrictions must be added in the rule
    class.
    c                      y N selffunctionarguments      H/var/www/openai/venv/lib/python3.12/site-packages/nltk/ccg/combinator.pycan_combinez&UndirectedBinaryCombinator.can_combine           c                      y r	   r
   r   s      r   combinez"UndirectedBinaryCombinator.combine    r   r   N__name__
__module____qualname____doc__r   r   r   r
   r   r   r   r      s/    	    r   r   )	metaclassc                   0    e Zd ZdZed        Zed        Zy)DirectedBinaryCombinatorz
    Wrapper for the undirected binary combinator.
    It takes left and right categories, and decides which is to be
    the function, and which the argument.
    It then decides whether or not they can be combined.
    c                      y r	   r
   r   leftrights      r   r   z$DirectedBinaryCombinator.can_combine-   r   r   c                      y r	   r
   r   s      r   r   z DirectedBinaryCombinator.combine1   r   r   Nr   r
   r   r   r   r   %   s/        r   r   c                   *    e Zd ZdZddZd Zd Zd Zy)ForwardCombinatorz
    Class representing combinators where the primary functor is on the left.

    Takes an undirected combinator, and a predicate which adds constraints
    restricting the cases in which it may apply.
    c                 .    || _         || _        || _        y r	   _combinator
_predicate_suffixr   
combinator	predicatesuffixs       r   __init__zForwardCombinator.__init__>       %#r   c                 b    | j                   j                  ||      xr | j                  ||      S r	   r&   r   r'   r   s      r   r   zForwardCombinator.can_combineC   s2    ++D%8 
T__%>
 	
r   c              #   X   K   | j                   j                  ||      E d {    y 7 wr	   r&   r   r   s      r   r   zForwardCombinator.combineH   s"     ##++D%888    *(*c                 8    d| j                    | j                   S )N>r&   r(   r   s    r   __str__zForwardCombinator.__str__K       4##$T\\N33r   N r   r   r   r   r-   r   r   r8   r
   r   r   r#   r#   6   s    


94r   r#   c                   *    e Zd ZdZddZd Zd Zd Zy)BackwardCombinatorzA
    The backward equivalent of the ForwardCombinator class.
    c                 .    || _         || _        || _        y r	   r%   r)   s       r   r-   zBackwardCombinator.__init__T   r.   r   c                 b    | j                   j                  ||      xr | j                  ||      S r	   r0   r   s      r   r   zBackwardCombinator.can_combineY   s2    ++E48 
T__%>
 	
r   c              #   X   K   | j                   j                  ||      E d {    y 7 wr	   r2   r   s      r   r   zBackwardCombinator.combine^   s"     ##++E4888r3   c                 8    d| j                    | j                   S )N<r6   r7   s    r   r8   zBackwardCombinator.__str__a   r9   r   Nr:   r<   r
   r   r   r>   r>   O   s    


94r   r>   c                   "    e Zd ZdZd Zd Zd Zy)UndirectedFunctionApplicationz
    Class representing function application.
    Implements rules of the form:
    X/Y Y -> X (>)
    And the corresponding backwards application rule
    c                 f    |j                         sy|j                         j                  |      d uS NF)is_functionarg	can_unifyr   s      r   r   z)UndirectedFunctionApplication.can_combinem   s-    ##%<<>++H5==r   c              #      K   |j                         sy |j                         j                  |      }|y |j                         j	                  |       y wr	   )rH   rI   rJ   res
substituter   r   r   subss       r   r   z%UndirectedFunctionApplication.combines   sJ     ##%||~''1<lln''--s   AAc                      y)Nr;   r
   r7   s    r   r8   z%UndirectedFunctionApplication.__str__}   s    r   Nr   r   r   r   r   r   r8   r
   r   r   rE   rE   e   s    >.r   rE   c                 >    | j                         j                         S r	   dir
is_forwardr   r    s     r   forwardOnlyrW      s    88:  ""r   c                 >    |j                         j                         S r	   rT   is_backwardrV   s     r   backwardOnlyr[      s    99;""$$r   c                   "    e Zd ZdZd Zd Zd Zy)UndirectedCompositionz
    Functional composition (harmonic) combinator.
    Implements rules of the form
    X/Y Y/Z -> X/Z (B>)
    And the corresponding backwards and crossed variations.
    c                    |j                         r|j                         sy|j                         j                         rM|j                         j                         r/|j                         j	                  |j                               d uS yrG   )rH   rT   can_composerI   rJ   rL   r   s      r   r   z!UndirectedComposition.can_combine   sf     $$&8+?+?+A<<>%%'HLLN,F,F,H||~//?4GGr   c              #     K   |j                         r|j                         sy |j                         j                         r|j                         j                         r|j                         j	                  |j                               }|Xt        |j                         j                  |      |j                         j                  |      |j                                y y y y wr	   )rH   rT   r_   rI   rJ   rL   r   rM   rN   s       r   r   zUndirectedComposition.combine   s     $$&8+?+?+A<<>%%'HLLN,F,F,H<<>++HLLN;D(LLN--d3LLN--d3LLN    -I's   C(C*c                      y)NBr
   r7   s    r   r8   zUndirectedComposition.__str__       r   NrQ   r
   r   r   r]   r]      s    
r   r]   c                 ~    | j                         j                         xr |j                         j                         S r	   rS   rV   s     r   bothForwardre      s+    88:  "?uyy{'='='??r   c                 ~    | j                         j                         xr |j                         j                         S r	   rY   rV   s     r   bothBackwardrg      s+    88:!!#A		(?(?(AAr   c                 ~    | j                         j                         xr |j                         j                         S r	   )rT   rU   rZ   rV   s     r   crossedDirsri      s+    88:  "@uyy{'>'>'@@r   c                     t        | |      sy| j                         j                         s|j                         j                         ry| j                         j	                         S rG   )ri   rT   	can_crossrI   is_primitiverV   s     r   backwardBxConstraintrm      sJ    tU#88:!eiik&;&;&=88:""$$r   x)r,   c                   "    e Zd ZdZd Zd Zd Zy)UndirectedSubstitutionz
    Substitution (permutation) combinator.
    Implements rules of the form
    Y/Z (X\Y)/Z -> X/Z (<Sx)
    And other variations.
    c                    |j                         s|j                         ry|j                         j                         ry|j                         j                         sy|j                         j	                         r|j                         j	                         sy|j                         j                         |j                         k(  xr! |j                         |j                         k(  S rG   )rl   rL   rI   rT   r_   r   s      r   r   z"UndirectedSubstitution.can_combine   s      "h&;&;&= <<>&&(||~**,**,1K1K1M""$6 
LLNhlln,	
r   c              #      K   | j                  ||      rHt        |j                         j                         |j                         |j	                                y y wr	   )r   r   rL   rI   rT   r   s      r   r   zUndirectedSubstitution.combine   sJ     Hh/$""$hllnhlln  0s   AAc                      y)NSr
   r7   s    r   r8   zUndirectedSubstitution.__str__   rc   r   NrQ   r
   r   r   rp   rp      s    
"r   rp   c                     t        | |      sy| j                         j                         j                         xr | j	                         j                         S rG   )re   rL   rT   rU   rI   rl   rV   s     r   forwardSConstraintrv      s?    tU#88:>>&&(FTXXZ-D-D-FFr   c                 .   | j                         j                         s|j                         j                         ryt        | |      sy|j                         j                         j	                         xr |j                         j                         S rG   )rT   rk   re   rL   rZ   rI   rl   rV   s     r   backwardSxConstraintrx     se    88:!eiik&;&;&=tU#99;??((*Iuyy{/G/G/IIr   c                     | j                         j                         r/| j                         } | j                         j                         r/| S r	   )rL   rH   )categs    r   innermostFunctionr{     s8    
))+
!
!
#		 ))+
!
!
#Lr   c                   "    e Zd ZdZd Zd Zd Zy)UndirectedTypeRaisez1
    Undirected combinator for type raising.
    c                     |j                         r|j                         j                         syt        |      }t        j	                  t
        j                               }|yy)NFT)rH   rL   r{   r   rJ   	arg_categrI   )r   r   rI   rO   s       r   r   zUndirectedTypeRaise.can_combine  sN     !cggi&;&;&=$ ~~immo.r   c           	   #     K   |j                         r.|j                         r|j                         j                         sy t        |      }|j	                  |j                               }|W|j                         j                  |      }t        |t        |||j                               |j                                 y y wr	   )	rl   rH   rL   r{   rJ   rI   rM   r   rT   )r   r   rI   rO   xcats        r   r   zUndirectedTypeRaise.combine/  s     !!#(9cggi>S>S>U  $!!#''),779''-D$(xCswwy\  s   CCc                      y)NTr
   r7   s    r   r8   zUndirectedTypeRaise.__str__?  rc   r   NrQ   r
   r   r   r}   r}     s    & r   r}   c                     t        |      }|j                         j                         xr |j                         j	                         S r	   )r{   rT   rZ   rL   rl   r   r    rI   s      r   forwardTConstraintr   H  s5    
E
"C779  "?swwy'='='??r   c                     t        |       }|j                         j                         xr |j                         j	                         S r	   )r{   rT   rU   rL   rl   r   s      r   backwardTConstraintr   M  s5    
D
!C779!>cggi&<&<&>>r   N)"r   abcr   r   nltk.ccg.apir   r   r   r#   r>   rE   rW   r[   ForwardApplicationBackwardApplicationr]   re   rg   ri   rm   ForwardCompositionBackwardComposition
BackwardBxrp   rv   rx   ForwardSubstitution
BackwardSxr{   r}   r   r   ForwardT	BackwardTr
   r   r   <module>r      sd   ( +7 * "40 4241 4,$> @#
%
 ''D'FT ()F)H,W 6 D@B
A% ''<'>L ()>)@,O   1#

 7  HGJ ((>(@BTU  6 8:NPST

)4 )b@
? 024FG246IJ	r   