Sub topic 14
Agreement for Spans Such as Named Entities
This chapter assumes you know Cohen's kappa from earlier in the tutorial (observed agreement, chance agreement, and how they combine). It also assumes you know what a named entity span is and can read basic Python.
Why kappa does not fit spans directly
Cohen's kappa needs a fixed set of items, and every annotator must assign each item one label. Classifying 500 reviews as positive, neutral or negative fits this. Each review is an item.
Span annotation does not fit. In named entity recognition (NER), annotators decide where entities are and what type each has. Nobody hands them a list of candidate items. One annotator may mark three entities in a sentence and another may mark two. A span such as "New York City" can be marked in full, as "New York", or not at all. There is no obvious table of items by labels to feed into the kappa formula.
Annotators can disagree on three separate things:
- Existence: whether a span is an entity at all.
- Boundaries: where the span starts and ends.
- Type: which label it gets (PER, ORG, LOC and so on).
A good agreement measure for spans should tell you which of these causes the disagreement.
Span-level F1 with exact matching
The standard approach treats one annotator as the reference and the other as the prediction. You then compute precision, recall and F1, the same measures used to evaluate a NER model. A span counts as a match only if its start, end and label are all identical.
def span_f1(spans_a, spans_b):
a, b = set(spans_a), set(spans_b)
if not a and not b:
return 1.0
matched = len(a & b)
precision = matched / len(a) if a else 0.0
recall = matched / len(b) if b else 0.0
if precision + recall == 0:
return 0.0
return 2 * precision * recall / (precision + recall)
# Each span is (start_char, end_char, label)
ann1 = [(0, 6, "PER"), (20, 26, "ORG"), (40, 46, "LOC")]
ann2 = [(0, 6, "PER"), (20, 26, "LOC"), (40, 46, "LOC")]
print(span_f1(ann1, ann2))The output is about 0.667. Two of the three spans match exactly. The span at characters 20 to 26 has the same boundaries but a different type, so it counts as a miss.
Three details matter here:
- F1 is symmetric for exact matching. Swapping which annotator is the reference swaps precision and recall, but F1 stays the same. You do not need to decide who is "right".
- Both empty is perfect agreement. The function returns 1.0 when neither annotator marked anything. Without this rule you would divide by zero on sentences with no entities.
- Aggregate before you average. Over a whole corpus, count matches, reference spans and other-annotator spans across all documents (micro averaging). Averaging per-document F1 lets short documents with one or two spans dominate.
Strict, relaxed and partial matching
Exact matching is harsh. If one annotator includes "the" in "the United Nations" and the other does not, the whole span counts as a disagreement even though both found the same entity. You can compute several versions of the score and compare them.
| Matching rule | A span matches when | What it reveals |
|---|---|---|
| Strict | Start, end and label are all equal | The overall quality of the data as it will be used for training |
| Boundary only | Start and end are equal, label ignored | How much of the disagreement is about the type |
| Type only on overlap | Spans overlap and labels are equal | How much of the disagreement is about boundaries |
| Overlap | Spans overlap, label ignored | Whether annotators found the same entities at all |
The gaps between these scores tell you where to look. If the strict score is 0.62 and the overlap score is 0.91, the annotators mostly find the same entities and disagree about edges. The fix is a boundary rule in the guidelines, such as "exclude leading articles and titles". If boundary-only is high but strict is low, the label definitions are the problem, for example the ORG versus LOC confusion in the code above.
When you use overlap matching, enforce one-to-one pairing. Otherwise one long span from annotator A can "match" three short spans from annotator B and inflate the score.
Token-level kappa and why it flatters you
An alternative is to convert spans into one label per token and compute kappa over tokens. Each token gets a type label or "O" for outside any entity. Now there is a fixed set of items, so kappa applies.
from sklearn.metrics import cohen_kappa_score
def to_tokens(spans, n_tokens):
labels = ["O"] * n_tokens
for start, end, label in spans: # token indices, end is exclusive
for i in range(start, end):
labels[i] = label
return labels
ann1 = [(0, 2, "PER"), (10, 12, "ORG"), (20, 22, "LOC")]
ann2 = [(0, 2, "PER"), (10, 12, "LOC"), (20, 22, "LOC")]
t1 = to_tokens(ann1, 30)
t2 = to_tokens(ann2, 30)
print(round(cohen_kappa_score(t1, t2), 3))This prints 0.808. The span-level score for the same entities is 0.667. The difference comes from the "O" label, which covers 24 of the 30 tokens. Both annotators agree on every one of those tokens, so observed agreement is 28 of 30 (0.933) even though one of three entities is mislabeled. This is the prevalence problem from earlier in the tutorial in a new form. Real documents are mostly non-entity text, so token-level kappa rewards annotators for agreeing on the obvious majority.
Token-level kappa has two more problems. A long entity contributes many tokens, so it counts for more than a short one. A single boundary slip also gets counted as one token error instead of one entity error. The measure is not useless, but it answers a different question than span F1.
Other options
- Krippendorff's alpha with a span distance. Alpha can handle missing data and custom distance functions. You can define a distance that gives partial credit to overlapping spans. This is more flexible but harder to implement and explain.
- Candidate-set kappa. If your pipeline proposes candidate spans (for example all noun phrases), each candidate becomes an item with a type or "not an entity" label, and standard kappa works. The result depends on how the candidates were generated.
- More than two annotators. Compute pairwise F1 for every pair and report the mean, or compare each annotator against the adjudicated gold standard. Fleiss' kappa is not appropriate for raw spans for the same reason Cohen's kappa is not.
Common mistakes
- Comparing character offsets when one tool uses inclusive ends and another uses exclusive ends. Normalize offsets first, or every span will look like a boundary error.
- Letting whitespace or punctuation differences change boundaries. Decide whether trailing punctuation is part of the span, and put that rule in the guidelines.
- Counting nested or overlapping entities twice. If your schema allows nesting, the set-based function above still works, but token-level conversion does not, because a token can have two labels.
- Reporting one number. Give strict and relaxed scores, and per-type scores, since rare types such as MISC often agree far less than PER.
Exercise
Two annotators label the same document. Annotator A marks (0, 8, "ORG"), (15, 22, "PER"), (30, 36, "LOC"). Annotator B marks (0, 8, "ORG"), (15, 20, "PER"), (30, 36, "LOC"), (50, 55, "PER"). Using the span_f1 function, what is the strict F1, and what does the boundary-only (start and end both equal, label ignored) score tell you that the strict score does not?
Show answer
A has 3 spans and B has 4. Only (0, 8, "ORG") and (30, 36, "LOC") match exactly, so matched = 2. Precision = 2/3 and recall = 2/4 = 0.5, relative to A and B. F1 = 2 × (2/3) × 0.5 / (2/3 + 0.5) = (2/3) / (7/6) ≈ 0.571. The PER span at 15 to 22 versus 15 to 20 is a boundary disagreement, and the span (50, 55, "PER") exists only in B. Boundary-only F1 is the same, 0.571, because the labels agree wherever the boundaries agree. This shows the loss comes entirely from boundaries and from a missing span, not from type confusion, so the guidelines should address where spans end and what counts as an entity.