U á€C^¶ã@sÆdZddlmZddlmZmZddlmZmZddl m Z ddl m Z m Z mZdd lmZiZd ad d d d id fd d„Zd dd id ddfdd„Zdd„Zifdd„Zifdd„Zdd„Zdd„Zd 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 é)Úunicode_literalsé)ÚDependencyRendererÚEntityRendereré)ÚDocÚSpan)Úb_to_str)ÚErrorsÚWarningsÚ user_warning)Ú is_in_jupyterNÚdepFc sttfttfdœ}||kr,ttjj|d�ƒ‚t|t t t fƒrB|g}dd„|Dƒ}t dd„|Dƒƒslttj ƒ‚||\}‰|ˆd�}|sš‡‡fdd„|Dƒn|} |j| ||d � ¡td <td } td k rÎt| ƒ} |sà|d krütƒrüd d lm} m} | | | ƒƒS| S)a?Render displaCy visualisation. docs (list or Doc): Document(s) to visualise. style (unicode): 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 (unicode): Rendered HTML markup. DOCS: https://spacy.io/api/top-level#displacy.render USAGE: https://spacy.io/usage/visualizers )rÚent)ÚstylecSs"g|]}t|tƒs|n| ¡‘qS©)Ú isinstancerZas_doc©Ú.0Úobjrrú:/tmp/pip-install-6_kvzl1k/spacy/spacy/displacy/__init__.pyÚ .szrender..css|]}t|tttfƒVqdS)N)rrrÚdictrrrrÚ /szrender..)Úoptionscsg|]}ˆ|ˆƒ‘qSrr)rÚdoc©Ú converterrrrr3s)ÚpageÚminifyÚparsedNr)ÚdisplayÚHTML)rÚ parse_depsrÚ parse_entsÚ ValueErrorr ZE087ÚformatrrrrÚallZE096ÚrenderÚstripÚ_htmlÚRENDER_WRAPPERr ZIPython.core.displayr!r") ÚdocsrrrZjupyterrÚmanualZ factoriesZrendererr Úhtmlr!r"rrrr(s*þ    r(Tiˆz0.0.0.0c Cs ddlm}tƒrttjƒt||||||d�| ||t¡} t d  |¡ƒt d  ||¡ƒz4z |   ¡Wn"t k rŒt d  |¡ƒYnXW5|   ¡XdS)a9Serve displaCy visualisation. docs (list or Doc): Document(s) to visualise. style (unicode): 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 (unicode): Host to serve visualisation. DOCS: https://spacy.io/api/top-level#displacy.serve USAGE: https://spacy.io/usage/visualizers r)Ú simple_server)rrrrr-z Using the '{}' visualizerzServing on http://{}:{} ... z Shutting down server on port {}.N)Úwsgirefr/r r r ZW011r(Z make_serverÚappÚprintr&Ú server_closeÚ serve_foreverÚKeyboardInterrupt) r,rrrrr-ÚportÚhostr/ZhttpdrrrÚserve@s   r8cCs6tdƒtdƒfg}|tdƒ|ƒtdjdd�}|gS)Ns Content-typestext/html; charset=utf-8s200 OKr zutf-8)Úencoding)r r*Úencode)ÚenvironZstart_responseÚheadersÚresrrrr1isr1c Csòt|jƒ |jdgd�¡}|js*ttjƒ| dd¡r€|  ¡�<}t |j ƒD]*}|j j |j j|j jdœ}|j||d�qJW5QRX| dd¡�rNg}|d d …D]l}|jsž| d ¡js¶qž|j}|jd } | t|ƒkræ|| jræ| d 7} qÆ||| …} | | |j |j|jf¡qž|  ¡�4}|D](\} } } } | | | dœ}|j| |d��qW5QRX| d ¡�rjd d„|Dƒ}ndd„|Dƒ}g}|D]`}|j|jjk�r²| |j|jj|jddœ¡n,|j|jjk�r€| |jj|j|jddœ¡�q€||t|ƒdœS)z®Generate dependency parse in {'words': [], 'arcs': []} format. doc (Doc): Document do parse. RETURNS (dict): Generated dependency parse keyed by words and arcs. Ú user_data)ÚexcludeZcollapse_phrasesF)ÚtagÚlemmaÚent_type)ÚattrsZcollapse_punctTNéÿÿÿÿrZ fine_grainedcSsg|]}|j|jdœ‘qS©)Útextr@)rFÚtag_©rÚwrrrr“szparse_deps..cSsg|]}|j|jdœ‘qSrE)rFZpos_rHrrrr•sÚleft)ÚstartÚendÚlabelÚdirÚright)ÚwordsÚarcsÚsettings)rÚvocabÚ from_bytesÚto_bytesZ is_parsedr r ZW005ÚgetZ retokenizeÚlistZ noun_chunksÚrootrGZlemma_Z ent_type_ÚmergeZis_punctZnborÚiÚlenÚappendÚheadZdep_Úget_doc_settings)Zorig_docrrZ retokenizerÚnprCZspansÚwordrKrLÚspanr@rArBrPrQrrrr#qsX   ý      ÿüÿr#cCsRdd„|jDƒ}|sttjƒt|dƒr6|j dd¡nd}t|ƒ}|j|||dœS)z¾Generate named entities in [{start: i, end: i, label: 'label'}] format. doc (Doc): Document do parse. RETURNS (dict): Generated entities keyed by text (original text) and ents. cSsg|]}|j|j|jdœ‘qS))rKrLrM)Z start_charZend_charZlabel_)rrrrrr®sÿzparse_ents..r>ÚtitleN)rFÚentsrbrR) rcr r ZW006Úhasattrr>rVr^rF)rrrcrbrRrrrr$¨sþ r$cCs(t|dƒs ttjjt|ƒd�ƒ‚|adS)a4Set 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__)rN)rdr%r ZE110r&Útyper+)ÚfuncrrrÚset_render_wrapper¹s rhcCs|j|jj dd¡dœS)NÚ directionZltr)Úlangri)Zlang_rSZwriting_systemrV)rrrrr^Êsþr^)Ú__doc__Ú __future__rr(rrÚtokensrrÚcompatr Úerrorsr r r Úutilr r*r+r8r1r#r$rhr^rrrrÚs8   ÿ -ø ) 7