File size: 8,630 Bytes
f9bc0c1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | from __future__ import annotations
import importlib.util
import json
import sys
from argparse import Namespace
from pathlib import Path
def _load_tool():
path = Path(__file__).resolve().parents[1] / "tools" / "apply_page_reading_order_geometry_profile.py"
spec = importlib.util.spec_from_file_location("apply_page_reading_order_geometry_profile", path)
assert spec is not None
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def _write_document(root: Path) -> None:
doc_dir = root / "sample-1"
doc_dir.mkdir(parents=True)
lines = [
("right-low", "right low", [500, 100, 40, 10]),
("left-top", "left top", [100, 10, 40, 10]),
("right-top", "right top", [500, 10, 40, 10]),
]
document_lines = [
{"line_id": line_id, "page_index": 0, "text": text, "bbox": bbox, "confidence": 0.9, "metadata": {}}
for line_id, text, bbox in lines
]
blocks = [
{
"block_id": f"block-{index}",
"block_type": "text",
"page_index": 0,
"bbox": bbox,
"order": index,
"text": text,
"confidence": 0.9,
"line_ids": [line_id],
"table_id": None,
"figure_id": None,
"metadata": {},
}
for index, (line_id, text, bbox) in enumerate(lines, start=1)
]
payload = {
"schema_version": "1.0",
"source_path": "page.png",
"tables": [],
"figures": [],
"metadata": {},
"pages": [{"page_index": 0, "width": 800, "height": 200, "metadata": {}, "text_lines": document_lines, "blocks": blocks}],
}
(doc_dir / "document.json").write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
def _write_document_with_sample(root: Path, sample_id: str) -> None:
doc_dir = root / sample_id
doc_dir.mkdir(parents=True)
lines = [
("a", f"{sample_id} a", [100, 10, 40, 10]),
("b", f"{sample_id} b", [500, 10, 40, 10]),
]
payload = {
"schema_version": "1.0",
"source_path": f"{sample_id}.png",
"tables": [],
"figures": [],
"metadata": {},
"pages": [
{
"page_index": 0,
"width": 800,
"height": 200,
"metadata": {},
"text_lines": [
{"line_id": line_id, "page_index": 0, "text": text, "bbox": bbox, "confidence": 0.9, "metadata": {}}
for line_id, text, bbox in lines
],
"blocks": [
{
"block_id": f"block-{index}",
"block_type": "text",
"page_index": 0,
"bbox": bbox,
"order": index,
"text": text,
"confidence": 0.9,
"line_ids": [line_id],
"table_id": None,
"figure_id": None,
"metadata": {},
}
for index, (line_id, text, bbox) in enumerate(lines, start=1)
],
}
],
}
(doc_dir / "document.json").write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
def test_apply_page_reading_order_geometry_profile_orders_by_chunks(tmp_path: Path) -> None:
tool = _load_tool()
source = tmp_path / "docs"
_write_document(source)
profile = tmp_path / "profile.json"
profile.write_text(
json.dumps(
{
"chunks": [
{"chunk_index": 1, "cx_median": 520, "y_min": 0, "y_max": 120, "width_median": 40},
{"chunk_index": 2, "cx_median": 120, "y_min": 0, "y_max": 120, "width_median": 40},
]
},
ensure_ascii=False,
)
+ "\n",
encoding="utf-8",
)
report = tool.apply_profile(
Namespace(
source_documents_dir=source,
profile=profile,
out=tmp_path / "out",
x_scale=1.0,
y_scale=80.0,
outside_weight=2.0,
)
)
assert report["assignment_count"] == 3
document = json.loads((tmp_path / "out" / "sample-1" / "document.json").read_text(encoding="utf-8"))
page = document["pages"][0]
assert [line["text"] for line in page["text_lines"]] == ["right top", "right low", "left top"]
assert page["text_lines"][0]["metadata"]["reading_order_geometry_profile_assignment"]["chunk_index"] == 1
assert (tmp_path / "out" / "reading-order-geometry-profile-assignments.jsonl").is_file()
def test_apply_page_reading_order_geometry_profile_requires_claim_safe_when_requested(tmp_path: Path) -> None:
tool = _load_tool()
source = tmp_path / "docs"
_write_document(source)
profile = tmp_path / "profile.json"
profile.write_text(
json.dumps(
{
"profile_claim_safe": False,
"chunks": [
{"chunk_index": 1, "cx_median": 520, "y_min": 0, "y_max": 120, "width_median": 40},
],
},
ensure_ascii=False,
)
+ "\n",
encoding="utf-8",
)
try:
tool.apply_profile(
Namespace(
source_documents_dir=source,
profile=profile,
out=tmp_path / "out",
x_scale=1.0,
y_scale=80.0,
outside_weight=2.0,
require_profile_claim_safe=True,
)
)
except ValueError as exc:
assert "profile_claim_safe is not true" in str(exc)
else:
raise AssertionError("unsafe profile should be rejected when claim-safe profile is required")
def test_apply_page_reading_order_geometry_profile_uses_profile_map_per_sample(tmp_path: Path) -> None:
tool = _load_tool()
source = tmp_path / "docs"
_write_document_with_sample(source, "left-first")
_write_document_with_sample(source, "right-first")
left_profile = tmp_path / "left-profile.json"
right_profile = tmp_path / "right-profile.json"
left_profile.write_text(
json.dumps(
{
"profile_claim_safe": True,
"chunks": [
{"chunk_index": 1, "cx_median": 120, "y_min": 0, "y_max": 100, "width_median": 40},
{"chunk_index": 2, "cx_median": 520, "y_min": 0, "y_max": 100, "width_median": 40},
],
},
ensure_ascii=False,
)
+ "\n",
encoding="utf-8",
)
right_profile.write_text(
json.dumps(
{
"profile_claim_safe": True,
"chunks": [
{"chunk_index": 1, "cx_median": 520, "y_min": 0, "y_max": 100, "width_median": 40},
{"chunk_index": 2, "cx_median": 120, "y_min": 0, "y_max": 100, "width_median": 40},
],
},
ensure_ascii=False,
)
+ "\n",
encoding="utf-8",
)
profile_map = tmp_path / "profile-map.json"
profile_map.write_text(
json.dumps(
{
"profiles": [
{"sample_id": "left-first", "profile": str(left_profile)},
{"sample_id": "right-first", "profile": str(right_profile)},
]
},
ensure_ascii=False,
indent=2,
)
+ "\n",
encoding="utf-8",
)
report = tool.apply_profile(
Namespace(
source_documents_dir=source,
profile=None,
profile_map=profile_map,
out=tmp_path / "out",
x_scale=1.0,
y_scale=80.0,
outside_weight=2.0,
require_profile_claim_safe=True,
)
)
assert report["profile_claim_safe"] is True
assert len(report["profile_usage"]) == 2
left_doc = json.loads((tmp_path / "out" / "left-first" / "document.json").read_text(encoding="utf-8"))
right_doc = json.loads((tmp_path / "out" / "right-first" / "document.json").read_text(encoding="utf-8"))
assert [line["text"] for line in left_doc["pages"][0]["text_lines"]] == ["left-first a", "left-first b"]
assert [line["text"] for line in right_doc["pages"][0]["text_lines"]] == ["right-first b", "right-first a"]
|