PythiloLesson 9 of 12 · progress kept in this browser

Privacy and redaction

An on-device model never sends text anywhere, which is its main advantage. When a request must go to a cloud model, send as little personal data as possible: replace each email address, phone number and card number with a placeholder, keep the mapping on the device, and put the real values back into the reply.

The same value should get the same placeholder every time, so the model can still tell that two mentions are one person.

import re

EMAIL = re.compile(
    r"[\w.+-]+@[\w-]+(\.[\w-]+)+")
text = "Mail ana@example.com today"
print(EMAIL.sub("[EMAIL_1]", text))

Regular expressions catch the formats you planned for, not names or addresses written in a sentence. Redaction lowers the risk; it does not make sending private text safe.

Your task

Write redact(text) returning (redacted, mapping): replace emails, then 16-digit card numbers (four groups of four, optionally separated by one space or hyphen), then phone numbers (+ and 8 to 15 digits, single spaces or hyphens allowed between digits) with [EMAIL_1], [CARD_1], [PHONE_1], numbered per kind in order of appearance, the same value getting the same placeholder. mapping maps placeholder to original. Write restore(text, mapping).