Joh*_*ohn 15 python tree nlp text-parsing nltk
有没有办法以一种编程方式将绘图图像从tree.draw()保存到图像文件?我试着查看文档,但我找不到任何东西.
alv*_*vas 12
使用该nltk.draw.tree.TreeView
对象自动创建画布框:
>>> 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')
Run Code Online (Sandbox Code Playgroud)
然后:
>>> import os
>>> os.system('convert output.ps output.png')
Run Code Online (Sandbox Code Playgroud)
[output.png]:
小智 11
我有完全相同的需求,并查看nltk.draw.tree
我找到解决方案的源代码:
from nltk import Tree
from nltk.draw.util import CanvasFrame
from nltk.draw import TreeWidget
cf = CanvasFrame()
t = Tree.fromstring('(S (NP this tree) (VP (V is) (AdjP pretty)))')
tc = TreeWidget(cf.canvas(),t)
cf.add_widget(tc,10,10) # (10,10) offsets
cf.print_to_file('tree.ps')
cf.destroy()
Run Code Online (Sandbox Code Playgroud)
输出文件是postscript,您可以使用终端上的ImageMagick将其转换为图像文件:
$ convert tree.ps tree.png
Run Code Online (Sandbox Code Playgroud)
我认为这是一个快速而肮脏的解决方案; 它可能是低效的,因为它显示画布并在以后销毁它(也许有一个选项来禁用显示,我找不到).如果有更好的方法,请告诉我.
要添加Minjoon的答案,您可以更改树的字体和颜色,使其看起来更像NLTK .draw()
版本,如下所示:
tc['node_font'] = 'arial 14 bold'
tc['leaf_font'] = 'arial 14'
tc['node_color'] = '#005990'
tc['leaf_color'] = '#3F8F57'
tc['line_color'] = '#175252'
Run Code Online (Sandbox Code Playgroud)
之前(左)和之后(右):