2024-05-15 11:32:51 -07:00
|
|
|
"""Test suite for `unstructured.partition.doc` module."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-05-15 18:53:15 -04:00
|
|
|
import os
|
2024-05-15 11:32:51 -07:00
|
|
|
import pathlib
|
|
|
|
import tempfile
|
2023-02-17 09:30:23 -05:00
|
|
|
|
2023-02-27 17:30:54 +01:00
|
|
|
import pytest
|
2024-05-02 09:00:00 -07:00
|
|
|
from pytest_mock import MockFixture
|
2023-02-17 09:30:23 -05:00
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
from test_unstructured.unit_utils import (
|
|
|
|
CaptureFixture,
|
|
|
|
assert_round_trips_through_JSON,
|
|
|
|
example_doc_path,
|
|
|
|
)
|
|
|
|
from unstructured.chunking.basic import chunk_elements
|
2023-02-27 17:30:54 +01:00
|
|
|
from unstructured.documents.elements import (
|
|
|
|
Address,
|
2024-05-15 11:32:51 -07:00
|
|
|
CompositeElement,
|
|
|
|
Element,
|
2023-02-27 17:30:54 +01:00
|
|
|
ListItem,
|
|
|
|
NarrativeText,
|
2023-11-01 22:22:17 -07:00
|
|
|
Table,
|
2024-05-15 11:32:51 -07:00
|
|
|
TableChunk,
|
2023-02-27 17:30:54 +01:00
|
|
|
Text,
|
|
|
|
Title,
|
|
|
|
)
|
2023-02-17 09:30:23 -05:00
|
|
|
from unstructured.partition.doc import partition_doc
|
|
|
|
from unstructured.partition.docx import partition_docx
|
|
|
|
|
2024-05-15 18:53:15 -04:00
|
|
|
is_in_docker = os.path.exists("/.dockerenv")
|
2023-02-17 09:30:23 -05:00
|
|
|
|
2024-05-15 18:53:15 -04:00
|
|
|
|
|
|
|
def test_partition_doc_matches_partition_docx(request):
|
|
|
|
# NOTE(robinson) - was having issues with the tempfile not being found in the docker tests
|
|
|
|
if is_in_docker:
|
|
|
|
request.applymarker(pytest.mark.xfail)
|
2024-05-15 11:32:51 -07:00
|
|
|
doc_file_path = example_doc_path("simple.doc")
|
|
|
|
docx_file_path = example_doc_path("simple.docx")
|
|
|
|
|
|
|
|
assert partition_doc(doc_file_path) == partition_docx(docx_file_path)
|
2023-02-17 09:30:23 -05:00
|
|
|
|
|
|
|
|
2024-05-14 13:57:31 -07:00
|
|
|
# -- document-source (file or filename) ----------------------------------------------------------
|
2023-02-17 09:30:23 -05:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_filename(expected_elements: list[Element], capsys: CaptureFixture[str]):
|
|
|
|
elements = partition_doc(example_doc_path("simple.doc"))
|
|
|
|
|
2023-02-17 09:30:23 -05:00
|
|
|
assert elements == expected_elements
|
2024-05-15 11:32:51 -07:00
|
|
|
assert all(e.metadata.filename == "simple.doc" for e in elements)
|
|
|
|
assert all(e.metadata.file_directory == example_doc_path("") for e in elements)
|
2023-06-08 12:33:06 -04:00
|
|
|
assert capsys.readouterr().out == ""
|
|
|
|
assert capsys.readouterr().err == ""
|
|
|
|
|
2023-02-17 09:30:23 -05:00
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_file_with_libre_office_filter(
|
|
|
|
expected_elements: list[Element], capsys: CaptureFixture[str]
|
|
|
|
):
|
|
|
|
with open(example_doc_path("simple.doc"), "rb") as f:
|
2023-07-17 13:54:44 -04:00
|
|
|
elements = partition_doc(file=f, libre_office_filter="MS Word 2007 XML")
|
2024-05-15 11:32:51 -07:00
|
|
|
|
2023-07-17 13:54:44 -04:00
|
|
|
assert elements == expected_elements
|
|
|
|
assert capsys.readouterr().out == ""
|
|
|
|
assert capsys.readouterr().err == ""
|
2024-05-15 11:32:51 -07:00
|
|
|
assert all(e.metadata.filename is None for e in elements)
|
2023-07-17 13:54:44 -04:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_file_with_no_libre_office_filter(
|
|
|
|
expected_elements: list[Element], capsys: CaptureFixture[str]
|
|
|
|
):
|
|
|
|
with open(example_doc_path("simple.doc"), "rb") as f:
|
2023-07-17 13:54:44 -04:00
|
|
|
elements = partition_doc(file=f, libre_office_filter=None)
|
2024-05-15 11:32:51 -07:00
|
|
|
|
2023-02-17 09:30:23 -05:00
|
|
|
assert elements == expected_elements
|
2023-06-08 12:33:06 -04:00
|
|
|
assert capsys.readouterr().out == ""
|
|
|
|
assert capsys.readouterr().err == ""
|
2024-05-15 11:32:51 -07:00
|
|
|
assert all(e.metadata.filename is None for e in elements)
|
2023-07-05 15:02:22 -05:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_raises_when_both_a_filename_and_file_are_specified():
|
|
|
|
doc_file_path = example_doc_path("simple.doc")
|
2023-02-17 09:30:23 -05:00
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
with open(doc_file_path, "rb") as f:
|
|
|
|
with pytest.raises(ValueError, match="Exactly one of filename and file must be specified"):
|
|
|
|
partition_doc(filename=doc_file_path, file=f)
|
2023-02-17 09:30:23 -05:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_raises_when_neither_a_file_path_nor_a_file_like_object_are_provided():
|
|
|
|
with pytest.raises(ValueError, match="Exactly one of filename and file must be specified"):
|
2023-02-17 09:30:23 -05:00
|
|
|
partition_doc()
|
2023-06-30 09:44:46 -05:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_raises_with_missing_doc(tmp_path: pathlib.Path):
|
|
|
|
doc_filename = str(tmp_path / "asdf.doc")
|
2024-05-14 13:57:31 -07:00
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
with pytest.raises(ValueError, match="asdf.doc does not exist"):
|
2024-05-14 13:57:31 -07:00
|
|
|
partition_doc(filename=doc_filename)
|
|
|
|
|
|
|
|
|
|
|
|
# -- `include_metadata` arg ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_filename_excludes_metadata_when_so_instructed():
|
|
|
|
elements = partition_doc(example_doc_path("simple.doc"), include_metadata=False)
|
|
|
|
assert all(e.metadata.to_dict() == {} for e in elements)
|
2023-06-30 09:44:46 -05:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_file_excludes_metadata_when_so_instructed():
|
|
|
|
with open(example_doc_path("simple.doc"), "rb") as f:
|
2024-05-14 13:57:31 -07:00
|
|
|
elements = partition_doc(file=f, include_metadata=False)
|
2023-06-30 09:44:46 -05:00
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
assert all(e.metadata.to_dict() == {} for e in elements)
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
2024-05-14 13:57:31 -07:00
|
|
|
# -- .metadata.filename --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_filename_prefers_metadata_filename_when_provided(
|
|
|
|
expected_elements: list[Element],
|
2024-05-14 13:57:31 -07:00
|
|
|
):
|
2024-05-15 11:32:51 -07:00
|
|
|
elements = partition_doc(example_doc_path("simple.doc"), metadata_filename="test")
|
2024-05-14 13:57:31 -07:00
|
|
|
|
|
|
|
assert elements == expected_elements
|
|
|
|
assert all(element.metadata.filename == "test" for element in elements)
|
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_file_prefers_metadata_filename_when_provided():
|
|
|
|
with open(example_doc_path("simple.doc"), "rb") as f:
|
2024-05-14 13:57:31 -07:00
|
|
|
elements = partition_doc(file=f, metadata_filename="test")
|
2024-05-15 11:32:51 -07:00
|
|
|
|
|
|
|
assert all(e.metadata.filename == "test" for e in elements)
|
2024-05-14 13:57:31 -07:00
|
|
|
|
|
|
|
|
|
|
|
# -- .metadata.last_modified ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2024-05-15 18:04:06 -07:00
|
|
|
def test_partition_doc_from_filename_pulls_last_modified_from_filesystem(mocker: MockFixture):
|
2024-05-15 11:32:51 -07:00
|
|
|
filesystem_last_modified = "2029-07-05T09:24:28"
|
2023-07-26 15:10:14 -04:00
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.doc.get_last_modified_date",
|
2024-05-15 11:32:51 -07:00
|
|
|
return_value=filesystem_last_modified,
|
2023-07-26 15:10:14 -04:00
|
|
|
)
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
elements = partition_doc(example_doc_path("fake.doc"))
|
2023-07-26 15:10:14 -04:00
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
assert all(e.metadata.last_modified == filesystem_last_modified for e in elements)
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_filename_prefers_metadata_last_modified_when_provided(
|
|
|
|
mocker: MockFixture,
|
2023-07-26 15:10:14 -04:00
|
|
|
):
|
2024-05-15 11:32:51 -07:00
|
|
|
filesystem_last_modified = "2029-07-05T09:24:28"
|
|
|
|
metadata_last_modified = "2020-07-05T09:24:28"
|
2023-07-26 15:10:14 -04:00
|
|
|
mocker.patch(
|
2024-05-15 11:32:51 -07:00
|
|
|
"unstructured.partition.doc.get_last_modified_date", return_value=filesystem_last_modified
|
2023-07-26 15:10:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
elements = partition_doc(
|
2024-05-15 11:32:51 -07:00
|
|
|
example_doc_path("simple.doc"), metadata_last_modified=metadata_last_modified
|
2023-07-26 15:10:14 -04:00
|
|
|
)
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
assert all(e.metadata.last_modified == metadata_last_modified for e in elements)
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_file_suppresses_last_modified_from_file_by_default(mocker: MockFixture):
|
2024-05-02 09:00:00 -07:00
|
|
|
modified_date_on_file = "2029-07-05T09:24:28"
|
2023-07-26 15:10:14 -04:00
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.doc.get_last_modified_date_from_file",
|
2024-05-02 09:00:00 -07:00
|
|
|
return_value=modified_date_on_file,
|
2023-07-26 15:10:14 -04:00
|
|
|
)
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
with open(example_doc_path("simple.doc"), "rb") as f:
|
2023-07-26 15:10:14 -04:00
|
|
|
elements = partition_doc(file=f)
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
assert all(e.metadata.last_modified is None for e in elements)
|
2024-03-18 02:09:44 +01:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_file_pulls_last_modified_from_file_when_date_from_file_obj_arg_is_True(
|
2024-05-02 11:21:18 -07:00
|
|
|
mocker: MockFixture,
|
|
|
|
):
|
|
|
|
modified_date_on_file = "2024-05-01T09:24:28"
|
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.doc.get_last_modified_date_from_file",
|
|
|
|
return_value=modified_date_on_file,
|
|
|
|
)
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
with open(example_doc_path("simple.doc"), "rb") as f:
|
2024-05-02 11:21:18 -07:00
|
|
|
elements = partition_doc(file=f, date_from_file_object=True)
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
assert all(e.metadata.last_modified == modified_date_on_file for e in elements)
|
2024-05-02 11:21:18 -07:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_file_gets_None_last_modified_when_file_has_no_last_modified():
|
|
|
|
with open(example_doc_path("simple.doc"), "rb") as f:
|
|
|
|
sf = tempfile.SpooledTemporaryFile()
|
2024-05-14 13:57:31 -07:00
|
|
|
sf.write(f.read())
|
|
|
|
sf.seek(0)
|
|
|
|
elements = partition_doc(file=sf, date_from_file_object=True)
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
assert all(e.metadata.last_modified is None for e in elements)
|
2024-05-14 13:57:31 -07:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_from_file_prefers_metadata_last_modified_when_provided(mocker: MockFixture):
|
|
|
|
"""Even when `date_from_file_object` arg is `True`."""
|
|
|
|
last_modified_on_file = "2029-07-05T09:24:28"
|
|
|
|
metadata_last_modified = "2020-07-05T09:24:28"
|
2023-07-26 15:10:14 -04:00
|
|
|
mocker.patch(
|
|
|
|
"unstructured.partition.doc.get_last_modified_date_from_file",
|
2024-05-15 11:32:51 -07:00
|
|
|
return_value=last_modified_on_file,
|
2023-07-26 15:10:14 -04:00
|
|
|
)
|
2024-05-15 11:32:51 -07:00
|
|
|
|
|
|
|
with open(example_doc_path("simple.doc"), "rb") as f:
|
2024-03-18 02:09:44 +01:00
|
|
|
elements = partition_doc(
|
2024-05-15 11:32:51 -07:00
|
|
|
file=f, metadata_last_modified=metadata_last_modified, date_from_file_object=True
|
2024-03-18 02:09:44 +01:00
|
|
|
)
|
2023-07-26 15:10:14 -04:00
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
assert all(e.metadata.last_modified == metadata_last_modified for e in elements)
|
2023-07-26 15:10:14 -04:00
|
|
|
|
|
|
|
|
2024-05-14 13:57:31 -07:00
|
|
|
# -- language-recognition metadata ---------------------------------------------------------------
|
2023-07-26 15:10:14 -04:00
|
|
|
|
2024-05-14 13:57:31 -07:00
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_adds_languages_metadata():
|
|
|
|
elements = partition_doc(example_doc_path("simple.doc"))
|
|
|
|
assert all(e.metadata.languages == ["eng"] for e in elements)
|
2024-05-14 13:57:31 -07:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_respects_detect_language_per_element_arg():
|
|
|
|
elements = partition_doc(
|
|
|
|
example_doc_path("language-docs/eng_spa_mult.doc"), detect_language_per_element=True
|
|
|
|
)
|
|
|
|
assert [e.metadata.languages for e in elements] == [
|
|
|
|
["eng"],
|
|
|
|
["spa", "eng"],
|
|
|
|
["eng"],
|
|
|
|
["eng"],
|
|
|
|
["spa"],
|
|
|
|
]
|
2024-05-14 13:57:31 -07:00
|
|
|
|
|
|
|
|
|
|
|
# -- miscellaneous -------------------------------------------------------------------------------
|
2023-08-29 16:59:26 -04:00
|
|
|
|
|
|
|
|
2023-11-01 22:22:17 -07:00
|
|
|
def test_partition_doc_grabs_emphasized_texts():
|
|
|
|
expected_emphasized_text_contents = ["bold", "italic", "bold-italic", "bold-italic"]
|
|
|
|
expected_emphasized_text_tags = ["b", "i", "b", "i"]
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
elements = partition_doc(example_doc_path("fake-doc-emphasized-text.doc"))
|
2023-11-01 22:22:17 -07:00
|
|
|
|
|
|
|
assert isinstance(elements[0], Table)
|
|
|
|
assert elements[0].metadata.emphasized_text_contents == expected_emphasized_text_contents
|
|
|
|
assert elements[0].metadata.emphasized_text_tags == expected_emphasized_text_tags
|
|
|
|
|
|
|
|
assert elements[1] == NarrativeText("I am a bold italic bold-italic text.")
|
|
|
|
assert elements[1].metadata.emphasized_text_contents == expected_emphasized_text_contents
|
|
|
|
assert elements[1].metadata.emphasized_text_tags == expected_emphasized_text_tags
|
|
|
|
|
|
|
|
assert elements[2] == NarrativeText("I am a normal text.")
|
|
|
|
assert elements[2].metadata.emphasized_text_contents is None
|
|
|
|
assert elements[2].metadata.emphasized_text_tags is None
|
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_round_trips_through_json():
|
|
|
|
"""Elements produced can be serialized then deserialized without loss."""
|
|
|
|
assert_round_trips_through_JSON(partition_doc(example_doc_path("simple.doc")))
|
2023-08-29 16:59:26 -04:00
|
|
|
|
2023-09-11 16:00:14 -05:00
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_chunks_elements_when_chunking_strategy_is_specified():
|
|
|
|
document_path = example_doc_path("simple.doc")
|
|
|
|
elements = partition_doc(document_path)
|
|
|
|
chunks = partition_doc(document_path, chunking_strategy="basic")
|
2023-09-11 16:00:14 -05:00
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
# -- all chunks are chunk element-types --
|
|
|
|
assert all(isinstance(c, (CompositeElement, Table, TableChunk)) for c in chunks)
|
|
|
|
# -- chunks from partitioning match those produced by chunking elements in separate step --
|
|
|
|
assert chunks == chunk_elements(elements)
|
2023-10-10 20:47:56 -05:00
|
|
|
|
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
def test_partition_doc_assigns_deterministic_and_unique_element_ids():
|
|
|
|
document_path = example_doc_path("duplicate-paragraphs.doc")
|
2023-10-10 20:47:56 -05:00
|
|
|
|
2024-05-15 11:32:51 -07:00
|
|
|
ids = [element.id for element in partition_doc(document_path)]
|
|
|
|
ids_2 = [element.id for element in partition_doc(document_path)]
|
|
|
|
|
|
|
|
# -- ids should match even though partitioned separately --
|
|
|
|
assert ids == ids_2
|
|
|
|
# -- ids should be unique --
|
|
|
|
assert len(ids) == len(set(ids))
|
2023-10-10 20:47:56 -05:00
|
|
|
|
2024-05-14 13:57:31 -07:00
|
|
|
|
|
|
|
# == module-level fixtures =======================================================================
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2024-05-15 11:32:51 -07:00
|
|
|
def expected_elements() -> list[Element]:
|
2024-05-14 13:57:31 -07:00
|
|
|
return [
|
|
|
|
Title("These are a few of my favorite things:"),
|
|
|
|
ListItem("Parrots"),
|
|
|
|
ListItem("Hockey"),
|
|
|
|
Title("Analysis"),
|
|
|
|
NarrativeText("This is my first thought. This is my second thought."),
|
|
|
|
NarrativeText("This is my third thought."),
|
|
|
|
Text("2023"),
|
|
|
|
Address("DOYLESTOWN, PA 18901"),
|
|
|
|
]
|