| from __future__ import annotations |
|
|
| from dataclasses import dataclass |
| from typing import Any |
|
|
| from geometry import ( |
| Box, |
| polygon_area, |
| polygon_intersection, |
| point_in_convex_polygon, |
| ) |
|
|
|
|
| REDUNDANT_BOX_MIN_OVERLAP_RATIO = 0.95 |
| IMAGE_RELATED_LABELS = frozenset( |
| { |
| "Photo", |
| "Graphics", |
| "SealFigure", |
| "FrontPicture", |
| } |
| ) |
| HEADER_TITLE_LIKE_LABELS = frozenset( |
| { |
| "Headline", |
| "Kicker", |
| "Banner", |
| "Deck", |
| "Subhead", |
| "Nameplate", |
| "Masthead", |
| "FrontStory", |
| } |
| ) |
| IMAGE_HEADER_FILTER_LABELS = IMAGE_RELATED_LABELS | HEADER_TITLE_LIKE_LABELS |
|
|
|
|
| def _normalize_whitespace(text: str) -> str: |
| """Local helper to avoid circular import with spatial.py.""" |
| return " ".join(text.split()) |
|
|
|
|
| @dataclass(frozen=True) |
| class Word: |
| box: Box |
| text: str |
|
|
| @property |
| def center(self) -> tuple[float, float]: |
| return ( |
| (self.box.x_min + self.box.x_max) / 2.0, |
| (self.box.y_min + self.box.y_max) / 2.0, |
| ) |
|
|
|
|
| @dataclass(frozen=True) |
| class AnnotationBox: |
| box_id: str |
| rect: Box |
| text: str |
| has_transcription: bool |
| rotation: float |
| polygon: tuple[tuple[float, float], ...] |
| bounds: Box |
| labels: tuple[str, ...] = () |
| letter_count: int = 0 |
| latin_or_cyrillic_letter_count: int = 0 |
| non_armenian_letter_ratio: float = 0.0 |
| excluded_as_non_armenian_text: bool = False |
| parent_box_id: str | None = None |
| reading_order: int | None = None |
|
|
| def contains_point(self, x: float, y: float) -> bool: |
| if not self.bounds.contains_point(x, y): |
| return False |
| return point_in_convex_polygon((x, y), self.polygon) |
|
|
| def overlap_area_with_box(self, box: Box) -> float: |
| if self.bounds.intersection(box) is None: |
| return 0.0 |
| return polygon_area(polygon_intersection(box.to_polygon(), self.polygon)) |
|
|
|
|
| @dataclass(frozen=True) |
| class PredictedRow: |
| row_id: str |
| words: list[Word] |
|
|
| @property |
| def text(self) -> str: |
| return " ".join(word.text for word in self.words if word.text.strip()) |
|
|
|
|
| def annotation_box_contains_annotation_box(container: AnnotationBox, inner: AnnotationBox) -> bool: |
| if container.box_id == inner.box_id: |
| return False |
| if polygon_area(container.polygon) <= polygon_area(inner.polygon): |
| return False |
| if ( |
| inner.bounds.x_min < container.bounds.x_min - 1e-6 |
| or inner.bounds.y_min < container.bounds.y_min - 1e-6 |
| or inner.bounds.x_max > container.bounds.x_max + 1e-6 |
| or inner.bounds.y_max > container.bounds.y_max + 1e-6 |
| ): |
| return False |
| return all(container.contains_point(point[0], point[1]) for point in inner.polygon) |
|
|
|
|
| def annotation_box_overlap_ratio(container: AnnotationBox, inner: AnnotationBox) -> float: |
| inner_area = polygon_area(inner.polygon) |
| if inner_area <= 0: |
| return 0.0 |
| overlap_area = polygon_area(polygon_intersection(inner.polygon, container.polygon)) |
| return overlap_area / inner_area |
|
|
|
|
| def annotation_boxes_have_compatible_text(container: AnnotationBox, inner: AnnotationBox) -> bool: |
| container_text = _normalize_whitespace(container.text) |
| inner_text = _normalize_whitespace(inner.text) |
| if not container_text or not inner_text: |
| return False |
| return ( |
| container_text == inner_text |
| or inner_text in container_text |
| or container_text in inner_text |
| ) |
|
|
|
|
| def decomposable_container_box_ids(boxes: list[AnnotationBox]) -> frozenset[str]: |
| """Return box_ids of containers whose text is exactly reproduced by |
| concatenating (in reading order) two or more boxes it contains. |
| |
| Some annotations squeeze multiple side-by-side columns (e.g. a folioline |
| with page number / title / date, or a paragraph split across two print |
| columns) into one wide box, encoding the correct reading order only in |
| the transcription's word order. When that box also has separately |
| annotated children covering the same content, evaluating against the |
| children directly sidesteps having to guess the column layout from |
| geometry alone. |
| """ |
| textful = [b for b in boxes if b.has_transcription and b.text.strip()] |
|
|
| |
| |
| |
| |
| |
| |
| children_by_declared_parent: dict[str, list[AnnotationBox]] = {} |
| for b in textful: |
| if b.parent_box_id: |
| children_by_declared_parent.setdefault(b.parent_box_id, []).append(b) |
|
|
| def reconstructs(children: list[AnnotationBox], container_text: str) -> bool: |
| if len(children) < 2: |
| return False |
| order_keys = [ |
| lambda b: (b.rect.y_min, b.rect.x_min), |
| lambda b: (b.rect.x_min, b.rect.y_min), |
| ] |
| if all(b.reading_order is not None for b in children): |
| order_keys.insert(0, lambda b: b.reading_order) |
| for order_key in order_keys: |
| ordered = sorted(children, key=order_key) |
| reconstructed = _normalize_whitespace(" ".join(b.text for b in ordered)) |
| if reconstructed == container_text: |
| return True |
| return False |
|
|
| decomposable: set[str] = set() |
| for container in textful: |
| container_text = _normalize_whitespace(container.text) |
| if len(container_text) < 10: |
| continue |
|
|
| |
| |
| |
| |
| |
| |
| |
| declared_children = children_by_declared_parent.get(container.box_id, []) |
| geometric_children = [ |
| b |
| for b in textful |
| if b.box_id != container.box_id |
| and annotation_box_contains_annotation_box(container, b) |
| ] |
|
|
| if reconstructs(declared_children, container_text) or reconstructs( |
| geometric_children, container_text |
| ): |
| decomposable.add(container.box_id) |
| return frozenset(decomposable) |
|
|
|
|
| def filter_redundant_annotation_boxes(boxes: list[AnnotationBox]) -> list[AnnotationBox]: |
| filtered_boxes: list[AnnotationBox] = [] |
| for candidate in boxes: |
| is_redundant = False |
| for other in boxes: |
| if other.box_id == candidate.box_id: |
| continue |
| if polygon_area(other.polygon) <= polygon_area(candidate.polygon): |
| continue |
| if not annotation_boxes_have_compatible_text(other, candidate): |
| continue |
| if annotation_box_contains_annotation_box(other, candidate): |
| is_redundant = True |
| break |
| if annotation_box_overlap_ratio(other, candidate) >= REDUNDANT_BOX_MIN_OVERLAP_RATIO: |
| is_redundant = True |
| break |
| if not is_redundant: |
| filtered_boxes.append(candidate) |
| return filtered_boxes |
|
|
|
|
| def annotation_box_type(annotation_box: AnnotationBox) -> str | None: |
| return annotation_box.labels[0] if annotation_box.labels else None |
|
|
|
|
| def annotation_box_metadata(annotation_box: AnnotationBox) -> dict[str, Any]: |
| return { |
| "box_id": annotation_box.box_id, |
| "box_type": annotation_box_type(annotation_box), |
| "labels": list(annotation_box.labels), |
| } |
|
|
|
|
| def gt_box_report(annotation_box: AnnotationBox) -> dict[str, Any]: |
| |
| rounded = [round(v, 3) for v in [ |
| annotation_box.bounds.x_min, |
| annotation_box.bounds.y_min, |
| annotation_box.bounds.x_max, |
| annotation_box.bounds.y_max, |
| ]] |
| return { |
| **annotation_box_metadata(annotation_box), |
| "box": rounded, |
| } |
|
|