108 lines
2.9 KiB
Python
108 lines
2.9 KiB
Python
"""In-memory representation of a read document: pages, positioned text
|
|
lines and embedded images. Every reader produces this shape, so the
|
|
segmentation and NLP layers never care whether a page came from a PDF text
|
|
layer, Tesseract, or a scanned TIFF."""
|
|
from dataclasses import dataclass, field
|
|
from typing import Literal
|
|
|
|
PageSource = Literal["text", "ocr"]
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class BBox:
|
|
"""Normalized to the page (0..1, origin top-left), so coordinates are
|
|
comparable across renders at any DPI."""
|
|
|
|
x0: float
|
|
y0: float
|
|
x1: float
|
|
y1: float
|
|
|
|
@property
|
|
def area(self) -> float:
|
|
return max(0.0, self.x1 - self.x0) * max(0.0, self.y1 - self.y0)
|
|
|
|
def as_list(self) -> list[float]:
|
|
return [round(self.x0, 4), round(self.y0, 4), round(self.x1, 4), round(self.y1, 4)]
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Word:
|
|
"""One word of a line with its horizontal extent on the page (0..1)."""
|
|
|
|
text: str
|
|
x0: float
|
|
x1: float
|
|
|
|
@property
|
|
def center(self) -> float:
|
|
return (self.x0 + self.x1) / 2
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Line:
|
|
"""`words`, when the reader knows them, are the line's words in order;
|
|
`text` is then exactly their texts joined by single spaces, so a
|
|
character offset in `text` maps to a position on the page."""
|
|
|
|
text: str
|
|
bbox: BBox
|
|
confidence: float = 1.0
|
|
words: tuple[Word, ...] = ()
|
|
|
|
def x_at(self, offset: int) -> float | None:
|
|
"""Left edge of the word holding character `offset` of `text`."""
|
|
position = 0
|
|
for word in self.words:
|
|
end = position + len(word.text)
|
|
if offset < end + 1:
|
|
return word.x0
|
|
position = end + 1
|
|
return None
|
|
|
|
def text_between(self, x0: float, x1: float) -> str:
|
|
"""The words whose center falls in `[x0, x1)`: one column's share of a line."""
|
|
return " ".join(word.text for word in self.words if x0 <= word.center < x1)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class EmbeddedImage:
|
|
page_number: int
|
|
bbox: BBox
|
|
width_px: int | None = None
|
|
height_px: int | None = None
|
|
|
|
@property
|
|
def kind(self) -> str:
|
|
"""`scan` when the image is the page itself (a scanned sheet), `figure` otherwise."""
|
|
return "scan" if self.bbox.area >= 0.8 else "figure"
|
|
|
|
|
|
@dataclass
|
|
class Page:
|
|
number: int # 1-indexed
|
|
lines: list[Line]
|
|
source: PageSource
|
|
images: list[EmbeddedImage] = field(default_factory=list)
|
|
|
|
@property
|
|
def text(self) -> str:
|
|
return "\n".join(line.text for line in self.lines)
|
|
|
|
|
|
@dataclass
|
|
class Document:
|
|
name: str
|
|
media_type: str
|
|
pages: list[Page]
|
|
raw: bytes = field(repr=False, default=b"")
|
|
|
|
@property
|
|
def page_count(self) -> int:
|
|
return len(self.pages)
|
|
|
|
def page(self, number: int) -> Page:
|
|
if not 1 <= number <= len(self.pages):
|
|
raise IndexError(f"page {number} out of range (1..{len(self.pages)})")
|
|
return self.pages[number - 1]
|