leideng commited on
Commit
5ea2130
·
verified ·
1 Parent(s): ab1cf54

add script to split by domain and subdomain

Browse files
Files changed (1) hide show
  1. split_data_by_domain.py +87 -0
split_data_by_domain.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+
3
+ import argparse
4
+ import json
5
+ import re
6
+ import unicodedata
7
+ from collections import defaultdict
8
+ from pathlib import Path
9
+
10
+
11
+ def slugify(value: str) -> str:
12
+ normalized = unicodedata.normalize("NFKD", value)
13
+ ascii_value = normalized.encode("ascii", "ignore").decode("ascii")
14
+ slug = re.sub(r"[^a-zA-Z0-9]+", "-", ascii_value.lower()).strip("-")
15
+ return slug or "unknown"
16
+
17
+
18
+ def load_records(input_path: Path) -> list[dict]:
19
+ with input_path.open("r", encoding="utf-8") as f:
20
+ data = json.load(f)
21
+
22
+ if not isinstance(data, list):
23
+ raise ValueError(f"{input_path} must contain a top-level JSON array.")
24
+
25
+ for index, item in enumerate(data):
26
+ if not isinstance(item, dict):
27
+ raise ValueError(f"Record at index {index} is not a JSON object.")
28
+
29
+ return data
30
+
31
+
32
+ def group_records(records: list[dict]) -> dict[tuple[str, str], list[dict]]:
33
+ grouped: dict[tuple[str, str], list[dict]] = defaultdict(list)
34
+
35
+ for index, record in enumerate(records):
36
+ domain = record.get("domain")
37
+ sub_domain = record.get("sub_domain")
38
+
39
+ if not domain or not sub_domain:
40
+ raise ValueError(
41
+ f"Record at index {index} is missing a non-empty 'domain' or 'sub_domain'."
42
+ )
43
+
44
+ grouped[(str(domain), str(sub_domain))].append(record)
45
+
46
+ return grouped
47
+
48
+
49
+ def write_groups(grouped_records: dict[tuple[str, str], list[dict]], output_dir: Path) -> None:
50
+ for (domain, sub_domain), records in grouped_records.items():
51
+ domain_dir = output_dir / slugify(domain)
52
+ output_path = domain_dir / f"{slugify(sub_domain)}.json"
53
+
54
+ domain_dir.mkdir(parents=True, exist_ok=True)
55
+ with output_path.open("w", encoding="utf-8") as f:
56
+ json.dump(records, f, ensure_ascii=False, indent=2)
57
+ f.write("\n")
58
+
59
+
60
+ def parse_args() -> argparse.Namespace:
61
+ parser = argparse.ArgumentParser(
62
+ description="Split a JSON array into data/<domain>/<sub-domain>.json files."
63
+ )
64
+ parser.add_argument(
65
+ "--input",
66
+ type=Path,
67
+ default=Path("data.json"),
68
+ help="Path to the source JSON file. Default: data.json",
69
+ )
70
+ parser.add_argument(
71
+ "--output-dir",
72
+ type=Path,
73
+ default=Path("data"),
74
+ help="Directory where split files will be written. Default: data",
75
+ )
76
+ return parser.parse_args()
77
+
78
+
79
+ def main() -> None:
80
+ args = parse_args()
81
+ records = load_records(args.input)
82
+ grouped_records = group_records(records)
83
+ write_groups(grouped_records, args.output_dir)
84
+
85
+
86
+ if __name__ == "__main__":
87
+ main()