File size: 3,058 Bytes
f66643d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Enums and label mappings for the scanned PDF pipeline.

This module contains:
- ElementCategory: The 5 translation handling categories
- SuryaLabel: String constants for Surya's hyphenated labels
- SURYA_LABEL_MAP: Mapping from Surya labels to ElementCategory
- DEFAULT_CATEGORY: Fallback for unknown labels
"""

from enum import Enum


class ElementCategory(str, Enum):
    """Categories determining how downstream stages handle each element.

    Values:
        BYPASS: Pixel-copy from original; never translate (Picture, Figure, Form)
        FLOWING_TEXT: Translate full source_text; may merge adjacent blocks
        IN_PLACE: Translate source_text; render at exact bbox position
        TABLE: Translate each cell's source_text; render cell grid
        EQUATION: source_text contains extracted OCR text to translate;
            equation_words preserves word-level OCR boxes for precise placement
    """

    BYPASS = "BYPASS"
    FLOWING_TEXT = "FLOWING_TEXT"
    IN_PLACE = "IN_PLACE"
    TABLE = "TABLE"
    EQUATION = "EQUATION"


class SuryaLabel:
    """String constants for Surya layout labels (v0.9+ hyphenated format).

    These match the exact strings returned by Surya's LayoutPredictor.
    """

    # Text elements -> FLOWING_TEXT
    TEXT = "Text"
    LIST_ITEM = "ListItem"
    FOOTNOTE = "Footnote"

    # Headers/footers/captions -> IN_PLACE
    SECTION_HEADER = "SectionHeader"
    PAGE_HEADER = "PageHeader"
    PAGE_FOOTER = "PageFooter"
    CAPTION = "Caption"
    TABLE_OF_CONTENTS = "TableOfContents"

    # Graphics -> BYPASS
    PICTURE = "Picture"
    FIGURE = "Figure"
    FORM = "Form"

    # Tables -> TABLE
    TABLE = "Table"

    # Math -> EQUATION
    EQUATION = "Equation"

    # Code
    CODE = "Code"


# Mapping from Surya labels to ElementCategory
SURYA_LABEL_MAP: dict[str, ElementCategory] = {
    # FLOWING_TEXT: regular text blocks that can be translated and reflowed
    SuryaLabel.TEXT: ElementCategory.FLOWING_TEXT,
    SuryaLabel.LIST_ITEM: ElementCategory.FLOWING_TEXT,
    SuryaLabel.FOOTNOTE: ElementCategory.FLOWING_TEXT,
    # IN_PLACE: text that must be rendered at exact position
    SuryaLabel.SECTION_HEADER: ElementCategory.FLOWING_TEXT,
    SuryaLabel.PAGE_HEADER: ElementCategory.FLOWING_TEXT,
    SuryaLabel.PAGE_FOOTER: ElementCategory.FLOWING_TEXT,
    SuryaLabel.CAPTION: ElementCategory.IN_PLACE,
    SuryaLabel.TABLE_OF_CONTENTS: ElementCategory.IN_PLACE,
    # BYPASS: graphics that should be copied without modification
    SuryaLabel.PICTURE: ElementCategory.BYPASS,
    SuryaLabel.FIGURE: ElementCategory.BYPASS,
    SuryaLabel.FORM: ElementCategory.FLOWING_TEXT,
    # TABLE: structured data requiring cell-level translation
    SuryaLabel.TABLE: ElementCategory.TABLE,
    # EQUATION: math content to be preserved as-is
    SuryaLabel.EQUATION: ElementCategory.EQUATION,
    # CODE: programming code blocks (treat as IN_PLACE for now)
    SuryaLabel.CODE: ElementCategory.BYPASS,
}

# Default category for unknown Surya labels
DEFAULT_CATEGORY = ElementCategory.FLOWING_TEXT