【问题标题】:Save SpaCy render file as SVG using DisplaCy使用 DisplaCy 将 SpaCy 渲染文件保存为 SVG
【发布时间】:2019-05-17 07:47:45
【问题描述】:

我有以下代码:

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(最近一次调用最后一次):

文件“”,第 8 行,在 output_path.open("w", encoding="utf-8").write(svg)

文件 "C:\Users****\AppData\Local\Continuum\miniconda3\lib\pathlib.py", 第 1183 行,开放 opener=self._opener)

文件 "C:\Users****\AppData\Local\Continuum\miniconda3\lib\pathlib.py", 第 1037 行,在 _opener 中 return self._accessor.open(self, flags, mode)

文件 "C:\Users****\AppData\Local\Continuum\miniconda3\lib\pathlib.py", 第 387 行,已包装 返回 strfunc(str(pathobj), *args) FileNotFoundError: [Errno 2] 没有这样的文件或目录: '\images\dependency_plot.svg'

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

【问题讨论】:

    标签: python spacy dependency-parsing


    【解决方案1】:

    我认为你有 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)
    

    【讨论】:

    • 我做了更改,我收到错误TypeError: write() argument must be str, not None
    • 我认为您需要删除jupyter=True 您是否也这样做了?喜欢删除那个jupyter部分吗?我添加了适合我的代码
    • 你的代码给了我一个TypeError: write() argument must be str, not None
    • 属性jupyter=False 对我有用。
    【解决方案2】:

    我关注了@Petr Matuska 的回答,遇到了大家都在评论的错误。在调试时,我发现SpaCy documentation 中也提到了两个问题。

    • 当你想保存时,在渲染方法中包含jupyter=False 树为 SVG。这样你就不会在 Jupyter 上看到输出,但是你 可以打开保存的文件查看结果。
    • 使用整个文档 渲染而不是单个句子。请参阅他们的注释 官网

    重要提示因为每个可视化都是作为单独的 SVG,导出 .svg 文件仅在您渲染单个文件时有效 医生一次。 (这是有道理的——毕竟,每个可视化 应该是一个独立的图形。)所以不要在 一,遍历它们并分别导出它们。

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

    这是直接来自SpaCy documentation 的代码 sn-p,非常适合我。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-03
      • 2012-01-12
      • 2015-02-26
      • 2016-12-17
      • 2023-04-08
      • 2020-07-10
      • 2018-07-02
      • 1970-01-01
      相关资源
      最近更新 更多