# Copyright (c) 2026 LlamaIndex, Inc. All rights reserved. # Licensed under the OCRia-1 Model and Code License (LICENSE at the root of this repository). # This is not open-source software: see the license for permitted uses. """Conservative reading-order paths for horizontal left-to-right prose. Coordinates are image pixels. Word indices remain in transcript order, so a skipped attention outlier does not delete text. Unsupported layouts fall back to the existing spatial clustering; tables, formulas and code bypass this pass. """ import statistics WordBox = list[float] def paragraph_paths(boxes: list[WordBox]) -> list[list[int]] | None: """Return supported word paths, splitting column continuations when justified. Nearby words form spatial components. Within each component a longest path must move right on a line or wrap onto a nearby lower line. At least 60% of words must support the result. Separate columns need three words each and non-interleaving transcript spans; otherwise keep only a dominant path. """ n = len(boxes) if n < 3: return None height = max(1.0, statistics.median(b[3] - b[1] for b in boxes)) parents = list(range(n)) def root(i: int) -> int: while parents[i] != i: parents[i] = parents[parents[i]] i = parents[i] return i # Proximity establishes candidate columns independently of transcript order. # A nearby wrong word can still belong here; the ordered path rejects it. for i, a in enumerate(boxes): for j in range(i): b = boxes[j] dx = max(a[0] - b[2], b[0] - a[2], 0.0) dy = max(a[1] - b[3], b[1] - a[3], 0.0) if dx <= 4 * height and dy <= 2.5 * height: parents[root(i)] = root(j) components: dict[int, list[int]] = {} for i in range(n): components.setdefault(root(i), []).append(i) paths: list[list[int]] = [] for indices in components.values(): lengths = {i: 1 for i in indices} previous: dict[int, int] = {} for pos, i in enumerate(indices): b = boxes[i] for j in indices[:pos]: a = boxes[j] overlap = min(a[3], b[3]) - max(a[1], b[1]) same_line = overlap >= 0.5 * min(a[3] - a[1], b[3] - b[1]) if same_line: follows = b[0] >= a[0] + 0.2 * height and -0.5 * height <= b[0] - a[2] <= 4 * height else: follows = ( b[1] >= a[1] + 0.5 * height and b[1] - a[3] <= 2.5 * height and b[0] < a[2] ) if follows and lengths[j] + 1 > lengths[i]: lengths[i] = lengths[j] + 1 previous[i] = j end = max(indices, key=lengths.__getitem__) path = [end] while path[-1] in previous: path.append(previous[path[-1]]) path.reverse() if len(path) >= 3: paths.append(path) if not paths: return None paths.sort(key=lambda p: p[0]) if sum(map(len, paths)) < 0.6 * n: return None # A column jump is supported by whole runs, not a single distant word. # Interleaved components or a jump down to unrelated text are ambiguous. for left, right in zip(paths, paths[1:]): gutter = min(boxes[i][0] for i in right) - max(boxes[i][2] for i in left) if left[-1] >= right[0] or gutter < height or boxes[right[0]][1] > boxes[left[-1]][1]: dominant = max(paths, key=len) return [dominant] if len(dominant) >= 0.6 * n else None return paths