
    i1                        d dl mZ d dlmZmZmZmZ d dlmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z! d dl"m#Z$ d dl%m&Z&m'Z' d dl(m)Z)m*Z* ddl+m,Z, dd	l-m.Z. dd
l/m0Z0 ddl1m1Z1 ddl2m3Z3 ddl4m5Z5 dZ6	 d0dede7dee8   deee0   e*f   fdZ9d Z:	 d0de7de8de7dee8   deee0   e*f   f
dZ;	 	 d1de7de8de7de8dee8   deee0   e*f   fdZ<	 d0de7de8de7deee)e)e)f   e)f   dee8   deee0   e*f   fdZ=	 d0deee0   ee*   f   deee0   e*f   dee8   deee0   e*f   fd Z>defd!Z?	 d0d"e8d#ee@   dee8   deee0   e*f   fd$ZA	 d0deee0   ee*   f   de7dee8   deee0   e*f   fd%ZBdd&deee0   ee*   f   d'ee*e*f   d(eee*e*f      deee0   e*f   fd)ZCdefd*ZD	 d0dede7d+e7d,e7d-e7d.e7dee8   deee0   e*f   fd/ZEy)2    )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   )extract_ngrams)StaticVectors   )get_tok2vec_widthixNtok2vecexclusive_classesnOreturnc           	      &    t        | |dddd|      S )a1  
    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)r0   r1   use_reduce_firstuse_reduce_lastuse_reduce_maxuse_reduce_meanr2   )build_reduce_text_classifier)r0   r1   r2   s      h/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/spacy/ml/models/textcat.py build_simple_cnn_text_classifierr;   .   s&     (+     c                     t        ||      }| j                  d|j                  d          | j                  d|d       | S )Noutput_layerr   r2   T)force)r$   set_reflayersset_dim)modelnew_nOresizable_layers      r:   resize_and_set_refrF   B   s>    "?F;O	MM./"8"8";<	MM$dM+Lr<   
ngram_sizeno_output_layerc           	      6    t        | |||t        |            S )Nr2   r1   rG   rH   r2   sparse_linear)_build_bow_text_classifierr   )r1   rG   rH   r2   s       r:   build_bow_text_classifierrN   I   s%     &+'"b) r<   lengthc           
          |dk  r)t        t        j                  j                  |            d|dz
  j	                         z  }t        | |||t        ||            S )Nr.   )rO   r+   )r2   rO   rK   )
ValueErrorr)   E1056format
bit_lengthrM   r   )r1   rG   rH   rO   r2   s        r:   build_bow_text_classifier_v3rU   X   sd     z,,F,;<< 6A:))++F%+'%F; r<   rL   c                 <   ddd}t        j                  dt        i      5  d }|st        |d<   | r
t	               n	t               }t        |t        t        |            }t        |t              |z	  }t        ||j                        }|r|t        ||j                        z	  }d d d        | j                  dt        t        |             j!                  d	|       |  |j"                  d
<   t        t$              |j"                  d<   |S # 1 sw Y   mxY w)Nr   bW>>rX   fill_defaultsresize_layer)attrr2   r>   multi_labelrE   resize_output)r   define_operatorsr   	NEG_VALUEr    r   r   r   r#   r,   r(   r!   opsrB   r   intr@   attrsrF   )	r1   rG   rH   rL   r2   r\   r>   rE   rC   s	            r:   rM   rM   n   s    !$M			u	. F!*M#3D-/(*L5> !7}U6
 z5H		*XlL4D4DEEEF 
~dDbM*	MM.-0%6!6EKK#*O$EKK  L)F Fs   BDDlinear_modelc                 J   |j                   d    }t        j                  t        t        d      5  | j                  d      }t        |      }t        ||      }t        |      }| t               z	  |z	  t               z	  t        ||z	  t        d      z	        z	  }|r|dz  nd }	|rt        ||	      }
nt        ||	      t               z	  }
||z  |
z	  }|j!                  d|        d d d        j#                  d      d	ur"| |j%                  dt'        t(        |             |j!                  d
|j+                  d
             |j!                  d       |j!                  d       |j!                  d       | |j                   d<   t,        |_        |S # 1 sw Y   xY w)Nr`   rZ   |r2   r2   nIrm           r+   r0   Fr>   attention_layermaxout_layer
norm_layer)rg   r   rc   r   r   maybe_get_dimr   r   r
   r   r   r   r   r   r   r   r@   has_dimrB   r   rf   get_refinit_ensemble_textcatr"   )r0   rh   r2   r1   widthrp   rq   rr   	cnn_model	nO_doubler>   rC   s               r:   build_text_classifier_v2rz      s    )..}==			u; ?	@ *%%d+-e451%(
} | 
2gclBC	D 	 !BFd	"bY7L!RI6(*DL	)l:i)'*( }}T%'BNdDbM*	MM.,"6"6~"FG	MM#_5	MM.,/	MM,
+%6!6EKK&EJL;* *s   B7FF"c                    | j                  d      }|j                  |       t        |       }| j                  d      j                  d|       | j                  d      j                  d|       | j                  d      j                  d|       | j                  d      j                  d|       | j                  d      j                  d|       t	        | ||       | S )Nr0   rp   r2   rq   rm   rr   ru   
initializer/   rB   
init_chainrC   XYr0   tok2vec_widths        r:   rv   rv      s     mmI&Gq%e,M	MM#$,,T=A	MM.!))$>	MM.!))$>	MM,''m<	MM,''m<uaLr<   rw   dropoutc                 R   t        j                  t        t        d      5  t	        |       t               z	  t        |       z	  t               z	  t        t        | |             dz  z	  t        ||       z	  }|r|t        |      z	  }|t               z	  }d d d        |S # 1 sw Y   S xY w)N)rZ   z**r+   )r   rc   r   r   r-   r   r   r   r   r   r   r   r   )rw   r   r2   rC   s       r:   build_text_classifier_lowdatar      s    
 
		uE :	; $% }"5)* | UE*+q0	1
 b% ! 	 WW--E#$ L$ Ls   A1BB&c                    | j                  d      }t        | t        ||      t        ||            }t	        j
                  dt        i      5  |rt        |      }nt        |      t               z	  }||z	  }d d d        j                  d      dur"| |j                  dt        t        |             |j                  d       | |j                  d<   |S # 1 sw Y   bxY w)	Nr2   )rm   r2   )r0   nonlinear_layerkey_transformrZ   rJ   Fr>   r`   )rs   3_build_parametric_attention_with_residual_nonlinearr   r	   r   rc   r   r   r   r   rt   rB   r   rf   r@   rg   )r0   r1   r2   rw   parametric_attentionr>   rC   s          r:   %build_textcat_parametric_attention_v1r      s    
 !!$'EN%E2e.
 
		u	. 5"b>L!R=HJ6L$45 }}T%'BNdDbM*	MM.,/%6!6EKKL5 5s   ,CC')r   r   r   c                    t        j                  t        t        d      5  | j	                  d      }t        ||      }t        |      }| t               z	  |z	  t               z	  t        ||z	  t        d      z	        z	  }t        |_        |j                  d|        |j                  d|       |j                  d|       |j                  d	|       |j                  d
|       |cd d d        S # 1 sw Y   y xY w)Nrj   r2   )r2   r   rn   ro   r0   rp   r   r   rr   )r   rc   r   r   rs   r   r
   r   r   r   r   2_init_parametric_attention_with_residual_nonlinearr"   r@   )r0   r   r   rw   rp   rr   r   s          r:   r   r      s     
		u; ?	@ $%%d+0EW%(
} | :5EF	G 	 %W!$$Y8$$%6H$$_mD$$%6H$$\:>#)$ $ $s   CC22C;c                 H   | j                  d      }|j                  |       t        |       }| j                  d      j                  d|       | j                  d      j                  d|       | j                  d      j                  d|       | j                  d      j                  d|       | j                  d      j                  d|       | j                  d      j                  d|       | j                  d      j                  d|       t	        | ||       | S )Nr0   rp   r2   r   rm   r   rr   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<uaLr<   r5   r6   r7   r8   c                    ddd}g }|r|j                  t                      |r|j                  t                      |r|j                  t                      |r|j                  t	                      t        |      st        t        j                        t        j                  dt        i      5  | t               z	  t        | z	  }	| j                  d      }
|
|
t        |      z  nd}|r8t        ||      }t         |d<   t#        |t%        t&        |      	      }|	|z	  }n9t)        ||      }t#        |t%        t&        |      	      }|	|z	  t+               z	  }|j-                  d
|       t%        t.        |      |j0                  d<   ddd       j-                  d|        | |j3                  dt5        t6        |             | |j0                  d<   |S # 1 sw Y   OxY w)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   rW   rZ   r2   Nrl   rX   r[   r]   r>   ra   rb   r0   r`   )appendr   r   r   r   lenrQ   r)   E1057r   rc   r   r   r   rs   r   rd   r   r   r#   r   r   r@   rF   rg   rB   r   rf   )r0   r1   r5   r6   r7   r8   r2   r\   
reductionscnn
nO_tok2vecrm   r>   rE   rC   s                  r:   r9   r9   "  s   6 !$MJ,.)+-(*,'+-(z?&&			u	. 
&+z*BB**40
-7-CZ#j/)"bR0L!*M#%.$*-&O ?*E!RB/L'$*-O ?*hj8Enl3'.+(
O$1
8 
MM)W%	~dDbM*%6!6EKKLA
 
s   3CGG')N)i   N)F	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&   rg   r(   errorsr)   tokensr*   r,   staticvectorsr-   r0   r/   rd   boolrf   r;   rF   rN   rU   rM   rz   rv   floatr   r   r   r   r9    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<   