Week 05 · Sequence labelling and entity-level F1
Measure entity boundaries separately from token accuracy.
← Week 4 · Subject contents · Week 6 →
Measure entity boundaries separately from token accuracy.
Work through the problem
A BIO tag marks the beginning (B), interior (I), or outside (O) of an entity. An exact entity match requires both boundaries and type to agree. If the correct entity is New York and a system extracts just New, one token can be right while the entity is wrong.
For exact span counts, precision=TP/(TP+FP), recall=TP/(TP+FN), and F1=2TP/(2TP+FP+FN). Most tokens can be outside entities, allowing a high token accuracy even when boundary errors make extraction unusable.
Run the Python lab
tokens = ["I","saw","New","York","and","Paris","today"]
gold = ["O","O","B-LOC","I-LOC","O","B-LOC","O"]
pred = ["O","O","B-LOC","O","O","B-LOC","O"]
def spans(tags):
result = set()
for i, tag in enumerate(tags):
if tag.startswith("B-"):
j = i+1
while j<len(tags) and tags[j]=="I-"+tag[2:]:
j+=1
result.add((i,j,tag[2:]))
return result
a,b = spans(gold),spans(pred)
tp,fp,fn = len(a&b),len(b-a),len(a-b)
print("Token accuracy:",round(sum(x==y for x,y in zip(gold,pred))/len(tokens),3))
print("Entity precision, recall, F1:",tp/(tp+fp),tp/(tp+fn),2*tp/(2*tp+fp+fn))
assert (tp,fp,fn)==(1,1,1)
Output from the lab
Token accuracy: 0.857
Entity precision, recall, F1: 0.5 0.5 0.5
Practice
- How do nested entities complicate BIO tagging?
Check your reasoning
- A single tag per token cannot represent overlapping spans without a richer representation.
← Week 4 · Subject contents · Week 6 →
Further reading · Jurafsky and Martin · Speech and Language Processing