python - 如何在 NLTK Python 中将依赖图保存为图像
问题描述
我发现了一些帖子,包括这篇关于如何生成依赖图的帖子。但是,我正在寻找将依赖关系图导出为 PNG/SVG 图像的选项。
为简单起见,假设我们已经解析了一个句子The quick brown fox jumps over the lazy dog.
,并且我们有to_conll(4)格式的输出。现在如果将此结果传递给DependencyGrpah
班级
from nltk.parse import DependencyGraph
DependencyGraph(
'''The DT 4 det
quick JJ 4 amod
brown JJ 4 amod
fox NN 5 nsubj
jumps VBZ 0 ROOT
over IN 9 case
the DT 9 det
lazy JJ 9 amod
dog NN 5 obl
. . 5 punct'''
)
它产生以下图像
我的问题是如何将此图像保存为 PNG/SVG 文件?
解决方案
您可以将其从 a 转换.ps
为 a .png
。
import os
from nltk.draw.tree import TreeView
TreeView(GRAPH)._cframe.print_to_file('tree.ps')
os.system('convert tree.ps tree.png')
推荐阅读
- wordpress - WordPress 主菜单链接有项目符号并且不是水平的
- firebase - 通过 Flutter 应用使用 Firebase 登录谷歌
- javascript - 我如何在 Spring Boot 中使用这个图表 js?
- flutter - 如何在颤动中知道以前的路线名称?
- python - 如何使用 pickle 模块更新记录?
- android - Gradle:compileOnly 和 runtimeOnly
- ios - 如何更改 Flutter 中的 ios 模拟器?
- laravel - 如何使用 laravel 6 配置 AdminLTE 3 javascript 链接?
- php - 如何在 PHP 中制作不同的 JSON 结构
- kotlin - 为什么 Swagger 不检测可选的 JSON 属性?