<div dir="ltr"><div>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.<br>
<br></div><div>Thanks for any ideas!<br>Steve<br></div><div><br>import nltk, re, pretty<br><br>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")<br>
sentences = [nltk.word_tokenize(sent) for sent in sentences] <br>sentences = [nltk.pos_tag(sent) for sent in sentences]<br><br>grammar = "NP: {<DT>?<JJ>*<NN>}" <br><br>cp = nltk.RegexpParser(grammar) <br>
result = cp.parse(sentences[0]) <br>print result # this works<br><br></div><div>#output<br>(S<br> I/PRP<br> love/VBP<br> ipython/JJ<br> for/IN<br> (NP prototyping/NN)<br> and/CC<br> (NP research/NN)<br> in/IN<br>
(NP python/NN)<br> and/CC<br> NLTK/NNP<br> ./.)<br></div><div><br></div>result.draw() #this doesn't work, kernel busy message and appears to hang ipython notebook<br></div>