| """ |
| Deduplicate flagged_documents.csv.gz by URL. |
| ============================================= |
| Same page can appear multiple times from different Common Crawl snapshots. |
| Keeps the first occurrence of each URL. |
| |
| Usage: |
| python deduplicate_flagged.py \ |
| --input audit_output/flagged_documents.csv.gz \ |
| --output audit_output/flagged_documents_deduped.csv.gz |
| """ |
|
|
| import csv |
| import gzip |
| import argparse |
|
|
| csv.field_size_limit(10_000_000) |
|
|
|
|
| def deduplicate(input_path, output_path): |
| seen = set() |
| kept = 0 |
| total = 0 |
|
|
| with gzip.open(input_path, "rt") as fin, \ |
| gzip.open(output_path, "wt") as fout: |
| reader = csv.DictReader(fin) |
| writer = csv.DictWriter(fout, fieldnames=reader.fieldnames) |
| writer.writeheader() |
| for row in reader: |
| total += 1 |
| url = row.get("url", "") |
| if url and url not in seen: |
| seen.add(url) |
| writer.writerow(row) |
| kept += 1 |
| if total % 100000 == 0: |
| print(f" {total:,} processed, {kept:,} kept", flush=True) |
|
|
| print(f"\nDone: {total:,} total → {kept:,} unique ({total - kept:,} duplicates removed)") |
|
|
|
|
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--input", default="audit_output/flagged_documents.csv.gz") |
| parser.add_argument("--output", default="audit_output/flagged_documents_deduped.csv.gz") |
| args = parser.parse_args() |
|
|
| deduplicate(args.input, args.output) |
|
|