A PII redactor built by hand, with its false positives and misses
All numbers and addresses are made up. The card number is a test number that passes the Luhn check. Read the two false hits and the miss.
Personal data (PII) is anything that can identify a person. Detect and redact it before it reaches a model or a log, know that simple detectors both miss things and flag innocent text, and keep only what you need for only as long as you need it.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write mask_emails(text) that replaces every email address with [EMAIL_n] numbered from 1 in order of first appearance (the same address gets the same number), and returns (masked_text, mapping) where the mapping is a dict from label to address.
Write luhn_ok(number_string) and test it with "4539148803436467" and "1234567890123456".
Explain it without notes
Why should personal data be redacted before it reaches a model or a log?
What are false positives and false negatives in a PII detector, and which matters more?
Why add a Luhn check to a card-number pattern?
How can a model draft a reply to a customer without seeing the customer's email address?
Expected output
email:
in: Write to ana@example.com about the invoice.
out: Write to [EMAIL] about the invoice.
phone:
in: Call me on +91 98765 43210 tomorrow.
out: Call me on [PHONE] tomorrow.
card:
in: The card 4539 1488 0343 6467 was charged.
out: The card [CARD] was charged.
false hit: order id:
in: Order 1234567890 has shipped.
out: Order [PHONE] has shipped.
false hit: date:
in: The meeting is on 2024-01-15.
out: The meeting is on [PHONE].
miss: spelled out:
in: Ana Rao lives in Pune, write ana at example dot com.
out: Ana Rao lives in Pune, write ana at example dot com.