[IPython-User] displaying sentence parse trees from nltk from ipython notebook

Steve Hill kindageeky@gmail....
Sat Jan 26 14:49:40 CST 2013


I'm messing around with nltk in the notebook and was trying to produce a
graphic (as a popup or inline) from ipython notebook. The thing is, this
works from ipython / python command line, but not from the notebook.  The
behavior is that the kernel busy message is displayed and nothing happens.
I've played with the command line arguments such as "--pylab=tk" or
"--gui=osx" or "--gui=tk", etc.  but can seem to get the rendered sentence
tree to display.   I'm only posing this because we'll be doing a lot with
NLTK and we love the ipython notebook feature.   It looks like the drawing
in nltk uses Tkinter, so tried various ways to target tk.  For clarity, I
am relatively new to python / ipython, so apologies in advance if I've
missed something in the docs, but googled this for a couple hours, looked
at the NLTK source, etc, and decided it was time to ask for help. Sample
code is below.

Thanks for any ideas!
Steve

import nltk, re, pretty

sentences = nltk.sent_tokenize("I love ipython for prototyping and research
in python and NLTK. I hope to figure out how to display sentence trees with
POS tags in ipython")
sentences = [nltk.word_tokenize(sent) for sent in sentences]
sentences = [nltk.pos_tag(sent) for sent in sentences]

grammar = "NP: {<DT>?<JJ>*<NN>}"

cp = nltk.RegexpParser(grammar)
result = cp.parse(sentences[0])
print result  # this works

#output
(S
  I/PRP
  love/VBP
  ipython/JJ
  for/IN
  (NP prototyping/NN)
  and/CC
  (NP research/NN)
  in/IN
  (NP python/NN)
  and/CC
  NLTK/NNP
  ./.)

result.draw() #this doesn't work, kernel busy message and appears to hang
ipython notebook
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-user/attachments/20130126/5d2861d4/attachment.html 


More information about the IPython-User mailing list