Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How could I inline nltk graph to jupyter notebook? #1765

Closed
hyzhak opened this issue Jul 3, 2017 · 7 comments
Closed

How could I inline nltk graph to jupyter notebook? #1765

hyzhak opened this issue Jul 3, 2017 · 7 comments

Comments

@hyzhak
Copy link

hyzhak commented Jul 3, 2017

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!

@hyzhak
Copy link
Author

hyzhak commented Jul 5, 2017

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.

@alvations
Copy link
Contributor

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.

@rmalouf
Copy link
Contributor

rmalouf commented Oct 20, 2017

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)

@cforelle
Copy link

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

@rmalouf
Copy link
Contributor

rmalouf commented Nov 14, 2017

Good catch!

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

@MartinNeighbours
Copy link

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

@rawlins
Copy link

rawlins commented Dec 10, 2018

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 #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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants