Sub topic 4
Cleaning, Normalising and Deduplicating the Raw Questions
Assume you understand basic AI search evaluation, can read simple Python, and know that personal data must be handled carefully. This chapter focuses on turning messy questions from support tickets, search logs, sales notes, communities, and reviews into a reliable raw prompt set.
Raw questions are rarely ready to use. Customers may ask the same thing with different spelling, spacing, punctuation, or contact details. A single person may also submit the same question several times. Cleaning makes these records easier to compare without changing what the customer meant.
Scrub personal data first
Remove personal data before storing, sharing, or analysing the questions. Personal data includes email addresses, phone numbers, order IDs, names linked to account details, and free-text descriptions that identify a person.
Use a replacement token such as [EMAIL] instead of deleting the whole sentence. The token preserves the question's structure:
Can I change [email protected] on my account?Can I change [EMAIL] on my account?
Both questions express the same intent. The second is safer to retain.
Automated patterns catch obvious emails and phone-like numbers, but they will miss some sensitive values. Review samples manually, especially when data comes from support tickets or private communities.
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)
questions = [
"Can I change [email protected] on my account?",
"Please call +1 (555) 010-2020 about my subscription.",
]
for question in questions:
print(scrub(question))Normalise for comparison
Normalisation creates a comparison form while preserving the original wording separately. A comparison form commonly:
- Converts text to lowercase.
- Replaces runs of whitespace with one space.
- Removes leading and trailing whitespace.
- Applies consistent Unicode handling when multiple writing systems or punctuation forms are present.
- Optionally standardises known spelling variants.
Do not use the normalised form as the final prompt. Lowercasing and punctuation removal can make a question harder for a reviewer to interpret. Store both values:
| Field | Example |
|---|---|
| original | Can I move my subscription to a different email address? |
| comparison | can i move my subscription to a different email address? |
| source | support_tickets |
Keep transformations conservative. Changing “cancel” to “pause,” or “refund” to “credit,” could change the intent. Add a transformation only when you have verified that it does not alter meaning in your domain.
Deduplicate in stages
Start with exact deduplication after scrubbing and normalisation. Keep the first original wording, or keep the version from the source you trust most. Record how many raw rows collapsed into each retained question.
import re
from collections import Counter
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 comparison_form(text):
return re.sub(r"\s+", " ", scrub(text)).strip().casefold()
def exact_dedupe(questions):
retained = {}
counts = Counter()
for question in questions:
key = comparison_form(question)
counts[key] += 1
retained.setdefault(key, scrub(question))
return [
{"text": text, "count": counts[key]}
for key, text in retained.items()
]
questions = [
"Can I move my subscription to a different email address?",
" Can I move my subscription to a different email address? ",
"Can I move my subscription to [EMAIL]?",
]
for row in exact_dedupe(questions):
print(row)Exact deduplication will not catch paraphrases such as “How do I update the email on my subscription?” Those cases need a second review stage. Use semantic similarity, clustering, or human review to suggest possible matches. Do not merge automatically when the questions differ in product, audience, eligibility, time period, or requested outcome.
Preserve evidence and audit decisions
A clean prompt set should remain traceable to its source. Useful fields include:
- A stable question ID.
- Original text and cleaned text.
- Source system and collection date.
- Exact duplicate count.
- Reviewer or rule that merged records.
- Intent and topic labels, added later.
This record lets you inspect whether deduplication overrepresents one channel. Support tickets may contain repeated follow-ups, while search logs may contain many one-time questions. Counts describe collection behaviour as well as customer demand.
Exercise
Two questions differ only in capitalisation and extra spaces. Should they usually become two prompts?
Show answer
No. Their comparison forms are equivalent, so they should usually become one retained prompt with a duplicate count.