Fix sort_page_element. ensures that sorting is stable and not random. (#3978)

The sort_page_element() use the element id to sort the elements.
Two executions of the same code, on the same file, produce different
results. The order of the elements is random.
This makes it impossible to write stable unit tests, for example, or to
obtain reproducible results.
This commit is contained in:
Philippe PRADOS 2025-04-07 17:57:20 +02:00 committed by GitHub
parent dfa17bd3a0
commit d570f4624b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View File

@ -5,6 +5,9 @@
### Features
### Fixes
- The sort_page_element() use the element id to sort the elements.
Two executions of the same code, on the same file, produce different results. The order of the elements is random.
This makes it impossible to write stable unit tests, for example, or to obtain reproducible results.
- **Do not use NLP to determine element types for extracted elements with hi_res.** This avoids extraneous Title elements in hi_res outputs. This only applies to *extracted* elements, meaning text objects that are found outside of Object Detection objects which get mapped to *inferred* elements. (*extracted* and *inferred* elements get merged together to form the list of `Element`s returned by `pdf_partition()`)
## 0.17.5

View File

@ -1603,3 +1603,24 @@ def test_partition_pdf_with_specified_ocr_agents(mocker):
assert spy.call_args_list[0][1] == {"language": "eng", "ocr_agent_module": OCR_AGENT_TESSERACT}
assert spy.call_args_list[1][1] == {"language": "en", "ocr_agent_module": OCR_AGENT_PADDLE}
def test_reproductible_pdf_loader():
from glob import glob
for f in glob(example_doc_path("pdf/layout-parser-paper.pdf")):
elements_1 = pdf.partition_pdf(
filename=f,
strategy=PartitionStrategy.AUTO,
infer_table_structure=False,
)
for _ in range(4):
elements_2 = pdf.partition_pdf(
filename=f,
strategy=PartitionStrategy.AUTO,
infer_table_structure=False,
)
for e1, e2 in zip(elements_1, elements_2):
assert e1.text == e2.text, f"load two time {f=} return differents results"
else:
break

View File

@ -179,7 +179,6 @@ def sort_page_elements(
key=lambda el: (
el.metadata.coordinates.points[0][1] if el.metadata.coordinates else float("inf"),
el.metadata.coordinates.points[0][0] if el.metadata.coordinates else float("inf"),
el.id,
),
)
else: