Sub topic 1
What Information Extraction Covers
Prerequisites
This chapter assumes you can write basic Python, read JSON, and know roughly what a large language model does. You should also understand precision, recall, and F1 at an introductory level.
What information extraction covers
Information extraction (IE) is the task of pulling specific facts out of unstructured text and storing them in a fixed form. Common task types include:
- Named entity recognition (NER): finding spans such as people, places, and organizations.
- Relation extraction: linking two entities, such as a person and their employer.
- Key-value extraction: filling fields such as vendor, total, and currency from an invoice.
Defining the schema and labeling rules
A schema lists each field, its type, and its allowed values. Labeling guidelines settle the edge cases. For example, does "Acme Ltd." match the vendor "Acme", and does a span include a trailing period? Without written rules, two annotators label the same text differently, and your scores then measure disagreement instead of system quality.
Building a gold dataset
A gold dataset is a set of texts with correct answers, labeled by people who follow the guidelines. Sample the texts from the inputs your system will actually see. Have two annotators label a subset and measure how often they agree before you trust the labels. Keep this set separate from any examples you place in prompts.
Measuring extraction quality with span-level F1
A predicted span is correct only when its start, end, and label all match the gold span exactly. Then:
- Precision = correct predictions / all predictions
- Recall = correct predictions / all gold spans
- F1 = 2 × precision × recall / (precision + recall)
def span_f1(gold, pred):
gold, pred = set(gold), set(pred)
tp = len(gold & pred)
precision = tp / len(pred) if pred else 0.0
recall = tp / len(gold) if gold else 0.0
if precision + recall == 0:
return precision, recall, 0.0
f1 = 2 * precision * recall / (precision + recall)
return precision, recall, f1
gold = [(0, 12, "VENDOR"), (20, 27, "TOTAL")]
pred = [(0, 12, "VENDOR"), (20, 26, "TOTAL")]
print(span_f1(gold, pred))The second total span ends one character early, so it does not match. The output is (0.5, 0.5, 0.5).
Exercise
The gold set has 4 spans. The system predicts 5 spans, and 3 of the predictions match gold exactly. What are precision, recall and F1?
Show answer
Precision = 3/5 = 0.60. Recall = 3/4 = 0.75. F1 = 2 × 0.60 × 0.75 / (0.60 + 0.75) ≈ 0.67.