Dependency parser visualization - Rendering html to an image (TEST)

1. spaCy

spaCy is an open-source software library for advanced Natural Language Processing (NLP). As the website introduces, it is a tool of “Industrial-Strength Natural Language Processing in Python”. Its fast speed and many more extraordinary features and extenions make it more and more popular among researchers. To install spaCy, you could refer to the Documentation of spaCy for a step-by-step instruction.

Step 1️⃣ (After you have installed spaCy…)

python -m venv .env
source .env/bin/activate

Step 2️⃣ Call python3

python3

Step 3️⃣ Import spaCy and displacy

>>> import spacy
>>> from spacy import displacy

Step 4️⃣ Load a language model (English in our case)

>>> nlp = spacy.load("en_core_web_sm")

Step 5️⃣ Input test doc. and render it by using visualizer dep

>>> doc = nlp(u"Rats are various medium-sized, long-tailed rodents.")
>>> displacy.render(doc, style="dep")

Step 6️⃣ Copy the rendered html data to Markdown file

`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:lang="en" id="2851aa0234bb4d70b2439e627e0531df-0" class="displacy" width="1450" height="487.0" direction="ltr" style="max-width: none; height: 487.0px; color: #000000; background: #ffffff; font-family: Arial; direction: ltr">\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="397.0">\n    <tspan class="displacy-word" fill="currentColor" x="50">Rats</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="50">NOUN</tspan>\n</text>\n\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="397.0">\n    <tspan class="displacy-word" fill="currentColor" x="225">are</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="225">VERB</tspan>\n</text>\n\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="397.0">\n    <tspan class="displacy-word" fill="currentColor" x="400">various</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="400">ADJ</tspan>\n</text>\n\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="397.0">\n    <tspan class="displacy-word" fill="currentColor" x="575">medium-</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="575">ADJ</tspan>\n</text>\n\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="397.0">\n    <tspan class="displacy-word" fill="currentColor" x="750">sized,</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="750">ADJ</tspan>\n</text>\n\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="397.0">\n    <tspan class="displacy-word" fill="currentColor" x="925">long-</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="925">ADV</tspan>\n</text>\n\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="397.0">\n    <tspan class="displacy-word" fill="currentColor" x="1100">tailed</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="1100">VERB</tspan>\n</text>\n\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="397.0">\n    <tspan class="displacy-word" fill="currentColor" x="1275">rodents.</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="1275">NOUN</tspan>\n</text>\n\n<g class="displacy-arrow">\n    <path class="displacy-arc" id="arrow-2851aa0234bb4d70b2439e627e0531df-0-0" stroke-width="2px" d="M70,352.0 C70,264.5 210.0,264.5 210.0,352.0" fill="none" stroke="currentColor"/>\n    <text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">\n        <textPath xlink:href="#arrow-2851aa0234bb4d70b2439e627e0531df-0-0" class="displacy-label" startOffset="50%" side="left" fill="currentColor" text-anchor="middle">nsubj</textPath>\n    </text>\n    <path class="displacy-arrowhead" d="M70,354.0 L62,342.0 78,342.0" fill="currentColor"/>\n</g>\n\n<g class="displacy-arrow">\n    <path class="displacy-arc" id="arrow-2851aa0234bb4d70b2439e627e0531df-0-1" stroke-width="2px" d="M420,352.0 C420,89.5 1270.0,89.5 1270.0,352.0" fill="none" stroke="currentColor"/>\n    <text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">\n        <textPath xlink:href="#arrow-2851aa0234bb4d70b2439e627e0531df-0-1" class="displacy-label" startOffset="50%" side="left" fill="currentColor" text-anchor="middle">amod</textPath>\n    </text>\n    <path class="displacy-arrowhead" d="M420,354.0 L412,342.0 428,342.0" fill="currentColor"/>\n</g>\n\n<g class="displacy-arrow">\n    <path class="displacy-arc" id="arrow-2851aa0234bb4d70b2439e627e0531df-0-2" stroke-width="2px" d="M595,352.0 C595,264.5 735.0,264.5 735.0,352.0" fill="none" stroke="currentColor"/>\n    <text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">\n        <textPath xlink:href="#arrow-2851aa0234bb4d70b2439e627e0531df-0-2" class="displacy-label" startOffset="50%" side="left" fill="currentColor" text-anchor="middle">amod</textPath>\n    </text>\n    <path class="displacy-arrowhead" d="M595,354.0 L587,342.0 603,342.0" fill="currentColor"/>\n</g>\n\n<g class="displacy-arrow">\n    <path class="displacy-arc" id="arrow-2851aa0234bb4d70b2439e627e0531df-0-3" stroke-width="2px" d="M770,352.0 C770,177.0 1265.0,177.0 1265.0,352.0" fill="none" stroke="currentColor"/>\n    <text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">\n        <textPath xlink:href="#arrow-2851aa0234bb4d70b2439e627e0531df-0-3" class="displacy-label" startOffset="50%" side="left" fill="currentColor" text-anchor="middle">amod</textPath>\n    </text>\n    <path class="displacy-arrowhead" d="M770,354.0 L762,342.0 778,342.0" fill="currentColor"/>\n</g>\n\n<g class="displacy-arrow">\n    <path class="displacy-arc" id="arrow-2851aa0234bb4d70b2439e627e0531df-0-4" stroke-width="2px" d="M945,352.0 C945,264.5 1085.0,264.5 1085.0,352.0" fill="none" stroke="currentColor"/>\n    <text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">\n        <textPath xlink:href="#arrow-2851aa0234bb4d70b2439e627e0531df-0-4" class="displacy-label" startOffset="50%" side="left" fill="currentColor" text-anchor="middle">advmod</textPath>\n    </text>\n    <path class="displacy-arrowhead" d="M945,354.0 L937,342.0 953,342.0" fill="currentColor"/>\n</g>\n\n<g class="displacy-arrow">\n    <path class="displacy-arc" id="arrow-2851aa0234bb4d70b2439e627e0531df-0-5" stroke-width="2px" d="M1120,352.0 C1120,264.5 1260.0,264.5 1260.0,352.0" fill="none" stroke="currentColor"/>\n    <text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">\n        <textPath xlink:href="#arrow-2851aa0234bb4d70b2439e627e0531df-0-5" class="displacy-label" startOffset="50%" side="left" fill="currentColor" text-anchor="middle">amod</textPath>\n    </text>\n    <path class="displacy-arrowhead" d="M1120,354.0 L1112,342.0 1128,342.0" fill="currentColor"/>\n</g>\n\n<g class="displacy-arrow">\n    <path class="displacy-arc" id="arrow-2851aa0234bb4d70b2439e627e0531df-0-6" stroke-width="2px" d="M245,352.0 C245,2.0 1275.0,2.0 1275.0,352.0" fill="none" stroke="currentColor"/>\n    <text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">\n        <textPath xlink:href="#arrow-2851aa0234bb4d70b2439e627e0531df-0-6" class="displacy-label" startOffset="50%" side="left" fill="currentColor" text-anchor="middle">attr</textPath>\n    </text>\n    <path class="displacy-arrowhead" d="M1275.0,354.0 L1283.0,342.0 1267.0,342.0" fill="currentColor"/>\n</g>\n</svg>'

2. Render HTML to an image

Rats NOUN are VERB various ADJ medium- ADJ sized, ADJ long- ADV tailed VERB rodents. NOUN nsubj amod amod amod advmod amod attr