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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
"""
BoccaccioAI - Data Filtering / Cleaning Pipeline (Streaming)

Takes raw Italian text from CulturaX JSONL shards and applies sequential
quality-filtering stages to produce a clean dataset:

  Pass 1 - Heuristic filtering (streaming, shard per shard):
    - Length, alpha ratio, punctuation, repeated lines
    - Writes surviving docs to data/heuristic/

  Pass 2 - Exact deduplication (xxhash, streaming):
    - Reads heuristic-passed docs, computes xxhash of normalized text
    - Only 8-byte hashes held in RAM (~200MB for 10M docs)
    - Writes deduplicated docs to data/filtered/

  Pass 3 - Perplexity filtering (optional, requires KenLM model):
    - Streaming, shard per shard

Memory usage: ~500MB per shard during Pass 1,
              ~200MB for hash set during Pass 2.

De Lauretis Tech
"""

from __future__ import annotations

import argparse
import json
import logging
import string
import sys
from functools import partial
from multiprocessing import Pool
from pathlib import Path
from typing import Any, Iterator

from tqdm import tqdm

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(name)s - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger("boccaccio.data.filter")

# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Filter and clean raw CulturaX Italian JSONL shards for BoccaccioAI.",
    )
    parser.add_argument(
        "--input-dir",
        type=str,
        default="data/raw",
        help="Directory containing raw JSONL shards (default: data/raw).",
    )
    parser.add_argument(
        "--output-dir",
        type=str,
        default="data/filtered",
        help="Directory for filtered output shards (default: data/filtered).",
    )
    parser.add_argument(
        "--jaccard-threshold",
        type=float,
        default=0.85,
        help="MinHash LSH Jaccard similarity threshold (default: 0.85).",
    )
    parser.add_argument(
        "--min-doc-length",
        type=int,
        default=200,
        help="Minimum document length in characters (default: 200).",
    )
    parser.add_argument(
        "--max-doc-length",
        type=int,
        default=100_000,
        help="Maximum document length in characters (default: 100000).",
    )
    parser.add_argument(
        "--num-perm",
        type=int,
        default=128,
        help="Number of permutations for MinHash signatures (default: 128).",
    )
    parser.add_argument(
        "--kenlm-model",
        type=str,
        default=None,
        help="Path to a KenLM .arpa/.binary model file. If omitted the "
             "perplexity filter stage is skipped.",
    )
    parser.add_argument(
        "--max-perplexity",
        type=float,
        default=1000.0,
        help="Maximum perplexity threshold (default: 1000).",
    )
    parser.add_argument(
        "--num-workers",
        type=int,
        default=4,
        help="Number of parallel workers for heuristic filtering (default: 4).",
    )
    return parser.parse_args()


# ---------------------------------------------------------------------------
# I/O helpers (streaming)
# ---------------------------------------------------------------------------


def iter_jsonl_shards(input_dir: str) -> tuple[list[Path], Iterator[dict[str, Any]]]:
    """Return (shard_files, generator) that yields docs one at a time.

    The generator reads shard files sequentially, yielding parsed JSON dicts.
    Memory usage is O(1) -- only one line in memory at a time.
    """
    input_path = Path(input_dir)
    if not input_path.is_dir():
        logger.error("Input directory does not exist: %s", input_dir)
        sys.exit(1)

    shard_files = sorted(input_path.glob("*.jsonl"))
    if not shard_files:
        logger.error("No .jsonl files found in %s", input_dir)
        sys.exit(1)

    logger.info("Found %d shard(s) in %s", len(shard_files), input_dir)

    def _gen() -> Iterator[dict[str, Any]]:
        for shard in shard_files:
            with open(shard, "r", encoding="utf-8") as fh:
                for line in fh:
                    line = line.strip()
                    if line:
                        yield json.loads(line)

    return shard_files, _gen()


def iter_jsonl_shard_file(path: Path) -> Iterator[dict[str, Any]]:
    """Yield docs from a single JSONL file."""
    with open(path, "r", encoding="utf-8") as fh:
        for line in fh:
            line = line.strip()
            if line:
                yield json.loads(line)


def write_jsonl_shards(
    docs: list[dict[str, Any]],
    output_dir: str,
    docs_per_shard: int = 50_000,
    prefix: str = "filtered",
) -> int:
    """Write *docs* as JSONL shards into *output_dir*. Returns shard count."""
    output_path = Path(output_dir)
    output_path.mkdir(parents=True, exist_ok=True)

    total_shards = (len(docs) + docs_per_shard - 1) // docs_per_shard
    logger.info(
        "Writing %s documents to %d shard(s) in %s",
        f"{len(docs):,}",
        total_shards,
        output_dir,
    )

    for shard_idx in range(total_shards):
        start = shard_idx * docs_per_shard
        end = min(start + docs_per_shard, len(docs))
        shard_name = f"{prefix}_{shard_idx:05d}.jsonl"
        shard_path = output_path / shard_name

        with open(shard_path, "w", encoding="utf-8") as fh:
            for doc in docs[start:end]:
                fh.write(json.dumps(doc, ensure_ascii=False) + "\n")

    logger.info("Finished writing %d shard(s).", total_shards)
    return total_shards


# ---------------------------------------------------------------------------
# Stage 1 -- Heuristic Filtering (streaming, shard per shard)
# ---------------------------------------------------------------------------

_PUNCTUATION_SET = set(string.punctuation)


def _passes_heuristic(
    doc: dict[str, Any],
    min_doc_length: int,
    max_doc_length: int,
) -> bool:
    """Return True if *doc* passes every heuristic quality check."""
    text: str = doc.get("text", "")
    length = len(text)

    # Length bounds.
    if length < min_doc_length or length > max_doc_length:
        return False

    # Alphabetic character ratio >= 0.70.
    alpha_count = sum(1 for ch in text if ch.isalpha())
    if alpha_count / length < 0.70:
        return False

    # Punctuation density <= 0.15.
    punct_count = sum(1 for ch in text if ch in _PUNCTUATION_SET)
    if punct_count / length > 0.15:
        return False

    # Repeated-line ratio <= 0.30.
    lines = text.splitlines()
    if lines:
        unique_lines = set(lines)
        duplicate_ratio = 1.0 - len(unique_lines) / len(lines)
        if duplicate_ratio > 0.30:
            return False

        # Average line length >= 20 characters.
        avg_line_length = sum(len(line) for line in lines) / len(lines)
        if avg_line_length < 20:
            return False

    return True


def heuristic_filter_streaming(
    input_dir: str,
    output_dir: str,
    min_doc_length: int,
    max_doc_length: int,
    num_workers: int,
) -> tuple[int, int]:
    """Apply heuristic filters shard-by-shard (streaming).

    Reads each shard, filters in parallel, writes surviving docs to output_dir.
    Returns (total_input, total_kept).
    """
    logger.info(
        "Stage 1 - Heuristic filtering (streaming, workers=%d) ...",
        num_workers,
    )

    input_path = Path(input_dir)
    shard_files = sorted(input_path.glob("*.jsonl"))
    if not shard_files:
        logger.error("No .jsonl files found in %s", input_dir)
        sys.exit(1)

    output_path = Path(output_dir)
    output_path.mkdir(parents=True, exist_ok=True)

    check_fn = partial(
        _passes_heuristic,
        min_doc_length=min_doc_length,
        max_doc_length=max_doc_length,
    )

    total_input = 0
    total_kept = 0
    shard_out_idx = 0
    docs_per_shard = 50_000
    buffer: list[dict[str, Any]] = []

    def flush_buffer() -> None:
        nonlocal shard_out_idx, buffer
        if not buffer:
            return
        shard_name = f"heuristic_{shard_out_idx:05d}.jsonl"
        shard_path = output_path / shard_name
        with open(shard_path, "w", encoding="utf-8") as fh:
            for doc in buffer:
                fh.write(json.dumps(doc, ensure_ascii=False) + "\n")
        shard_out_idx += 1
        buffer = []

    for shard in tqdm(shard_files, desc="Heuristic filter"):
        # Read shard into memory (one shard = ~500MB, safe for 16GB RAM)
        shard_docs = list(iter_jsonl_shard_file(shard))
        total_input += len(shard_docs)

        # Filter in parallel
        with Pool(processes=num_workers) as pool:
            verdicts = list(
                pool.imap(check_fn, shard_docs, chunksize=512)
            )

        for doc, ok in zip(shard_docs, verdicts):
            if ok:
                buffer.append(doc)
                total_kept += 1
                if len(buffer) >= docs_per_shard:
                    flush_buffer()

        # Free shard memory
        del shard_docs

    flush_buffer()

    removed = total_input - total_kept
    logger.info(
        "Stage 1 complete: input %s, kept %s, removed %s (%.1f%%)",
        f"{total_input:,}",
        f"{total_kept:,}",
        f"{removed:,}",
        (removed / total_input * 100) if total_input else 0,
    )
    return total_input, total_kept


# ---------------------------------------------------------------------------
# Stage 2 -- Exact Deduplication (xxhash, streaming)
# ---------------------------------------------------------------------------


def deduplicate_exact_streaming(
    input_dir: str,
    output_dir: str,
) -> tuple[int, int]:
    """Remove exact-duplicate documents using xxhash (streaming).

    Reads heuristic-passed docs shard-by-shard, computes xxhash of the
    normalized text, and keeps only the first occurrence of each hash.

    Memory: ~200MB for 10M docs (set of 8-byte hashes).
    Time: ~10 minutes for 30GB of text on 8 vCPU.

    Returns (total_input, total_kept).
    """
    import xxhash

    input_path = Path(input_dir)
    shard_files = sorted(input_path.glob("*.jsonl"))
    if not shard_files:
        logger.error("No .jsonl files found in %s", input_dir)
        sys.exit(1)

    logger.info(
        "Stage 2 - Exact deduplication (xxhash, streaming) on %d shards ...",
        len(shard_files),
    )

    output_path = Path(output_dir)
    output_path.mkdir(parents=True, exist_ok=True)

    seen_hashes: set[int] = set()
    total_docs = 0
    duplicates = 0
    kept = 0
    shard_out_idx = 0
    docs_per_shard = 50_000
    buffer: list[dict[str, Any]] = []

    def flush_buffer() -> None:
        nonlocal shard_out_idx, buffer
        if not buffer:
            return
        shard_name = f"filtered_{shard_out_idx:05d}.jsonl"
        shard_path = output_path / shard_name
        with open(shard_path, "w", encoding="utf-8") as fh:
            for doc in buffer:
                fh.write(json.dumps(doc, ensure_ascii=False) + "\n")
        shard_out_idx += 1
        buffer = []

    for shard in tqdm(shard_files, desc="Exact dedup"):
        for line in open(shard, "r", encoding="utf-8"):
            line = line.strip()
            if not line:
                continue
            doc = json.loads(line)
            text: str = doc.get("text", "")
            total_docs += 1

            # Normalize: strip whitespace, lowercase for hash
            # (catches duplicates with trivial formatting differences)
            normalized = " ".join(text.split()).lower()
            doc_hash = xxhash.xxh64(normalized).intdigest()

            if doc_hash in seen_hashes:
                duplicates += 1
                continue

            seen_hashes.add(doc_hash)
            kept += 1
            buffer.append(doc)
            if len(buffer) >= docs_per_shard:
                flush_buffer()

    flush_buffer()

    logger.info(
        "Stage 2 complete: input %s, kept %s, removed %s duplicates (%.1f%%)",
        f"{total_docs:,}",
        f"{kept:,}",
        f"{duplicates:,}",
        (duplicates / total_docs * 100) if total_docs else 0,
    )
    return total_docs, kept


# ---------------------------------------------------------------------------
# Stage 3 -- Perplexity Filtering (optional, streaming)
# ---------------------------------------------------------------------------


def perplexity_filter_streaming(
    input_dir: str,
    output_dir: str,
    kenlm_model_path: str,
    max_perplexity: float,
) -> tuple[int, int]:
    """Remove documents whose KenLM perplexity exceeds *max_perplexity*.

    Streaming: reads shard-by-shard, writes passing docs to output_dir.
    """
    try:
        import kenlm  # type: ignore[import-untyped]
    except ImportError:
        logger.warning(
            "kenlm is not installed -- skipping perplexity filtering. "
            "Install it with: pip install https://github.com/kpu/kenlm/archive/master.zip"
        )
        # Just copy input to output
        import shutil
        shutil.rmtree(output_dir, ignore_errors=True)
        shutil.copytree(input_dir, output_dir)
        return 0, 0

    if not Path(kenlm_model_path).is_file():
        logger.warning(
            "KenLM model file not found at %s -- skipping perplexity filtering.",
            kenlm_model_path,
        )
        import shutil
        shutil.rmtree(output_dir, ignore_errors=True)
        shutil.copytree(input_dir, output_dir)
        return 0, 0

    logger.info(
        "Stage 3 - Perplexity filtering (streaming, model=%s, max_perplexity=%.1f) ...",
        kenlm_model_path,
        max_perplexity,
    )

    model = kenlm.Model(kenlm_model_path)

    input_path = Path(input_dir)
    shard_files = sorted(input_path.glob("*.jsonl"))

    output_path = Path(output_dir)
    output_path.mkdir(parents=True, exist_ok=True)

    total = 0
    kept = 0
    shard_out_idx = 0
    docs_per_shard = 50_000
    buffer: list[dict[str, Any]] = []

    def flush_buffer() -> None:
        nonlocal shard_out_idx, buffer
        if not buffer:
            return
        shard_name = f"ppl_filtered_{shard_out_idx:05d}.jsonl"
        shard_path = output_path / shard_name
        with open(shard_path, "w", encoding="utf-8") as fh:
            for doc in buffer:
                fh.write(json.dumps(doc, ensure_ascii=False) + "\n")
        shard_out_idx += 1
        buffer = []

    for shard in tqdm(shard_files, desc="Perplexity filter"):
        for line in open(shard, "r", encoding="utf-8"):
            line = line.strip()
            if not line:
                continue
            doc = json.loads(line)
            text: str = doc.get("text", "")
            total += 1

            log_score = model.score(text, bos=True, eos=True)
            num_words = len(text.split()) or 1
            ppl = 10.0 ** (-log_score / num_words)

            if ppl <= max_perplexity:
                buffer.append(doc)
                kept += 1
                if len(buffer) >= docs_per_shard:
                    flush_buffer()

    flush_buffer()

    removed = total - kept
    logger.info(
        "Stage 3 complete: input %s, kept %s, removed %s",
        f"{total:,}",
        f"{kept:,}",
        f"{removed:,}",
    )
    return total, kept


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------


def main() -> None:
    args = parse_args()

    logger.info("=== BoccaccioAI Data Filtering Pipeline ===")
    logger.info("Input dir:          %s", args.input_dir)
    logger.info("Output dir:         %s", args.output_dir)
    logger.info("Min doc length:     %d", args.min_doc_length)
    logger.info("Max doc length:     %d", args.max_doc_length)
    logger.info("Jaccard threshold:  %.2f", args.jaccard_threshold)
    logger.info("Num permutations:   %d", args.num_perm)
    logger.info("KenLM model:        %s", args.kenlm_model or "(none)")
    logger.info("Max perplexity:     %.1f", args.max_perplexity)
    logger.info("Workers:            %d", args.num_workers)

    # Intermediate directory for heuristic-passed docs
    heuristic_dir = str(Path(args.output_dir).parent / "heuristic")

    # ─── Pass 1: Heuristic filtering (streaming) ──────────
    total_input, heuristic_kept = heuristic_filter_streaming(
        input_dir=args.input_dir,
        output_dir=heuristic_dir,
        min_doc_length=args.min_doc_length,
        max_doc_length=args.max_doc_length,
        num_workers=args.num_workers,
    )

    # ─── Pass 2: Exact deduplication (xxhash, streaming) ──
    dedup_input, dedup_kept = deduplicate_exact_streaming(
        input_dir=heuristic_dir,
        output_dir=args.output_dir,
    )

    # ─── Pass 3: Perplexity filtering (optional) ──────────
    if args.kenlm_model is not None:
        ppl_dir = str(Path(args.output_dir).parent / "ppl_filtered")
        ppl_input, ppl_kept = perplexity_filter_streaming(
            input_dir=args.output_dir,
            output_dir=ppl_dir,
            kenlm_model_path=args.kenlm_model,
            max_perplexity=args.max_perplexity,
        )
        final_count = ppl_kept
    else:
        logger.info("Stage 3 - Skipped (no --kenlm-model provided).")
        final_count = dedup_kept

    # ─── Cleanup intermediate heuristic dir ───────────────
    import shutil
    heuristic_path = Path(heuristic_dir)
    if heuristic_path.exists():
        shutil.rmtree(heuristic_path)
        logger.info("Cleaned up intermediate directory: %s", heuristic_dir)

    # ─── Summary ──────────────────────────────────────────
    reduction = (1.0 - final_count / total_input) * 100.0 if total_input else 0.0
    logger.info("=== Filtering Summary ===")
    logger.info("Original documents:  %s", f"{total_input:,}")
    logger.info("After heuristic:     %s", f"{heuristic_kept:,}")
    logger.info("After dedup:         %s", f"{dedup_kept:,}")
    logger.info("Final documents:     %s", f"{final_count:,}")
    logger.info("Reduction:           %.2f%%", reduction)


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        logger.warning("Filtering interrupted by user.")
        sys.exit(130)