NLP: Redact personal data

Hide emails, card numbers and phone numbers before text is shared or used for training.

What to build

A program that replaces personal data with labels and counts the replacements.

Requirements

  • Emails match [\w.+-]+@[\w-]+(?:\.[\w-]+)+ and become [EMAIL].
  • Card numbers are four groups of four digits, separated all by spaces, all by dashes, or not at all: (?<!\d)\d{4}([ -]?)\d{4}\1\d{4}\1\d{4}(?!\d). They become [CARD].
  • Phone numbers are three digits (optionally in brackets, then an optional space; or followed by an optional space or dash), then three digits, a dash and four digits: (?<!\d)(?:\(\d{3}\) ?|\d{3}[ -]?)\d{3}-\d{4}(?!\d). They become [PHONE].
  • Replace emails first, then cards, then phones, in every line. Print the redacted lines, then EMAIL a, PHONE b, CARD c with the total counts.

Input

Line 1: n. Then n lines of text.

Output

n lines, then the counts.

Constraints

n ≤ 100; each line has at most 500 characters.

Example

Input:

2
Mail ana.b@uni.edu or call (555) 123-4567.
Card 1234 5678 9012 3456 expires soon.

Output:

Mail [EMAIL] or call [PHONE].
Card [CARD] expires soon.
EMAIL 1, PHONE 1, CARD 1

Why it works

Running the most specific patterns first stops a shorter pattern from taking part of a longer match, and the lookarounds and the back-reference keep ordinary numbers such as order numbers from being hidden by mistake.

Examples

Example 1 · Input
2
Mail ana.b@uni.edu or call (555) 123-4567.
Card 1234 5678 9012 3456 expires soon.
Expected output
Mail [EMAIL] or call [PHONE].
Card [CARD] expires soon.
EMAIL 1, PHONE 1, CARD 1
Example 2 · Input
1
Order 12345 shipped to bo@x.co.uk, ref 555-1234.
Expected output
Order 12345 shipped to [EMAIL], ref 555-1234.
EMAIL 1, PHONE 0, CARD 0

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Use `re.subn`, which returns the new text and the number of replacements, and run the three patterns in the order email, card, phone.

Hint 2

`(?<!\d)` and `(?!\d)` stop a pattern from matching inside a longer number; a back-reference `\1` makes all the separators of a card number the same.

Solution

Show one correct solution (try it yourself first)
import re

EMAIL = r"[\w.+-]+@[\w-]+(?:\.[\w-]+)+"
CARD = r"(?<!\d)\d{4}([ -]?)\d{4}\1\d{4}\1\d{4}(?!\d)"
PHONE = r"(?<!\d)(?:\(\d{3}\) ?|\d{3}[ -]?)\d{3}-\d{4}(?!\d)"
totals = {"EMAIL": 0, "PHONE": 0, "CARD": 0}
for _ in range(int(input())):
    line = input()
    for label, pattern in (("EMAIL", EMAIL), ("CARD", CARD), ("PHONE", PHONE)):
        line, n = re.subn(pattern, f"[{label}]", line)
        totals[label] += n
    print(line)
print(f"EMAIL {totals['EMAIL']}, PHONE {totals['PHONE']}, CARD {totals['CARD']}")

Lesson: Regular expressions for text →