我想使用 spaCy 在这里提供的代码保存 spaCy 的可视化效果:https://spacy.io/usage/visualizers
这是我的代码:
nlp = spacy.load("en_core_web_sm")
doc = nlp(u"Autonomous cars shift insurance liability toward manufacturers")
svg = spacy.displacy.render(doc, style="dep")
output_path = Path(os.path.join("./", "sentence.svg"))
output_path.open('w', encoding="utf-8").write(svg)
但是当我执行此代码时,出现错误:TypeError: write() argument must be str, not None
那么如何保存 spacy.displace.render 的输出?我该如何修复这个错误?
最佳答案
问题中提供的代码在 PyCharm 等 IDE 中执行时工作正常。
但是,displacy.render()
在 Jupyter 笔记本中的工作方式略有不同,请参见文档 here和 here :
If you’re running a Jupyter notebook, displaCy will detect this and return the markup in a format ready to be rendered and exported.
您可以通过在 render()
调用中显式设置 jupyter=False
来覆盖此自动检测。
关于python - 当我将 displacy.render(doc, style ="dep") 的输出保存到 svg 文件时,出现 TypeError : write() argument must be str, not None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55723812/