Visualise dependency parse using Displacy (Python)
Asked Answered
G

2

7

I am following instruction on https://spacy.io/usage/visualizers and tried to visualise the dependency parse with the following code on both Jupyter Notebook and Spyder:

import spacy
from spacy import displacy
nlp = spacy.load('en')
doc = nlp(u'This is a sentence.')
displacy.serve(doc, style='dep')

and the output shows:

Serving on port 5000...
Using the 'dep' visualizer

Can anyone tell me if I am missing something here?

I am using Window 10, Visual Studio 2015, and the virtual environment that is running this is set up through Anaconda. Thanks in advance

Grosz answered 10/4, 2018 at 9:43 Comment(3)
Have you tried opening a browser on localhost:5000 ?Dutcher
Another suggestion: If you're in a Jupyter notebook, check out this docs section on using displaCy with Jupyter. Since you're already in a browser, you don't need to start a server and call displacy.serve – instead, you can use displacy.render. If the visualisation doesn't show up correctly, you can set jupyter=True to force Jupyter-style rendering.Rosenberger
Yes thank you both. Both answers works :)Grosz
C
7
import spacy
from spacy import displacy

nlp = spacy.load('en')
doc = nlp(u'This is a sentence.')

doc = nlp(u'Rats are various medium-sized, long-tailed rodents.')
displacy.render(doc, style='dep',jupyter=True)
Circumvallate answered 7/7, 2018 at 2:44 Comment(1)
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Cithara
N
0

You can view the results in a Web browser.

You just need to open the http://localhost:5000/ page, and you will see something like

enter image description here

Naturalize answered 1/3, 2021 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.