Sub topic 11
Extracting Subject-Verb-Object Relations
Assume you know basic Python, can run a spaCy pipeline, and understand nouns, verbs, subjects, and objects. This chapter uses dependency parsing to extract simple facts from sentences.
A subject-verb-object (SVO) relation represents an action as three parts:
- Subject: who or what performs the action.
- Verb: the action, represented by its lemma.
- Object: who or what receives the action.
For example, “Researchers published the dataset” becomes:
| Subject | Verb | Object |
|---|---|---|
| Researchers | publish | dataset |
Read the dependency structure
The parser connects each token to a governing token called its head. A subject usually has the dependency label nsubj, and a direct object usually has obj. Older models or other languages may use dobj for direct object.
The following diagram shows the structure of a simple sentence:
A relation is easiest to find by starting with a subject. The subject’s head should be a verb. Then inspect that verb’s children for an object.
Extract simple triples
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp(
"Researchers published the dataset and the editors reviewed the paper."
)
for token in doc:
if token.dep_ != "nsubj" or token.head.pos_ != "VERB":
continue
subject = token
verb = token.head
objects = [
child for child in verb.children
if child.dep_ in {"obj", "dobj"}
]
for obj in objects:
print(subject.text, verb.lemma_, obj.text)Typical output is:
Researchers publish dataset
editors review paperverb.lemma_ changes “published” to “publish” and “reviewed” to “review”. This makes relations easier to group. The object text contains only the root token in this basic version. For “the large dataset”, the result may be “dataset” rather than the entire noun phrase.
> callout note > Dependency labels depend on the language and trained model. Check the actual output before hard-coding rules. In English pipelines, nsubj and obj are common, but dobj may appear in older resources.
Include complete noun phrases
A noun chunk gives a more useful subject or object phrase. Find the chunk whose root is the dependency token, then use its full text.
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The research team published a detailed dataset.")
chunks_by_root = {chunk.root.i: chunk.text for chunk in doc.noun_chunks}
for token in doc:
if token.dep_ != "nsubj" or token.head.pos_ != "VERB":
continue
verb = token.head
for child in verb.children:
if child.dep_ in {"obj", "dobj"}:
subject_text = chunks_by_root.get(token.i, token.text)
object_text = chunks_by_root.get(child.i, child.text)
print(subject_text, verb.lemma_, object_text)The output is similar to:
The research team publish a detailed datasetThis approach works well for straightforward active sentences. It does not automatically resolve pronouns, implied subjects, or references across sentences.
Common failure cases
- Passive voice: In “The dataset was published by researchers”, the dataset may be
nsubjpass, while researchers may be anagent. The active SVO rule will miss or misread it. - Coordinated clauses: A sentence with “and” can contain several subject-verb pairs. Process every matching subject instead of stopping after the first.
- Complex objects: Objects can be clauses, prepositional phrases, or coordinated nouns rather than one noun token.
- Parser errors: Informal text, domain-specific terms, spelling mistakes, and long sentences can produce incorrect heads or labels.
Exercise
What triple should the basic extractor produce for “The analyst examined the report”?
Show answer
It should produce analyst examine report, using the subject token, the verb lemma, and the object token.