Relation Extraction in NLP
From entities to facts
Recognizing that a sentence mentions 'Elon Musk' and 'SpaceX' is useful, but the real information is the relationship between them — 'Elon Musk founded SpaceX.' Relation Extraction (RE) is the task of identifying and classifying semantic relationships between pairs of entities in text, turning unstructured sentences into structured (subject, relation, object) triples — the building block of knowledge graphs.
Supervised relation classification
The classic supervised formulation: given a sentence with two tagged entity mentions, classify the relation between them from a fixed schema (e.g., founded_by, located_in, employed_by, or no_relation). Early approaches used hand-crafted features (the dependency path between the two entities, words between them, their entity types) fed into an SVM or log-linear classifier. Neural approaches encode the sentence with a CNN or LSTM and inject positional information (how far each word is from each entity) so the model can attend to the relevant span connecting them; modern approaches fine-tune a transformer with entity markers inserted directly into the input text, letting self-attention naturally model the relationship.
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModelForSequenceClassification.from_pretrained(
"bert-base-uncased", num_labels=5 # e.g., founded_by, located_in, employed_by, acquired_by, no_relation
)
def mark_entities(text, e1_span, e2_span):
# Insert special markers around each entity so the model can
# distinguish "which two entities are we classifying the relation between"
s1, e1 = e1_span
s2, e2 = e2_span
marked = (
text[:s1] + "[E1]" + text[s1:e1] + "[/E1]" +
text[e1:s2] + "[E2]" + text[s2:e2] + "[/E2]" + text[e2:]
)
return marked
sentence = "Elon Musk founded SpaceX in 2002."
marked_input = mark_entities(sentence, (0, 9), (18, 24))
inputs = tokenizer(marked_input, return_tensors="pt")
logits = model(**inputs).logits
predicted_relation = torch.argmax(logits, dim=-1)Distant supervision and open relation extraction
Manually labeling relation examples at scale is expensive, so distant supervision generates training data automatically: given a known fact in a knowledge base (SpaceX founded_by Elon Musk), find every sentence in a large corpus that mentions both entities together, and heuristically label all of them as expressing that relation — even though many such sentences don't actually state the relationship, introducing substantial label noise that downstream models must be robust to (often addressed via attention mechanisms that downweight noisy sentences, or multi-instance learning). Open Relation Extraction (OpenIE) sidesteps the fixed-schema problem entirely by extracting relation phrases directly from text as free-form strings ('founded', 'co-founded in') rather than classifying into predefined categories — more flexible, less structured, harder to aggregate into a clean knowledge graph.
| Paradigm | Relation schema | Data requirement | Output structure |
|---|---|---|---|
| Supervised classification | Fixed, predefined | Hand-labeled sentence-relation pairs | Clean, schema-consistent triples |
| Distant supervision | Fixed (from KB) | Existing KB facts + large text corpus | Noisy but scalable triples |
| OpenIE | Open-ended, free text | Minimal / rule-based or self-supervised | Unstructured relation phrases |
| LLM-based extraction (prompting) | Flexible, schema given in-context | Zero to few-shot | Structured output, schema fidelity varies |
Relation extraction is the backbone of automated knowledge graph construction
Cross-sentence and implicit relations are routinely missed
What's Next?
Entities, their links, and their relations are the raw material that higher-level language understanding tasks — like answering a natural-language question directly — are ultimately built on top of.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.