Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(textbook): expose header rebuild and preserve final page range

Bingxi Zhao (Frank)c323516312f0
diff --git a/deeptutor/textbook_struct/__init__.py b/deeptutor/textbook_struct/__init__.py
index 9e81b203c..6adde8142 100644
--- a/deeptutor/textbook_struct/__init__.py
+++ b/deeptutor/textbook_struct/__init__.py
@@ -6,11 +6,15 @@ adjacent merge), zero LLM.
Nothing in this package imports deeptutor.
"""
-from .chapter_rebuild import Chapter, rebuild
+from .chapter_rebuild import Chapter, rebuild, rebuild_from_headers_level, verify_offset
from .column_blacklist import COLUMN_BLACKLIST
+from .page_headers import rebuild_from_headers
__all__ = [
"Chapter",
"rebuild",
+ "rebuild_from_headers",
+ "rebuild_from_headers_level",
+ "verify_offset",
"COLUMN_BLACKLIST",
]
diff --git a/deeptutor/textbook_struct/chapter_rebuild.py b/deeptutor/textbook_struct/chapter_rebuild.py
index 6ab2fefa7..412c9c693 100644
--- a/deeptutor/textbook_struct/chapter_rebuild.py
+++ b/deeptutor/textbook_struct/chapter_rebuild.py
@@ -72,6 +72,12 @@ def merge_adjacent(blocks: list[dict], gap: float = 40.0) -> list[dict]:
return merged
+def layout_page_count(layout: dict) -> int:
+ """Exclusive upper page bound, including sparse physical page indices."""
+ pages = layout.get("pdf_info", [])
+ return max([len(pages), *(page["page_idx"] + 1 for page in pages)])
+
+
def rebuild(
layout: dict,
*,
@@ -100,7 +106,7 @@ def rebuild(
)
)
chapters = _dedupe_keep_last(chapters) # TOC page duplicates body hits — keep body
- return assign_page_ranges(chapters, page_count=len(layout.get("pdf_info", [])))
+ return assign_page_ranges(chapters, page_count=layout_page_count(layout))
def _dedupe_keep_last(chapters: list[Chapter]) -> list[Chapter]:
@@ -117,12 +123,13 @@ def _dedupe_keep_last(chapters: list[Chapter]) -> list[Chapter]:
def assign_page_ranges(chapters: list[Chapter], *, page_count: int) -> list[Chapter]:
- """Chapter i spans [start_i, start_{i+1}); the last runs to the last page."""
+ """Return half-open page ranges, including the final physical page."""
+ chapters.sort(key=lambda chapter: chapter.page_idx)
for i, chapter in enumerate(chapters):
chapter.end_page_idx = (
chapters[i + 1].page_idx
if i + 1 < len(chapters)
- else max(page_count - 1, chapter.page_idx)
+ else max(page_count, chapter.page_idx + 1)
)
return chapters
@@ -211,7 +218,7 @@ def rebuild_from_headers_level(layout: dict, unit: str = "课") -> list[Chapter]
raise ValueError(f"unknown unit: {unit}")
chapters: list[Chapter] = []
seen: set[str] = set()
- page_count = len(layout.get("pdf_info", []))
+ page_count = layout_page_count(layout)
for page in layout.get("pdf_info", []):
footers, printed = page_facts(page)
for title in footers:
diff --git a/deeptutor/textbook_struct/page_headers.py b/deeptutor/textbook_struct/page_headers.py
index d08fe81ea..adcc122eb 100644
--- a/deeptutor/textbook_struct/page_headers.py
+++ b/deeptutor/textbook_struct/page_headers.py
@@ -15,7 +15,7 @@ from __future__ import annotations
import re
-from .chapter_rebuild import CHAPTER_RE, Chapter, assign_page_ranges
+from .chapter_rebuild import CHAPTER_RE, Chapter, assign_page_ranges, layout_page_count
def _block_text(block: dict) -> str:
@@ -82,7 +82,7 @@ def rebuild_from_headers(layout: dict) -> list[Chapter]:
"""
chapters: list[Chapter] = []
seen: set[str] = set()
- page_count = len(layout.get("pdf_info", []))
+ page_count = layout_page_count(layout)
for page in layout.get("pdf_info", []):
footers, printed = page_facts(page)
for title in footers:
diff --git a/tests/textbook_struct/test_page_headers.py b/tests/textbook_struct/test_page_headers.py
index 835cb1094..d6dc4c265 100644
--- a/tests/textbook_struct/test_page_headers.py
+++ b/tests/textbook_struct/test_page_headers.py
@@ -10,7 +10,7 @@ footer path.
from __future__ import annotations
-from deeptutor.textbook_struct.chapter_rebuild import rebuild_from_headers_level
+from deeptutor.textbook_struct import rebuild_from_headers_level, verify_offset
from deeptutor.textbook_struct.page_headers import (
normalize_header_chapter,
page_facts,
@@ -104,6 +104,8 @@ def test_rebuild_level_builds_chapters_from_headers() -> None:
"第2章 一元二次函数、方程和不等式",
]
assert [c.page_idx for c in chapters] == [1, 19]
+ # Ranges are half-open, even when layout pages have sparse indices.
+ assert [c.end_page_idx for c in chapters] == [19, 20]
assert chapters[0].meta["printed_page"] == 2
assert chapters[1].meta["printed_page"] == 20
# Constant offset: physical page − printed page = 1 across the whole book.
@@ -111,8 +113,6 @@ def test_rebuild_level_builds_chapters_from_headers() -> None:
def verify_offsets_ok(chapters) -> bool:
- from deeptutor.textbook_struct.chapter_rebuild import verify_offset
-
return verify_offset(chapters)["ok"]