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.

Abu Shoeb
  • 4,747
  • 2
  • 40
  • 45
Kusi
  • 785
  • 1
  • 10
  • 21

2 Answers2

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)
Petr Matuska
  • 553
  • 5
  • 15
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)
Dharman
  • 30,962
  • 25
  • 85
  • 135
Abu Shoeb
  • 4,747
  • 2
  • 40
  • 45