test_pdf2text_recogPara_Common.py 7.39 KB
Newer Older
赵小蒙's avatar
赵小蒙 committed
1
2
import unittest

赵小蒙's avatar
赵小蒙 committed
3
from post_proc.detect_para import (
赵小蒙's avatar
赵小蒙 committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    is_bbox_overlap,
    is_in_bbox,
    is_line_right_aligned_from_neighbors,
    is_line_left_aligned_from_neighbors,
)

# from ... pdf2text_recogPara import * # another way to import

"""
Execute the following command to run the test under directory code-clean:

    python -m test.test_para.test_pdf2text_recogPara_Common
    
    or 
    
    pytest -v -s app/pdf_toolbox/test/test_para/test_pdf2text_recogPara_Common.py
    
"""


class TestIsBboxOverlap(unittest.TestCase):
    def test_overlap(self):
        bbox1 = [0, 0, 10, 10]
        bbox2 = [5, 5, 15, 15]
        result = is_bbox_overlap(bbox1, bbox2)
        self.assertTrue(result)

    def test_no_overlap(self):
        bbox1 = [0, 0, 10, 10]
        bbox2 = [11, 11, 15, 15]
        result = is_bbox_overlap(bbox1, bbox2)
        self.assertFalse(result)

    def test_partial_overlap(self):
        bbox1 = [0, 0, 10, 10]
        bbox2 = [5, 5, 15, 15]
        result = is_bbox_overlap(bbox1, bbox2)
        self.assertTrue(result)

    def test_same_bbox(self):
        bbox1 = [0, 0, 10, 10]
        bbox2 = [0, 0, 10, 10]
        result = is_bbox_overlap(bbox1, bbox2)
        self.assertTrue(result)


# Test is_in_bbox function
class TestIsInBbox(unittest.TestCase):
    def test_bbox1_in_bbox2(self):
        bbox1 = [0, 0, 10, 10]
        bbox2 = [0, 0, 20, 20]
        result = is_in_bbox(bbox1, bbox2)
        self.assertTrue(result)

    def test_bbox1_not_in_bbox2(self):
        bbox1 = [0, 0, 30, 30]
        bbox2 = [0, 0, 20, 20]
        result = is_in_bbox(bbox1, bbox2)
        self.assertFalse(result)

    def test_bbox1_equal_to_bbox2(self):
        bbox1 = [0, 0, 20, 20]
        bbox2 = [0, 0, 20, 20]
        result = is_in_bbox(bbox1, bbox2)
        self.assertTrue(result)

    def test_bbox1_partially_in_bbox2(self):
        bbox1 = [10, 10, 30, 30]
        bbox2 = [0, 0, 20, 20]
        result = is_in_bbox(bbox1, bbox2)
        self.assertFalse(result)


# Test is_line_right_aligned_from_neighbors function
class TestIsLineRightAlignedFromNeighbors(unittest.TestCase):
    def test_right_aligned_with_prev_line(self):
        curr_line_bbox = [0, 0, 100, 100]
        prev_line_bbox = [0, 0, 90, 100]
        next_line_bbox = None
        avg_char_width = 10
        direction = 0
        result = is_line_right_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_right_aligned_with_next_line(self):
        curr_line_bbox = [0, 0, 100, 100]
        prev_line_bbox = None
        next_line_bbox = [0, 0, 110, 100]
        avg_char_width = 10
        direction = 1
        result = is_line_right_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_right_aligned_with_both_lines(self):
        curr_line_bbox = [0, 0, 100, 100]
        prev_line_bbox = [0, 0, 90, 100]
        next_line_bbox = [0, 0, 110, 100]
        avg_char_width = 10
        direction = 2
        result = is_line_right_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_not_right_aligned_with_prev_line(self):
        curr_line_bbox = [0, 0, 100, 100]
        prev_line_bbox = [0, 0, 80, 100]
        next_line_bbox = None
        avg_char_width = 10
        direction = 0
        result = is_line_right_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_not_right_aligned_with_next_line(self):
        curr_line_bbox = [0, 0, 100, 100]
        prev_line_bbox = None
        next_line_bbox = [0, 0, 120, 100]
        avg_char_width = 10
        direction = 1
        result = is_line_right_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_not_right_aligned_with_both_lines(self):
        curr_line_bbox = [0, 0, 100, 100]
        prev_line_bbox = [0, 0, 80, 100]
        next_line_bbox = [0, 0, 120, 100]
        avg_char_width = 10
        direction = 2
        result = is_line_right_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_invalid_direction(self):
        curr_line_bbox = [0, 0, 100, 100]
        prev_line_bbox = None
        next_line_bbox = None
        avg_char_width = 10
        direction = 3
        result = is_line_right_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)


# Test is_line_left_aligned_from_neighbors function
class TestIsLineLeftAlignedFromNeighbors(unittest.TestCase):

    def test_left_aligned_with_prev_line(self):
        curr_line_bbox = [10, 20, 30, 40]
        prev_line_bbox = [5, 20, 30, 40]
        next_line_bbox = None
        avg_char_width = 5.0
        direction = 0
        result = is_line_left_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_left_aligned_with_next_line(self):
        curr_line_bbox = [10, 20, 30, 40]
        prev_line_bbox = None
        next_line_bbox = [15, 20, 30, 40]
        avg_char_width = 5.0
        direction = 1
        result = is_line_left_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_left_aligned_with_both_lines(self):
        curr_line_bbox = [10, 20, 30, 40]
        prev_line_bbox = [5, 20, 30, 40]
        next_line_bbox = [15, 20, 30, 40]
        avg_char_width = 5.0
        direction = 2
        result = is_line_left_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_not_left_aligned_with_prev_line(self):
        curr_line_bbox = [10, 20, 30, 40]
        prev_line_bbox = [5, 20, 30, 40]
        next_line_bbox = None
        avg_char_width = 5.0
        direction = 0
        result = is_line_left_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_not_left_aligned_with_next_line(self):
        curr_line_bbox = [10, 20, 30, 40]
        prev_line_bbox = None
        next_line_bbox = [15, 20, 30, 40]
        avg_char_width = 5.0
        direction = 1
        result = is_line_left_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_not_left_aligned_with_both_lines(self):
        curr_line_bbox = [10, 20, 30, 40]
        prev_line_bbox = [5, 20, 30, 40]
        next_line_bbox = [15, 20, 30, 40]
        avg_char_width = 5.0
        direction = 2
        result = is_line_left_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)

    def test_invalid_direction(self):
        curr_line_bbox = [10, 20, 30, 40]
        prev_line_bbox = None
        next_line_bbox = None
        avg_char_width = 5.0
        direction = 3
        result = is_line_left_aligned_from_neighbors(curr_line_bbox, prev_line_bbox, next_line_bbox, avg_char_width, direction)
        self.assertFalse(result)


if __name__ == "__main__":
    unittest.main()