o
    i(                     @   s  d Z ddlZddlmZmZmZ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mZ dd	lmZmZmZ i Zdad
dddi dfdeeeeeef  eeef dedededee deeef dedefddZd
ddi ddddfdeee ef dedededeeef dededededdfddZdd Zi fd eeef deeef deeef fd!d"Zi fd#edeeef deeef fd$d%Zi fd#edeeef deeef fd&d'Z d(eegef ddfd)d*Z!d#edeeef fd+d,Z"dS )-z
spaCy's built in visualization suite for dependencies and named entities.

DOCS: https://spacy.io/api/top-level#displacy
USAGE: https://spacy.io/usage/visualizers
    N)AnyCallableDictIterableOptionalUnion   )ErrorsWarnings)DocSpan)find_available_portis_in_jupyter   )DependencyRendererEntityRendererSpanRendererdepFdocsstylepageminifyjupyteroptionsmanualreturnc                    sL  t tfttfttfd}||vrttjj	|dt
| tttfr$| g} dd | D } tdd | D s9ttj|| \} |d}	|sP fdd| D n| }
|rn| D ]}t
|trmd	|v rmt|d	 d
d d|d	< qV|	j|
||d td< td }tdurt|}|s|du rt rddlm} ddlm} ||d	|S |S )a  Render displaCy visualisation.

    docs (Union[Iterable[Union[Doc, Span, dict]], Doc, Span, dict]]): Document(s) to visualise.
        a 'dict' is only allowed here when 'manual' is set to True
    style (str): Visualisation style, 'dep' or 'ent'.
    page (bool): Render markup as full HTML page.
    minify (bool): Minify HTML markup.
    jupyter (bool): Override Jupyter auto-detection.
    options (dict): Visualiser-specific options, e.g. colors.
    manual (bool): Don't parse `Doc` and instead expect a dict/list of dicts.
    RETURNS (str): Rendered SVG or HTML markup.

    DOCS: https://spacy.io/api/top-level#displacy.render
    USAGE: https://spacy.io/usage/visualizers
    )r   entspan)r   c                 S   s"   g | ]}t |ts|n| qS  )
isinstancer   as_doc.0objr   r   K/home/ubuntu/.local/lib/python3.10/site-packages/spacy/displacy/__init__.py
<listcomp>5   s   " zrender.<locals>.<listcomp>c                 s   s     | ]}t |tttfV  qd S )N)r   r   r   dictr!   r   r   r$   	<genexpr>6   s    zrender.<locals>.<genexpr>)r   c                    s   g | ]} |qS r   r   )r"   doc	converterr   r   r$   r%   :   s    entsc                 S   s   | d | d fS )Nstartendr   )xr   r   r$   <lambda>>   s    zrender.<locals>.<lambda>)key)r   r   parsedNr   )HTML)displayz&<span class="tex2jax_ignore">{}</span>)r   
parse_depsr   
parse_entsr   parse_spans
ValueErrorr	   E087formatr   r   r   r&   allE096sortedrenderstrip_htmlRENDER_WRAPPERr   IPython.core.displayr2   IPython.displayr3   )r   r   r   r   r   r   r   	factoriesrenderer_funcrendererr1   r(   htmlr2   r3   r   r)   r$   r=      s8   

r=   Ti  z0.0.0.0porthostauto_select_portc	                 C   s   ddl m}	 t|||}t rttj t| |||||d |		||t
}
td| d td| d| d z&z|
  W n tyQ   td	| d
 Y nw W |
  dS W |
  dS |
  w )a  Serve displaCy visualisation.

    docs (list or Doc): Document(s) to visualise.
    style (str): Visualisation style, 'dep' or 'ent'.
    page (bool): Render markup as full HTML page.
    minify (bool): Minify HTML markup.
    options (dict): Visualiser-specific options, e.g. colors.
    manual (bool): Don't parse `Doc` and instead expect a dict/list of dicts.
    port (int): Port to serve visualisation.
    host (str): Host to serve visualisation.
    auto_select_port (bool): Automatically select a port if the specified port is in use.

    DOCS: https://spacy.io/api/top-level#displacy.serve
    USAGE: https://spacy.io/usage/visualizers
    r   )simple_server)r   r   r   r   r   z
Using the 'z' visualizerzServing on http://:z ...
zShutting down server on port .N)wsgirefrJ   r   r   warningswarnr
   W011r=   make_serverappprintserve_foreverKeyboardInterruptserver_close)r   r   r   r   r   r   rG   rH   rI   rJ   httpdr   r   r$   serveM   s"   rX   c                 C   s&   dg}|d| t d jdd}|gS )N)zContent-typeztext/html; charset=utf-8z200 OKr1   zutf-8)encoding)r?   encode)environstart_responseheadersresr   r   r$   rR   y   s   
rR   orig_docc                    s@  t | tr	|  } t| j| jddgd}|ds"t	t
j |ddrW| #}t|jD ]}|jj|jj|jjd}|j||d q2W d	   n1 sRw   Y  |d
drg }|d	d D ]@}|jsp|djsqqe|j}|jd }	|	t|k r||	 jr|	d7 }	|	t|k r||	 js|||	 }
||
|j|j|jf qe| }|D ]\}
}}}|||d}|j|
|d qW d	   n1 sw   Y  |d|d  fdd|D }g }|D ]/}|j|jjk r||j|jj|jdd q|j|jjkr||jj|j|jdd q||t| dS )a  Generate dependency parse in {'words': [], 'arcs': []} format.

    orig_doc (Union[Doc, Span]): Document to parse.
    options (Dict[str, Any]): Dependency parse specific visualisation options.
    RETURNS (dict): Generated dependency parse keyed by words and arcs.
    	user_data
user_hooks)excludeDEPcollapse_phrasesF)taglemmaent_type)attrsNcollapse_punctTr   fine_grained	add_lemmac                    s0   g | ]}|j r|jn|j r|jnd dqS )N)textre   rf   )rm   tag_pos_lemma_)r"   wrl   rk   r   r$   r%      s    zparse_deps.<locals>.<listcomp>left)r,   r-   labeldirright)wordsarcssettings)r   r   r    r   vocab
from_bytesto_byteshas_annotationrN   rO   r
   W005get
retokenizelistnoun_chunksrootrn   rp   	ent_type_mergeis_punctnborilenappendheaddep_get_doc_settings)r_   r   r(   retokenizernprh   spanswordr,   r-   r   re   rf   rg   rw   rx   r   rr   r$   r4      sp   
	






r4   r(   c                    sd   | dd  fdd| jD }|sttj t| dr$| j ddnd}t| }| j	|||dS )a   Generate named entities in [{start: i, end: i, label: 'label'}] format.

    doc (Doc): Document to parse.
    options (Dict[str, Any]): NER-specific visualisation options.
    RETURNS (dict): Generated entities keyed by text (original text) and ents.
    kb_url_templateNc              	      s>   g | ]}|j |j|j|jr|jnd  r |jnddqS ) #)r,   r-   rt   kb_idkb_url)
start_charend_charlabel_kb_id_r9   )r"   r   r   r   r$   r%      s    zparse_ents.<locals>.<listcomp>r`   title)rm   r+   r   ry   )
r   r+   rN   rO   r
   W006hasattrr`   r   rm   )r(   r   r+   r   ry   r   r   r$   r5      s   

r5   c                    s   | dd | dd} fdd| j |g D }dd | D }|s5t| j }ttjj||d t	| d	rA| j
 d
dnd}t| }| j||||dS )a  Generate spans in [{start_token: i, end_token: i, label: 'label'}] format.

    doc (Doc): Document to parse.
    options (Dict[str, any]): Span-specific visualisation options.
    RETURNS (dict): Generated span types keyed by text (original text) and spans.
    r   N	spans_keyscc                    sF   g | ]}|j |j|j|j|j|jr|jnd  r |jnddqS )r   r   )r,   r-   start_token	end_tokenrt   r   r   )r   r   r,   r-   r   r   r9   )r"   r   r   r   r$   r%      s    
zparse_spans.<locals>.<listcomp>c                 S   s   g | ]}|j qS r   )rm   )r"   tokenr   r   r$   r%      s    )r   keysr`   r   )rm   r   r   ry   tokens)r   r   r   r   rN   rO   r
   W117r9   r   r`   r   rm   )r(   r   r   r   r   r   r   ry   r   r   r$   r6      s"   

r6   funcc                 C   s(   t | dsttjjt| d| adS )a4  Set an optional wrapper function that is called around the generated
    HTML markup on displacy.render. This can be used to allow integration into
    other platforms, similar to Jupyter Notebooks that require functions to be
    called around the HTML. It can also be used to implement custom callbacks
    on render, or to embed the visualization in a custom page.

    func (callable): Function to call around markup before rendering it. Needs
        to take one argument, the HTML markup, and should return the desired
        output of displacy.render.
    __call__)r#   N)r   r7   r	   E110r9   typer@   )r   r   r   r$   set_render_wrapper  s   
r   c                 C   s   | j | jjdddS )N	directionltr)langr   )lang_rz   writing_systemr   )r(   r   r   r$   r     s   r   )#__doc__rN   typingr   r   r   r   r   r   errorsr	   r
   r   r   r   utilr   r   r=   r   r   r   r?   r@   r&   strboolintrX   rR   r4   r5   r6   r   r   r   r   r   r$   <module>   s     

;
	

,



*D*%