461bf6fd40
CI / lint (3.11) (push) Blocked by required conditions
CI / lint (3.12) (push) Blocked by required conditions
CI / lint (3.13) (push) Blocked by required conditions
CI / shellcheck (push) Waiting to run
CI / shfmt (push) Waiting to run
CI / setup (3.11) (push) Waiting to run
CI / setup (3.12) (push) Waiting to run
CI / setup (3.13) (push) Waiting to run
CI / check-licenses (3.12) (push) Blocked by required conditions
CI / test_unit (3.11) (push) Blocked by required conditions
CI / test_unit (3.12) (push) Blocked by required conditions
CI / test_unit (3.13) (push) Blocked by required conditions
CI / test_unit_no_extras (3.11) (push) Blocked by required conditions
CI / test_unit_no_extras (3.12) (push) Blocked by required conditions
CI / test_json_to_html (3.12) (push) Blocked by required conditions
CI / test_unit_no_extras (3.13) (push) Blocked by required conditions
CI / test_unit_dependency_extras (csv, 3.11, --extra csv) (push) Blocked by required conditions
CI / test_unit_dependency_extras (csv, 3.12, --extra csv) (push) Blocked by required conditions
CI / test_unit_dependency_extras (csv, 3.13, --extra csv) (push) Blocked by required conditions
CI / test_unit_dependency_extras (docx, 3.11, --extra docx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (docx, 3.12, --extra docx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (docx, 3.13, --extra docx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (markdown, 3.11, --extra md) (push) Blocked by required conditions
CI / test_unit_dependency_extras (markdown, 3.12, --extra md) (push) Blocked by required conditions
CI / test_unit_dependency_extras (markdown, 3.13, --extra md) (push) Blocked by required conditions
CI / test_unit_dependency_extras (odt, 3.11, --extra odt) (push) Blocked by required conditions
CI / test_unit_dependency_extras (odt, 3.12, --extra odt) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pdf-image, 3.12, --extra pdf --extra image --extra paddleocr) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pdf-image, 3.13, --extra pdf --extra image --extra paddleocr) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pptx, 3.13, --extra pptx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (odt, 3.13, --extra odt) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pdf-image, 3.11, --extra pdf --extra image --extra paddleocr) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pptx, 3.11, --extra pptx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pptx, 3.12, --extra pptx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pypandoc, 3.11, --extra epub --extra org --extra rtf --extra rst) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pypandoc, 3.12, --extra epub --extra org --extra rtf --extra rst) (push) Blocked by required conditions
CI / test_unit_dependency_extras (pypandoc, 3.13, --extra epub --extra org --extra rtf --extra rst) (push) Blocked by required conditions
CI / test_unit_dependency_extras (xlsx, 3.11, --extra xlsx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (xlsx, 3.12, --extra xlsx) (push) Blocked by required conditions
CI / test_unit_dependency_extras (xlsx, 3.13, --extra xlsx) (push) Blocked by required conditions
CI / test_ingest_src (3.12) (push) Blocked by required conditions
CI / test_json_to_markdown (3.12) (push) Blocked by required conditions
CI / changelog (push) Waiting to run
CI / test_dockerfile (push) Blocked by required conditions
CodeQL / Analyze (python) (push) Waiting to run
Build And Push Docker Image / set-short-sha (push) Waiting to run
Build And Push Docker Image / build-images (linux/amd64, opensource-linux-8core) (push) Blocked by required conditions
Build And Push Docker Image / build-images (linux/arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
Build And Push Docker Image / publish-images (push) Blocked by required conditions
Partition Benchmark / setup (push) Waiting to run
Partition Benchmark / Measure and compare partition() runtime (push) Blocked by required conditions
91 lines
3.2 KiB
Python
91 lines
3.2 KiB
Python
from functools import partial
|
|
|
|
import pytest
|
|
|
|
from unstructured.chunking.basic import chunk_elements
|
|
from unstructured.chunking.title import chunk_by_title
|
|
from unstructured.documents.elements import ElementMetadata, NarrativeText, Text, Title
|
|
|
|
|
|
@pytest.fixture(params=[chunk_elements, partial(chunk_by_title, combine_text_under_n_chars=0)])
|
|
def chunking_fn(request):
|
|
return request.param
|
|
|
|
|
|
def test_combining_html_metadata_when_multiple_elements_in_composite_element(chunking_fn):
|
|
metadata_1 = '<h1 class="Title" id="1">Header </h1>'
|
|
metadata_2 = '<time class="CalendarDate" id="2">Date: October 30, 2023 </time>'
|
|
metadata_3 = (
|
|
'<form class="Form" id="3"> '
|
|
'<label class="FormField" for="company-name" id="4">Form field name </label>'
|
|
'<input class="FormFieldValue" id="5" value="Example value" />'
|
|
"</form>"
|
|
)
|
|
combined_metadata = " ".join([metadata_1, metadata_2, metadata_3])
|
|
|
|
elements = [
|
|
Title(text="Header", metadata=ElementMetadata(text_as_html=metadata_1)),
|
|
Text(text="Date: October 30, 2023", metadata=ElementMetadata(text_as_html=metadata_2)),
|
|
Text(
|
|
text="Form field name Example value", metadata=ElementMetadata(text_as_html=metadata_3)
|
|
),
|
|
]
|
|
chunks = chunking_fn(elements)
|
|
assert len(chunks) == 1
|
|
assert chunks[0].metadata.text_as_html == combined_metadata
|
|
|
|
|
|
def test_combining_html_metadata_with_nested_relationship_between_elements(chunking_fn):
|
|
"""
|
|
Ground truth
|
|
<Document>
|
|
<Page>
|
|
<Section>
|
|
<p>First</p>
|
|
<p>Second</p>
|
|
</Section>
|
|
</Page>
|
|
</Document>
|
|
Elements: Document, Page, Section, Paragraph, Paragraph
|
|
Chunk 1: Document, Page, Section, Paragraph
|
|
|
|
Chunk 2:
|
|
Paragraph
|
|
"""
|
|
|
|
metadata_1 = '<div class="Section" id="1" />'
|
|
metadata_2 = '<p class="Paragraph" id="2">First </p>'
|
|
metadata_3 = '<p class="Paragraph" id="3">Second </p>'
|
|
|
|
elements = [
|
|
Text(text="", metadata=ElementMetadata(text_as_html=metadata_1)),
|
|
NarrativeText(
|
|
text="First", metadata=ElementMetadata(text_as_html=metadata_2, parent_id="1")
|
|
),
|
|
NarrativeText(
|
|
text="Second", metadata=ElementMetadata(text_as_html=metadata_3, parent_id="1")
|
|
),
|
|
]
|
|
chunks = chunking_fn(elements, max_characters=6)
|
|
assert len(chunks) == 2
|
|
assert chunks[0].text == "First"
|
|
assert chunks[1].text == "Second"
|
|
|
|
assert chunks[0].metadata.text_as_html == metadata_1 + " " + metadata_2
|
|
assert chunks[1].metadata.text_as_html == metadata_3
|
|
|
|
|
|
def test_html_metadata_exist_in_both_element_when_text_is_split(chunking_fn):
|
|
"""Mimic behaviour of elements with non-html metadata"""
|
|
metadata_1 = '<h1 class="Title" id="1">Header </h1>'
|
|
elements = [
|
|
Title(text="Header", metadata=ElementMetadata(text_as_html=metadata_1)),
|
|
]
|
|
chunks = chunking_fn(elements, max_characters=3)
|
|
assert len(chunks) == 2
|
|
|
|
assert chunks[0].text == "Hea"
|
|
assert chunks[1].text == "der"
|
|
assert chunks[0].metadata.text_as_html == '<h1 class="Title" id="1">Header </h1>'
|
|
assert chunks[1].metadata.text_as_html == '<h1 class="Title" id="1">Header </h1>'
|