U C^@sdZddlmZddlmZmZddlmZmZddl m Z ddl m Z m Z mZdd lmZiZd ad d d d id fd dZd dd id ddfddZddZifddZifddZddZddZd 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)DependencyRendererEntityRenderer)DocSpan)b_to_str)ErrorsWarnings user_warning) is_in_jupyterNdepFc sttfttfd}||kr,ttjj|dt|t t t frB|g}dd|D}t dd|Dslttj ||\}|d}|sfdd|Dn|} |j| ||d td <td } td k rt| } |s|d krtrd 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 )rent)stylecSs"g|]}t|ts|n|qS) isinstancerZas_doc.0objrr:/tmp/pip-install-6_kvzl1k/spacy/spacy/displacy/__init__.py .szrender..css|]}t|tttfVqdS)N)rrrdictrrrr /szrender..)optionscsg|]}|qSrr)rdoc converterrrrr3s)pageminifyparsedNr)displayHTML)r parse_depsr parse_ents ValueErrorr ZE087formatrrrrallZE096renderstrip_htmlRENDER_WRAPPERr ZIPython.core.displayr!r") docsrrrZjupyterrmanualZ factoriesZrendererr htmlr!r"rrrr(s*    r(Tiz0.0.0.0c Csddlm}trttjt||||||d|||t} t d |t d ||z4z | Wn"t k rt 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_serverappprintr& server_close serve_foreverKeyboardInterrupt) r,rrrrr-porthostr/Zhttpdrrrserve@s   r8cCs6tdtdfg}|td|tdjdd}|gS)Ns Content-typestext/html; charset=utf-8s200 OKr zutf-8)encoding)r r*encode)environZstart_responseheadersresrrrr1isr1c Cst|j|jdgd}|js*ttj|ddr| <}t |j D]*}|j j |j j|j jd}|j||dqJW5QRX|ddrNg}|d d D]l}|js|d jsq|j}|jd } | t|kr|| jr| d 7} q||| } || |j |j|jfq| 4}|D](\} } } } | | | d}|j| |dqW5QRX|d rjd d|D}ndd|D}g}|D]`}|j|jjkr||j|jj|jddn,|j|jjkr||jj|j|jddq||t|dS)zGenerate 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)taglemmaent_type)attrsZcollapse_punctTNrZ fine_grainedcSsg|]}|j|jdqS)textr@)rFtag_rwrrrrszparse_deps..cSsg|]}|j|jdqSrE)rFZpos_rHrrrrsleft)startendlabeldirright)wordsarcssettings)rvocab from_bytesto_bytesZ is_parsedr r ZW005getZ retokenizelistZ noun_chunksrootrGZlemma_Z ent_type_mergeZis_punctZnborilenappendheadZdep_get_doc_settings)Zorig_docrrZ retokenizernprCZspanswordrKrLspanr@rArBrPrQrrrr#qsX         r#cCsRdd|jD}|sttjt|dr6|jddnd}t|}|j|||dS)zGenerate 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|jdqS))rKrLrM)Z start_charZend_charZlabel_)rrrrrrszparse_ents..r>titleN)rFentsrbrR) rcr r ZW006hasattrr>rVr^rF)rrrcrbrRrrrr$s r$cCs(t|ds 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+)funcrrrset_render_wrappers rhcCs|j|jjdddS)N directionZltr)langri)Zlang_rSZwriting_systemrV)rrrrr^sr^)__doc__ __future__rr(rrtokensrrcompatr errorsr r r utilr r*r+r8r1r#r$rhr^rrrrs8    - ) 7