liveBook · Manning

Dependency graph

As explained in Natural Language Processing in Action, Second Edition

A dependency tree is a tree representation of grammatical relationships in which words depend on other words within a sentence.

A dependency graph represents grammatical relationships among words. Arcs connect dependents to the words that shape their meaning, revealing how a sentence branches and nests. The root is the token with no incoming arrowheads; other words depend on it directly or indirectly. section 2.1.4

Visualizing a sentence with spaCy

Load spaCy's en_core_web_sm model, process a sentence, select its first sentence, and render a dependency diagram with displaCy. The diagram exposes token relationships alongside parts of speech and lemmas, turning an otherwise linear sentence into a navigable structure. section 2.1.4

Dependency graphs also provide a bridge from language processing to knowledge representation. Parsed facts can become connections between entities in a knowledge graph, supporting queries about relationships and grounding language applications in explicit facts. section 11.0

>>> from spacy import displacy
>>> sentence = list(doc.sents)[0]       #1
>>> svg = displacy.render(sentence, style="dep",
...     jupyter=False)                          #2
>>> open('sentence_diagram.svg', 'w').write(svg)       #3
>>> # displacy.serve(sentence, style="dep")     #4
>>> # !firefox 127.0.0.1:5000
>>> displacy.render(sentence, style="dep")    #5

Run the displaCy listing to render an interactive or notebook-based dependency diagram, or save the resulting SVG for inspection. section 2.1.4

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains roots, dependents, branching, and visualization, but does not detail the parsing algorithm itself.
Examples
Demonstrates spaCy and displaCy on a sentence about survival, including a rendered SVG dependency diagram.
What it assumes
Assumes familiarity with tokens and part-of-speech tags while connecting those annotations to sentence meaning.
What you can do after
Operate spaCy and displaCy to render dependency diagrams and inspect grammatical relationships in processed text.
How it connects
Connects tokenization, part-of-speech tagging, lemmatization, dependency parsing, and knowledge graphs.

Also explained in

Compare how 5 books explain dependency graph →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.