Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion test_unstructured_inference/inference/test_layout_element.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unstructured_inference.inference.layoutelement import LayoutElement, TextRegion
from unstructured_inference.inference.layoutelement import LayoutElement, LayoutElements, TextRegion
import numpy as np


def test_layout_element_do_dict(mock_layout_element):
Expand All @@ -18,3 +19,32 @@ def test_layout_element_from_region(mock_rectangle):
region = TextRegion(bbox=mock_rectangle)

assert LayoutElement.from_region(region) == expected


def test_layout_elements_iter_support():
coords = np.array([[0, 0, 100, 100]])
texts = np.array(["sample"])
probs = np.array([0.9])
class_ids = np.array([0])
class_id_map = {0: "Text"}
sources = np.array(["test_source"])
text_as_html = np.array(["<p>sample</p>"])
table_as_cells = np.array([None])

layout_elements = LayoutElements(
element_coords=coords,
texts=texts,
element_probs=probs,
element_class_ids=class_ids,
element_class_id_map=class_id_map,
sources=sources,
text_as_html=text_as_html,
table_as_cells=table_as_cells,
)

# New feature test: __iter__() works
elements = list(layout_elements)
assert len(elements) == 1
assert isinstance(elements[0], LayoutElement)
assert elements[0].text == "sample"
assert elements[0].type == "Text"
3 changes: 3 additions & 0 deletions unstructured_inference/inference/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ def __post_init__(self):
def __getitem__(self, indices) -> TextRegions:
return self.slice(indices)

def __iter__(self):
return self.iter_elements()

def slice(self, indices) -> TextRegions:
"""slice text regions based on indices"""
return TextRegions(
Expand Down
3 changes: 3 additions & 0 deletions unstructured_inference/inference/layoutelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def __eq__(self, other: object) -> bool:
def __getitem__(self, indices):
return self.slice(indices)

def __iter__(self):
return self.iter_elements()

def slice(self, indices) -> LayoutElements:
"""slice and return only selected indices"""
return LayoutElements(
Expand Down
Loading