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

OPEN SOURCE

software/src/context/pericope.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/context/pericope.py208 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 8a8ebc614e064c09adb83e193cd305d18faa1c818f69d280861dacc456ac6915
1"""Pericope management for chunk-based translation."""23from __future__ import annotations45import json6from pathlib import Path78from src.models.pericope import Pericope, PericopeIndex91011class PericopeManager:12    """Manages pericope data for chunk-based translation.1314    Loads pericope boundaries from JSON and provides lookup15    functionality to find the pericope containing any verse.16    """1718    def __init__(self, data_path: Path | str | None = None) -> None:19        """Initialize the pericope manager.2021        Args:22            data_path: Path to pericopes.json. If None, uses default.23        """24        if data_path is None:25            self._data_path = Path("data/metadata/pericopes.json")26        else:27            self._data_path = Path(data_path)2829        self._indexes: dict[str, PericopeIndex] = {}30        self._loaded = False3132    @property33    def data_path(self) -> Path:34        """Get the data file path."""35        return self._data_path3637    def _ensure_loaded(self) -> None:38        """Ensure pericope data is loaded."""39        if not self._loaded:40            self.load()4142    def load(self) -> None:43        """Load pericope data from JSON file."""44        self._indexes.clear()45        self._loaded = True4647        if not self._data_path.exists():48            return4950        with self._data_path.open("r", encoding="utf-8") as f:51            data = json.load(f)5253        for book_name, book_data in data.get("books", {}).items():54            pericopes = []55            for p_data in book_data.get("pericopes", []):56                pericopes.append(Pericope(**p_data))5758            self._indexes[book_name] = PericopeIndex(59                book=book_name,60                name=book_data.get("name", book_name.title()),61                pericopes=pericopes,62                sblgnt_paragraphs=book_data.get("sblgnt_paragraphs"),63            )6465    def reload(self) -> None:66        """Force reload from file."""67        self._loaded = False68        self.load()6970    def get_pericope(71        self, book: str, chapter: int, verse: int72    ) -> Pericope | None:73        """Get the pericope containing a verse.7475        Args:76            book: Book name.77            chapter: Chapter number.78            verse: Verse number.7980        Returns:81            The Pericope containing the verse, or None if not found.82        """83        self._ensure_loaded()8485        index = self._indexes.get(book.lower())86        if index is None:87            return None8889        return index.find_pericope(chapter, verse)9091    def get_pericope_by_id(self, pericope_id: str) -> tuple[str, Pericope] | None:92        """Get a pericope by its ID.9394        Args:95            pericope_id: The pericope identifier.9697        Returns:98            Tuple of (book_name, Pericope) if found, None otherwise.99        """100        self._ensure_loaded()101102        for book_name, index in self._indexes.items():103            pericope = index.get_pericope_by_id(pericope_id)104            if pericope:105                return (book_name, pericope)106107        return None108109    def get_book_pericopes(self, book: str) -> list[Pericope]:110        """Get all pericopes for a book.111112        Args:113            book: Book name.114115        Returns:116            List of pericopes for the book.117        """118        self._ensure_loaded()119120        index = self._indexes.get(book.lower())121        if index is None:122            return []123124        return index.pericopes125126    def get_chapter_pericopes(self, book: str, chapter: int) -> list[Pericope]:127        """Get all pericopes that include verses from a chapter.128129        Args:130            book: Book name.131            chapter: Chapter number.132133        Returns:134            List of pericopes containing verses from the chapter.135        """136        self._ensure_loaded()137138        index = self._indexes.get(book.lower())139        if index is None:140            return []141142        return index.get_chapter_pericopes(chapter)143144    def get_available_books(self) -> list[str]:145        """Get list of books with pericope data.146147        Returns:148            List of book names.149        """150        self._ensure_loaded()151        return list(self._indexes.keys())152153    def has_pericope_data(self, book: str) -> bool:154        """Check if pericope data exists for a book.155156        Args:157            book: Book name.158159        Returns:160            True if pericope data exists.161        """162        self._ensure_loaded()163        index = self._indexes.get(book.lower())164        return index is not None and len(index.pericopes) > 0165166    def get_book_index(self, book: str) -> PericopeIndex | None:167        """Get the full pericope index for a book.168169        Args:170            book: Book name.171172        Returns:173            PericopeIndex or None if not found.174        """175        self._ensure_loaded()176        return self._indexes.get(book.lower())177178    def get_statistics(self) -> dict:179        """Get statistics about loaded pericope data.180181        Returns:182            Dictionary with statistics.183        """184        self._ensure_loaded()185186        total_pericopes = 0187        total_verses = 0188        books_with_data = []189190        for book_name, index in self._indexes.items():191            if index.pericopes:192                books_with_data.append(book_name)193                total_pericopes += len(index.pericopes)194                for p in index.pericopes:195                    total_verses += p.verse_count196197        return {198            "books_with_data": len(books_with_data),199            "book_names": books_with_data,200            "total_pericopes": total_pericopes,201            "total_verses_covered": total_verses,202        }203204    def __len__(self) -> int:205        """Get total number of pericopes across all books."""206        self._ensure_loaded()207        return sum(len(idx.pericopes) for idx in self._indexes.values())208