How do you make NLTK draw() trees that are inline in iPython/Jupyter -
for matplotlib plots in ipython/jupyter can make notebook plot plots inline
%matplotlib inline
how can 1 same nltk draw() trees? here documentation http://www.nltk.org/api/nltk.draw.html
based on this answer:
import os ipython.display import image, display nltk.draw import treewidget nltk.draw.util import canvasframe def jupyter_draw_nltk_tree(tree): cf = canvasframe() tc = treewidget(cf.canvas(), tree) tc['node_font'] = 'arial 13 bold' tc['leaf_font'] = 'arial 14' tc['node_color'] = '#005990' tc['leaf_color'] = '#3f8f57' tc['line_color'] = '#175252' cf.add_widget(tc, 10, 10) cf.print_to_file('tmp_tree_output.ps') cf.destroy() os.system('convert tmp_tree_output.ps tmp_tree_output.png') display(image(filename='tmp_tree_output.png')) os.system('rm tmp_tree_output.ps tmp_tree_output.png')
little slow, job. if you're doing remotely, don't forget run ssh session -x
key (like ssh -x user@server.com
) tk initialize (no display name , no $display environment variable
-kind of error)
upd: seems last versions of jupyter , nltk work nicely together, can ipython.core.display.display(tree)
nice-looking tree-render embedded output.
Comments
Post a Comment