将 nltk 绘制的解析树保存到图像文件

2024-01-05

有没有办法以编程方式将 tree.draw() 中的绘制图像保存到图像文件?我尝试查看文档,但找不到任何内容。


使用nltk.draw.tree.TreeView https://github.com/nltk/nltk/blob/develop/nltk/draw/tree.py#L753自动创建画布框架的对象:

>>> from nltk.tree import Tree
>>> from nltk.draw.tree import TreeView
>>> t = Tree.fromstring('(S (NP this tree) (VP (V is) (AdjP pretty)))')
>>> TreeView(t)._cframe.print_to_file('output.ps')

Then:

>>> import os
>>> os.system('convert output.ps output.png')

[输出.png]:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 nltk 绘制的解析树保存到图像文件 的相关文章

随机推荐