Sub topic 14
Agreement for Spans Such as Named Entities
This chapter assumes you know basic Python, common NLP tasks such as named entity recognition (NER), and the meaning of precision, recall, and F1. It focuses on agreement between annotators who mark text spans, rather than assigning one label to each complete document or sentence.
Why ordinary kappa does not fit spans
A span annotation identifies a start position, an end position, and a label. For example, an annotator might mark Ada Lovelace as a PER entity.
Unlike text classification, span annotation does not provide one obvious, fixed set of items. Annotators may disagree about:
- Whether an entity exists.
- Where the entity starts or ends.
- Which entity type it has.
- Whether nested entities are allowed.
Because the two annotators may create different numbers of spans, Cohen's kappa is usually not the first measure to use. Instead, treat one annotator's spans as predictions and the other annotator's spans as the reference.
Exact-match span agreement
Represent each span as (start, end, label). Use character offsets or token offsets consistently. The end offset is commonly exclusive, so (0, 12) covers positions 0 through 11.
An exact match requires all three values to agree:
| Annotator A | Annotator B | Exact match? |
|---|---|---|
| `(0, 11, PER)` | `(0, 11, PER)` | Yes |
| `(0, 11, PER)` | `(0, 12, PER)` | No |
| `(0, 11, PER)` | `(0, 11, ORG)` | No |
Calculate precision, recall, and F1 from the matching spans:
- Precision is the fraction of annotator A's spans also found in annotator B's spans.
- Recall is the fraction of annotator B's spans found by annotator A.
- F1 combines precision and recall.
def span_f1(spans_a, spans_b):
a, b = set(spans_a), set(spans_b)
if not a and not b:
return 1.0
matches = len(a & b)
precision = matches / len(a) if a else 0.0
recall = matches / len(b) if b else 0.0
if precision + recall == 0:
return 0.0
return 2 * precision * recall / (precision + recall)
ann1 = [
(0, 6, "PER"),
(20, 26, "ORG"),
(40, 46, "LOC"),
]
ann2 = [
(0, 6, "PER"),
(20, 26, "LOC"),
(40, 46, "LOC"),
]
print(round(span_f1(ann1, ann2), 3))The output is 0.667: two of the three spans match exactly.
Choosing a matching rule
Exact matching is strict and useful when boundary accuracy matters. Other tasks need different rules.
- Boundary-only matching ignores the entity type. Use it to study whether annotators find the same text.
- Type-only matching is uncommon because it can match entities at different locations.
- Overlap matching counts spans as matching when their character or token ranges overlap. Define the required overlap before measuring.
- Partial matching gives credit for a correct entity with a slightly different boundary, but it needs a clear scoring rule.
Keep separate scores for boundaries and labels when possible. A low exact-match score may hide a simpler problem, such as annotators finding the same entities but disagreeing about whether titles belong inside the span.
Analyzing disagreements
Create examples for each disagreement type:
- One annotator marks a span and the other marks nothing.
- Both mark the same text with different boundaries.
- Both mark the same boundaries with different labels.
- One annotator marks nested entities and the other does not.
Use these examples to revise the guidelines. After revision, run another pilot. For a final dataset, record the matching rule, offset convention, allowed nesting, annotator identities or roles, and the number of items and spans evaluated.
Exercise
Annotator A marks three spans. Annotator B marks the same two spans exactly and one different span. What are exact-match precision, recall, and F1 if A is treated as the predictions and B as the reference?
Show answer
Precision is 2/3, recall is 2/3, and F1 is 2/3, or about 0.667.