【问题标题】:read pyTextRank file读取 pyTextRank 文件
【发布时间】:2021-01-04 11:57:57
【问题描述】:

我有一段文本,我希望使用 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

【问题讨论】:

    标签: plotly networkx graphviz pytextrank


    【解决方案1】:

    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 中有一个例子。

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-30
      相关资源
      最近更新 更多