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

In this post I briefly present how to use spaCy to visualize sentence denpendency parsing.

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.

Here I would like to demonstrate how to visualize the dependency relationship of a sentence by using dependency parser of spaCy:

Step 1️⃣ (After you 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 obtained html data to a Markdown file (like Markdown editor or Github) to display the image

2. Render HTML to an image

RatsNOUNareVERBvariousADJmedium-ADJsized,ADJlong-ADVtailedVERBrodents.NOUNnsubjamodamodamodadvmodamodattr

姚刚
姚刚
讲师

本人研究兴趣包括语料库语言学、对外西班牙语和学术话语。

相关