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.

Dependency parsing identifies how words depend on one another in a sentence. Like a sentence diagram, its tree gives an NLP pipeline a representation of grammatical relationships that can help it interpret text and extract relationships between entities. section 11.6

Unlike dependency parsing, which relates words, constituency parsing breaks a sentence into subphrases such as noun and verb phrases. Constituency parses can capture more syntactic information, but are described as slower to compute and harder to interpret. section 11.6

Inspect a sentence with spaCy

In “Gebru was unethically fired from her Ethical AI team,” spaCy labels “fired” as the root and “Gebru” as a passive subject. The root’s children include “Gebru,” “was,” “unethically,” “from,” and the final punctuation. The preposition “from” has “team” as a child. Together, the labels and children expose the sentence’s structure rather than just its word sequence. 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                   []

Inspect the token labels and child lists for the example sentence. section 11.6

Part-of-speech tags and named-entity labels complement the dependency tree: they help identify entities and verbs that may connect them. Extracted relationships can become edges between entities in a knowledge graph. Inspect the output before treating it as fact; entity labels can vary between models. section 11.4.2 section 11.0

Parsing can also support sentence segmentation when punctuation or capitalization is ambiguous. It costs processing time: in one manuscript example, spaCy took about 16 seconds versus 0.022 seconds for a regular-expression approach. A faster approach may reduce downstream accuracy, so compare speed and segmentation quality on the documents you need to process. spaCy also offers a statistical sentence segmenter that can run without the full parser pipeline. section 11.2.2

>>> import spacy
>>> nlp = spacy.load('en_core_web_md')
>>> doc = nlp("Are you an M.D. Dr. Gebru? either way you are brilliant.")
>>> [s.text for s in doc.sents]
['Are you an M.D. Dr. Gebru?', 'either way you are brilliant.']

See spaCy segment a sentence containing abbreviations and unusual capitalization. section 11.2.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through token labels, children, and tree visualization; contrasts constituency parsing without explaining parser internals.
Examples
Inspects a passive sentence about Gebru and segments a sentence containing abbreviations and unusual capitalization.
What you can do after
Apply it via spaCy to inspect dependency labels and token children.
How it connects
Connects named-entity recognition, part-of-speech tagging, sentence segmentation, 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.