KatInterview

Scoring Parsed Payroll Form Output Against Ground Truth

Arrays & HashingModel answer
MediumModel answerArrays & Hashingdata-normalizationhash-mapsevaluation-metrics

Last updated

Write a scorer that compares fields extracted from tax forms to a ground-truth mapping, normalizing keys and currency, then reporting match rate, missing and mismatched fields, and precision/recall.

Scoring a Tax-Form Extractor

A machine-learning service reads scanned wage-and-tax statement PDFs and emits one Python dictionary per form it recognized. You are asked to build the offline scorer that checks those outputs against a human-verified answer key.

Inputs

  • extracted_records: a list of 1 to 50 dictionaries produced by the service. Keys may be capitalized or spelled differently from the answer key, and values may be strings such as "$1,200.50", numbers, or None. Entries may lack some fields entirely.
  • reference_data: a dictionary that maps each employer identifier (an EIN such as "12-3456789") to a dictionary of the true field values, e.g. {'wages': 50000.00, 'tax': 5000.00}.

Task

Implement evaluate_extraction(extracted_records, reference_data) -> dict so that it does the following.

  1. Link records to the key. Pair each extracted record with a reference entry using the employer identifier. When several extracted records share one identifier, combine them into a single view of that employer (or choose the best candidate) and say how you decided.
  2. Normalize before comparing.
    • Lowercase all keys, and map known alternative names to the canonical name (for example tax_withheld means tax).
    • Turn currency strings like "$1,200.50" into floats.
    • Treat None and blank values as "not extracted."
  3. Measure quality. For each reference employer, list the expected fields that are missing and the ones whose values differ. Also compute the overall match rate (the fraction of reference employers that received at least one extracted record), plus precision and recall at the field level.

Signature

def evaluate_extraction(extracted_records: list[dict], reference_data: dict) -> dict:
    ...

Example

extracted = [{'ein': '12-3456789', 'wages': '$50,000.00'}]
reference = {'12-3456789': {'wages': 50000.00, 'tax': 5000.00}}

Expected result:

{
    'match_rate': 1.0,
    'errors': {'12-3456789': {'missing_fields': ['tax'], 'mismatched_values': {}}}
}

Rules and edge cases

  • Handle None values and absent keys without raising.
  • If the extractor used a different name for a field (e.g. tax_withheld for tax), it should still be credited once the name is normalized.
  • If an extracted record contains fields that the reference schema does not define (possible leakage of unrelated data), they must not count as correct; decide how to report them and how they affect precision.
  • Be explicit about what happens with records whose identifier is absent or unknown, and about division by zero in the metrics.

You may add extra keys to the report (such as precision and recall) as long as the structure in the example is preserved.

Hints

Hint 1

Split the work into three independent stages: normalize keys and values, match and merge records by identifier, then score field by field.

Hint 2

Index the reference by a digit-only version of the EIN so that '12-3456789' and '123456789' land on the same entry, but report errors under the original key.

Hint 3

Every expected field should fall in exactly one bucket: correct, missing, or mismatched. Extra fields only affect precision, and empty denominators should return 1.0 instead of dividing by zero.