Skip to content

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

fix(textbook): retain shared boundary pages in chapter spans

Bingxi Zhao (Frank)ad958a5cd77f
diff --git a/deeptutor/textbook_struct/chapter_rebuild.py b/deeptutor/textbook_struct/chapter_rebuild.py
index 412c9c693..4fd957195 100644
--- a/deeptutor/textbook_struct/chapter_rebuild.py
+++ b/deeptutor/textbook_struct/chapter_rebuild.py
@@ -127,7 +127,7 @@ def assign_page_ranges(chapters: list[Chapter], *, page_count: int) -> list[Chap
chapters.sort(key=lambda chapter: chapter.page_idx)
for i, chapter in enumerate(chapters):
chapter.end_page_idx = (
- chapters[i + 1].page_idx
+ max(chapters[i + 1].page_idx, chapter.page_idx + 1)
if i + 1 < len(chapters)
else max(page_count, chapter.page_idx + 1)
)
diff --git a/tests/textbook_struct/test_page_headers.py b/tests/textbook_struct/test_page_headers.py
index d6dc4c265..a3b97e1bd 100644
--- a/tests/textbook_struct/test_page_headers.py
+++ b/tests/textbook_struct/test_page_headers.py
@@ -10,7 +10,8 @@ footer path.
from __future__ import annotations
-from deeptutor.textbook_struct import rebuild_from_headers_level, verify_offset
+from deeptutor.textbook_struct import Chapter, rebuild_from_headers_level, verify_offset
+from deeptutor.textbook_struct.chapter_rebuild import assign_page_ranges
from deeptutor.textbook_struct.page_headers import (
normalize_header_chapter,
page_facts,
@@ -127,3 +128,11 @@ def test_rebuild_level_unit_filter_drops_other_levels() -> None:
}
chapters = rebuild_from_headers_level(layout, unit="节")
assert [c.title for c in chapters] == ["第1节 函数的概念", "第2节 函数的表示法"]
+
+
+def test_chapters_starting_on_one_page_both_include_that_page() -> None:
+ chapters = [Chapter("A", 2, []), Chapter("B", 2, [])]
+ assert [chapter.end_page_idx for chapter in assign_page_ranges(chapters, page_count=3)] == [
+ 3,
+ 3,
+ ]