Sub topic 3
Removing Personal Data Before Anything Else
This chapter covers the privacy step in the pipeline. It sits after you have collected raw questions and before you clean, group or label them. It assumes you can read short Python scripts and know what counts as personal data, such as names, contact details and account identifiers. It also assumes you have a raw export of customer questions from one or more sources.
Why scrubbing comes first
Real customer questions are the raw material of your prompt set. They are also messy in ways that matter for privacy. Customers paste order numbers into support chats. They sign emails with their full name. They mention a phone number so an agent can call back. None of this belongs in a prompt set.
Scrub before anything else for three reasons:
- Copies multiply. Every later step (deduplication, clustering, labelling, sampling) writes a new copy of the text. If personal data is still present, each copy is a new place it can leak from.
- Prompts get shared. A prompt set is sent to AI assistants and AI search features. It is also shown to reviewers, stored in version control and sometimes published in reports. Anything in the text travels with it.
- Later tools may be external. If you use a hosted model to help with clustering or labelling, raw text would leave your environment. Scrubbed text lowers that risk.
Scrubbing early also helps deduplication. Two questions that differ only by an email address become identical once the address is replaced by a placeholder, so they can be merged.
What counts as personal data in a question
Personal data is any information that can identify a person, alone or when combined with other information. In customer questions it shows up in several forms.
| Category | Examples | Easy to detect with a pattern? |
|---|---|---|
| Contact details | Email address, phone number, postal address | Mostly yes for email and phone, harder for addresses |
| Names | "Hi, I'm Maria Lopez" | No, needs a name list or an NLP model |
| Account identifiers | Order number, customer ID, licence key | Yes, if you know your own formats |
| Financial data | Card number, IBAN | Yes, with checksum validation |
| Network identifiers | IP address, device ID | Yes |
| Free-text details | "I live next to the old mill in Ashford" | No, needs human review |
The first column of "easy" cases is where simple pattern matching pays off. The last rows are where it fails, and you need a second layer of defence.
Replace, do not delete
The scrubbing step should replace each detected item with a typed placeholder such as [EMAIL] or [PHONE]. Deleting the text instead leaves broken sentences. Placeholders keep the structure of the question intact.
Compare these two versions of the same input:
- Original: "Please change my login email from [email protected] to [email protected]"
- Deleted: "Please change my login email from to"
- Placeholder: "Please change my login email from [EMAIL] to [EMAIL]"
The placeholder version still tells you the intent (an account change) and still reads like a real question. It also stays useful for the intent and cluster steps later in the pipeline.
Use one placeholder per category, not per person. If you used [EMAIL_1] and [EMAIL_2], you would preserve a link between values, which is more information than you need.
A first scrubbing function
The following script uses regular expressions (patterns that match text shapes) to catch the obvious cases. It also adds the cleaning and deduplication step that comes next in the pipeline, so you can see how the two fit together.
import re
EMAIL = re.compile(r"[\w.+-]+@[\w-]+\.[\w.-]+")
PHONE = re.compile(r"\+?\d[\d\s().-]{7,}\d")
def scrub(text):
text = EMAIL.sub("[EMAIL]", text)
return PHONE.sub("[PHONE]", text)
def normalise(text):
return re.sub(r"\s+", " ", text).strip().lower()
def clean_and_dedupe(questions):
seen = {}
for q in questions:
cleaned = scrub(q)
seen.setdefault(normalise(cleaned), cleaned)
return list(seen.values())
if __name__ == "__main__":
raw = [
"Can I move my subscription to [email protected]?",
"can I move my subscription to [email protected]?",
"Call me on +44 20 7946 0958 about my refund",
]
for line in clean_and_dedupe(raw):
print(line)Running it prints two lines. The first two questions collapse into one because, after scrubbing and normalising, they are identical. The function keeps the first original wording as the surviving version.
Note the order inside clean_and_dedupe. The text is scrubbed first, then normalised to build the comparison key. If you normalised first, a lowercase email could slip past a pattern that expected something else, and you could also compare values that still contain personal data.
Why the order of patterns matters
Patterns can overlap. A long card number looks like a phone number to the PHONE pattern above, because both are runs of digits with optional separators. If you apply the phone pattern first, a card number is labelled [PHONE]. The text is still safe, but the label is wrong, and wrong labels distort any counts you report.
Apply the most specific patterns first and the most general last. A sensible order is:
- Email addresses.
- Card numbers, confirmed with a checksum (see below).
- Account and order identifiers in your own formats.
- IP addresses.
- Phone numbers, the most permissive pattern.
Specific patterns remove their matches before the broad pattern sees them.
Validating card numbers
A pattern for 13 to 19 digits will also match many harmless numbers, such as tracking codes. The Luhn algorithm, a checksum used by payment cards, removes most of those false matches.
import re
CARD = re.compile(r"\b(?:\d[ -]?){13,19}\b")
def luhn_ok(digits):
total = 0
for i, ch in enumerate(reversed(digits)):
n = int(ch)
if i % 2 == 1:
n *= 2
if n > 9:
n -= 9
total += n
return total % 10 == 0
def scrub_cards(text):
def repl(match):
digits = re.sub(r"\D", "", match.group())
return "[CARD]" if luhn_ok(digits) else match.group()
return CARD.sub(repl, text)
print(scrub_cards("My card 4111 1111 1111 1111 was charged twice"))
print(scrub_cards("Parcel 1234 5678 9012 3456 has not arrived"))The first line prints with [CARD] in place of the number. The second keeps the parcel number, because it fails the checksum. That is the trade-off you are choosing: fewer false alarms, at the cost of missing a mistyped card number.
Your own identifier formats
The most valuable patterns are often specific to your business. If order numbers look like ORD-2024-001234 or customer IDs are eight digits starting with C, write patterns for them. Generic tools cannot know these formats.
import re
ORDER = re.compile(r"\bORD-\d{4}-\d{6}\b")
CUSTOMER = re.compile(r"\bC\d{8}\b")
def scrub_internal(text):
text = ORDER.sub("[ORDER_ID]", text)
return CUSTOMER.sub("[CUSTOMER_ID]", text)
print(scrub_internal("Where is ORD-2024-001234 for account C12345678?"))Ask the team that owns each source system for the real formats and for a few anonymised examples. Test your patterns against those examples before running them on the full export.
Names and free text
Regular expressions cannot reliably find a person's name. "Hi, this is Will" and "I will call you" both contain the word. Three approaches work, with different costs:
- Named-entity recognition (NER). A natural-language library tags spans of text as people, places and organisations. It catches many names but also produces false positives and misses unusual names.
- Source fields. Many systems store the customer's name in a separate field. Remove or ignore that field entirely, and never concatenate it into the question text.
- Signature and greeting stripping. Cut common patterns such as "Regards," followed by the rest of the message, and opening lines like "Hi team,". Questions rarely need them.
For a prompt set, a pragmatic design is to avoid the problem: keep only the question sentence, not the whole message. A support email of ten lines usually contains one or two real questions. Extracting just those removes most signatures and sign-offs.
Add a review layer
Because automated scrubbing has gaps, add a measured check after it.
- Search for leftovers. Run broad detectors over the scrubbed output. Look for the
@symbol, long digit runs, capitalised word pairs, and words such as "street", "road" and "avenue". - Sample and read. Pull a random sample of scrubbed questions, such as 200, and have a person read them. Count how many still hold personal data.
- Decide with a threshold. If even one sample line leaks a card number or full address, fix the rule and run again. For names, agree an acceptable rate with your privacy team.
- Drop what you cannot fix. If a question is mostly personal detail, discard it. A lost question costs little. A leaked record costs a great deal.
Keep a small log of counts per placeholder (for example, 412 emails, 96 phone numbers). A sudden change between runs often means a source format changed or a pattern broke.
Where scrubbing fits in the pipeline
Only the scrubbed branch should be visible to the people and tools that do the remaining work. The raw copy, if you must keep one, stays in restricted storage with a retention limit. Often the best choice is to delete it once the scrubbed set is approved.
Common mistakes
- Scrubbing after clustering or labelling. Earlier steps have already copied the raw text. Always run scrubbing first.
- Keeping a lookup table of replacements. A table that maps
[EMAIL]back to the original address undoes the work. Do not store it. - Leaving metadata behind. Ticket IDs, agent names, timestamps and user IDs in neighbouring columns can identify a person even if the question text is clean. Drop columns you do not need.
- Trusting one pass. Run the review layer every time the source or the patterns change.
- Over-scrubbing. A pattern that removes every number deletes useful content such as "plan with 5 seats" or "version 3". Keep patterns specific, and check that scrubbed questions still read naturally.
Summary of the step
Scrubbing turns raw customer text into text you can safely copy, share and process. Replace detected items with typed placeholders, apply specific patterns before general ones, validate cards with a checksum, add patterns for your own identifiers, and always follow automation with a human check. The output feeds directly into the next chapter, where you normalise and deduplicate the cleaned questions.
Exercise
A scrubbed question reads "Please refund order [PHONE]". The original was "Please refund order 4111 1111 1111 1111". Name the cause and give two changes that would fix it.
Show answer
The broad phone pattern ran before any card or order-number pattern, so it matched the card digits. Fix it by applying more specific patterns first (card with Luhn validation, then internal ID formats) and the phone pattern last. Also check whether the number was really a card or an order ID, and add a pattern for your own order format so it gets the right placeholder.
Exercise
Your team wants to keep a table that maps each placeholder back to the original value "in case we need it". Why is this a bad idea for a prompt set, and what should you do instead if you need to trace a prompt to its source?
Show answer
The table preserves exactly the personal data you removed, so anyone with access to it can reverse the scrubbing, and it must be protected like the raw data. Instead, keep a non-identifying record ID for each question (such as "q-0042") that points to a row in the restricted source system, where access is controlled and retention is limited.