Project: Build a Semantic Duplicate Detector

MetaCyberGuru Academy

AdvancedEstimated learning effort: 105 minutesFree, no sign-up requiredPublished by Muhammad AzharCourse version: August 2026

Back to Embeddings and Neural NLP

Duplicate detection looks like similarity search, but the product decision is a calibrated pair classification problem with asymmetric costs.

Candidate generation retrieves likely neighbours efficiently. A decision rule then labels duplicate, related or different using reviewed pairs. Keeping those stages separate prevents an expensive all-pairs comparison.

Random pair splits leak near-identical documents. Group families of duplicates into one fold and preserve hard negatives that share vocabulary but describe different outcomes. Keep duplicate families and hard negatives with every threshold report so future detectors face the same cases.

Separate Candidate Retrieval from Duplicate Decisions

  • Define candidate generation, pair scoring and accept-review-reject routes as separate stages.
  • Score labelled sentence pairs and convert the scores into accept, review and reject routes.
  • Evaluate duplicate families and hard negatives without leaking near-identical pairs across splits.
  • Evaluate duplicate, related and unrelated pairs separately, then justify the review band around your decision threshold.

From Nearest Neighbours to a Reviewed Merge Route

Random pair splits leak near-identical documents. Group families of duplicates into one fold and preserve hard negatives that share vocabulary but describe different outcomes.

The labelled pairs expose false merges and missed duplicates directly. Review them before running approximate search across a large collection.

Review the Pairs the Threshold Cannot Settle

Thresholds come from the cost of missed duplicates and false merges. Offer a review band instead of pretending one cosine value is universal.

Calibrate Three Routes, Not One Magic Cutoff

A similarity score alone cannot authorise a merge. Verify family-level splits, decision costs and the manual review band.

For Project Build a Semantic Duplicate Detector, change one setting at a time while the data split, comparison baseline and metric remain fixed.

What to record before scaling Project Build a Semantic Duplicate Detector

Before a longer semantic duplicate detection NLP run, write the data source, split rule, dependency versions and acceptance criteria.

Keep a fixed Project Build a Semantic Duplicate Detector failure set and a short limitations note so later changes can be compared rather than guessed.

Build the Project Build a Semantic Duplicate Detector example

Score the supplied pairs and reproduce all three routes first. Add records only after assigning family IDs and hard negatives.

Keep the data and split fixed while you change the semantic duplicate detection NLP decision.

Install:

python -m pip install sentence-transformers scikit-learn
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
model=SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
pairs=[('Cannot reset my password','Password reset does not work'),('Refund is late','Password reset does not work'),('Invoice PDF missing','Cannot download my invoice')]
flat=[text for pair in pairs for text in pair]
E=model.encode(flat,normalize_embeddings=True)
for i,pair in enumerate(pairs):
    score=float(cosine_similarity(E[2*i:2*i+1],E[2*i+1:2*i+2])[0,0])
    route='duplicate' if score>=0.82 else 'review' if score>=0.60 else 'different'
    print(round(score,3),route,pair)

Expected Duplicate, Review and Reject Routes

Three similarity scores and routes. The thresholds are teaching placeholders and must be selected from reviewed development pairs before use.

Confirm accept, review and reject outcomes against labelled pairs, especially cases that share vocabulary but differ in meaning.

If the Project Build a Semantic Duplicate Detector result differs, print intermediate values and confirm the documented dependency versions first.

Diagnose failures in Project Build a Semantic Duplicate Detector

Inspect family grouping, candidate recall, score calibration and route thresholds before retraining the encoder.

Checks for the Project: Build a Semantic Duplicate Detector example
SymptomLikely causeUseful check
False merges share product wordsTopical similarity is mistaken for same issueAdd hard negatives and a review band
Test results look unrealistically strongDuplicate families cross train and testSplit by connected duplicate group
Every new item compares with every old itemNo candidate index existsRetrieve top neighbours first, then classify pairs

Deliver the duplicate detector

Create reviewed duplicate, related and different pairs with stable source IDs.

  1. Build leakage-safe groups
  2. Compare TF-IDF and sentence embeddings
  3. Plot precision and recall across thresholds
  4. Choose automatic and review bands from costs
  5. Store evidence and model revision

Definition of done: The tool never auto-merges uncertain pairs and reproduces threshold analysis from reviewed data.

Stretch task: Add an approximate vector index and verify recall against exact search on a sample.

Check your Project Build a Semantic Duplicate Detector reasoning

Complete the duplicate-detection questions before opening the explanations. They check candidate recall, leakage control and cost-based routing.

1. Why separate candidate generation and decision?
Check the answer

Answer: To scale retrieval while preserving a task-specific decision stage.

2. What is a hard negative?
Check the answer

Answer: A pair with substantial overlap that must still be classified as different.

3. Why group duplicate families in a split?
Check the answer

Answer: To prevent near-identical family members from leaking across evaluation.

Primary references for Project Build a Semantic Duplicate Detector

Recalibrate the detector when the encoder, candidate index or merge policy changes. Archive labelled families, hard negatives and route costs.

Save your place

Completion is stored only in this browser on this device.

Share this page

Share this page with the people who will use it next.

X Facebook LinkedIn WhatsApp Email

Discussion

No comments yet. Add the first useful question or observation.