|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from unstructured.chunking.basic import chunk_elements |
| 6 | +from unstructured.chunking.title import chunk_by_title |
| 7 | +from unstructured.documents.ontology import Column, Document, Page, Paragraph |
| 8 | +from unstructured.embed.openai import OpenAIEmbeddingConfig, OpenAIEmbeddingEncoder |
| 9 | +from unstructured.partition.html import partition_html |
| 10 | +from unstructured.partition.html.transformations import ( |
| 11 | + ontology_to_unstructured_elements, |
| 12 | + parse_html_to_ontology, |
| 13 | +) |
| 14 | +from unstructured.partition.json import partition_json |
| 15 | +from unstructured.staging.base import elements_from_json |
| 16 | + |
| 17 | + |
| 18 | +def test_page_number_is_passed_correctly(): |
| 19 | + ontology = Document( |
| 20 | + children=[ |
| 21 | + Page( |
| 22 | + children=[Paragraph(text="Paragraph1")], |
| 23 | + additional_attributes={"data-page-number": "1"}, |
| 24 | + ), |
| 25 | + Page( |
| 26 | + children=[Paragraph(text="Paragraph2")], |
| 27 | + additional_attributes={"data-page-number": "2"}, |
| 28 | + ), |
| 29 | + ] |
| 30 | + ) |
| 31 | + unstructured_elements = ontology_to_unstructured_elements(ontology) |
| 32 | + page1, p1, page2, p2 = unstructured_elements |
| 33 | + assert p1.metadata.page_number == 1 |
| 34 | + assert p2.metadata.page_number == 2 |
| 35 | + |
| 36 | + |
| 37 | +def test_invalid_page_number_is_not_passed(): |
| 38 | + ontology = Document( |
| 39 | + children=[ |
| 40 | + Page( |
| 41 | + children=[Paragraph(text="Paragraph1")], |
| 42 | + additional_attributes={"data-page-number": "invalid"}, |
| 43 | + ) |
| 44 | + ] |
| 45 | + ) |
| 46 | + unstructured_elements = ontology_to_unstructured_elements(ontology) |
| 47 | + page1, p1 = unstructured_elements |
| 48 | + assert not p1.metadata.page_number |
| 49 | + |
| 50 | + |
| 51 | +def test_depth_is_passed_correctly(): |
| 52 | + ontology = Document( |
| 53 | + children=[ |
| 54 | + Page(children=[Paragraph(text="Paragraph1")]), |
| 55 | + Page( |
| 56 | + children=[ |
| 57 | + Column(children=[Paragraph(text="Paragraph2")]), |
| 58 | + Column(children=[Paragraph(text="Paragraph3")]), |
| 59 | + ] |
| 60 | + ), |
| 61 | + ] |
| 62 | + ) |
| 63 | + |
| 64 | + unstructured_elements = ontology_to_unstructured_elements(ontology) |
| 65 | + page1, p1, page2, c1, p2, c2, p3 = unstructured_elements |
| 66 | + |
| 67 | + assert page1.metadata.category_depth == 0 |
| 68 | + assert page2.metadata.category_depth == 0 |
| 69 | + |
| 70 | + assert p1.metadata.category_depth == 1 |
| 71 | + |
| 72 | + assert c2.metadata.category_depth == 1 |
| 73 | + assert c1.metadata.category_depth == 1 |
| 74 | + |
| 75 | + assert p2.metadata.category_depth == 2 |
| 76 | + assert p3.metadata.category_depth == 2 |
| 77 | + |
| 78 | + |
| 79 | +def test_chunking_is_applied_on_elements(): |
| 80 | + ontology = Document( |
| 81 | + children=[ |
| 82 | + Page(children=[Paragraph(text="Paragraph1")]), |
| 83 | + Page( |
| 84 | + children=[ |
| 85 | + Column(children=[Paragraph(text="Paragraph2")]), |
| 86 | + Column(children=[Paragraph(text="Paragraph3")]), |
| 87 | + ] |
| 88 | + ), |
| 89 | + ] |
| 90 | + ) |
| 91 | + |
| 92 | + unstructured_elements = ontology_to_unstructured_elements(ontology) |
| 93 | + |
| 94 | + chunked_basic = chunk_elements(unstructured_elements) |
| 95 | + assert str(chunked_basic[0]) == "Paragraph1\n\nParagraph2\n\nParagraph3" |
| 96 | + chunked_by_title = chunk_by_title(unstructured_elements) |
| 97 | + assert str(chunked_by_title[0]) == "Paragraph1\n\nParagraph2\n\nParagraph3" |
| 98 | + |
| 99 | + |
| 100 | +def test_embeddings_are_applied_on_elements(mocker): |
| 101 | + ontology = Document( |
| 102 | + children=[ |
| 103 | + Page(children=[Paragraph(text="Paragraph1")]), |
| 104 | + Page( |
| 105 | + children=[ |
| 106 | + Column(children=[Paragraph(text="Paragraph2")]), |
| 107 | + Column(children=[Paragraph(text="Paragraph3")]), |
| 108 | + ] |
| 109 | + ), |
| 110 | + ] |
| 111 | + ) |
| 112 | + |
| 113 | + unstructured_elements = ontology_to_unstructured_elements(ontology) |
| 114 | + # Mocked client with the desired behavior for embed_documents |
| 115 | + mock_client = mocker.MagicMock() |
| 116 | + mock_client.embed_documents.return_value = [1, 2, 3, 4, 5, 6, 7] |
| 117 | + |
| 118 | + # Mock get_client to return our mock_client |
| 119 | + mocker.patch.object(OpenAIEmbeddingConfig, "get_client", return_value=mock_client) |
| 120 | + |
| 121 | + encoder = OpenAIEmbeddingEncoder(config=OpenAIEmbeddingConfig(api_key="api_key")) |
| 122 | + elements = encoder.embed_documents( |
| 123 | + elements=unstructured_elements, |
| 124 | + ) |
| 125 | + |
| 126 | + assert len(elements) == 7 |
| 127 | + |
| 128 | + page1, p1, page2, c1, p2, c2, p3 = elements |
| 129 | + |
| 130 | + assert p1.embeddings == 2 |
| 131 | + assert p2.embeddings == 5 |
| 132 | + assert p3.embeddings == 7 |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.parametrize( |
| 136 | + ("html_file_path", "json_file_path"), |
| 137 | + [ |
| 138 | + ("html_files/example.html", "unstructured_json_output/example.json"), |
| 139 | + ], |
| 140 | +) |
| 141 | +def test_ingest(html_file_path, json_file_path): |
| 142 | + html_file_path = Path(__file__).parent / html_file_path |
| 143 | + json_file_path = Path(__file__).parent / json_file_path |
| 144 | + |
| 145 | + html_code = html_file_path.read_text() |
| 146 | + expected_json_elements = elements_from_json(str(json_file_path)) |
| 147 | + |
| 148 | + ontology = parse_html_to_ontology(html_code) |
| 149 | + unstructured_elements = ontology_to_unstructured_elements(ontology) |
| 150 | + assert unstructured_elements == expected_json_elements |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.parametrize("json_file_path", ["unstructured_json_output/example.json"]) |
| 154 | +def test_parsed_ontology_can_be_serialized_from_json(json_file_path): |
| 155 | + json_file_path = Path(__file__).parent / json_file_path |
| 156 | + |
| 157 | + expected_json_elements = elements_from_json(str(json_file_path)) |
| 158 | + |
| 159 | + json_elements_text = json_file_path.read_text() |
| 160 | + elements = partition_json(text=json_elements_text) |
| 161 | + |
| 162 | + assert len(elements) == len(expected_json_elements) |
| 163 | + for i in range(len(elements)): |
| 164 | + assert elements[i] == expected_json_elements[i] |
| 165 | + # The partitioning output comes from PDF file, so only stem is compared |
| 166 | + # as the suffix is different .pdf != .json |
| 167 | + assert Path(elements[i].metadata.filename).stem == json_file_path.stem |
| 168 | + |
| 169 | + |
| 170 | +@pytest.mark.parametrize( |
| 171 | + ("html_file_path", "json_file_path"), |
| 172 | + [ |
| 173 | + ("html_files/example.html", "unstructured_json_output/example.json"), |
| 174 | + ], |
| 175 | +) |
| 176 | +def test_parsed_ontology_can_be_serialized_from_html(html_file_path, json_file_path): |
| 177 | + html_file_path = Path(__file__).parent / html_file_path |
| 178 | + json_file_path = Path(__file__).parent / json_file_path |
| 179 | + |
| 180 | + expected_json_elements = elements_from_json(str(json_file_path)) |
| 181 | + html_code = html_file_path.read_text() |
| 182 | + |
| 183 | + predicted_elements = partition_html(text=html_code, html_parser_version="v2") |
| 184 | + assert len(expected_json_elements) == len(predicted_elements) |
| 185 | + |
| 186 | + for i in range(len(expected_json_elements)): |
| 187 | + assert expected_json_elements[i] == expected_json_elements[i] |
0 commit comments