o
    i                  
   @   s   d dl mZmZmZ d dlmZmZ d dlmZm	Z	 ddl
mZ deee	e	f e	f fddZdd
dZdedee	e	f dedee	ef fddZde	dedefddZde	dedee	ef fddZd	S )    )CallableListTuple)Modelto_numpy)Ints1dRagged   )registryreturnc                	   C   s   t dtg i i i tdS )zExtract spans from a sequence of source arrays, as specified by an array
    of (start, end) indices. The output is a ragged array of the
    extracted spans.
    extract_spans)layersrefsattrsdimsinit)r   forwardr    r   r   J/home/ubuntu/.local/lib/python3.10/site-packages/spacy/ml/extract_spans.pyr   	   s   r   Nc                 C   s   d S Nr   )modelXYr   r   r   r      s   r   r   source_spansis_trainc                    s   | j |\}jjdksJ t|j t dkr5t|j  jdddf jdddf  }ntjj|jj	|jj
djjt|jfdd}|jj	|jdtdtttf f fd	d
}||fS )z%Get subsequences from source vectors.r	   r   N   )dtypeidYr   c                    s*   t j }|j | j |fS r   )r   alloc2fscatter_adddataXd)r   dXindicesopsspans	x_lengthsx_shaper   r   backprop_windows)   s   z!forward.<locals>.backprop_windows)r%   r!   ndim_get_span_indiceslengthslenr   xpzerosshaper   r   )r   r   r   r   r   r)   r   r#   r   r      s   2&r   r&   r,   c           	   	   C   s|   t ||\}}g }d}t|D ])\}}|| j| }t|jd D ]}|t||df ||df  q!||7 }q| |S )zConstruct a flat array that has the indices we want to extract from the
    source data. For instance, if we want the spans (5, 9), (8, 10) the
    indices will be [5, 6, 7, 8, 8, 9].
    r   r   )_ensure_cpu	enumerater!   ranger0   extend	asarray1i)	r%   r&   r,   r$   offsetr   lengthspans_ijr   r   r   r+   1   s   "

r+   c                 C   s   t t| jt| jt|fS r   )r   r   r!   r,   )r&   r,   r   r   r   r1   A   s   r1   )NN)typingr   r   r   	thinc.apir   r   thinc.typesr   r   utilr
   r   r   boolr   r+   r1   r   r   r   r   <module>   s"    




"