1+ import pytest
2+ from layoutparser .elements import TextBlock
3+ from layoutparser .elements .layout_elements import Rectangle as LPRectangle
4+
5+ from unstructured_inference .constants import SUBREGION_THRESHOLD_FOR_OCR
16from unstructured_inference .inference .elements import TextRegion
27from unstructured_inference .inference .layoutelement import (
38 LayoutElement ,
49 aggregate_ocr_text_by_block ,
510 get_elements_from_ocr_regions ,
11+ merge_inferred_layout_with_ocr_layout ,
612 merge_text_regions ,
13+ supplement_layout_with_ocr_elements ,
714)
815
916
@@ -21,7 +28,7 @@ def test_aggregate_ocr_text_by_block():
2128 assert text == expected
2229
2330
24- def test_merge_text_regions (sample_layout ):
31+ def test_merge_text_regions (mock_embedded_text_regions ):
2532 expected = TextRegion (
2633 x1 = 437.83888888888885 ,
2734 y1 = 317.319341111111 ,
@@ -30,11 +37,11 @@ def test_merge_text_regions(sample_layout):
3037 text = "LayoutParser: A Unified Toolkit for Deep Learning Based Document Image" ,
3138 )
3239
33- merged_text_region = merge_text_regions (sample_layout )
40+ merged_text_region = merge_text_regions (mock_embedded_text_regions )
3441 assert merged_text_region == expected
3542
3643
37- def test_get_elements_from_ocr_regions (sample_layout ):
44+ def test_get_elements_from_ocr_regions (mock_embedded_text_regions ):
3845 expected = [
3946 LayoutElement (
4047 x1 = 437.83888888888885 ,
@@ -46,5 +53,110 @@ def test_get_elements_from_ocr_regions(sample_layout):
4653 ),
4754 ]
4855
49- elements = get_elements_from_ocr_regions (sample_layout )
56+ elements = get_elements_from_ocr_regions (mock_embedded_text_regions )
5057 assert elements == expected
58+
59+
60+ def test_supplement_layout_with_ocr_elements (mock_layout , mock_ocr_regions ):
61+ ocr_elements = [
62+ LayoutElement (
63+ r .x1 ,
64+ r .y1 ,
65+ r .x2 ,
66+ r .y2 ,
67+ text = r .text ,
68+ type = "UncategorizedText" ,
69+ )
70+ for r in mock_ocr_regions
71+ ]
72+
73+ final_layout = supplement_layout_with_ocr_elements (mock_layout , mock_ocr_regions )
74+
75+ # Check if the final layout contains the original layout elements
76+ for element in mock_layout :
77+ assert element in final_layout
78+
79+ # Check if the final layout contains the OCR-derived elements
80+ assert any (ocr_element in final_layout for ocr_element in ocr_elements )
81+
82+ # Check if the OCR-derived elements that are subregions of layout elements are removed
83+ for element in mock_layout :
84+ for ocr_element in ocr_elements :
85+ if ocr_element .is_almost_subregion_of (element , SUBREGION_THRESHOLD_FOR_OCR ):
86+ assert ocr_element not in final_layout
87+
88+
89+ def test_merge_inferred_layout_with_ocr_layout (mock_inferred_layout , mock_ocr_regions ):
90+ ocr_elements = [
91+ LayoutElement (
92+ r .x1 ,
93+ r .y1 ,
94+ r .x2 ,
95+ r .y2 ,
96+ text = r .text ,
97+ type = "UncategorizedText" ,
98+ )
99+ for r in mock_ocr_regions
100+ ]
101+
102+ final_layout = merge_inferred_layout_with_ocr_layout (mock_inferred_layout , mock_ocr_regions )
103+
104+ # Check if the inferred layout's text attribute is updated with aggregated OCR text
105+ assert final_layout [0 ].text == mock_ocr_regions [2 ].text
106+
107+ # Check if the final layout contains both original elements and OCR-derived elements
108+ assert all (element in final_layout for element in mock_inferred_layout )
109+ assert any (element in final_layout for element in ocr_elements )
110+
111+
112+ @pytest .mark .parametrize ("is_table" , [False , True ])
113+ def test_layout_element_extract_text (
114+ mock_layout_element ,
115+ mock_text_region ,
116+ mock_pil_image ,
117+ is_table ,
118+ ):
119+ if is_table :
120+ mock_layout_element .type = "Table"
121+
122+ extracted_text = mock_layout_element .extract_text (
123+ objects = [mock_text_region ],
124+ image = mock_pil_image ,
125+ extract_tables = True ,
126+ )
127+
128+ assert isinstance (extracted_text , str )
129+ assert "Sample text" in extracted_text
130+
131+ if mock_layout_element .type == "Table" :
132+ assert hasattr (mock_layout_element , "text_as_html" )
133+
134+
135+ def test_layout_element_do_dict (mock_layout_element ):
136+ expected = {
137+ "coordinates" : ((100 , 100 ), (100 , 300 ), (300 , 300 ), (300 , 100 )),
138+ "text" : "Sample text" ,
139+ "type" : "Text" ,
140+ "prob" : None ,
141+ }
142+
143+ assert mock_layout_element .to_dict () == expected
144+
145+
146+ def test_layout_element_from_region (mock_rectangle ):
147+ expected = LayoutElement (100 , 100 , 300 , 300 , None , None )
148+
149+ assert LayoutElement .from_region (mock_rectangle ) == expected
150+
151+
152+ def test_layout_element_from_lp_textblock ():
153+ mock_text_block = TextBlock (
154+ block = LPRectangle (100 , 100 , 300 , 300 ),
155+ text = "Sample Text" ,
156+ type = "Text" ,
157+ score = 0.99 ,
158+ )
159+
160+ expected = LayoutElement (100 , 100 , 300 , 300 , "Sample Text" , "Text" , 0.99 )
161+
162+ assert LayoutElement .from_lp_textblock (mock_text_block ) == expected
0 commit comments