Back: Introduction

English dependency parsing with spaCy

In the previous activity, we used spaCy to obtain tokenization, lemmas, and POS annotations. The same pipeline also provides dependency annotations, which represent grammatical relationships between words. We will focus particularly on:

  • HEAD: the word that a token depends on
  • DEPREL: the grammatical relationship between the token and its head

Loading the pipeline

import spacy

nlp_en = spacy.load("en_core_web_trf")

Inspecting dependency annotations

Process a sentence:

text = "The students analyzed the data carefully."

doc = nlp_en(text)

Inspect the dependency information:

for token in doc:
    print(
        token.text,
        token.pos_,
        token.head.text,
        token.dep_,
        sep="\t"
    )

The important attributes are:

  • token.text: the current token
  • token.pos_: its UPOS category
  • token.head.text: its syntactic head
  • token.dep_: its dependency relation to the head

For example:

students    NOUN    analyzed    nsubj
data        NOUN    analyzed    dobj
carefully   ADV     analyzed    advmod

Read each row from the perspective of the current token:

students → analyzed (nsubj)

means that students depends on analyzed as its subject.

Practice: inspecting dependency relations

Write several English sentences of your own and process them with spaCy.

for doc in nlp_en.pipe(sentences):

    print("\nSentence:", doc.text)

    for token in doc:
        print(
            token.text,
            token.pos_,
            token.head.text,
            token.dep_,
            sep="\t"
        )

For each sentence, identify:

  1. Which token is the root?
  2. Which token is the subject?
  3. Is there an object?
  4. What is the head of each token?
  5. What grammatical relationship does each DEPREL represent?

Next: CoNLL-U format