v9.1.1
Menu

NER F1 Calculator

mediumPRONamed Entity RecognitionNLP 2: Applications

Compute entity-level precision, recall, and F1 score for NER evaluation.

An entity is considered correct only if both the span (start and end positions) AND the entity type match exactly.

Input:

  • predicted: list of predicted entities, each a (start, end, type) tuple
  • gold: list of gold/true entities, each a (start, end, type) tuple

Output: A string with three values separated by spaces: "precision recall f1" (each rounded to 4 decimal places, e.g. 0.6667 1.0 0.8). If precision or recall is undefined (0/0), output 0.0 for that metric and F1.

Example

Input
3
0 1 PER
4 6 LOC
8 8 ORG
2
0 1 PER
4 6 LOC
Output
0.6667 1.0 0.8
Reasoning
  • Inputs: predicted holds 3 entities — (0,1,PER)(0,1,\text{PER}), (4,6,LOC)(4,6,\text{LOC}), (8,8,ORG)(8,8,\text{ORG}) — and gold holds 2: (0,1,PER)(0,1,\text{PER}), (4,6,LOC)(4,6,\text{LOC}). Each entity is a token span (start, end, inclusive) plus a type label.
  • Matching: a prediction counts only if its start, end and type all equal some gold entity. The PER span 0–1 and the LOC span 4–6 agree exactly; the ORG at token 8 has no gold partner, so it is a false positive. Correct =2= 2.
  • Precision asks "how many predictions were right": 2/3≈0.66672/3 \approx 0.6667. Recall asks "how many gold entities were found": 2/2=1.02/2 = 1.0.
  • F1: use the exact fractions, 2⋅23⋅123+1=4/35/3=0.8\frac{2 \cdot \frac{2}{3} \cdot 1}{\frac{2}{3} + 1} = \frac{4/3}{5/3} = 0.8. Rounding only happens at the end.
  • Output: the string 0.6667 1.0 0.8, values rounded to 4 decimal places with trailing zeros dropped. Notice the extra ORG guess cost precision but not recall; a wrong type on a correct span (say (0,1,ORG)(0,1,\text{ORG}) against a gold (0,1,PER)(0,1,\text{PER})) would cost both.

Constraints

  • An entity match requires exact start, end, AND type match
  • Precision = correct / predicted_count
  • Recall = correct / gold_count
  • F1 = 2 * P * R / (P + R), or 0.0 if P + R = 0
  • Round to 4 decimal places

Learn this in

More from NLP 2: Applications

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.