File size: 8,792 Bytes
1ef1de7 | 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 | """Convert OCR lines into blocks, tables, figures, and reading order."""
from __future__ import annotations
import re
from .engines import EngineOutput
from .metrics import bbox_iou
from .normalization import normalize_ocr_text
from .schemas import Block, Document, Figure, Page, Table, TextLine, union_bbox
from .tables import infer_table_from_lines, looks_like_table_line
FIGURE_MARKER_RE = re.compile(r"^\s*(?:\[(?:figure|chart|image|चित्र)[^\]]*\]|(?:figure|fig\.?|chart|चित्र)\s*[::]?)", re.IGNORECASE)
CAPTION_RE = re.compile(r"^\s*(?:caption|figure|fig\.?|चित्र|तालिका)\s*[\s\d०-९IVXivx.-]*[::]", re.IGNORECASE)
def sort_lines_reading_order(lines: list[TextLine]) -> list[TextLine]:
if not lines:
return []
heights = [line.bbox.h for line in lines if line.bbox.h > 0]
line_height = sorted(heights)[len(heights) // 2] if heights else 20
bucket_size = max(8, line_height * 0.75)
return sorted(lines, key=lambda line: (round(line.bbox.y / bucket_size), line.bbox.x, line.bbox.y))
def build_document(source_path: str, output: EngineOutput) -> Document:
pages: list[Page] = []
tables = list(output.tables)
figures = list(output.figures)
for raw_page in output.pages:
sorted_lines = sort_lines_reading_order(raw_page.text_lines)
for index, line in enumerate(sorted_lines):
if line.line_id is None:
line.line_id = f"p{raw_page.page_index}-l{index}"
blocks: list[Block] = []
represented_figure_ids: set[str] = set()
order = 0
index = 0
while index < len(sorted_lines):
line = sorted_lines[index]
if looks_like_figure_marker(line.text):
marker_line = line
caption_line: TextLine | None = None
index += 1
if index < len(sorted_lines) and _looks_like_caption_after_marker(marker_line, sorted_lines[index]):
caption_line = sorted_lines[index]
index += 1
figure_lines = [marker_line] + ([caption_line] if caption_line is not None else [])
figure_id = f"p{raw_page.page_index}-f{len(figures)}"
caption = normalize_ocr_text(caption_line.text).strip() if caption_line is not None else None
summary = normalize_ocr_text(marker_line.text).strip()
figure = Figure(
figure_id=figure_id,
page_index=raw_page.page_index,
bbox=union_bbox([item.bbox for item in figure_lines]),
caption=caption,
summary=summary,
confidence=_average_confidence(figure_lines),
metadata={"line_ids": [item.line_id or "" for item in figure_lines]},
)
figures.append(figure)
represented_figure_ids.add(figure_id)
blocks.append(
Block(
block_id=f"p{raw_page.page_index}-b{order}",
block_type="figure",
page_index=raw_page.page_index,
bbox=figure.bbox,
order=order,
figure_id=figure_id,
confidence=figure.confidence,
line_ids=[item.line_id or "" for item in figure_lines],
text=caption or summary,
)
)
order += 1
continue
if looks_like_table_line(line.text):
table_lines = [line]
index += 1
while index < len(sorted_lines) and looks_like_table_line(sorted_lines[index].text):
table_lines.append(sorted_lines[index])
index += 1
table_id = f"p{raw_page.page_index}-t{len(tables)}"
table = infer_table_from_lines(table_lines, table_id)
existing_table = _find_overlapping_table(tables, table)
represented_table = existing_table or table
if existing_table is None:
tables.append(table)
blocks.append(
Block(
block_id=f"p{raw_page.page_index}-b{order}",
block_type="table",
page_index=raw_page.page_index,
bbox=represented_table.bbox,
order=order,
table_id=represented_table.table_id,
confidence=represented_table.confidence,
line_ids=[line.line_id or "" for line in table_lines],
)
)
order += 1
continue
text_lines = [line]
index += 1
while (
index < len(sorted_lines)
and not looks_like_table_line(sorted_lines[index].text)
and not looks_like_figure_marker(sorted_lines[index].text)
):
previous = text_lines[-1]
current = sorted_lines[index]
vertical_gap = current.bbox.y - previous.bbox.bottom
if vertical_gap > max(previous.bbox.h, 24) * 1.5:
break
text_lines.append(current)
index += 1
text = "\n".join(normalize_ocr_text(item.text).strip() for item in text_lines if item.text.strip())
if not text.strip():
continue
confidence = _average_confidence(text_lines)
block_type = "title" if order == 0 and len(text) <= 120 and len(text_lines) == 1 else "text"
blocks.append(
Block(
block_id=f"p{raw_page.page_index}-b{order}",
block_type=block_type,
page_index=raw_page.page_index,
bbox=union_bbox([item.bbox for item in text_lines]),
order=order,
text=text,
confidence=confidence,
line_ids=[item.line_id or "" for item in text_lines],
)
)
order += 1
for figure in [item for item in figures if item.page_index == raw_page.page_index and item.figure_id not in represented_figure_ids]:
blocks.append(
Block(
block_id=f"p{raw_page.page_index}-b{order}",
block_type="figure",
page_index=raw_page.page_index,
bbox=figure.bbox,
order=order,
figure_id=figure.figure_id,
confidence=figure.confidence,
text=figure.caption or figure.summary or "",
)
)
order += 1
blocks = sorted(blocks, key=lambda block: (block.bbox.y, block.bbox.x, block.order))
for order_index, block in enumerate(blocks):
block.order = order_index
pages.append(
Page(
page_index=raw_page.page_index,
width=raw_page.width,
height=raw_page.height,
text_lines=sorted_lines,
blocks=blocks,
metadata=raw_page.metadata,
)
)
return Document(source_path=source_path, pages=pages, tables=tables, figures=figures, metadata=dict(output.metadata))
def looks_like_figure_marker(text: str) -> bool:
return bool(FIGURE_MARKER_RE.match(text.strip()))
def looks_like_caption(text: str) -> bool:
return bool(CAPTION_RE.match(text.strip()))
def _looks_like_caption_after_marker(marker_line: TextLine, candidate: TextLine) -> bool:
if looks_like_caption(candidate.text):
return True
if looks_like_figure_marker(candidate.text) or looks_like_table_line(candidate.text):
return False
if not candidate.text.strip():
return False
vertical_gap = candidate.bbox.y - marker_line.bbox.bottom
max_gap = max(marker_line.bbox.h, candidate.bbox.h, 24) * 1.5
return 0 <= vertical_gap <= max_gap
def _find_overlapping_table(tables: list[Table], candidate: Table, *, iou_threshold: float = 0.9) -> Table | None:
for table in tables:
if table.page_index != candidate.page_index:
continue
if bbox_iou(table.bbox, candidate.bbox) >= iou_threshold:
return table
return None
def _average_confidence(lines: list[TextLine]) -> float | None:
confidences = [item.confidence for item in lines if item.confidence is not None]
return sum(confidences) / len(confidences) if confidences else None
|