2023-03-14 11:52:21 -04:00
|
|
|
import os
|
|
|
|
import pathlib
|
|
|
|
|
2023-10-12 12:47:55 -07:00
|
|
|
from test_unstructured.unit_utils import assert_round_trips_through_JSON
|
2023-09-11 16:00:14 -05:00
|
|
|
from unstructured.chunking.title import chunk_by_title
|
2023-09-15 09:51:22 -07:00
|
|
|
from unstructured.documents.elements import Table, Text
|
2023-03-14 11:52:21 -04:00
|
|
|
from unstructured.partition.epub import partition_epub
|
2023-10-05 15:26:47 -05:00
|
|
|
from unstructured.partition.utils.constants import UNSTRUCTURED_INCLUDE_DEBUG_METADATA
|
2023-03-14 11:52:21 -04:00
|
|
|
|
|
|
|
DIRECTORY = pathlib.Path(__file__).parent.resolve()
|
2023-10-26 12:22:40 -05:00
|
|
|
EXAMPLE_DOCS_PATH = os.path.join(DIRECTORY, "..", "..", "..", "example-docs")
|
2023-03-14 11:52:21 -04:00
|
|
|
|
2023-03-30 16:54:29 -04:00
|
|
|
|
2023-03-14 11:52:21 -04:00
|
|
|
def test_partition_epub_from_filename():
|
2023-10-26 12:22:40 -05:00
|
|
|
filename = os.path.join(EXAMPLE_DOCS_PATH, "winter-sports.epub")
|
2023-03-14 11:52:21 -04:00
|
|
|
elements = partition_epub(filename=filename)
|
|
|
|
assert len(elements) > 0
|
|
|
|
assert elements[0].text.startswith("The Project Gutenberg eBook of Winter Sports")
|
2023-07-05 15:02:22 -05:00
|
|
|
for element in elements:
|
|
|
|
assert element.metadata.filename == "winter-sports.epub"
|
2023-10-05 15:26:47 -05:00
|
|
|
if UNSTRUCTURED_INCLUDE_DEBUG_METADATA:
|
|
|
|
assert {element.metadata.detection_origin for element in elements} == {"epub"}
|
2023-07-05 15:02:22 -05:00
|
|
|
|
|
|
|
|
2023-09-12 18:27:05 -07:00
|
|
|
def test_partition_epub_from_filename_returns_table_in_elements():
|
2023-10-26 12:22:40 -05:00
|
|
|
filename = os.path.join(EXAMPLE_DOCS_PATH, "winter-sports.epub")
|
2023-09-12 18:27:05 -07:00
|
|
|
elements = partition_epub(filename=filename)
|
|
|
|
assert len(elements) > 0
|
2023-10-26 12:22:40 -05:00
|
|
|
assert (
|
|
|
|
elements[14].text.replace("\n", " ")
|
|
|
|
== Table(
|
2024-01-04 13:53:19 -08:00
|
|
|
text="Contents. List of Illustrations "
|
2023-10-26 12:22:40 -05:00
|
|
|
"(In certain versions of this etext [in certain browsers] "
|
|
|
|
"clicking on the image will bring up a larger version.) "
|
2024-01-04 13:53:19 -08:00
|
|
|
"(etext transcriber's note)",
|
2023-10-26 12:22:40 -05:00
|
|
|
).text
|
2023-09-12 18:27:05 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-15 09:51:22 -07:00
|
|
|
def test_partition_epub_from_filename_returns_uns_elements():
|
2023-10-26 12:22:40 -05:00
|
|
|
filename = os.path.join(EXAMPLE_DOCS_PATH, "winter-sports.epub")
|
2023-09-15 09:51:22 -07:00
|
|
|
elements = partition_epub(filename=filename)
|
|
|
|
assert len(elements) > 0
|
|
|
|
assert isinstance(elements[0], Text)
|
|
|
|
|
|
|
|
|
2023-07-05 15:02:22 -05:00
|
|
|
def test_partition_epub_from_filename_with_metadata_filename():
|
2023-10-26 12:22:40 -05:00
|
|
|
filename = os.path.join(EXAMPLE_DOCS_PATH, "winter-sports.epub")
|
2023-07-05 15:02:22 -05:00
|
|
|
elements = partition_epub(filename=filename, metadata_filename="test")
|
|
|
|
assert len(elements) > 0
|
|
|
|
assert all(element.metadata.filename == "test" for element in elements)
|
2023-03-14 11:52:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_epub_from_file():
|
2023-10-26 12:22:40 -05:00
|
|
|
filename = os.path.join(EXAMPLE_DOCS_PATH, "winter-sports.epub")
|
2023-03-14 11:52:21 -04:00
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_epub(file=f)
|
|
|
|
assert len(elements) > 0
|
|
|
|
assert elements[0].text.startswith("The Project Gutenberg eBook of Winter Sports")
|
2023-07-05 15:02:22 -05:00
|
|
|
for element in elements:
|
|
|
|
assert element.metadata.filename is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_epub_from_file_with_metadata_filename():
|
2023-10-26 12:22:40 -05:00
|
|
|
filename = os.path.join(EXAMPLE_DOCS_PATH, "winter-sports.epub")
|
2023-07-05 15:02:22 -05:00
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_epub(file=f, metadata_filename="test")
|
|
|
|
assert len(elements) > 0
|
|
|
|
for element in elements:
|
|
|
|
assert element.metadata.filename == "test"
|
2023-06-30 09:44:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_epub_from_filename_exclude_metadata():
|
2023-10-26 12:22:40 -05:00
|
|
|
filename = os.path.join(EXAMPLE_DOCS_PATH, "winter-sports.epub")
|
2023-06-30 09:44:46 -05:00
|
|
|
elements = partition_epub(filename=filename, include_metadata=False)
|
|
|
|
assert elements[0].metadata.filetype is None
|
|
|
|
assert elements[0].metadata.page_name is None
|
|
|
|
assert elements[0].metadata.filename is None
|
2023-08-12 16:02:06 -05:00
|
|
|
assert elements[0].metadata.section is None
|
2023-06-30 09:44:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_epub_from_file_exlcude_metadata():
|
2023-10-26 12:22:40 -05:00
|
|
|
filename = os.path.join(EXAMPLE_DOCS_PATH, "winter-sports.epub")
|
2023-06-30 09:44:46 -05:00
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_epub(file=f, include_metadata=False)
|
|
|
|
assert elements[0].metadata.filetype is None
|
|
|
|
assert elements[0].metadata.page_name is None
|
|
|
|
assert elements[0].metadata.filename is None
|
2023-08-12 16:02:06 -05:00
|
|
|
assert elements[0].metadata.section is None
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_epub_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/winter-sports.epub",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
mocker.patch(
|
2023-10-26 12:22:40 -05:00
|
|
|
"unstructured.partition.html.get_last_modified_date",
|
2023-07-26 15:10:14 -04:00
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
elements = partition_epub(filename=filename)
|
|
|
|
|
2023-07-31 19:55:43 -07:00
|
|
|
assert elements[0].metadata.last_modified == mocked_last_modification_date
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_epub_custom_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/winter-sports.epub",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
expected_last_modification_date = "2020-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.html.get_last_modified_date",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
elements = partition_epub(
|
|
|
|
filename=filename,
|
2023-07-31 19:55:43 -07:00
|
|
|
metadata_last_modified=expected_last_modification_date,
|
2023-07-26 15:10:14 -04:00
|
|
|
)
|
|
|
|
|
2023-07-31 19:55:43 -07:00
|
|
|
assert elements[0].metadata.last_modified == expected_last_modification_date
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_epub_from_file_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/winter-sports.epub",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
2023-10-26 12:22:40 -05:00
|
|
|
"unstructured.partition.html.get_last_modified_date_from_file",
|
2023-07-26 15:10:14 -04:00
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_epub(file=f)
|
|
|
|
|
2023-07-31 19:55:43 -07:00
|
|
|
assert elements[0].metadata.last_modified == mocked_last_modification_date
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_epub_from_file_custom_metadata_date(
|
|
|
|
mocker,
|
|
|
|
filename="example-docs/winter-sports.epub",
|
|
|
|
):
|
|
|
|
mocked_last_modification_date = "2029-07-05T09:24:28"
|
|
|
|
expected_last_modification_date = "2020-07-05T09:24:28"
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.html.get_last_modified_date_from_file",
|
|
|
|
return_value=mocked_last_modification_date,
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(filename, "rb") as f:
|
2023-07-31 19:55:43 -07:00
|
|
|
elements = partition_epub(file=f, metadata_last_modified=expected_last_modification_date)
|
2023-07-26 15:10:14 -04:00
|
|
|
|
2023-07-31 19:55:43 -07:00
|
|
|
assert elements[0].metadata.last_modified == expected_last_modification_date
|
2023-08-29 16:59:26 -04:00
|
|
|
|
|
|
|
|
2023-10-12 12:47:55 -07:00
|
|
|
def test_partition_epub_with_json():
|
|
|
|
filename = "example-docs/winter-sports.epub"
|
2023-08-29 16:59:26 -04:00
|
|
|
elements = partition_epub(filename=filename)
|
|
|
|
|
2023-10-12 12:47:55 -07:00
|
|
|
assert_round_trips_through_JSON(elements)
|
2023-09-11 16:00:14 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_add_chunking_strategy_on_partition_epub(
|
2023-10-26 12:22:40 -05:00
|
|
|
filename=os.path.join(EXAMPLE_DOCS_PATH, "winter-sports.epub"),
|
2023-09-11 16:00:14 -05:00
|
|
|
):
|
|
|
|
elements = partition_epub(filename=filename)
|
|
|
|
chunk_elements = partition_epub(filename, chunking_strategy="by_title")
|
|
|
|
chunks = chunk_by_title(elements)
|
|
|
|
assert chunk_elements != elements
|
|
|
|
assert chunk_elements == chunks
|
2023-10-03 09:40:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_add_chunking_strategy_on_partition_epub_non_default(
|
2023-10-26 12:22:40 -05:00
|
|
|
filename=os.path.join(EXAMPLE_DOCS_PATH, "winter-sports.epub"),
|
2023-10-03 09:40:34 -07:00
|
|
|
):
|
|
|
|
elements = partition_epub(filename=filename)
|
|
|
|
chunk_elements = partition_epub(
|
|
|
|
filename,
|
|
|
|
chunking_strategy="by_title",
|
|
|
|
max_characters=5,
|
|
|
|
new_after_n_chars=5,
|
|
|
|
combine_text_under_n_chars=0,
|
|
|
|
)
|
|
|
|
chunks = chunk_by_title(
|
|
|
|
elements,
|
|
|
|
max_characters=5,
|
|
|
|
new_after_n_chars=5,
|
|
|
|
combine_text_under_n_chars=0,
|
|
|
|
)
|
|
|
|
assert chunk_elements != elements
|
|
|
|
assert chunk_elements == chunks
|
2023-10-10 20:47:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_partition_epub_element_metadata_has_languages():
|
2023-10-26 12:22:40 -05:00
|
|
|
filename = os.path.join(EXAMPLE_DOCS_PATH, "winter-sports.epub")
|
2023-10-10 20:47:56 -05:00
|
|
|
elements = partition_epub(filename=filename)
|
|
|
|
assert elements[0].metadata.languages == ["eng"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_epub_respects_detect_language_per_element():
|
|
|
|
filename = "example-docs/language-docs/eng_spa_mult.epub"
|
|
|
|
elements = partition_epub(filename=filename, detect_language_per_element=True)
|
|
|
|
langs = [element.metadata.languages for element in elements]
|
2023-10-26 12:22:40 -05:00
|
|
|
assert langs == [["eng"], ["spa", "eng"], ["eng"], ["eng"], ["spa"]]
|