Nltk: How could I inline nltk graph to jupyter notebook?

Created on 3 Jul 2017  ·  7Comments  ·  Source: nltk/nltk

I have already asked this question on stackoverflow without any luck and decide to duplicate it here.

According to the sources of nltk it draws graph by tkinter (GUI) but I need to inline this graph to jupyter notebook. And I'm trying to do it inside of official docker from anaconda3 in other words I don't need any popup GUI here but just image inside of notebook, that should be render on server side by nltk lib.

How could I overcome this by nltk? Maybe there is third party libs which could help there?

Sources of my try is here - the last 18th cell.

chunkGram = r"""Chunk: {<RB.?>*<VB.?>*<NNP>+<NN>?}"""
chunkParser = nltk.RegexpParser(chunkGram)

for i in tokenized_text[:5]:
    words = nltk.word_tokenize(i)
    tagged = nltk.pos_tag(words)
    chunked = chunkParser.parse(tagged)
    chunked.draw()

PS:
in the same time matplotlib inline by itself works like a charm. Could I use matplotlib for graph rendering?

Thanks!

bug corpus enhancement nice idea

Most helpful comment

Moving away from tkinter is a good idea in general, but there's already support for rendering trees as inline PNGs in notebooks:

````
import nltk
from IPython.display import display

parser = nltk.RegexpParser(r'NP: {<[NJ].*>+}')
tree = parser.parse(nltk.corpus.brown.tagged_sents()[0])
display(tree)
````

All 7 comments

ok possible work around could be by:

1 installing Xvfb,
2 making screenshot of tree and than
3 converting ps to png
3 inlining converted screenshot back to jupyter

but as for me it looks more like a dirty hack. There should be some more robust way to render tree.

I think it's a good idea to move away from tkinter for plots and move towards never graphic plotting libraries, e.g.matplotlib / seaborn.

Moving away from tkinter is a good idea in general, but there's already support for rendering trees as inline PNGs in notebooks:

````
import nltk
from IPython.display import display

parser = nltk.RegexpParser(r'NP: {<[NJ].*>+}')
tree = parser.parse(nltk.corpus.brown.tagged_sents()[0])
display(tree)
````

This doesn't work for me on a remote Jupyter notebook server. The tree's _repr_png_() is calling CanvasFrame(), which wants to create a tkinter window (and there is no display on the remote server). Not really sure what the right workaround is.

image

nltk=3.2.4
ipython==6.2.1
jupyter-core==4.4.0

Good catch!

Someone else just opened a new issue (#1887) about the same thing. Not sure about the procedure, but can we merge them?

Just to add to rmalouf solution which works for me after a few tweaks:

Mac
Jupyter
Python 2.7

You need to have ghostscript installed for this to work: https://wiki.scribus.net/canvas/Installation_and_Configuration_of_Ghostscript
brew install ghostscript

If this fails due xcrun: error: invalid active developer path

Then do the following first
see http://mds.is/xcrun-error/
xcode-select --install

I've recently put together a pure python=>SVG tree-drawing package that can be used as a drop-in replacement for Tree's png-based repr in Jupyter. It may not be appropriate for everyone using NLTK, as it requires python 3 + it is still pretty early in its release cycle (and it is partly aimed at doing a bunch of other stuff that is perhaps less relevant to this audience). But, it solves the issues raised in this thread (and https://github.com/nltk/nltk/issues/1887), which have also been plaguing me:

https://github.com/rawlins/svgling

(n.b. if you want to fully avoid tkinter-related issues in Jupyter, you may also need to remove _repr_png_() from Tree. This is because Jupyter tries all available _repr_*_ functions even though it only displays one in typical circumstances, and saves the output of all of them in the notebook file.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

libingnan54321 picture libingnan54321  ·  3Comments

peterbe picture peterbe  ·  5Comments

DavidNemeskey picture DavidNemeskey  ·  4Comments

stevenbird picture stevenbird  ·  3Comments

Chris00 picture Chris00  ·  3Comments