Add right-to-left reading order support to xy-cut sorting - #4420
Open
Nimra3261 wants to merge 1 commit into
Open
Add right-to-left reading order support to xy-cut sorting#4420Nimra3261 wants to merge 1 commit into
Nimra3261 wants to merge 1 commit into
Conversation
The xy-cut layout sorting (recursive_xy_cut / recursive_xy_cut_swapped) always visits column/block groups left-to-right, which is the wrong reading order for right-to-left scripts (Arabic, Hebrew, etc.) - the rightmost block on a line should come first, not last. Adds an rtl parameter (default False, fully backward compatible) that reverses the horizontal traversal order at each split while leaving vertical (top-to-bottom) order untouched, since that's the same for LTR and RTL documents. Threaded through sort_page_elements, sort_bboxes_by_xy_cut, and sort_text_regions, plus a new UNSTRUCTURED_XY_CUT_RTL env var matching the existing UNSTRUCTURED_XY_CUT_PRIMARY_DIRECTION pattern. Scope note: this fixes block/column reading order in the layout sorting step only. It does not touch OCR text recognition itself - Tesseract already produces correctly-ordered text within a single recognized line/paragraph when given the right language, so glyph-level bidi shaping is out of scope here. Addresses Unstructured-IO#3927
Contributor
There was a problem hiding this comment.
No issues found across 4 files
Shadow auto-approve: would auto-approve. Adds an opt-in RTL reading order parameter to xy-cut sorting, with tests confirming backward compatibility and correct reversal of horizontal order.
Re-trigger cubic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The xy-cut layout sorting (
recursive_xy_cut/recursive_xy_cut_swappedinunstructured/partition/utils/xycut.py) always visits column/block groups left-to-right. For right-to-left scripts (Arabic, Hebrew, etc.) this is the wrong reading order - the rightmost block on a line should come first, not last.Adds an
rtlparameter (defaultFalse, fully backward compatible) that reverses the horizontal traversal order at each split, while leaving vertical (top-to-bottom) order untouched - that part of reading order is the same for LTR and RTL documents. Threaded throughsort_page_elements,sort_bboxes_by_xy_cut, andsort_text_regions, plus a newUNSTRUCTURED_XY_CUT_RTLenv var matching the existingUNSTRUCTURED_XY_CUT_PRIMARY_DIRECTIONpattern.Scope
This fixes block/column reading order in the layout sorting step only. It does not touch OCR text recognition itself - Tesseract already produces correctly-ordered text within a single recognized line/paragraph when given the right language, so glyph-level bidi shaping is out of scope here. Happy to help with that side separately if it's still an open problem once this lands.
Testing
Added unit tests for both the low-level
xycut.pyfunctions and thesort_page_elementswrapper, verifying: (1) default (rtl=False) behavior is byte-identical to before - the existingtest_recursive_xy_cutfixture still passes unchanged, and (2)rtl=Truecorrectly mirrors reading order.Ran locally:
test_sorting.pyandtest_xycut.pyboth pass in full (17/17 and 13/13). I couldn't installunstructured-inferenceitself locally (pulls in torch and isn't needed for this change), so theTextRegions-dependent tests intest_sorting.pywere verified against a minimal local stub matching its documented interface rather than the real package - flagging that honestly in case CI surfaces something my stub didn't catch.Addresses #3927