Datasets:
File size: 936 Bytes
add8e69 | 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 | import json
import os
input_file = "segments.json"
output_dir = "test_segments"
print(f"Loading {input_file}...")
with open(input_file, "r") as f:
data = json.load(f)
surahs = {}
# Group verses by surah
for key, value in data.items():
surah_id, verse_id = key.split(":")
surah_num = int(surah_id)
# Use zero-padded surah ID like '001', '002', etc.
surah_str = f"{surah_num:03d}"
if surah_str not in surahs:
surahs[surah_str] = {}
surahs[surah_str][key] = value
# Create output directory
os.makedirs(output_dir, exist_ok=True)
# Write to separate JSON files
for surah_str, surah_data in surahs.items():
output_path = os.path.join(output_dir, f"{surah_str}.json")
with open(output_path, "w") as f:
# Saving compactly
json.dump(surah_data, f, separators=(',', ':'))
print(f"Successfully split into {len(surahs)} surah JSON files in '{output_dir}/' folder.")
|