Named entity evaluation
Quick answer
Named entity recognition (NER) identifies spans such as people, organizations or locations and assigns entity types. Exact-span evaluation requires both boundaries and type to match. Examples are static Python snippets tested locally on CPU; scikit-learn or PyTorch is required for selected examples. ByteMind execution is unverified.
Exact entity counts
truth={("d1",0,2,"ORG"),("d1",3,4,"LOC")}pred={("d1",0,1,"ORG"),("d1",3,4,"LOC")}tp=len(truth&pred);fp=len(pred-truth);fn=len(truth-pred)print(tp,fp,fn,2*tp/(2*tp+fp+fn))Expected output (locally tested): 1 1 1 0.5. The partial span is one FP and leaves one FN.
How it works
BIO tagging uses B-TYPE for a beginning, I-TYPE for continuation and O outside. For token-level spans use half-open intervals [start,end), so [1,3) includes positions 1 and 2. Decide how to handle malformed I tags (reject or repair with a documented convention); do not silently change the evaluation. Exact entity matching uses a set of (document_id,start,end,type) records, not just token labels. True positives are set intersection; FP and FN are unmatched predicted and true entities. Micro precision=TP/(TP+FP), recall=TP/(TP+FN), F1=2TP/(2TP+FP+FN), with an explicit zero-denominator policy. A boundary error is both an FP and FN. Token accuracy can be inflated by O labels. Character offsets must correspond to the same original/normalized text and tokenization; repeated entity strings in different positions are distinct. For actual systems also evaluate by entity type, language, and source group. Synthetic spans here use token indices; no external NER model is trained.
Token accuracy counterexample
true=["B-ORG","I-ORG"]+["O"]*18pred=["O"]*20print(sum(a==b for a,b in zip(true,pred))/len(true),0.0)Expected output (locally tested): 0.9 0.0. Predicting no entities gets high token accuracy but zero entity recall.
Common mistakes
Do not count partially overlapping spans as exact matches, ignore document IDs, or count O tokens as entities. Avoid reporting a perfect empty-set F1 without a stated policy.
Practice and next steps
Compare one exact and one boundary-mismatched prediction, then compute entity-level F1. Identify how normalization could shift offsets.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Named entity evaluation — knowledge check
Apply named entity evaluation to small examples and debugging scenarios.
What defines an exact NER true positive?
How is a wrong end boundary counted?
What does I-ORG usually mean?
Why is token accuracy insufficient?
What should offsets refer to?