Save SpaCy render file as SVG using DisplaCy
Asked Answered
C

2

7

I have the following code:

import spacy
from spacy import displacy
from pathlib import Path

nlp = spacy.load('en_core_web_sm', parse=True, tag=True, entity=True)

sentence_nlp = nlp("John go home to your family")
svg = displacy.render(sentence_nlp, style="dep", jupyter=True)

output_path = Path("/images/dependency_plot.svg")
output_path.open("w", encoding="utf-8").write(svg)

I am trying to write an the rendered file to an svg file in the images folder. However, I get the error:

Traceback (most recent call last):

File "", line 8, in output_path.open("w", encoding="utf-8").write(svg)

File "C:\Users****\AppData\Local\Continuum\miniconda3\lib\pathlib.py", line 1183, in open opener=self._opener)

File "C:\Users****\AppData\Local\Continuum\miniconda3\lib\pathlib.py", line 1037, in _opener return self._accessor.open(self, flags, mode)

File "C:\Users****\AppData\Local\Continuum\miniconda3\lib\pathlib.py", line 387, in wrapped return strfunc(str(pathobj), *args) FileNotFoundError: [Errno 2] No such file or directory: '\images\dependency_plot.svg'

The directory does exist and so I'm not really sure what I'm doing wrong. I have also looked at the spacy usage page https://spacy.io/usage/visualizers#jupyter and couldn't figure out what I'm doing wrong. I'm using spyder (if this information is required). Please assist.

Catheterize answered 17/5, 2019 at 7:47 Comment(0)
C
6

I think that you have 2 errors there. First you should fix your path - add "."

from:

output_path = Path("/images/dependency_plot.svg")

to:

output_path = Path("./images/dependency_plot.svg")

The second error is in this line

svg = displacy.render(sentence_nlp, style="dep", jupyter=True)

I think you need to remove jupyter=True to be able to write it in the svg file. Otherwise you will be given error like TypeError: write() argument must be str, not None

This works for me:

import spacy
from spacy import displacy
from pathlib import Path

nlp = spacy.load('en_core_web_sm', parse=True, tag=True, entity=True)

sentence_nlp = nlp("John go home to your family")
svg = displacy.render(sentence_nlp, style="dep")

output_path = Path("./images/dependency_plot.svg") # you can keep there only "dependency_plot.svg" if you want to save it in the same folder where you run the script 
output_path.open("w", encoding="utf-8").write(svg)
Carrera answered 17/5, 2019 at 12:30 Comment(4)
I did the changes, I get the error TypeError: write() argument must be str, not NoneCatheterize
I think you need to remove jupyter=True have you done this as well? Like removing that jupyter part? I have added code which works for meCarrera
your code is giving me a TypeError: write() argument must be str, not NoneMagnetograph
Attribute jupyter=False works for me.Tan
H
1

I followed @Petr Matuska's answer and encountered the error everyone is commenting about. While I debug I found two issues that are also mentioned in the SpaCy documentation.

  • Include jupyter=False in the render method when you want to save the tree as SVG. That way you won't see the output on Jupyter but you can open the saved file to see the result.
  • Use the entire document to render instead of individual sentences. See the note from their official website

Important note Since each visualization is generated as a separate SVG, exporting .svg files only works if you’re rendering one single doc at a time. (This makes sense – after all, each visualization should be a standalone graphic.) So instead of rendering all Docs at one, loop over them and export them separately.

  • For a single sentence, use it as sentences = ["This is an example."].

Here is the code snippet directly from the SpaCy documentation that perfectly works for me.

import spacy
from spacy import displacy
from pathlib import Path

nlp = spacy.load("en_core_web_sm")
sentences = ["This is an example.", "This is another one."]
for sent in sentences:
    doc = nlp(sent)
    svg = displacy.render(doc, style="dep", jupyter=False)
    file_name = '-'.join([w.text for w in doc if not w.is_punct]) + ".svg"
    output_path = Path("/images/" + file_name)
    output_path.open("w", encoding="utf-8").write(svg)
Hindi answered 19/1, 2021 at 16:11 Comment(1)
I followed this thread for entity tagging. The svg file that I get is an empty file. I cannot see any display on this file. I elaborate my problem #78151364 Can anyone please check?Clausewitz

© 2022 - 2024 — McMap. All rights reserved.