o
     ¢i·  ã                   @   sú   d dl mZ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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d„Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Z dS )#é    )ÚListÚOptionalÚcast)ÚLinearÚModelÚchainÚ
list2arrayÚuse_opsÚ	zero_init)ÚFloats2dé   )ÚLiteral)ÚErrors)ÚDoc)Úregistryé   ©ÚPrecomputableAffine)ÚTransitionModelNÚtok2vecÚ
state_type)ÚparserÚnerÚextra_state_tokensÚhidden_widthÚmaxout_piecesÚ	use_upperÚnOÚreturnc                 C   sÞ   |dkr|rdnd}n|dkr|rdnd}n	t tjj|dƒ‚|  d¡r)|  d¡nd	}t| tƒ t||ƒƒ} |  	d|¡ t
|r@|n|||  d¡|d
}	d	}
|rhtdƒ t|d	d}
W d	  ƒ n1 scw   Y  t| |	|
tƒS )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Úupper© r6   úJ/home/ubuntu/.local/lib/python3.10/site-packages/spacy/ml/models/parser.pyÚbuild_tb_parser_model   s0   4ý
ü
þr8   c                 C   s   t | |tdS )N)r   r%   Úinit_W)r   r
   r(   r6   r6   r7   r0   ]   s   r0   c                 C   s   t | |||dS )Nr#   r   r#   r6   r6   r7   r/   a   s   r/   c                 C   s   | j d r
t| |ƒS t| |ƒS )NÚ	has_upper)ÚattrsÚ_resize_upperÚ_resize_lower)ÚmodelÚnew_nOr6   r6   r7   r1   e   s   


r1   c                 C   s2  |   d¡}| d¡d u r| d|¡ | S || d¡kr| S |}| d¡}tdƒ t||d}W d   ƒ n1 s9w   Y  | d¡rŒ|j 	||¡}|j 
|¡}| d¡}| d¡}	| d¡r€| d¡}
||d |
…< |	|d |
…< t|
|ƒD ]
}| jd  |¡ qu| d|¡ | d|¡ || jd	< |  d|¡ | S )
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)r>   r?   r5   Úsmallerr%   ÚlargerÚlarger_WÚlarger_bÚ	smaller_WÚ	smaller_bÚold_nOÚir6   r6   r7   r<   k   s6   


ÿ





r<   c                 C   sz  |   d¡}| d¡d u r| d|¡ | S |}| d¡}| d¡}| d¡}t||||d}| d¡r°|j ||||¡}|j ||¡}	|j d|||¡}
| 	d¡}| 	d	¡}| 	d
¡}| d¡rž| 
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  |¡ q“| d|¡ | d	|	¡ | d
|
¡ || jd< |  d|¡ | S )Nr4   r   r%   r$   r&   )r   r%   r$   r&   r@   é   rA   Úpadr   rB   )rD   r,   r.   rE   r/   rF   rG   Úalloc4frH   rJ   r-   rK   r;   rL   rM   rN   rO   )r>   r?   r4   rP   r%   r$   r&   rQ   rR   rS   Ú
larger_padrT   rU   Úsmaller_padrV   rW   r6   r6   r7   r=      s:   









  
r=   )N)!Útypingr   r   r   Ú	thinc.apir   r   r   r   r	   r
   Úthinc.typesr   Úcompatr   Úerrorsr   Útokensr   Úutilr   Ú_precomputable_affiner   Útb_frameworkr   ÚboolÚintr8   r0   r/   r1   r<   r=   r6   r6   r6   r7   Ú<module>   s@     
ùÿþýüûúù
øO"