微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

读取 pyTextRank 文件

如何解决读取 pyTextRank 文件

我有一段文本,我希望使用 pytextrank 将其显示为图形。代码(从源代码复制)是

    import spacy
    nlp = spacy.load("en_core_web_sm")
    import pytextrank
    import graphviz
    tr = pytextrank.TextRank()
    nlp.add_pipe(tr.PipelineComponent,name='textrank',last=True)
    
    line = "the ballistic nuclear threat can be thwarted by building a nuclear shield"
    doc = nlp(line)
    tr.write_dot(path="graph.dot")

“it”向文件“graph.dot”写入一些内容。这看起来像第一个字段“digraph {}”的json文件在这一点上我迷路了。我如何创建一个漂亮的文本图表(或者根本就是一个图表)

谢谢,

安德烈亚斯

使用 ubuntu 20.04.1LTS、python 3.8、pytextrank 2.0.3

解决方法

PyTextRank 的新在线文档中有更新,尤其是示例代码,请参见 https://derwen.ai/docs/ptr/start/ 处的“入门”页面。 GitHub 存储库的 sample.py 脚本中也显示了类似的代码。

顺便说一句,最新版本是 3.0.1,它正在跟踪新的 spaCy 3.x 更新。

这是一个简单的用法:

import spacy
import pytextrank

# example text
text = "the ballistic nuclear threat can be thwarted by building a nuclear shield"

# load a spaCy model,depending on language,scale,etc.
nlp = spacy.load("en_core_web_sm")

# add PyTextRank to the spaCy pipeline
nlp.add_pipe("textrank",last=True)
doc = nlp(text)

# examine the top-ranked phrases in the document
for p in doc._.phrases:
    print("{:.4f} {:5d}  {}".format(p.rank,p.count,p.text))
    print(p.chunks)

输出将是:

0.1712     1  a nuclear shield
[a nuclear shield]
0.1652     1  the ballistic nuclear threat
[the ballistic nuclear threat]

如果您想在 Graphviz 或其他读取 DOT 文件格式的库中可视化引理图,您可以添加:

tr = doc._.textrank
tr.write_dot(path="graph.dot")

这会将输出写入 "graph.dot" 文件。有关如何读取和呈现的示例,请参阅 Graphviz 文档。

FWIW,我们目前正在整合 kglab 库,这将开辟更广泛的图形操作和可视化功能,因为它可以与其他库和文件格式集成。

此外,如果您对如何可视化 PyTextRank 的结果有任何建议或要求,在 https://github.com/DerwenAI/pytextrank/issues 创建问题真的很有帮助,我们的开发者社区可以提供更多帮助。>

如果我没有正确解释“将文本呈现为图形”,我深表歉意,因为另一种思考方式是使用 displaCy 依赖可视化器,它显示了记号的语法依赖图一句话。 spaCy tuTorial 中给出了一个示例。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。