【问题标题】:Using spacy in R Markdown with reticulate在 R Markdown 中使用 spacy 和 reticulate
【发布时间】:2019-12-28 09:28:06
【问题描述】:

我正在尝试在 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?

【问题讨论】:

    标签: python r r-markdown spacy reticulate


    【解决方案1】:

    假设您要输出到 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)
    ```
    

    【讨论】:

    • 谢谢,但即使在添加之后。 results='asis',输出是一样的。没有渲染 svg。在 YAML 中,输出为 html_document
    • 升级到 1.15 版后不开心。我在 Mac OS 上。会不会是个问题?
    • 非常感谢您的帮助!添加results='asis'后,我没有编织文档!我只是想执行代码块。我的错。但是现在当我编织它时,我看到了 SVG。再次感谢您的帮助和耐心!
    猜你喜欢
    • 2022-01-02
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2021-11-13
    相关资源
    最近更新 更多