Skip to content

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

feat(textbook): rebuild chapters from running headers embedded mid-text

interia4a01a8cffc66
diff --git a/deeptutor/textbook_struct/page_headers.py b/deeptutor/textbook_struct/page_headers.py
index 7d8428677..d08fe81ea 100644
--- a/deeptutor/textbook_struct/page_headers.py
+++ b/deeptutor/textbook_struct/page_headers.py
@@ -13,6 +13,8 @@ physical page offset per chapter instead of guessing it.
from __future__ import annotations
+import re
+
from .chapter_rebuild import CHAPTER_RE, Chapter, assign_page_ranges
@@ -22,8 +24,37 @@ def _block_text(block: dict) -> str:
).strip()
+#: Running-header variants that embed the chapter token mid-text — some
+#: publishers print the chapter name in the running *header* (embedded
+#: mid-text, e.g. “集合第1章”, “空间向量与立体几何 第6章”), often without a
+#: leading “第”; others print it in the footer. Header variants are extracted
+#: and normalized to the canonical “第N章 章名” shape so the level filter and
+#: dedupe see the same vocabulary the footer path produces.
+HEADER_CHAPTER_RE = re.compile(
+ r"^(?P<pre>.{0,20}?)\s*第\s*(?P<num>[一二三四五六七八九十百\d]+)\s*"
+ r"(?P<unit>[课章节单元])\s*(?P<post>.{0,30}?)\s*$"
+)
+
+
+def normalize_header_chapter(text: str) -> str | None:
+ """Normalize a mid-text chapter header to “第N单元 章名” (None = no match)."""
+ m = HEADER_CHAPTER_RE.match(text)
+ if not m:
+ return None
+ name = (m.group("pre") or m.group("post") or "").strip()
+ token = f"第{m.group('num')}{m.group('unit')}"
+ return f"{token} {name}" if name else token
+
+
def page_facts(page: dict) -> tuple[list[str], str]:
- """Return ``(chapter_shaped_footers, printed_page_number)`` for one page."""
+ """Return ``(chapter_shaped_footers, printed_page_number)`` for one page.
+
+ Chapter-shaped furniture is read from *both* running-header positions:
+ footer blocks matching ``CHAPTER_RE`` verbatim (publishers that print the
+ chapter name in the footer) and header blocks whose chapter token sits
+ mid-text (publishers that embed it in the header, normalized via
+ :func:`normalize_header_chapter`).
+ """
footers: list[str] = []
printed = ""
for block in page.get("discarded_blocks", []):
@@ -33,6 +64,10 @@ def page_facts(page: dict) -> tuple[list[str], str]:
btype = block.get("type")
if btype == "footer" and CHAPTER_RE.match(text):
footers.append(text)
+ elif btype == "header":
+ normalized = normalize_header_chapter(text)
+ if normalized is not None:
+ footers.append(normalized)
elif btype == "page_number" and text.isdigit():
printed = text
return footers, printed
diff --git a/tests/textbook_struct/test_page_headers.py b/tests/textbook_struct/test_page_headers.py
new file mode 100644
index 000000000..835cb1094
--- /dev/null
+++ b/tests/textbook_struct/test_page_headers.py
@@ -0,0 +1,129 @@
+"""Running-header chapter rebuild: footer + header channels both.
+
+Some textbooks print the chapter name embedded mid-text in the running
+*header* (e.g. “集合第1章” / “空间向量与立体几何 第6章”) while the footer
+only carries the publisher name — a footer-only pass then finds 0 chapters.
+Header-embedded chapter tokens are extracted and normalized to the
+“第N章 章名” shape, sharing the level filter and seen-dedupe with the
+footer path.
+"""
+
+from __future__ import annotations
+
+from deeptutor.textbook_struct.chapter_rebuild import rebuild_from_headers_level
+from deeptutor.textbook_struct.page_headers import (
+ normalize_header_chapter,
+ page_facts,
+)
+
+
+def _page(page_idx: int, discarded: list[dict]) -> dict:
+ return {"page_idx": page_idx, "discarded_blocks": discarded}
+
+
+def _footer(text: str) -> dict:
+ return {"type": "footer", "lines": [{"spans": [{"content": text}]}]}
+
+
+def _header(text: str) -> dict:
+ return {"type": "header", "lines": [{"spans": [{"content": text}]}]}
+
+
+def _page_number(n: str) -> dict:
+ return {"type": "page_number", "lines": [{"spans": [{"content": n}]}]}
+
+
+# ── normalize: three header forms + mismatch guards ──────────────────────
+
+
+def test_normalize_embedded_chapter_name_before_token() -> None:
+ assert normalize_header_chapter("集合第1章") == "第1章 集合"
+
+
+def test_normalize_embedded_chapter_name_after_token() -> None:
+ assert normalize_header_chapter("空间向量与立体几何 第6章") == "第6章 空间向量与立体几何"
+
+
+def test_normalize_bare_token() -> None:
+ assert normalize_header_chapter("第6章") == "第6章"
+
+
+def test_normalize_leading_token_keeps_footer_shape() -> None:
+ assert normalize_header_chapter("第6章 空间向量与立体几何") == "第6章 空间向量与立体几何"
+
+
+def test_normalize_rejects_book_title_and_prose() -> None:
+ # Book-title-with-year, a plain header without a chapter token, and long
+ # prose are not chapter boundaries.
+ assert normalize_header_chapter("高中数学人教A版2019") is None
+ assert normalize_header_chapter("数学·必修第一册") is None
+ assert normalize_header_chapter("a" * 60) is None
+
+
+# ── page_facts: dual-channel collection ──────────────────────────────────
+
+
+def test_page_facts_reads_footer_verbatim() -> None:
+ footers, printed = page_facts(
+ _page(3, [_footer("第一章 集合与常用逻辑用语"), _page_number("12")])
+ )
+ assert footers == ["第一章 集合与常用逻辑用语"]
+ assert printed == "12"
+
+
+def test_page_facts_reads_header_normalized() -> None:
+ footers, printed = page_facts(_page(4, [_header("集合第1章"), _page_number("13")]))
+ assert footers == ["第1章 集合"]
+ assert printed == "13"
+
+
+# ── rebuild_from_headers_level: header-driven chapter build, end to end ───
+
+
+def _header_layout() -> dict:
+ """Header layout: chapter name in the running header, each chapter
+ starting on its own page (printed numbers enable offset checking).
+
+ physical (1-based) − printed page number stays constant at 1 across the
+ book (the cover prints no page number; the body starts at printed 2).
+ """
+ return {
+ "pdf_info": [
+ _page(0, [_header("高中数学·必修第一册"), _page_number("1")]), # cover
+ _page(1, [_header("集合与常用逻辑用语 第1章"), _page_number("2")]),
+ _page(2, [_header("集合与常用逻辑用语 第1章"), _page_number("3")]),
+ _page(19, [_header("一元二次函数、方程和不等式 第2章"), _page_number("20")]),
+ ]
+ }
+
+
+def test_rebuild_level_builds_chapters_from_headers() -> None:
+ chapters = rebuild_from_headers_level(_header_layout(), unit="章")
+ assert [c.title for c in chapters] == [
+ "第1章 集合与常用逻辑用语",
+ "第2章 一元二次函数、方程和不等式",
+ ]
+ assert [c.page_idx for c in chapters] == [1, 19]
+ 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.
+ assert verify_offsets_ok(chapters)
+
+
+def verify_offsets_ok(chapters) -> bool:
+ from deeptutor.textbook_struct.chapter_rebuild import verify_offset
+
+ return verify_offset(chapters)["ok"]
+
+
+def test_rebuild_level_unit_filter_drops_other_levels() -> None:
+ # With two-level running headers (rotating “第X章/第Y节” pairs),
+ # unit="节" only follows section-level changes.
+ layout = {
+ "pdf_info": [
+ _page(0, [_header("函数 第2章"), _header("函数的概念 第1节"), _page_number("5")]),
+ _page(1, [_header("函数 第2章"), _header("函数的表示法 第2节"), _page_number("9")]),
+ ]
+ }
+ chapters = rebuild_from_headers_level(layout, unit="节")
+ assert [c.title for c in chapters] == ["第1节 函数的概念", "第2节 函数的表示法"]