在带网状结构的 R Markdown 中使用 spacy

Using spacy in R Markdown with reticulate

我正在尝试在 R Markdown 文档中使用 spacy。除了使用 displacy 进行渲染外,一切似乎都正常。这是我正在使用的代码块:

```{python}
from spacy import displacy
doc = nlp("My name is Dhiraj.")
displacy.render(doc)
```

当我编织文档时,或者即使我执行代码块,我也看不到带有箭头的 svg。相反,这是执行上述代码块时的输出:

'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="127-0" class="displacy" width="750" height="224.5" style="max-width: none; height: 224.5px; color: #000000; background: #ffffff; font-family: Arial">\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="134.5">\n    <tspan class="displacy-word" fill="currentColor" x="50">My</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="50">ADJ</tspan>\n</text>\n\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="134.5">\n    <tspan class="displacy-word" fill="currentColor" x="225">name</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="225">NOUN</tspan>\n</text>\n\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="134.5">\n    <tspan class="displacy-word" fill="currentColor" x="400">is</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="400">VERB</tspan>\n</text>\n\n<text class="displacy-token" fill="currentColor" text-anchor="middle" y="134.5">\n    <tspan class="displacy-word" fill="currentColor" x="575">Dhiraj.</tspan>\n    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="575">PROPN</tspan>\n</text>\n\n<g class="displacy-arrow">\n    <path class="displacy-arc" id="arrow-127-0-0" stroke-width="2px" d="M70,89.5 C70,2.0 225.0,2.0 225.0,89.5" fill="none" stroke="currentColor"/>\n    <text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">\n        <textPath xlink:href="#arrow-127-0-0" class="displacy-label" startOffset="50%" fill="currentColor" text-anchor="middle">poss</textPath>\n    </text>\n    <path class="displacy-arrowhead" d="M70,91.5 L62,79.5 78,79.5" fill="currentColor"/>\n</g>\n\n<g class="displacy-arrow">\n    <path class="displacy-arc" id="arrow-127-0-1" stroke-width="2px" d="M245,89.5 C245,2.0 400.0,2.0 400.0,89.5" fill="none" stroke="currentColor"/>\n    <text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">\n        <textPath xlink:href="#arrow-127-0-1" class="displacy-label" startOffset="50%" fill="currentColor" text-anchor="middle">nsubj</textPath>\n    </text>\n    <path class="displacy-arrowhead" d="M245,91.5 L237,79.5 253,79.5" fill="currentColor"/>\n</g>\n\n<g class="displacy-arrow">\n    <path class="displacy-arc" id="arrow-127-0-2" stroke-width="2px" d="M420,89.5 C420,2.0 575.0,2.0 575.0,89.5" fill="none" stroke="currentColor"/>\n    <text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">\n        <textPath xlink:href="#arrow-127-0-2" class="displacy-label" startOffset="50%" fill="currentColor" text-anchor="middle">attr</textPath>\n    </text>\n    <path class="displacy-arrowhead" d="M575.0,91.5 L583.0,79.5 567.0,79.5" fill="currentColor"/>\n</g>\n</svg>'

如何将其呈现为预期的 svg?

假设你输出到 HTML,如果你在块选项中有 results='asis',这应该可以工作,所以输出直接解释为 HTML:

```{python, results='asis'}
import spacy
from spacy import displacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("My name is Dhiraj.")
displacy.render(doc)
```

我的工作 RMarkdown 文件的全部内容:

---
title: "Python test"
output: html_document
---

```{python, results='asis'}
import spacy
from spacy import displacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("My name is Dhiraj.")
displacy.render(doc)
```