Dependency parsing 2. English dependency parsing
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 onDEPREL: 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 tokentoken.pos_: its UPOS categorytoken.head.text: its syntactic headtoken.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:
- Which token is the root?
- Which token is the subject?
- Is there an object?
- What is the head of each token?
- What grammatical relationship does each
DEPRELrepresent?