Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add update and tests for right-to-left documents [probably needs to be closed, not merged] #883

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docling/backend/docling_parse_v2_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pypdfium2 import PdfPage

from docling.backend.pdf_backend import PdfDocumentBackend, PdfPageBackend
from docling.datamodel.base_models import Cell, Size
from docling.datamodel.base_models import Cell, Size, TextDirection

if TYPE_CHECKING:
from docling.datamodel.document import InputDocument
Expand Down Expand Up @@ -97,6 +97,7 @@ def get_text_cells(self) -> Iterable[Cell]:
y0 = cell_data[cells_header.index("y0")]
x1 = cell_data[cells_header.index("x1")]
y1 = cell_data[cells_header.index("y1")]
ltr = cell_data[cells_header.index("left_to_right")]

if x1 < x0:
x0, x1 = x1, x0
Expand All @@ -116,6 +117,11 @@ def get_text_cells(self) -> Iterable[Cell]:
t=y1 * page_size.height / parser_height,
coord_origin=CoordOrigin.BOTTOMLEFT,
).to_top_left_origin(page_size.height),
text_direction=(
TextDirection.LEFT_TO_RIGHT
if ltr
else TextDirection.RIGHT_TO_LEFT
),
)
)
cell_counter += 1
Expand Down
6 changes: 6 additions & 0 deletions docling/datamodel/base_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,16 @@ class ErrorItem(BaseModel):
error_message: str


class TextDirection(str, Enum):
LEFT_TO_RIGHT = "left_to_right"
RIGHT_TO_LEFT = "right_to_left"


class Cell(BaseModel):
id: int
text: str
bbox: BoundingBox
text_direction: TextDirection = TextDirection.LEFT_TO_RIGHT


class OcrCell(Cell):
Expand Down
1 change: 1 addition & 0 deletions docling/models/ds_glm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ def __call__(self, conv_res: ConversionResult) -> DoclingDocument:
glm_doc = self.model.apply_on_doc(ds_doc_dict)

docling_doc: DoclingDocument = to_docling_document(glm_doc) # Experimental
1 == 1

# DEBUG code:
def draw_clusters_and_cells(ds_document, page_no, show: bool = False):
Expand Down
22 changes: 17 additions & 5 deletions docling/models/page_assemble_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Page,
PageElement,
Table,
TextDirection,
TextElement,
)
from docling.datamodel.document import ConversionResult
Expand Down Expand Up @@ -75,12 +76,23 @@ def __call__(
for cluster in page.predictions.layout.clusters:
# _log.info("Cluster label seen:", cluster.label)
if cluster.label in LayoutModel.TEXT_ELEM_LABELS:
textlines = []

dominant_text_direction = TextDirection.LEFT_TO_RIGHT

# Naive code: dominant text direction == direction of first cell.
for cell in cluster.cells:
dominant_text_direction = cell.text_direction
break

for cell in cluster.cells:
text = cell.text.replace("\x02", "-").strip()
if text:
# if dominant_text_direction == TextDirection.RIGHT_TO_LEFT:
# textlines.insert(0, text) # Prepend RTL text
# else:
textlines.append(text) # Append LTR text

textlines = [
cell.text.replace("\x02", "-").strip()
for cell in cluster.cells
if len(cell.text.strip()) > 0
]
text = self.sanitize_text(textlines)
text_el = TextElement(
label=cluster.label,
Expand Down
13 changes: 9 additions & 4 deletions docs/examples/batch_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def export_documents(
with (output_dir / f"{doc_filename}.md").open("w") as fp:
fp.write(conv_res.document.export_to_markdown())

conv_res.document.save_as_html(output_dir / f"{doc_filename}.html")

# Export Docling document format to text:
with (output_dir / f"{doc_filename}.txt").open("w") as fp:
fp.write(conv_res.document.export_to_markdown(strict_text=True))
Expand Down Expand Up @@ -103,10 +105,13 @@ def main():
logging.basicConfig(level=logging.INFO)

input_doc_paths = [
Path("./tests/data/2206.01062.pdf"),
Path("./tests/data/2203.01017v2.pdf"),
Path("./tests/data/2305.03393v1.pdf"),
Path("./tests/data/redp5110_sampled.pdf"),
Path("./tests/data/pdf/right_to_left_01.pdf"),
Path("./tests/data/pdf/right_to_left_02.pdf"),
Path("./tests/data/pdf/right_to_left_03.pdf"),
Path("./tests/data/pdf/2206.01062.pdf"),
Path("./tests/data/pdf/2203.01017v2.pdf"),
Path("./tests/data/pdf/2305.03393v1.pdf"),
Path("./tests/data/pdf/redp5110_sampled.pdf"),
]

# buf = BytesIO(Path("./test/data/2206.01062.pdf").open("rb").read())
Expand Down
Loading
Loading