461bf6fd40
CI / lint (3.11) (push) Has been cancelled
CI / lint (3.12) (push) Has been cancelled
CI / lint (3.13) (push) Has been cancelled
CI / shellcheck (push) Has been cancelled
CI / shfmt (push) Has been cancelled
CI / setup (3.11) (push) Has been cancelled
CI / setup (3.12) (push) Has been cancelled
CI / setup (3.13) (push) Has been cancelled
CI / check-licenses (3.12) (push) Has been cancelled
CI / test_unit (3.11) (push) Has been cancelled
CI / test_unit (3.12) (push) Has been cancelled
CI / test_unit (3.13) (push) Has been cancelled
CI / test_unit_no_extras (3.11) (push) Has been cancelled
CI / test_unit_no_extras (3.12) (push) Has been cancelled
CI / test_json_to_html (3.12) (push) Has been cancelled
CI / test_unit_no_extras (3.13) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.12, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.11, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.12, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.11, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.13, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.11, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.12, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.13, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.11, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.12, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.13, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.11, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.12, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.13, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.11, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.12, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.13, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.11, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.12, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.13, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.11, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.12, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.13, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
Build And Push Docker Image / set-short-sha (push) Has been cancelled
Partition Benchmark / setup (push) Has been cancelled
Partition Benchmark / Measure and compare partition() runtime (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.13, --extra xlsx) (push) Has been cancelled
CI / test_ingest_src (3.12) (push) Has been cancelled
CI / test_json_to_markdown (3.12) (push) Has been cancelled
CI / changelog (push) Has been cancelled
CI / test_dockerfile (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/amd64, opensource-linux-8core) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Build And Push Docker Image / publish-images (push) Has been cancelled
131 lines
4.2 KiB
Python
131 lines
4.2 KiB
Python
import pytest
|
|
|
|
from test_unstructured.unit_utils import example_doc_path
|
|
from unstructured.documents.elements import Text
|
|
from unstructured.partition import pdf, strategies
|
|
from unstructured.partition.utils.constants import PartitionStrategy
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"strategy",
|
|
[
|
|
PartitionStrategy.AUTO,
|
|
PartitionStrategy.FAST,
|
|
PartitionStrategy.OCR_ONLY,
|
|
PartitionStrategy.HI_RES,
|
|
],
|
|
)
|
|
def test_validate_strategy(strategy):
|
|
# Nothing should raise for a valid strategy
|
|
strategies.validate_strategy(strategy=strategy)
|
|
|
|
|
|
def test_validate_strategy_raises_for_fast_strategy():
|
|
with pytest.raises(ValueError):
|
|
strategies.validate_strategy(strategy=PartitionStrategy.FAST, is_image=True)
|
|
|
|
|
|
def test_validate_strategy_raises_for_bad_strategy():
|
|
with pytest.raises(ValueError):
|
|
strategies.validate_strategy("totally_guess_the_text")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("filename", "from_file", "expected"),
|
|
[
|
|
("layout-parser-paper-fast.pdf", True, True),
|
|
("copy-protected.pdf", True, True),
|
|
("loremipsum-flat.pdf", True, False),
|
|
("layout-parser-paper-fast.pdf", False, True),
|
|
("copy-protected.pdf", False, True),
|
|
("loremipsum-flat.pdf", False, False),
|
|
],
|
|
)
|
|
def test_is_pdf_text_extractable(filename, from_file, expected):
|
|
filename = example_doc_path(f"pdf/{filename}")
|
|
|
|
if from_file:
|
|
with open(filename, "rb") as f:
|
|
extracted_elements = pdf.extractable_elements(file=f)
|
|
else:
|
|
extracted_elements = pdf.extractable_elements(filename=filename)
|
|
|
|
pdf_text_extractable = any(
|
|
isinstance(el, Text) and el.text.strip()
|
|
for page_elements in extracted_elements
|
|
for el in page_elements
|
|
)
|
|
|
|
assert pdf_text_extractable is expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("pdf_text_extractable", "infer_table_structure"),
|
|
[
|
|
(True, True),
|
|
(False, True),
|
|
(True, False),
|
|
(False, False),
|
|
],
|
|
)
|
|
def test_determine_pdf_or_image_fast_strategy(pdf_text_extractable, infer_table_structure):
|
|
strategy = strategies.determine_pdf_or_image_strategy(
|
|
strategy=PartitionStrategy.FAST,
|
|
pdf_text_extractable=pdf_text_extractable,
|
|
infer_table_structure=infer_table_structure,
|
|
)
|
|
assert strategy == PartitionStrategy.FAST
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
(
|
|
"pdf_text_extractable",
|
|
"infer_table_structure",
|
|
"extract_images_in_pdf",
|
|
"extract_image_block_types",
|
|
"expected",
|
|
),
|
|
[
|
|
(True, True, True, ["Image"], PartitionStrategy.HI_RES),
|
|
(True, True, True, [], PartitionStrategy.HI_RES),
|
|
(True, True, False, ["Image"], PartitionStrategy.HI_RES),
|
|
(True, True, False, [], PartitionStrategy.HI_RES),
|
|
(True, False, True, ["Image"], PartitionStrategy.HI_RES),
|
|
(True, False, True, [], PartitionStrategy.HI_RES),
|
|
(True, False, False, ["Image"], PartitionStrategy.HI_RES),
|
|
(True, False, False, [], PartitionStrategy.FAST),
|
|
(False, True, True, ["Image"], PartitionStrategy.HI_RES),
|
|
(False, True, True, [], PartitionStrategy.HI_RES),
|
|
(False, True, False, ["Image"], PartitionStrategy.HI_RES),
|
|
(False, True, False, [], PartitionStrategy.HI_RES),
|
|
(False, False, True, ["Image"], PartitionStrategy.HI_RES),
|
|
(False, False, True, [], PartitionStrategy.HI_RES),
|
|
(False, False, False, ["Image"], PartitionStrategy.HI_RES),
|
|
(False, False, False, [], PartitionStrategy.OCR_ONLY),
|
|
],
|
|
)
|
|
def test_determine_pdf_auto_strategy(
|
|
pdf_text_extractable,
|
|
infer_table_structure,
|
|
extract_images_in_pdf,
|
|
extract_image_block_types,
|
|
expected,
|
|
):
|
|
strategy = strategies.determine_pdf_or_image_strategy(
|
|
strategy=PartitionStrategy.AUTO,
|
|
is_image=False,
|
|
pdf_text_extractable=pdf_text_extractable,
|
|
infer_table_structure=infer_table_structure,
|
|
extract_images_in_pdf=extract_images_in_pdf,
|
|
extract_image_block_types=extract_image_block_types,
|
|
)
|
|
assert strategy == expected
|
|
|
|
|
|
def test_determine_image_auto_strategy():
|
|
strategy = strategies.determine_pdf_or_image_strategy(
|
|
strategy=PartitionStrategy.AUTO,
|
|
is_image=True,
|
|
)
|
|
assert strategy == PartitionStrategy.HI_RES
|