Data ScienceNLP and text analytics

Prevent duplicate text leakage across data splits

PK
Pankit Kumar
Sr. Data Scientist at Parexel (a Goldman Sachs–backed company) · 20 September 2026 · 2 min read
Technically reviewed by Ishaan Sharma
In this article (5 sections)

Copied tickets, quoted email threads, template replies and minor punctuation edits can cross text partitions. A model then appears to generalize while recognizing nearly identical content. Split by source or conversation first, then screen exact and near duplicates.

Layer three checks

The NLP lab compares four training and four test strings with an exact set, lowercase/punctuation normalization and character n-gram cosine similarity.

python
from nlp_cases import duplicate_case

result = duplicate_case()
assert result["exact_matches"] == 1
assert result["normalized_matches"] >= result["exact_matches"]
assert result["near_matches_cosine_ge_0_8"] == 2
print(result["maximum_similarities"])

Exact and normalized checks each find one match. The simple normalizer misses ORDER-123 versus order 123 because punctuation removal joins one form; normalization itself needs tests. Character similarity at 0.8 identifies two candidates, including a shortened parcel phrase.

Similarity candidates are not automatic duplicates. Review source provenance, labels and intended generalization. Two users can independently send the same short phrase, while a long quoted thread can hide a copied section beneath new text.

Choose the source group

Keep all messages from one conversation, customer case or document family in one partition. For temporal prediction, also respect time. Remove quoted reply history or hash individual segments so repeated boilerplate does not dominate similarity.

If duplicates carry conflicting labels, investigate annotation policy rather than simply deleting one. Report exact same-label, exact conflicting-label, near same-source and near unknown-source counts.

Freeze the test after audit

Run duplicate detection before training and after every data refresh. Fit similarity vectorizers on training or use content-independent hashes. Store reviewed exceptions and fail the dataset build when unexpected exact cross-split copies appear.

The Data Science course connects this audit with group-aware splitting and credible evaluation.

Exercise

Create variants using case, punctuation, quoted threads and paraphrases. Measure candidate precision at several similarity thresholds, then choose a grouping rule based on provenance rather than model score.

Continue learning

This article is part of the NLP and text analytics sequence. Use the neighbouring tasks when you need the prerequisite or the next application.

Reference: scikit-learn group-aware cross-validation.

PK
Pankit Kumar
Lead Instructor, NeuraPath Academy

Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.

This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.

Explore Data Science
Counselling is free · no obligation

Not sure which programme fits?

Tell us your background and we will map it to the right entry point — including saying so when a cheaper programme is the better fit. A counsellor replies within one working day.