Katkat

Extract W-2 PDF Data and Evaluate It Against Reference JSON

Additive AiSoftware Engineer · Technical Screen

Last updated

This problem tests data structure manipulation, fuzzy matching logic, and evaluation metrics for comparing extracted AI document data against ground-truth schemas.

Problem Statement

You are building an evaluation pipeline for an AI system that extracts tax information from W-2 PDF forms. You are given a list of extracted_records (a list of dictionaries produced by the AI) and a reference_data dictionary containing the ground-truth values.

Task

Implement a function evaluate_extraction(extracted_records, reference_data) that performs the following:

  1. Record Matching: Match the extracted W-2 records to the reference data based on a unique identifier (e.g., employer_ein). If multiple records exist for an EIN, merge them or identify the best match.
  2. Normalization: Convert currency strings (e.g., "$1,200.50") to float, and standardize keys to lowercase.
  3. Error Calculation: Calculate the precision and recall of fields found versus expected. Report fields that are missing or mismatched.

Function Signature

def evaluate_extraction(extracted_records: list[dict], reference_data: dict) -> dict:
    # Returns a report of matching accuracy

Constraints

  • extracted_records: 1 to 50 entries.
  • reference_data: Contains key-value pairs representing the ground truth.
  • Handle potential None or missing keys in the extracted records gracefully.

Example

Input:

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

Output:

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

Edge Cases

  • The extracted data has a different key name (e.g., 'tax_withheld' vs 'tax').
  • The extracted record includes fields not present in the reference schema (potential leakage).

Hints

Hint 1

Normalize all numeric inputs by stripping non-digit characters and converting them to a common float format before comparison.

Hint 2

Use a dictionary to map EINs to their corresponding records for O(1) lookup during the matching phase.

Hint 3

For the evaluation metrics, consider iterating through the union of the sets of keys present in both the reference and the extracted records.

Follow-up questions

  • How would you handle OCR errors where a character like 'O' is read as '0'?
  • If the document is multi-page, how would you structure the `extracted_records` to ensure fields are grouped correctly under the right employer?
  • How would you extend this logic to perform 'soft' matching for address fields where minor spelling variations are acceptable?