question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How could I inline nltk graph to jupyter notebook?

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
rawlinscommented, 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 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.)

6reactions
rmaloufcommented, 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)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you make NLTK draw() trees that are inline in iPython ...
Based on this answer: import os from IPython.display import Image, display from nltk.draw import TreeWidget from nltk.draw.util import CanvasFrame def ...
Read more >
How to Plot Inline and With Qt - Matplotlib with IPython/Jupyter ...
In this tutorial, we'll go over how to plot inline plots using IPyhon/Jupyter notebooks, with Matplotlib in Python. We'll cover how to plot...
Read more >
Plotly - Plotting Inline with Jupyter Notebook - Tutorialspoint
Keep rest of the script as it is and run the notebook cell by pressing Shift+Enter. Graph will be displayed offline inside the...
Read more >
How to Use "%matplotlib inline" (With Examples) - Statology
This tutorial explains how to use the "%matplotlib inline" function in Python Jupyter notebooks, including an example.
Read more >
Displaying Matplotlib Graphs Inline in Jupyter Notebook
Displaying Matplotlib Graphs Inline in Jupyter Notebook. Updated to include how to modify the config file so this is the default behavior.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found