
    i                         d dl mZmZ d dlmZmZmZmZmZm	Z	 d dl
mZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ 	 ddeee   ee   f   ded   dededededee   defdZd Zd Zd Zd Zd Zy)    )ListOptional)LinearModelchain
list2arrayuse_ops	zero_init)Floats2d   )Literal)Errors)Doc   PrecomputableAffine)TransitionModelNtok2vec
state_type)parsernerextra_state_tokenshidden_widthmaxout_pieces	use_uppernOreturnc                    |dk(  r|rdnd}n5|dk(  r|rdnd}n)t        t        j                  j                  |            | j	                  d      r| j                  d      nd	}t        | t               t        ||            } | j                  d|       t        |r|n||| j                  d      |
      }	d	}
|r!t        d      5  t        |d	      }
d	d	d	       t        | |	|
t              S # 1 sw Y   xY w)a	  
    Build a transition-based parser model. Can apply to NER or dependency-parsing.

    Transition-based parsing is an approach to structured prediction where the
    task of predicting the structure is mapped to a series of state transitions.
    You might find this tutorial helpful as background:
    https://explosion.ai/blog/parsing-english-in-python

    The neural network state prediction model consists of either two or three
    subnetworks:

    * tok2vec: Map each token into a vector representations. This subnetwork
        is run once for each batch.
    * lower: Construct a feature-specific vector for each (token, feature) pair.
        This is also run once for each batch. Constructing the state
        representation is then simply a matter of summing the component features
        and applying the non-linearity.
    * upper (optional): A feed-forward network that predicts scores from the
        state representation. If not present, the output from the lower model is
        used as action scores directly.

    tok2vec (Model[List[Doc], List[Floats2d]]):
        Subnetwork to map tokens into vector representations.
    state_type (str):
        String value denoting the type of parser model: "parser" or "ner"
    extra_state_tokens (bool): Whether or not to use additional tokens in the context
        to construct the state vector. Defaults to `False`, which means 3 and 8
        for the NER and parser respectively. When set to `True`, this would become 6
        feature sets (for the NER) or 13 (for the parser).
    hidden_width (int): The width of the hidden layer.
    maxout_pieces (int): How many pieces to use in the state prediction layer.
        Recommended values are 1, 2 or 3. If 1, the maxout non-linearity
        is replaced with a ReLu non-linearity if use_upper=True, and no
        non-linearity if use_upper=False.
    use_upper (bool): Whether to use an additional hidden layer after the state
        vector in order to predict the action scores. It is recommended to set
        this to False for large pretrained models such as transformers, and True
        for smaller networks. The upper layer is computed on CPU, which becomes
        a bottleneck on larger GPU-based models, where it's also less necessary.
    nO (int or None): The number of actions the model will predict between.
        Usually inferred from data at the beginning of training, or loaded from
        disk.
    r         r      r   )valuer   Nr   nFnInPcpur   r%   )
ValueErrorr   E917formathas_dimget_dimr   r   r   set_dim_define_lowerr	   _define_upperr   resize_output)r   r   r   r   r   r   r   nr_feature_tokens	t2v_widthloweruppers              g/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/spacy/ml/models/parser.pybuild_tb_parser_modelr7      s    h X"4B!	u	!3A++*+=>>)0)>%DI|Y'G
 OOD,'$<"??4 	E EU^ 	2!RD1E	2 7E5-@@	2 	2s   C00C9c                 &    t        | |t              S )N)r   r%   init_W)r   r
   r(   s     r6   r0   r0   \   s    RBy11    c                      t        | |||      S )Nr#   r   r#   s       r6   r/   r/   `   s    "r::r:   c                 P    | j                   d   rt        | |      S t        | |      S )N	has_upper)attrs_resize_upper_resize_lower)modelnew_nOs     r6   r1   r1   d   s(    {{;UF++''r:   c                 4   | j                  d      }|j                  d      |j                  d|       | S ||j                  d      k(  r| S |}|j	                  d      }t        d      5  t        ||      }d d d        |j                  d      rj                  j                  ||      }|j                  j                  |      }|j                  d      }|j                  d      }	|j                  d      rJ|j                  d      }
||d |
 |	|d |
 t        |
|      D ]   }| j                  d   j                  |       " |j                  d|       |j                  d|       | j                   d	<   | j#                  d|       | S # 1 sw Y   xY w)
Nr5   r   r%   r'   r(   Wbunseen_classes)get_refr,   r.   r-   maybe_get_dimr	   r0   	has_paramopsalloc2falloc1f	get_paramranger>   add	set_param_layersset_ref)rA   rB   r5   smallerr%   largerlarger_Wlarger_b	smaller_W	smaller_bold_nOis               r6   r?   r?   j   s   MM'"E}}T"dF#	5==&	&G			t	$B	 1&R01 ::%%fb1::%%f-%%c*	%%c*	 ??4 __T*F )HWf )HWf66* 5,-11!45 	h'h'EMM"	MM'6"L+1 1s   +FFc                    | j                  d      }|j                  d      |j                  d|       | S |}|j                  d      }|j                  d      }|j                  d      }t	        ||||      }|j                  d      r?|j                  j                  ||||      }|j                  j                  ||      }	|j                  j                  d|||      }
|j                  d      }|j                  d	      }|j                  d
      }|j                  d      rm|j                  d      }||d d d|d d d d f<   ||
d d d d d|d d f<   ||	d|d d f<   t        ||      D ]   }| j                  d   j                  |       " |j                  d|       |j                  d	|	       |j                  d
|
       || j                  d<   | j!                  d|       | S )Nr4   r   r%   r$   r&   )r   r%   r$   r&   rD      rE   padr   rF   )rH   r,   r.   rI   r/   rJ   rK   alloc4frL   rN   r-   rO   r>   rP   rQ   rR   rS   )rA   rB   r4   rT   r%   r$   r&   rU   rV   rW   
larger_padrX   rY   smaller_padrZ   r[   s                   r6   r@   r@      s   MM'"E}}T"dF#G			t	$B			t	$B			t	$Bfr:F::%%b&"b9::%%fb1ZZ''2vr:
%%c*	%%c*	''.??4 __T*F*3HQ&!Q&',7Jq!QvXq()$-HQvXq[!66* 5,-11!45 	h'h'
+EMM!	MM'6"Lr:   )N)typingr   r   	thinc.apir   r   r   r   r	   r
   thinc.typesr   compatr   errorsr   tokensr   _precomputable_affiner   tb_frameworkr   boolintr7   r0   r/   r1   r?   r@    r:   r6   <module>rm      s    ! J J      7 * LA49d8n,-LA(LA LA 	LA
 LA LA 	LA LA^2;(D!r:   