我有一段文本,我希望使用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
发布于 2021-03-01 10:02:54
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依赖可视化工具,它显示句子中标记的语法依赖关系图。中给出了一个例子。
https://stackoverflow.com/questions/65562353
复制相似问题