How do you create NLTK draw () trees that are built into iPython / Jupyter

For Matplotlib plots in iPython / Jupyter, you can make laptop plot plots embedded with

%matplotlib inline 

How can I do the same for drawing NLTK () for trees? Here is the documentation http://www.nltk.org/api/nltk.draw.html

+6
source share
3 answers

Based on this answer:

 import os from IPython.display import Image, display from nltk.draw import TreeWidget from 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') 

A little slow but doing the job. If you do this remotely, remember to start the ssh session with the -X key (for example, ssh -X user@server.com ) so that Tk can initialize itself ( no display name and no $DISPLAY environment variable -kind of error)

UPD : it seems that the latest versions of jupyter and nltk work well together, so you can just do IPython.core.display.display(tree) to get an attractive tree-like rendering built into the output.

+7
source

2019 Update:

This works on a Jupyter laptop:

 from nltk.tree import Tree from IPython.display import display tree = Tree.fromstring('(S (NP this tree) (VP (V is) (AdjP pretty)))') IPython.core.display.display(tree) 

Requirements:

  • NLTK
  • Ghostscript
0
source

BK-EBDP, Dibutilon, U-47700, TH-PVP, 4C-PVP, Ethisolam

We are a professional manufacturer of all kinds of research chemicals in any quantity you want. Our products include; BK ebdp, ADBF, fentanyl, 5mbdb2201, Amb-f, Adbf, 5fadb, hexedrone, xanax, LSD, lysergic acid, diethylamide, ethylone, methylone, MDMA, mephedrone, ketamine, heroin, cocaine, speed, methamphetamine, MDPV, MDPV PVP, LSD, ephedrine hcl, pseudoephedrine hcl, piperionyl methyl ketone, oil / powder,

Our quality is the best you can find, and we sell it in small / large lots with guaranteed timely delivery.

Skype ID ................... chupacker1

Place your order by email: jiansong19@gmail.com

-4
source

All Articles