| import json |
| import os |
|
|
| def process_standard_structure(data, filename): |
| """ |
| מעבד קבצים עם מבנה סטנדרטי (תנ"ך, ש"ס, ירושלמי, משנה). |
| מבנה: קטגוריה -> תת-קטגוריה -> ספרים -> {שם_ספר: {pages: מספר}} |
| """ |
| records = [] |
| main_category = data.get('name') |
| content_type = data.get('content_type') |
|
|
| for subcategory in data.get('subcategories', []): |
| subcategory_name = subcategory.get('name') |
| for book_name, book_info in subcategory.get('books', {}).items(): |
| record = { |
| "source_file": filename, |
| "main_category": main_category, |
| "subcategory": subcategory_name, |
| "book": book_name, |
| "content_type": content_type, |
| "count": book_info.get('pages') |
| } |
| records.append(record) |
| return records |
|
|
| def process_rambam(data, filename): |
| """ |
| מעבד את קובץ הרמב"ם, השומר על החלוקה הפנימית ל'חלקים' (parts). |
| """ |
| records = [] |
| main_category = data.get('name') |
| content_type = data.get('content_type') |
|
|
| for subcategory in data.get('subcategories', []): |
| subcategory_name = subcategory.get('name') |
| for book_name, book_info in subcategory.get('books', {}).items(): |
| parts = book_info.get('parts', []) |
| |
| total_chapters = sum(part.get('end', 0) - part.get('start', 0) + 1 for part in parts) |
| |
| record = { |
| "source_file": filename, |
| "main_category": main_category, |
| "subcategory": subcategory_name, |
| "book": book_name, |
| "content_type": content_type, |
| "count": total_chapters, |
| "parts": parts |
| } |
| records.append(record) |
| return records |
|
|
| def process_halakha(data, filename): |
| """ |
| מעבד את קובץ ההלכה, המכיל מבנים שונים (טור/שו"ע, משנה ברורה). |
| """ |
| records = [] |
| main_category = data.get('name') |
|
|
| for subcategory in data.get('subcategories', []): |
| subcategory_name = subcategory.get('name') |
| content_type = subcategory.get('content_type') |
|
|
| for book_name, book_info in subcategory.get('books', {}).items(): |
| record = { |
| "source_file": filename, |
| "main_category": main_category, |
| "subcategory": subcategory_name, |
| "book": book_name, |
| "content_type": content_type |
| } |
|
|
| |
| if 'parts' in book_info: |
| parts = book_info.get('parts', []) |
| |
| total_simanim = sum(part.get('end', 0) - part.get('start', 0) + 1 for part in parts) |
| record['count'] = total_simanim |
| record['parts'] = parts |
| |
| |
| elif 'pages' in book_info: |
| record['count'] = book_info.get('pages') |
|
|
| |
| if 'exclude' in book_info: |
| record['excluded_units'] = book_info.get('exclude') |
| |
| records.append(record) |
| |
| return records |
|
|
|
|
| def main(): |
| """ |
| פונקציה ראשית שמנהלת את תהליך ההמרה. |
| """ |
| |
| file_processors = { |
| 'shas.json': process_standard_structure, |
| 'yerushalmi.json': process_standard_structure, |
| 'mishna.json': process_standard_structure, |
| 'tanach.json': process_standard_structure, |
| 'rambam.json': process_rambam, |
| 'halakha.json': process_halakha |
| } |
|
|
| all_records = [] |
| input_files = list(file_processors.keys()) |
|
|
| for filename in input_files: |
| if not os.path.exists(filename): |
| print(f"Warning: File '{filename}' not found. Skipping.") |
| continue |
| |
| try: |
| with open(filename, 'r', encoding='utf-8') as f: |
| data = json.load(f) |
| |
| processor_func = file_processors[filename] |
| records = processor_func(data, filename) |
| all_records.extend(records) |
| print(f"Processed '{filename}', added {len(records)} records.") |
|
|
| except json.JSONDecodeError: |
| print(f"Error: Could not decode JSON from '{filename}'.") |
| except Exception as e: |
| print(f"An unexpected error occurred while processing '{filename}': {e}") |
|
|
| output_filename = 'judaic_texts_dataset.jsonl' |
| try: |
| with open(output_filename, 'w', encoding='utf-8') as f_out: |
| for record in all_records: |
| |
| f_out.write(json.dumps(record, ensure_ascii=False) + '\n') |
| |
| print(f"\nSuccessfully created dataset file: '{output_filename}' with {len(all_records)} total records.") |
|
|
| except Exception as e: |
| print(f"An error occurred while writing the output file: {e}") |
|
|
|
|
| if __name__ == '__main__': |
| main() |