
    h1                        S SK Jr  S SKJrJrJrJr  S SKJrJ	r	J
r
JrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJ r J!r!  S SK"J#r$  S SK%J&r&J'r'  S SK(J)r)J*r*  SSK+J,r,  SS	K-J.r.  SS
K/J0r0  SSK1J2r2  SSK3J3r3  SSK4J5r5  SSK6J7r7  Sr8 S1S\S\9S\\:   S\\\0   \*4   4S jjr;S r< S1S\9S\:S\9S\\:   S\\\0   \*4   4
S jjr=  S2S\9S\:S\9S\:S\\:   S\\\0   \*4   4S jjr> S1S\9S\:S\9S\\\)\)\)4   \)4   S\\:   S\\\0   \*4   4S jjr? S1S\\\0   \\*   4   S \\\0   \*4   S\\:   S\\\0   \*4   4S! jjr@S\4S" jrA S1S#\:S$\\B   S\\:   S\\\0   \*4   4S% jjrC S1S\\\0   \\*   4   S\9S\\:   S\\\0   \*4   4S& jjrDSS'.S\\\0   \\*   4   S(\\*\*4   S)\\\*\*4      S\\\0   \*4   4S* jjrES\4S+ jrF S1S\S\9S,\9S-\9S.\9S/\9S\\:   S\\\0   \*4   4S0 jjrGg)3    )partial)ListOptionalTuplecast)DropoutGelu	LayerNormLinearLogisticMaxoutModelParametricAttentionParametricAttention_v2ReluSoftmaxSparseLinearSparseLinear_v2chaincloneconcatenatelist2raggedreduce_firstreduce_last
reduce_maxreduce_mean
reduce_sumresidual	resizablesoftmax_activationwith_cpu)init)resize_linear_weightedresize_model)ArrayXdFloats2d   )ORTH)Errors)Doc)registry   )extract_ngrams)StaticVectors   )get_tok2vec_widthixNtok2vecexclusive_classesnOreturnc           
           [        U USSSSUS9$ )a  
Build a simple CNN text classifier, given a token-to-vector model as inputs.
If exclusive_classes=True, a softmax non-linearity is applied, so that the
outputs sum to 1. If exclusive_classes=False, a logistic non-linearity
is applied instead, so that outputs are in the range [0, 1].
FT)r1   r2   use_reduce_firstuse_reduce_lastuse_reduce_maxuse_reduce_meanr3   )build_reduce_text_classifier)r1   r2   r3   s      Q/home/james-whalen/.local/lib/python3.13/site-packages/spacy/ml/models/textcat.py build_simple_cnn_text_classifierr<   /   s&     (+     c                 |    [        X!5      nU R                  SUR                  S   5        U R                  SUSS9  U $ )Noutput_layerr   r3   T)force)r$   set_reflayersset_dim)modelnew_nOresizable_layers      r;   resize_and_set_refrG   C   s<    "?;O	MM./"8"8";<	MM$dM+Lr=   
ngram_sizeno_output_layerc           
      *    [        U UUU[        US9S9$ )Nr3   r2   rH   rI   r3   sparse_linear)_build_bow_text_classifierr   )r2   rH   rI   r3   s       r;   build_bow_text_classifierrO   J   s%     &+'"b) r=   lengthc                     US:  a&  [        [        R                  R                  US95      eSUS-
  R	                  5       -  n[        U UUU[        XCS9S9$ )Nr/   )rP   r,   )r3   rP   rL   )
ValueErrorr)   E1056format
bit_lengthrN   r   )r2   rH   rI   rP   r3   s        r;   build_bow_text_classifier_v3rV   Y   sb     z,,F,;<< 6A:))++F%+'%; r=   rM   c           	      T   SSS.n[         R                  " S[        05         S nU(       d$  [        US'   U (       a
  [	        5       O	[        5       n[        U[        [        US9S9n[        U[        S9U-	  n[        XR                  5      nU(       a  U[        XfR                  5      -	  nS S S 5        Ub   WR                  S[        [        U5      5        WR!                  S	U5        U (       + UR"                  S
'   [        [$        WS9UR"                  S'   U$ ! , (       d  f       Nt= f)Nr   bW>>rY   fill_defaultsresize_layer)attrr3   r?   multi_labelrF   resize_output)r   define_operatorsr   	NEG_VALUEr    r   r   r   r#   r-   r(   r!   opsrC   r   intrA   attrsrG   )	r2   rH   rI   rM   r3   r]   r?   rF   rD   s	            r;   rN   rN   o   s     !$M			u	.!*M#3D-/(*L5> !7}U6
 z5H		*Xl4D4DEEE 
/ 
~dDbM*	MM.-0%6!6EKK#*O$EKK  L) 
/	.s   B	D
D'linear_modelc                 \   UR                   S   (       + n[        R                  " [        [        S.5         U R                  S5      n[        U5      n[        XDS9n[        US9nU [        5       -	  U-	  [        5       -	  [        Xg-	  [        S5      -	  5      -	  nU(       a  US-  OS n	U(       a
  [        X)S9n
O[        X)S9[        5       -	  n
X-  U
-	  nUR!                  SU 5        S S S 5        WR#                  S5      S	La#  Ub   UR%                  S['        [(        U5      5        UR!                  S
UR+                  S
5      5        UR!                  SW5        UR!                  SW5        UR!                  SW5        U(       + UR                   S'   [,        Ul        U$ ! , (       d  f       N= f)Nra   r[   |r3   r3   nIrn           r,   r1   Fr?   attention_layermaxout_layer
norm_layer)rh   r   rd   r   r   maybe_get_dimr   r   r
   r   r   r   r   r   r   r   rA   has_dimrC   r   rg   get_refinit_ensemble_textcatr"   )r1   ri   r3   r2   widthrq   rr   rs   	cnn_model	nO_doubler?   rD   s               r;   build_text_classifier_v2r{      s    )..}==			u; ?	@%%d+-e41%(
} | 2gclBC	D 	 !BFd	"b7L!R6(*DL)l:i)' 
A( }}T%'BNdDbM*	MM.,"6"6~"FG	MM#_5	MM.,/	MM,
+%6!6EKK&EJL; 
A	@s   B0F
F+c                    U R                  S5      nUR                  U5        [        U 5      nU R                  S5      R                  SU5        U R                  S5      R                  SU5        U R                  S5      R                  SU5        U R                  S5      R                  SU5        U R                  S5      R                  SU5        [	        XU5        U $ )Nr1   rq   r3   rr   rn   rs   rv   
initializer0   rC   
init_chainrD   XYr1   tok2vec_widths        r;   rw   rw      s     mmI&Gq%e,M	MM#$,,T=A	MM.!))$>	MM.!))$>	MM,''m<	MM,''m<uLr=   rx   dropoutc                 d   [         R                  " [        [        S.5         [	        U 5      [        5       -	  [        U 5      -	  [        5       -	  [        [        X 5      5      S-  -	  [        X 5      -	  nU(       a  U[        U5      -	  nU[        5       -	  nS S S 5        U$ ! , (       d  f       W$ = f)N)r[   z**r,   )r   rd   r   r   r.   r   r   r   r   r   r   r   r   )rx   r   r3   rD   s       r;   build_text_classifier_lowdatar      s    
 
		uE :	;% }"5)* | U*+q0	1
 b ! 	 WW--E# 
< L 
<	; Ls   A4B  
B/c           	         U R                  S5      n[        U [        X3S9[        X3S9S9n[        R
                  " S[        05         U(       a
  [        US9nO[        US9[        5       -	  nXE-	  nS S S 5        WR                  S5      SLa#  Ub   UR                  S[        [        U5      5        UR                  SW5        U(       + UR                  S'   U$ ! , (       d  f       Nm= f)	Nr3   )rn   r3   )r1   nonlinear_layerkey_transformr[   rK   Fr?   ra   )rt   3_build_parametric_attention_with_residual_nonlinearr   r	   r   rd   r   r   r   r   ru   rC   r   rg   rA   rh   )r1   r2   r3   rx   parametric_attentionr?   rD   s          r;   %build_textcat_parametric_attention_v1r      s    
 !!$'EN%2e.
 
		u	."b>L!R=HJ6L$4 
/ }}T%'BNdDbM*	MM.,/%6!6EKKL 
/	.s   *C
C&)r   r   r   c                    [         R                  " [        [        S.5         U R	                  S5      n[        X2S9n[        US9nU [        5       -	  U-	  [        5       -	  [        X-	  [        S5      -	  5      -	  n[        Ul        UR                  SU 5        UR                  SU5        UR                  SU5        UR                  S	U5        UR                  S
U5        UsS S S 5        $ ! , (       d  f       g = f)Nrk   r3   )r3   r   ro   rp   r1   rq   r   r   rs   )r   rd   r   r   rt   r   r
   r   r   r   r   2_init_parametric_attention_with_residual_nonlinearr"   rA   )r1   r   r   rx   rq   rs   r   s          r;   r   r      s     
		u; ?	@%%d+0EW%(
} | 5EF	G 	 %W!$$Y8$$%6H$$_mD$$%6H$$\:>#) 
A	@	@s   B?C++
C9c                 F   U R                  S5      nUR                  U5        [        U 5      nU R                  S5      R                  SU5        U R                  S5      R                  SU5        U R                  S5      R                  SU5        U R                  S5      R                  SU5        U R                  S5      R                  SU5        U R                  S5      R                  SU5        U R                  S5      R                  SU5        [	        XU5        U $ )Nr1   rq   r3   r   rn   r   rs   r}   r   s        r;   r   r     s     mmI&Gq%e,M	MM#$,,T=A	MM/"**4?	MM/"**4?	MM#$,,T=A	MM#$,,T=A	MM,''m<	MM,''m<uLr=   r6   r7   r8   r9   c           	         SSS.n/ nU(       a  UR                  [        5       5        U(       a  UR                  [        5       5        U(       a  UR                  [        5       5        U(       a  UR                  [	        5       5        [        U5      (       d  [        [        R                  5      e[        R                  " S[        05         U [        5       -	  [        U6 -	  n	U R                  S5      n
U
b  U
[        U5      -  OSnU(       a-  [        XkS9n[         US'   [#        U[%        [&        US9S	9nX-	  nO.[)        XkS9n[#        U[%        [&        US9S	9nX-	  [+        5       -	  nUR-                  S
U5        [%        [.        US9UR0                  S'   SSS5        WR-                  SU 5        Ub   UR3                  S[5        [6        U5      5        U(       + UR0                  S'   U$ ! , (       d  f       NY= f)a  Build a model that classifies pooled `Doc` representations.

Pooling is performed using reductions. Reductions are concatenated when
multiple reductions are used.

tok2vec (Model): the tok2vec layer to pool over.
exclusive_classes (bool): Whether or not classes are mutually exclusive.
use_reduce_first (bool): Pool by using the hidden representation of the
    first token of a `Doc`.
use_reduce_last (bool): Pool by using the hidden representation of the
    last token of a `Doc`.
use_reduce_max (bool): Pool by taking the maximum values of the hidden
    representations of a `Doc`.
use_reduce_mean (bool): Pool by taking the mean of all hidden
    representations of a `Doc`.
nO (Optional[int]): Number of classes.
r   rX   r[   r3   Nrm   rY   r\   r^   r?   rb   rc   r1   ra   )appendr   r   r   r   lenrR   r)   E1057r   rd   r   r   r   rt   r   re   r   r   r#   r   r   rA   rG   rh   rC   r   rg   )r1   r2   r6   r7   r8   r9   r3   r]   
reductionscnn
nO_tok2vecrn   r?   rF   rD   s                  r;   r:   r:   #  s   6 !$MJ,.)+-(*,'+-(z??&&			u	.&+z*BB**40
-7-CZ#j/)"b0L!*M#%.$*-&O *E!R/L'$*-O *hj8Enl3'.+(
O$1 
/8 
MM)W%	~dDbM*%6!6EKKLA 
/	.s   C
G**
G8)N)i   N)H	functoolsr   typingr   r   r   r   	thinc.apir   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   thinc.layers.chainr"   r   thinc.layers.resizabler#   r$   thinc.typesr%   r&   rh   r(   errorsr)   tokensr*   utilr+   r-   staticvectorsr.   r1   r0   re   boolrg   r<   rG   rO   rV   rN   r{   rw   floatr   r   r   r   r:    r=   r;   <module>r      s    . .      8 2 G )     + ) &	 BF'+19#
49h( 	  		
 49h&   	
 	 49h6   w897BC	
 	 49hD &49d8n,-&S	8+,& 	& 49h	&R% " ?C!%.6sm
49h. 49d8n,- 	 49h	: :>	$49d8n,-$ 8X-.$ E(H"456	$
 49h$:u 2 III I 	I
 I I 	I 49hIr=   