mirror of
https://github.com/lllin000/PaperForge.git
synced 2026-07-22 17:00:23 +00:00
113 lines
4.1 KiB
Python
113 lines
4.1 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
OCR_CORPUS = Path("D:/L/OB/Literature-hub/System/PaperForge/ocr")
|
|
|
|
|
|
def _load_json_path(key: str) -> Path | None:
|
|
return OCR_CORPUS / key / "json" / "result.json"
|
|
|
|
|
|
def _get_page_blocks(json_path: Path, page: int) -> list[dict]:
|
|
data = json.loads(json_path.read_text(encoding="utf-8"))
|
|
pageno = 0
|
|
for payload in data:
|
|
for res in payload.get("layoutParsingResults", []):
|
|
pageno += 1
|
|
if pageno != page:
|
|
continue
|
|
return res.get("prunedResult", {}).get("parsing_res_list", [])
|
|
return []
|
|
|
|
|
|
def test_fixture_interleaved_text_reordered() -> None:
|
|
json_path = _load_json_path("2GN9LMCW")
|
|
assert json_path and json_path.exists(), f"fixture not found: {json_path}"
|
|
|
|
blocks = _get_page_blocks(json_path, 11)
|
|
|
|
from paperforge.worker.ocr import block_sort_key, validate_block_order
|
|
from paperforge.worker.ocr_orchestrator import reorder_blocks_layered
|
|
|
|
sorted_blocks = sorted(blocks, key=block_sort_key)
|
|
validated = validate_block_order(sorted_blocks, 1200)
|
|
layered = reorder_blocks_layered(validated, page_width=1200, page_height=1600)
|
|
|
|
body_labels = {"text", "paragraph_title", "abstract"}
|
|
body_ordered = [b for b in layered if b.get("block_label", "") in body_labels]
|
|
|
|
assert len(body_ordered) >= 4, f"too few body blocks: {len(body_ordered)}"
|
|
|
|
left_count = 0
|
|
right_count = 0
|
|
prev_xc = None
|
|
mid = 600
|
|
for b in body_ordered:
|
|
bb = b.get("block_bbox", [0, 0, 0, 0])
|
|
xc = (bb[0] + bb[2]) / 2 if bb[2] > bb[0] else None
|
|
if xc is None:
|
|
continue
|
|
if xc < mid:
|
|
left_count += 1
|
|
if prev_xc is not None and prev_xc >= mid:
|
|
pass
|
|
else:
|
|
right_count += 1
|
|
prev_xc = xc
|
|
|
|
assert left_count >= 1
|
|
assert right_count >= 1
|
|
|
|
|
|
def test_session_regression_heading_retained() -> None:
|
|
json_path = _load_json_path("7C8829BD")
|
|
assert json_path and json_path.exists(), f"fixture not found: {json_path}"
|
|
|
|
blocks = _get_page_blocks(json_path, 7)
|
|
|
|
from paperforge.worker.ocr import block_sort_key, validate_block_order
|
|
from paperforge.worker.ocr_orchestrator import reorder_blocks_layered
|
|
|
|
sorted_blocks = sorted(blocks, key=block_sort_key)
|
|
validated = validate_block_order(sorted_blocks, 1191)
|
|
layered = reorder_blocks_layered(validated, page_width=1191, page_height=1684)
|
|
|
|
headings_found = []
|
|
for b in layered:
|
|
if b.get("block_label") == "paragraph_title":
|
|
headings_found.append(b.get("block_content", ""))
|
|
|
|
assert any("5 In vitro PEMF studies" in h for h in headings_found), f"missing 5 heading, found: {headings_found}"
|
|
assert any("5.1 Bone" in h for h in headings_found), f"missing 5.1 heading, found: {headings_found}"
|
|
|
|
heading_order = [h for h in headings_found if "5" in h or "6" in h or "7" in h]
|
|
heading_positions = {h: i for i, h in enumerate(headings_found)}
|
|
|
|
if "5.4" in str(headings_found) and "6 In vivo" in str(headings_found):
|
|
idx_54 = next(i for i, h in enumerate(headings_found) if "5.4" in h)
|
|
idx_6 = next(i for i, h in enumerate(headings_found) if "6 In vivo" in h)
|
|
assert idx_54 < idx_6, "5.4 must come before 6"
|
|
|
|
|
|
def test_session_regression_figure_not_in_unrelated_section() -> None:
|
|
json_path = _load_json_path("7C8829BD")
|
|
assert json_path and json_path.exists(), f"fixture not found: {json_path}"
|
|
|
|
blocks = _get_page_blocks(json_path, 14)
|
|
|
|
from paperforge.worker.ocr import block_sort_key, validate_block_order
|
|
from paperforge.worker.ocr_orchestrator import reorder_blocks_layered
|
|
|
|
sorted_blocks = sorted(blocks, key=block_sort_key)
|
|
validated = validate_block_order(sorted_blocks, 1191)
|
|
layered = reorder_blocks_layered(validated, page_width=1191, page_height=1684)
|
|
|
|
body_labels = {"text", "paragraph_title", "abstract"}
|
|
for b in layered:
|
|
content = b.get("block_content", "")
|
|
if "cartilage" in content.lower() and "distribution" in content.lower():
|
|
continue
|
|
|
|
assert True
|