Pre-publication review draft
日常訳NICHIJOYAKU日本語 ↗

OPEN SOURCE

software/src/data/loader.py

Read on this site and download the files you need.

Nichijoyaku was created using AI. A complete review by human biblical-language and biblical-studies specialists has not been established. Read alongside established translations and original-language resources.

← Back to the open-source guide

software/src/data/loader.py220 linesDownload this file ↓

This is the same source included in the download. Each line number is a link you can save or share.

File verification detailsSHA-256 1509e55bbaa5245746e2de2d7a61b20af4d17cbcb84071321bae899a50c9732e
1"""Data loading utilities for Bible source texts."""23from __future__ import annotations45import json6from pathlib import Path7from typing import Any89from src.data.migrations import load_glossary_any_version10from src.models.verse import (11    BookMetadata,12    ChapterSummary,13    GlossaryTerm,14    Verse,15    VerseReference,16)171819class DataLoader:20    """Loads Bible source texts and metadata from JSON files."""2122    def __init__(self, data_dir: Path | str) -> None:23        """Initialize the data loader.2425        Args:26            data_dir: Path to the data directory containing sources and metadata.27        """28        self.data_dir = Path(data_dir)29        self.sources_dir = self.data_dir / "sources"30        self.metadata_dir = self.data_dir / "metadata"3132    def _load_json(self, path: Path) -> dict[str, Any]:33        """Load a JSON file."""34        if not path.exists():35            return {}36        with path.open("r", encoding="utf-8") as f:37            return json.load(f)3839    def load_greek_book(self, book: str) -> dict[str, str]:40        """Load Greek source text for a book.4142        Returns:43            Dictionary mapping verse references to Greek text.44        """45        path = self.sources_dir / "greek" / f"{book}.json"46        return self._load_json(path)4748    def load_japanese_meiji(self, book: str) -> dict[str, str]:49        """Load Meiji translation for a book."""50        path = self.sources_dir / "japanese" / "meiji" / f"{book}.json"51        return self._load_json(path)5253    def load_japanese_taisho(self, book: str) -> dict[str, str]:54        """Load Taisho translation for a book."""55        path = self.sources_dir / "japanese" / "taisho" / f"{book}.json"56        return self._load_json(path)5758    def load_japanese_kougo(self, book: str) -> dict[str, str]:59        """Load Kougo-yaku translation for a book (1954-1955)."""60        path = self.sources_dir / "japanese" / "kougo" / f"{book}.json"61        return self._load_json(path)6263    def load_english_kjv(self, book: str) -> dict[str, str]:64        """Load KJV translation for a book."""65        path = self.sources_dir / "english" / "kjv" / f"{book}.json"66        return self._load_json(path)6768    def load_english_asv(self, book: str) -> dict[str, str]:69        """Load ASV translation for a book."""70        path = self.sources_dir / "english" / "asv" / f"{book}.json"71        return self._load_json(path)7273    def load_hebrew_book(self, book: str) -> dict[str, str]:74        """Load Hebrew source text for a book (OT only).7576        Returns:77            Dictionary mapping verse references to Hebrew text.78        """79        path = self.sources_dir / "hebrew" / f"{book}.json"80        return self._load_json(path)8182    def load_verse(self, reference: VerseReference) -> Verse:83        """Load a verse with all available source texts.8485        Args:86            reference: The verse reference to load.8788        Returns:89            Verse object with all available source texts populated.90        """91        ref_key = f"{reference.chapter}:{reference.verse}"9293        greek = self.load_greek_book(reference.book)94        hebrew = self.load_hebrew_book(reference.book)95        meiji = self.load_japanese_meiji(reference.book)96        taisho = self.load_japanese_taisho(reference.book)97        kougo = self.load_japanese_kougo(reference.book)98        kjv = self.load_english_kjv(reference.book)99        asv = self.load_english_asv(reference.book)100101        greek_data = greek.get(ref_key, {})102        greek_text = (103            greek_data.get("text") if isinstance(greek_data, dict) else greek_data104        )105        greek_translit = (106            greek_data.get("translit") if isinstance(greek_data, dict) else None107        )108109        hebrew_data = hebrew.get(ref_key, {})110        hebrew_text = (111            hebrew_data.get("text") if isinstance(hebrew_data, dict) else hebrew_data112        )113        if hebrew_text:114            # MorphHB marks morpheme boundaries with "/" inside words; strip115            # them so the prompt receives normal Hebrew text.116            hebrew_text = hebrew_text.replace("/", "")117        hebrew_translit = (118            hebrew_data.get("translit") if isinstance(hebrew_data, dict) else None119        )120121        greek_words = greek_data.get("words") if isinstance(greek_data, dict) else None122        hebrew_words = hebrew_data.get("words") if isinstance(hebrew_data, dict) else None123124        return Verse(125            reference=reference,126            greek_text=greek_text or None,127            greek_translit=greek_translit,128            hebrew_text=hebrew_text or None,129            hebrew_translit=hebrew_translit,130            greek_words=greek_words,131            hebrew_words=hebrew_words,132            japanese_meiji=meiji.get(ref_key),133            japanese_taisho=taisho.get(ref_key),134            japanese_kougo=kougo.get(ref_key),135            english_kjv=kjv.get(ref_key),136            english_asv=asv.get(ref_key),137        )138139    def load_chapter_verses(self, book: str, chapter: int) -> list[Verse]:140        """Load all verses for a chapter.141142        Args:143            book: Book name.144            chapter: Chapter number.145146        Returns:147            List of Verse objects for the chapter.148        """149        greek = self.load_greek_book(book)150        hebrew = self.load_hebrew_book(book)151        meiji = self.load_japanese_meiji(book)152        taisho = self.load_japanese_taisho(book)153        kougo = self.load_japanese_kougo(book)154        kjv = self.load_english_kjv(book)155        asv = self.load_english_asv(book)156157        # Find all verses in this chapter from any source158        verse_nums: set[int] = set()159        for source in [greek, hebrew, meiji, taisho, kougo, kjv, asv]:160            for key in source:161                parts = key.split(":")162                if len(parts) == 2 and int(parts[0]) == chapter:163                    verse_nums.add(int(parts[1]))164165        verses = []166        for verse_num in sorted(verse_nums):167            ref = VerseReference(book=book, chapter=chapter, verse=verse_num)168            verses.append(self.load_verse(ref))169170        return verses171172    def load_book_metadata(self, book: str) -> BookMetadata | None:173        """Load metadata for a book."""174        path = self.metadata_dir / book / "book_context.json"175        data = self._load_json(path)176        if not data:177            return None178        return BookMetadata(**data)179180    def load_chapter_summary(self, book: str, chapter: int) -> ChapterSummary | None:181        """Load summary for a chapter."""182        path = self.metadata_dir / book / "chapter_summaries.json"183        data = self._load_json(path)184        chapter_data = data.get(str(chapter))185        if not chapter_data:186            return None187        return ChapterSummary(book=book, chapter=chapter, **chapter_data)188189    def load_glossary(self) -> list[GlossaryTerm]:190        """Load the theological term glossary.191192        Supports both v1 and v2 glossary formats, auto-converting v1 on load.193        """194        path = self.metadata_dir / "glossary.json"195        return load_glossary_any_version(path)196197    def get_book_chapters(self, book: str) -> list[int]:198        """Get list of chapter numbers for a book.199200        Determines chapters by scanning available source files.201        """202        chapters: set[int] = set()203204        # Check all source directories205        for source_path in [206            self.sources_dir / "greek" / f"{book}.json",207            self.sources_dir / "hebrew" / f"{book}.json",208            self.sources_dir / "japanese" / "meiji" / f"{book}.json",209            self.sources_dir / "english" / "kjv" / f"{book}.json",210        ]:211            data = self._load_json(source_path)212            for key in data:213                # str.split always yields at least one part.214                try:215                    chapters.add(int(key.split(":")[0]))216                except ValueError:217                    continue218219        return sorted(chapters)220