File size: 12,348 Bytes
803b5e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"""

Deduplication pipeline for the code corpus.



Three stages of deduplication, applied in order:



  1. EXACT DEDUP (document-level)

     - SHA-256 hash of normalized content (whitespace-collapsed, lowercased)

     - Drops byte-identical and whitespace-only-differing documents.



  2. NEAR-DEDUP (document-level, MinHash + LSH)

     - Shingles each document into character n-grams.

     - Computes MinHash signatures.

     - Uses LSH banding to cluster near-duplicates (Jaccard >= threshold).

     - Keeps one representative per cluster (the longest, highest-quality).



  3. LINE-LEVEL DEDUP (cross-document)

     - Builds a global set of exact line hashes.

     - Removes lines that appear in >threshold documents (boilerplate:

       license headers, auto-generated stubs, repeated imports, etc.).

     - Drops documents that become too short / too sparse after line removal.



Input:  data/raw_large/documents.jsonl

Output: data/dedup/documents_dedup.jsonl



Reports statistics at each stage so we can verify the pipeline is working.

"""

import hashlib
import json
import os
import re
import sys
from collections import Counter, defaultdict

from datasketch import MinHash, MinHashLSH

PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_DIR = os.path.join(PROJECT_DIR, "data")
RAW_DOCS_PATH = os.path.join(DATA_DIR, "raw_large", "documents.jsonl")
DEDUP_DIR = os.path.join(DATA_DIR, "dedup")
DEDUP_DOCS_PATH = os.path.join(DEDUP_DIR, "documents_dedup.jsonl")
STATS_PATH = os.path.join(DEDUP_DIR, "dedup_stats.json")

# ─── MinHash / LSH parameters ────────────────────────────────────────────────
SHINGLE_SIZE = 5          # character 5-grams
NUM_PERM = 128            # MinHash permutations (higher = more accurate)
LSH_THRESHOLD = 0.5       # Jaccard similarity threshold for near-dup
LSH_BANDS = 32            # LSH bands (NUM_PERM / LSH_ROWS should be integer)
LSH_ROWS = NUM_PERM // LSH_BANDS  # 128 / 32 = 4

# ─── Line-level dedup parameters ────────────────────────────────────────────
LINE_BOILERPLATE_THRESHOLD = 50  # lines appearing in >= this many docs are boilerplate
MIN_LINES_AFTER_DEDUP = 5        # drop docs with fewer lines after line dedup
MIN_CHARS_AFTER_DEDUP = 200      # drop docs shorter than this after line dedup


def normalize_content(text: str) -> str:
    """Normalize content for exact-hash comparison: collapse whitespace, lowercase."""
    # Collapse all whitespace sequences to single space
    text = re.sub(r"\s+", " ", text)
    return text.strip().lower()


def exact_hash(text: str) -> str:
    """SHA-256 of normalized content."""
    return hashlib.sha256(normalize_content(text).encode("utf-8")).hexdigest()


def shingles(text: str, k: int = SHINGLE_SIZE) -> set[str]:
    """Character k-gram shingles of normalized text."""
    text = re.sub(r"\s+", " ", text).strip().lower()
    if len(text) < k:
        return {text}
    return {text[i : i + k] for i in range(len(text) - k + 1)}


def minhash_signature(text: str, num_perm: int = NUM_PERM) -> MinHash:
    """Compute MinHash signature for a document."""
    m = MinHash(num_perm=num_perm)
    for s in shingles(text):
        m.update(s.encode("utf-8"))
    return m


# ─── Stage 1: Exact dedup ────────────────────────────────────────────────────

def stage1_exact_dedup(docs: list[dict]) -> tuple[list[dict], dict]:
    """Remove exact (and whitespace-only) duplicates. Keep first occurrence."""
    print("\n" + "=" * 60)
    print("STAGE 1: Exact document-level dedup (SHA-256 of normalized content)")
    print("=" * 60)

    seen_hashes = {}
    kept = []
    dropped = 0

    for i, doc in enumerate(docs):
        h = exact_hash(doc["content"])
        if h in seen_hashes:
            dropped += 1
            continue
        seen_hashes[h] = i
        kept.append(doc)

        if (i + 1) % 5000 == 0:
            print(f"  Processed {i+1}/{len(docs)} | kept {len(kept)} | dropped {dropped}")

    stats = {
        "input": len(docs),
        "kept": len(kept),
        "dropped_exact": dropped,
        "drop_rate": dropped / max(len(docs), 1),
    }
    print(f"  Input: {len(docs)} | Kept: {len(kept)} | Dropped: {dropped} "
          f"({stats['drop_rate']:.1%})")
    return kept, stats


# ─── Stage 2: Near-dedup (MinHash LSH) ───────────────────────────────────────

def stage2_near_dedup(docs: list[dict]) -> tuple[list[dict], dict]:
    """Cluster near-duplicates via MinHash LSH, keep best representative per cluster."""
    print("\n" + "=" * 60)
    print(f"STAGE 2: Near-dedup (MinHash LSH, threshold={LSH_THRESHOLD}, "
          f"perm={NUM_PERM}, bands={LSH_BANDS})")
    print("=" * 60)

    # Build LSH index
    lsh = MinHashLSH(
        threshold=LSH_THRESHOLD,
        num_perm=NUM_PERM,
        params=(LSH_BANDS, LSH_ROWS),
    )

    signatures = {}
    kept_indices = set()

    for i, doc in enumerate(docs):
        content = doc["content"]
        if len(content) < 50:
            # Too short for meaningful MinHash β€” keep as-is
            kept_indices.add(i)
            continue

        mh = minhash_signature(content)
        signatures[i] = mh

        # Query for existing near-duplicates
        candidates = lsh.query(mh)

        if candidates:
            # This doc is a near-duplicate of an existing one.
            # Compare lengths and keep the longer one.
            existing_best = max(candidates, key=lambda j: len(docs[int(j)]["content"]))
            if len(content) > len(docs[int(existing_best)]["content"]):
                # New doc is longer β€” replace the existing representative
                kept_indices.discard(int(existing_best))
                kept_indices.add(i)
                # We can't easily remove from LSH, so just add the new one.
                # The old one is dropped via kept_indices tracking.
                try:
                    lsh.insert(str(i), mh)
                except Exception:
                    pass
            # else: keep existing, drop this one
        else:
            # No near-duplicate found β€” keep this doc
            kept_indices.add(i)
            try:
                lsh.insert(str(i), mh)
            except Exception:
                pass

        if (i + 1) % 5000 == 0:
            print(f"  Processed {i+1}/{len(docs)} | clusters so far: {len(kept_indices)}")

    kept = [docs[i] for i in sorted(kept_indices)]
    dropped = len(docs) - len(kept)

    stats = {
        "input": len(docs),
        "kept": len(kept),
        "dropped_near": dropped,
        "drop_rate": dropped / max(len(docs), 1),
        "threshold": LSH_THRESHOLD,
        "num_perm": NUM_PERM,
    }
    print(f"  Input: {len(docs)} | Kept: {len(kept)} | Dropped: {dropped} "
          f"({stats['drop_rate']:.1%})")
    return kept, stats


# ─── Stage 3: Line-level dedup ───────────────────────────────────────────────

def stage3_line_dedup(docs: list[dict]) -> tuple[list[dict], dict]:
    """Remove boilerplate lines that appear across many documents."""
    print("\n" + "=" * 60)
    print(f"STAGE 3: Line-level dedup (boilerplate threshold={LINE_BOILERPLATE_THRESHOLD})")
    print("=" * 60)

    # First pass: count how many documents each normalized line appears in
    line_doc_count = Counter()
    for doc in docs:
        lines = doc["content"].split("\n")
        # Use normalized line hash to save memory
        seen_in_doc = set()
        for line in lines:
            norm = line.strip()
            if not norm:
                continue
            # Normalize: collapse internal whitespace, lowercase
            norm = re.sub(r"\s+", " ", norm).lower()
            if len(norm) < 5:  # skip very short lines (braces, etc.)
                continue
            h = hashlib.md5(norm.encode("utf-8")).hexdigest()
            seen_in_doc.add(h)
        for h in seen_in_doc:
            line_doc_count[h] += 1

    # Identify boilerplate lines (appear in >= threshold documents)
    boilerplate_hashes = {h for h, c in line_doc_count.items() if c >= LINE_BOILERPLATE_THRESHOLD}
    print(f"  Boilerplate lines identified: {len(boilerplate_hashes)}")

    # Second pass: remove boilerplate lines from each document
    kept = []
    dropped_too_short = 0
    lines_removed_total = 0

    for doc in docs:
        lines = doc["content"].split("\n")
        new_lines = []
        for line in lines:
            norm = line.strip()
            if norm:
                norm_h = re.sub(r"\s+", " ", norm).lower()
                if len(norm_h) >= 5:
                    h = hashlib.md5(norm_h.encode("utf-8")).hexdigest()
                    if h in boilerplate_hashes:
                        lines_removed_total += 1
                        continue
            new_lines.append(line)

        new_content = "\n".join(new_lines).strip()
        new_line_count = len([l for l in new_lines if l.strip()])

        if new_line_count < MIN_LINES_AFTER_DEDUP or len(new_content) < MIN_CHARS_AFTER_DEDUP:
            dropped_too_short += 1
            continue

        doc = dict(doc)
        doc["content"] = new_content
        kept.append(doc)

    stats = {
        "input": len(docs),
        "kept": len(kept),
        "dropped_too_short": dropped_too_short,
        "lines_removed": lines_removed_total,
        "boilerplate_hashes": len(boilerplate_hashes),
    }
    print(f"  Input: {len(docs)} | Kept: {len(kept)} | "
          f"Dropped (too short): {dropped_too_short} | "
          f"Lines removed: {lines_removed_total}")
    return kept, stats


# ─── Main ────────────────────────────────────────────────────────────────────

def main():
    os.makedirs(DEDUP_DIR, exist_ok=True)

    print(f"Loading documents from {RAW_DOCS_PATH}...")
    docs = []
    with open(RAW_DOCS_PATH, "r", encoding="utf-8") as f:
        for line in f:
            docs.append(json.loads(line))
    print(f"  Loaded {len(docs):,} documents")
    print(f"  Total size: {sum(len(d['content']) for d in docs) / 1e6:.1f} MB")

    all_stats = {"input_count": len(docs)}

    # Stage 1: Exact dedup
    docs, s1 = stage1_exact_dedup(docs)
    all_stats["stage1_exact"] = s1

    # Stage 2: Near-dedup
    docs, s2 = stage2_near_dedup(docs)
    all_stats["stage2_near"] = s2

    # Stage 3: Line-level dedup
    docs, s3 = stage3_line_dedup(docs)
    all_stats["stage3_line"] = s3

    # Final stats
    final_size = sum(len(d["content"]) for d in docs)
    all_stats["final_count"] = len(docs)
    all_stats["final_size_mb"] = final_size / 1e6

    # Language distribution
    lang_counts = {}
    for d in docs:
        lang_counts[d["language"]] = lang_counts.get(d["language"], 0) + 1
    all_stats["language_distribution"] = lang_counts

    print("\n" + "=" * 60)
    print("DEDUP COMPLETE")
    print("=" * 60)
    print(f"  Input:   {all_stats['input_count']:,} documents")
    print(f"  Final:   {len(docs):,} documents")
    print(f"  Size:    {final_size / 1e6:.1f} MB")
    print(f"  Languages: {lang_counts}")

    # Write deduped documents
    with open(DEDUP_DOCS_PATH, "w", encoding="utf-8") as f:
        for doc in docs:
            f.write(json.dumps(doc, ensure_ascii=False) + "\n")
    print(f"\nDeduped documents written to {DEDUP_DOCS_PATH}")

    # Write stats
    with open(STATS_PATH, "w") as f:
        json.dump(all_stats, f, indent=2)
    print(f"Stats written to {STATS_PATH}")


if __name__ == "__main__":
    main()