
    ha(                         S r SSKrSSKJrJrJrJrJrJr  SSK	J
r
Jr  SSKJrJr  SSKJrJr  SS	KJrJrJr  0 rSqS
SSS0 S4S\\\\\\4      \\\4   S\S\S\S\\   S\\\4   S\S\4S jjrS
SS0 SSSS4S\\\   \4   S\S\S\S\\\4   S\S\S\S\SS4S jjrS r0 4S\\\4   S\\\4   S\\\4   4S jjr0 4S\S\\\4   S\\\4   4S  jjr0 4S\S\\\4   S\\\4   4S! jjr S"\\/\4   SS4S# jr!S\S\\\4   4S$ jr"g)%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                    [         [        4[        [        4[        [
        4S.nX;  a&  [        [        R                  R                  US95      e[        U [        [        [        45      (       a  U /n U  Vs/ s H*  n[        U[        5      (       d  UOUR                  5       PM,     n n[        S U  5       5      (       d  [        [        R                   5      eXq   u  pU	" US9nU(       d  U  Vs/ s H
  o" X5      PM     snOU nU(       a9  U  H3  n[        U[        5      (       d  M  SU;   d  M"  [#        US   S S9US'   M5     UR%                  XUS9R'                  5       [(        S	'   [(        S	   n[*        b  [+        U5      nU(       d  Uc4  [-        5       (       a%  S
SKJnJn  U" U" SR                  U5      5      5      $ U$ s  snf s  snf )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              3   X   #    U  H   n[        U[        [        [        45      v   M"     g 7f)N)
isinstancer   r   dict).0objs     Q/home/james-whalen/.local/lib/python3.13/site-packages/spacy/displacy/__init__.py	<genexpr>render.<locals>.<genexpr>5   s!     BTcz#T4011Ts   (*)r   entsc                     U S   U S   4$ )Nstartend )xs    r%   <lambda>render.<locals>.<lambda>=   s    7QuX@V    )key)r   r   parsedr   )HTMLdisplayz&<span class="tex2jax_ignore">{}</span>)r   
parse_depsr   
parse_entsr   parse_spans
ValueErrorr
   E087formatr!   r   r   r"   as_docallE096sortedrenderstrip_htmlRENDER_WRAPPERr   IPython.core.displayr3   r4   )r   r   r   r   r   r   r   	factoriesr$   renderer_func	converterrendererdocr2   htmlr3   r4   s                    r%   r?   r?      s   2 #J/
+{+I
 ++%+899$dD)**vJNO$3z#t,,C#**,>$DOBTBBB%%(/MW-H>D6#i%6$FC#t$$3$S[6VWF  oofoGMMOE(O?D!d#7?} 	7tDKKDQRSSK) P
 7s   71G1GTi  z0.0.0.0porthostauto_select_portc	           	         SSK Jn	  [        XgU5      n[        5       (       a$  [        R
                  " [        R                  5        [        XX#XES9  U	R                  Xv[        5      n
[        SU S35        [        SU SU S35         U
R                  5         U
R                  5         g! [         a    [        S	U S
35         N,f = f! U
R                  5         f = f)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)wsgirefrN   r   r   warningswarnr   W011r?   make_serverappprintserve_foreverKeyboardInterruptserver_close)r   r   r   r   r   r   rJ   rK   rL   rN   httpds              r%   server\   K   s    4 &t+;<Dhmm$
44W%%d#6E	M%
-.	tfAdV6
23 	  7-dV1567 	s$   	B* *CC	 CC	 	Cc                 L    S/nU" SU5        [         S   R                  SS9nU/$ )N)zContent-typeztext/html; charset=utf-8z200 OKr2   zutf-8)encoding)rA   encode)environstart_responseheadersress       r%   rV   rV   w   s2    ;<G8W%
/
 
 '
 
2C5Lr0   orig_docc                    [        U [        5      (       a  U R                  5       n [        U R                  5      R                  U R                  SS/S95      nUR                  S5      (       d$  [        R                  " [        R                  5        UR                  SS5      (       a  UR                  5        n[        UR                  5       HT  nUR                   R"                  UR                   R$                  UR                   R&                  S.nUR)                  XES9  MV     S	S	S	5        UR                  S
S5      (       Ga  / nUS	S  H  nUR*                  (       d   UR-                  S5      R*                  (       d  M6  UR.                  nUR.                  S-   n	U	[1        U5      :  a<  X)   R*                  (       a)  U	S-  n	U	[1        U5      :  a  X)   R*                  (       a  M)  X(U	 n
UR3                  XR"                  UR$                  UR&                  45        M     UR                  5        nU H  u  ppXUS.nUR)                  XS9  M     S	S	S	5        UR                  S5      nUR                  S5      nU Vs/ s HC  nUR4                  U(       a  UR"                  OUR6                  U(       a  UR$                  OS	S.PME     nn/ nU H  nUR.                  UR8                  R.                  :  a@  UR3                  UR.                  UR8                  R.                  UR:                  SS.5        Mg  UR.                  UR8                  R.                  :  d  M  UR3                  UR8                  R.                  UR.                  UR:                  SS.5        M     UU[=        U 5      S.$ ! , (       d  f       GN= f! , (       d  f       GNu= fs  snf )zGenerate 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_lemma)textrk   rl   left)r*   r+   labeldirright)wordsarcssettings)r!   r   r;   r   vocab
from_bytesto_byteshas_annotationrR   rS   r   W005get
retokenizelistnoun_chunksroottag_lemma_	ent_type_mergeis_punctnborilenappendrs   pos_headdep_get_doc_settings)rd   r   rH   retokenizernprn   spanswordr*   r+   r   rk   rl   rm   rq   rr   wrx   ry   s                      r%   r5   r5   ~   s    (D!!??$
hnn

(
(;"=>C e$$hmm${{%u--^^3??+77<<WW^^ " 1 1
 !!"!2 ,  {{#T**HD}}DIIaL$9$9FFE&&1*CC.SX%6%6q C.SX%6%6%6S>DLL$		4;;GH  ^^.3*5 #J!!$!4 /4  ;;~.LK(I  A	 FF)166qvv!*QXX	

  
  D66DIIKKKK&&tyyQWX VVdiikk!KK!YY[[66!YY"	  D6Fx6PQQa & s    :A.N25!OA
O2
O
OrH   c           
         UR                  SS5      nU R                   Vs/ s Hh  nUR                  UR                  UR                  UR
                  (       a  UR
                  OSU(       a  UR                  UR
                  5      OSS.PMj     nnU(       d$  [        R                  " [        R                  5        [        U S5      (       a  U R                  R                  SS5      OSn[        U 5      nU R                  XEUS.$ s  snf )	zGenerate 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_templateN #)r*   r+   ru   kb_idkb_urlrf   title)rs   r(   r   rz   )r   r(   
start_charend_charlabel_kb_id_r:   rR   rS   r   W006hasattrrf   r   rs   )rH   r   r   r   r(   r   rz   s          r%   r6   r6      s     kk"3T:O 88	 C ^^<<ZZ#&::SZZ2<Ko,,SZZ8QT	
  	 	 hmm$07[0I0ICMMgt,tE$HHHdQQ	s   A/Dc                 ,   UR                  SS5      nUR                  SS5      nU R                  R                  U/ 5       Vs/ s H~  nUR                  UR                  UR                  UR
                  UR                  UR                  (       a  UR                  OSU(       a  UR                  UR                  5      OSS.PM     nnU  Vs/ s H  ofR                  PM     nnU(       dT  [        U R                  R                  5       5      n[        R                  " [        R                  R                  X8S95        [!        U S	5      (       a  U R"                  R                  S
S5      OSn	[%        U 5      n
U R                  UU	U
US.$ s  snf s  snf )zGenerate 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scr   r   )r*   r+   start_token	end_tokenru   r   r   )r   keysrf   r   )rs   r   r   rz   tokens)r   r   r   r   r*   r+   r   r   r:   rs   r   r   rR   rS   r   W117r   rf   r   )rH   r   r   r   r   r   tokenr   r   r   rz   s              r%   r7   r7      sH    kk"3T:OK.I IIMM)R0 1D __==::[[$(KKT[[R=Lo,,T[[9RU	
 1 
  '**cUjjcF*CIINN$%hmm**Y*JK07[0I0ICMMgt,tE$H ' +s   BFFfuncc                     [        U S5      (       d/  [        [        R                  R	                  [        U 5      S95      eU qg)a  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   r8   r
   E110r:   typerB   )r   s    r%   set_render_wrapperr      s7     4$$++T
+;<<Nr0   c                 h    U R                   U R                  R                  R                  SS5      S.$ )N	directionltr)langr   )lang_r{   writing_systemr   )rH   s    r%   r   r     s-    		YY--11+uE r0   )#__doc__rR   typingr   r   r   r   r   r   errorsr
   r   r   r   r   utilr   r   r?   r   r   r   rA   rB   r"   strboolintr\   rV   r5   r6   r7   r   r   r,   r0   r%   <module>r      s9    A A %  5 D D

 " 5
sD$/0#tTA
B55 5 	5
 d^5 #s(^5 5 	5t  ")
s"
#)) ) 	)
 #s(^) ) ) ) ) 
)X ;=ARCIAR)-c3hAR	#s(^ARH 46 RC R$sCx. R$sCx. R2 57 "S "4S> "4S> "JXseSj1 d "# $sCx. r0   