Quranic-Recitation-Data / split_segments.py
zaibihassan's picture
Add files using upload-large-folder tool
add8e69 verified
Raw
History Blame
936 Bytes
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.")