Files
DeepTutor/deeptutor/textbook_struct/chapter_rebuild.py
Antonio-Faure 63ef9b7c2c chore: remove dead Python modules and helpers (#2253)
* chore: remove dead Python modules and helpers

Delete unreferenced P0 code identified by the dead-code audit:
- services/llm/registry.py and its test-only coverage
- logging/adapters package (LlamaIndex log forwarding)
- textbook_struct.chapter_rebuild.detect_frames
- load_config_with_main_async and its async file helper
- memory.trace.iter_by_ids / latest_ts
- TokenTracker.format_summary
- WeixinChannel._api_get
- ArxivSearchTool.extract_arxiv_id_from_url
- FileLibraryStore migration scaffolding (_run_migrations et al.)
- eight small dead helpers in services, api/utils, and knowledge
- redundant runtime C2CMessage import in the QQ channel

* chore: drop the prompt-language shim and a dead storage helper

The book agents were the last in-tree users of blocks/_language.py, which
only re-exported services.prompt.language. Import the real module and
delete the shim. storage._storage_path_from_version_entry had no caller.

Verified: ruff F401/F811/F841/F821 on changed files; py_compile.

---------

Co-authored-by: Antonio-Faure <209759495+Antonio-Faure@users.noreply.github.com>
2026-12-02 10:50:16 +08:00

218 lines
8.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Layered chapter rebuild from MinerU ``layout.json``.
Layers (all deterministic, no LLM):
0. column blacklist — feature-column names are never chapters (closed vocabulary)
1. regex — ``第X课/章/节/单元`` headings (works regardless of height)
2. position filter — heading must sit in the page-top band (y0 < top)
3. adjacent merge — same-page title blocks split by line-wrap rejoin
Input is the MinerU layout dict (``layout["pdf_info"]``), one page object per
page with ``page_idx`` / ``para_blocks``; blocks carry ``type`` / ``bbox`` /
``lines[].spans[].content``.
"""
from __future__ import annotations
from dataclasses import dataclass, field
import re
from .column_blacklist import COLUMN_BLACKLIST
CHAPTER_RE = re.compile(r"^第\s*[一二三四五六七八九十百\d]+\s*[课章节单元]")
# Title blocks whose text is shorter than this are layout noise (figure
# captions etc.) — never chapters even if they regex-match.
MIN_TITLE_CHARS = 4
@dataclass
class Chapter:
"""One rebuilt chapter: title + start page (0-based) + bbox on that page."""
title: str
page_idx: int
bbox: list[float]
end_page_idx: int | None = None # filled by assign_page_ranges
level: int = 1 # reserved: 1=lesson/chapter (课/章), 2=section (框/节)
meta: dict = field(default_factory=dict)
def block_text(block: dict) -> str:
return "".join(
span.get("content", "") for line in block.get("lines", []) for span in line.get("spans", [])
).strip()
def merge_adjacent(blocks: list[dict], gap: float = 40.0) -> list[dict]:
"""Rejoin same-page title blocks split by line-wrap.
Sort by y0; vertical gap < ``gap`` with x-overlap merges into the first
block (bbox union, text concat). Mutates nothing — returns new dicts.
"""
merged: list[dict] = []
for block in sorted(blocks, key=lambda b: b["bbox"][1]):
if merged:
prev = merged[-1]
vgap = block["bbox"][1] - prev["bbox"][3]
x_overlap = min(block["bbox"][2], prev["bbox"][2]) - max(
block["bbox"][0], prev["bbox"][0]
)
if vgap < gap and x_overlap > 0:
prev["bbox"] = [
min(prev["bbox"][0], block["bbox"][0]),
min(prev["bbox"][1], block["bbox"][1]),
max(prev["bbox"][2], block["bbox"][2]),
max(prev["bbox"][3], block["bbox"][3]),
]
prev["text"] = (prev.get("text", "") + block_text(block)).strip()
continue
item = dict(block)
item["text"] = block_text(block)
merged.append(item)
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,
*,
top_band: float = 120.0,
merge_gap: float = 40.0,
) -> list[Chapter]:
"""Run the layered pipeline over ``layout`` and return chapter starts."""
chapters: list[Chapter] = []
for page in layout.get("pdf_info", []):
title_blocks = [b for b in page.get("para_blocks", []) if b.get("type") == "title"]
for block in merge_adjacent(title_blocks, gap=merge_gap):
text = block.get("text", "")
if text in COLUMN_BLACKLIST: # layer 0 — closed column names
continue
if not CHAPTER_RE.match(text): # layer 1 — regex first
continue
if len(text.replace(" ", "")) < MIN_TITLE_CHARS:
continue
if block["bbox"][1] >= top_band: # layer 2 — page-top band
continue
chapters.append(
Chapter(
title=text,
page_idx=page["page_idx"],
bbox=list(block["bbox"]),
)
)
chapters = _dedupe_keep_last(chapters) # TOC page duplicates body hits — keep body
return assign_page_ranges(chapters, page_count=layout_page_count(layout))
def _dedupe_keep_last(chapters: list[Chapter]) -> list[Chapter]:
"""Same title appearing on TOC page AND in the body: keep the body hit."""
by_title: dict[str, Chapter] = {}
order: list[str] = []
for chapter in chapters:
if chapter.title in by_title:
# Keep the body hit (higher page). Comparing page_idx holds even when
# pdf_info is not ascending by page; relying on list order did not.
if chapter.page_idx > by_title[chapter.title].page_idx:
by_title[chapter.title] = chapter
else:
by_title[chapter.title] = chapter
order.append(chapter.title)
return [by_title[t] for t in order]
def assign_page_ranges(chapters: list[Chapter], *, page_count: int) -> list[Chapter]:
"""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 = (
max(chapters[i + 1].page_idx, chapter.page_idx + 1)
if i + 1 < len(chapters)
else max(page_count, chapter.page_idx + 1)
)
return chapters
# ── Level-aware boundaries + printed-offset consistency check ──────────────
UNIT_RE_MAP = {
"课": re.compile(r"^第\s*[一二三四五六七八九十百\d]+\s*课"),
"章": re.compile(r"^第\s*[一二三四五六七八九十百\d]+\s*章"),
"节": re.compile(r"^第\s*[一二三四五六七八九十百\d]+\s*节"),
"单元": re.compile(r"^第\s*[一二三四五六七八九十百\d]+\s*单元"),
}
PARENT_UNITS = {
"课": ("单元", "章"),
"章": ("单元",),
"节": ("单元", "章"),
"单元": (),
}
def rebuild_from_headers_level(layout: dict, unit: str = "课") -> list[Chapter]:
"""Level-aware rebuild: only ``unit``-level running-header changes mark boundaries.
In textbooks with two-level running headers (e.g. rotating
"第X章/第Y节" pairs), a change at the other level must not start a
chapter — otherwise pseudo-chapters get split out.
"""
# Imported here to avoid a circular import (page_headers imports this
# module's CHAPTER_RE / Chapter / assign_page_ranges at top level).
from .page_headers import page_facts
unit_re = UNIT_RE_MAP.get(unit)
if unit_re is None:
raise ValueError(f"unknown unit: {unit}")
chapters: list[Chapter] = []
parent_titles: dict[str, str] = {}
last_boundary: tuple[str, ...] | None = None
page_count = layout_page_count(layout)
for page in sorted(layout.get("pdf_info", []), key=lambda item: item["page_idx"]):
footers, printed = page_facts(page)
for title in footers:
for parent_unit in PARENT_UNITS[unit]:
if UNIT_RE_MAP[parent_unit].match(title):
parent_titles[parent_unit] = title
if parent_unit == "单元":
parent_titles.pop("章", None)
for title in footers:
if not unit_re.match(title):
continue
boundary = (
*(parent_titles.get(parent_unit, "") for parent_unit in PARENT_UNITS[unit]),
title,
)
if boundary == last_boundary:
continue
last_boundary = boundary
chapters.append(
Chapter(
title=title,
page_idx=page["page_idx"],
bbox=[],
meta={"printed_page": int(printed) if printed.isdigit() else None},
)
)
return assign_page_ranges(chapters, page_count=page_count)
def verify_offset(chapters: list[Chapter]) -> dict:
"""Printed-offset consistency: physical (1-based) − printed page number.
An offset that varies across chapters means a chapter boundary was
mis-detected.
"""
offsets = sorted(
{c.page_idx + 1 - c.meta["printed_page"] for c in chapters if c.meta.get("printed_page")}
)
return {
"offsets": offsets,
"consistent": len(offsets) <= 1,
"ok": bool(offsets) and len(offsets) == 1,
}