liveBook · Manning

Dependency parsing

As explained in Natural Language Processing in Action, Second Edition

A software component that identifies dependency relations between words in a sentence.

A sentence can name several people, organizations, and places without making their relationships obvious. Dependency parsing identifies how words relate to one another, producing a tree that helps an information-extraction pipeline find relationships between named entities. Those relationships can become edges in a knowledge graph, but a grammatical link is not, by itself, a verified fact. section 11.6 section 11.0

The tree connects words according to their roles in a sentence. Part-of-speech tags help identify nouns and verbs; dependency labels describe roles such as a subject or a modifier. This differs from constituency parsing, which groups words into phrases such as noun phrases and verb phrases. Dependency trees are the practical focus here because constituency results are slower to compute and harder to interpret. section 11.4.2 section 11.6

Inspect the words around a verb

Run `doc = nlp("Gebru was unethically fired from her Ethical AI team.")`, then inspect each token’s `pos_`, `ent_type_`, `dep_`, and `children`. In the resulting parse, `fired` is `ROOT`; its children include `Gebru`, `was`, `unethically`, and `from`. `Gebru` has the passive-subject label `nsubjpass`, while `from` leads toward `team`. Read these links as evidence about the sentence’s structure, not as independent confirmation of its claim. section 11.6

>>> def doc2df(doc):
...     df = pd.DataFrame([token_dict2(t) for t in doc])
...     return df.set_index('TOK')
>>> doc2df(doc)
               POS   TAG ENT_TYPE        DEP             children
TOK
Gebru        PROPN   NNP   PERSON  nsubjpass                   []
was            AUX   VBD             auxpass                   []
unethically    ADV    RB              advmod                   []
fired         VERB   VBN                ROOT  [Gebru, was, une...
from           ADP    IN                prep               [team]
her           PRON  PRP$                poss                   []
Ethical      PROPN   NNP      ORG   compound                   []
AI           PROPN   NNP      ORG   compound            [Ethical]
team          NOUN    NN                pobj            [her, AI]
.            PUNCT     .               punct                   []

The token table places part-of-speech tags, entity types, dependency labels, and children side by side, making the tree inspectable one word at a time. section 11.6

For a visual check, spaCy’s `displacy.render` can write the parsed sentence to an HTML file. Inspecting the tree is useful before turning token relationships into entity relationships: an entity may span multiple tokens, and entity labels can differ across spaCy models. The parser also has a processing cost. Its dependency structure helps spaCy segment sentences with ambiguous punctuation, but a simpler regular-expression segmenter can be much faster at the expense of downstream accuracy. section 11.6 section 11.4.2 section 11.2.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through token-level dependency labels and child links in spaCy, then visualizes a tree; does not detail parser training.
Examples
Inspects a spaCy parse of a sentence about Gebru and renders its dependency tree with displaCy.
What it assumes
Builds on token-level part-of-speech tagging and named entity recognition while defining dependency parsing through sentence diagrams.
What you can do after
Apply it via spaCy to inspect word dependencies and visualize sentence structure before extracting entity relationships.
How it connects
Connects part-of-speech tagging, named entity recognition, sentence segmentation, constituency parsing, information extraction, and knowledge graphs.

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.