Sub topic 7
Finding near duplicates with shingles and Jaccard similarity
Exact hashing misses pages that differ by a few words, such as a product page where only the color changes. This chapter shows how to measure that kind of similarity. It assumes you can read Python, have a crawl export with extracted body text, and know the difference between exact duplicates, near duplicates and thin pages from earlier chapters.
Why hashes are not enough
A content hash changes completely when one character changes. Two pages that share 95 percent of their text get different hashes, so the exact-duplicate step reports them as unrelated. Near-duplicate detection measures overlap instead of equality.
Run it on main content only. If navigation, footers and cookie notices are still in the text, every page on the site looks similar to every other page.
Shingles: turning text into overlapping phrases
A shingle is a run of k consecutive words. A page becomes a set of its shingles. With k = 5, the sentence "our red running shoes are lightweight" gives "our red running shoes are" and "red running shoes are lightweight".
Overlapping phrases keep word order. A set of single words would call two pages similar just because they use the same vocabulary. A change of one word only affects the k shingles that contain it, so most of the page still matches.
Jaccard similarity
Jaccard similarity is the number of shingles two pages share divided by the number of distinct shingles across both pages. It runs from 0 (nothing shared) to 1 (identical sets).
def shingles(text, k=5):
words = text.lower().split()
if len(words) < k:
return {" ".join(words)} if words else set()
return {" ".join(words[i:i + k]) for i in range(len(words) - k + 1)}
def jaccard(a, b):
if not a and not b:
return 0.0
return len(a & b) / len(a | b)
page_a = "Our red running shoes are lightweight and built for long road runs in all weather"
page_b = "Our blue running shoes are lightweight and built for long road runs in all weather"
print(round(jaccard(shingles(page_a), shingles(page_b)), 2))Each page has 12 shingles. The color word sits in the first shingle only (the first five-word window), so one shingle differs on each side. They share 11 and the union is 13, so the score is 0.85. A single word change in a short page already costs 15 points. On a 600-word page the same change would score about 0.99.
Comparing many pages
Comparing every pair grows with the square of the page count. For a few thousand pages this is fine. Reduce the work first: only compare pages within the same template, and skip pages already grouped as exact duplicates.
from itertools import combinations
import pandas as pd
df = pd.read_csv("crawl.csv") # columns: url, template, body_text
df = df[df["body_text"].notna()]
pairs = []
for template, group in df.groupby("template"):
sets = {row.url: shingles(row.body_text) for row in group.itertuples()}
for (u1, s1), (u2, s2) in combinations(sets.items(), 2):
score = jaccard(s1, s2)
if score >= 0.8:
pairs.append((u1, u2, round(score, 2)))
print(pd.DataFrame(pairs, columns=["url_a", "url_b", "jaccard"]))This reuses the shingles and jaccard functions above. For tens of thousands of pages, look at MinHash with locality-sensitive hashing, which approximates Jaccard without comparing every pair.
Choosing a threshold and avoiding common errors
There is no universal cutoff. Start around 0.8 and read a sample of pairs just above and below it.
| Mistake | Effect | Fix |
|---|---|---|
| Boilerplate left in the text | Scores inflate across the whole site | Extract main content first |
| k too small (1 or 2) | Unrelated pages look similar | Use 4 to 6 words |
| Very short pages | One changed word swings the score | Review them by hand or treat them as thin pages |
| Comparing across templates | Wasted work and misleading pairs | Compare within a template |
Exercise
Two product pages each contain 100 distinct shingles. They share 60 of them. What is the Jaccard similarity, and would a threshold of 0.8 flag the pair?
Show answer
The union is 100 + 100 - 60 = 140 shingles. The score is 60 / 140, about 0.43. A threshold of 0.8 would not flag the pair.
Exercise
Why does the sample code compare pages only within the same template?
Show answer
Pages from different templates rarely duplicate each other, and comparing every pair grows with the square of the page count. Grouping by template cuts the work and keeps the results relevant.