使用 DisplaCy 将 SpaCy 渲染文件保存为 SVG

Save SpaCy render file as SVG using DisplaCy

我有以下代码:

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)

我正在尝试将渲染文件写入图像文件夹中的 svg 文件。 但是,我收到错误消息:

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'

该目录确实存在,所以我不确定我做错了什么。我还查看了 spacy 用法页面 https://spacy.io/usage/visualizers#jupyter,但无法弄清楚我做错了什么。我正在使用 spyder(如果需要此信息)。 请协助。

我认为你有 2 个错误。 首先你应该修复你的路径 - 添加“。”

来自:

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

至:

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

第二个错误在这一行

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

我认为您需要删除 jupyter=True 才能将其写入 svg 文件。否则你会得到像 TypeError: write() argument must be str, not None

这样的错误

这对我有用:

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 的回答,遇到了每个人都在评论的错误。在调试时,我发现 SpaCy documentation.

中也提到了两个问题
  • 想保存的时候在render方法中包含jupyter=False 树作为 SVG。这样你就不会在 Jupyter 上看到输出,但你 可以打开保存的文件查看结果。
  • 使用整个文档 渲染而不是单个句子。看到他们的笔记 官网

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.

  • 对于单个句子,将其用作sentences = ["This is an example."]

这是直接来自 SpaCy documentation 的代码片段,非常适合我。

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)