-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_classes.py
More file actions
62 lines (48 loc) · 1.69 KB
/
Copy pathmy_classes.py
File metadata and controls
62 lines (48 loc) · 1.69 KB
1
2
3
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
import numpy
class TextBox(object):
def __init__(self, line):
line_split = line.strip().split(",", maxsplit=8)
self.xspan = (int(line_split[0]), int(line_split[4]))
self.yspan = (int(line_split[1]), int(line_split[5]))
self.x = (self.xspan[0] + self.xspan[1]) / 2
self.y = (self.yspan[0] + self.yspan[1]) / 2
self.text = line_split[8]
def __repr__(self):
return self.text
class TextLine(object):
def __init__(self, text_box=None):
if isinstance(text_box, TextBox):
self.text = [text_box.text]
self.xs = [text_box.x]
self.y = text_box.y
self.yspan = text_box.yspan
else:
self.text = []
self.xs = []
self.yspan = None
def insert(self, text_box):
if not (
(text_box.yspan[0] < self.y < text_box.yspan[1])
and (self.yspan[0] < text_box.y < self.yspan[1])
):
raise ValueError
try:
at = next(i for i, v in enumerate(self.xs) if v > text_box.x)
self.text.insert(at, text_box.text)
self.xs.insert(at, text_box.x)
except StopIteration:
self.text.append(text_box.text)
self.xs.append(text_box.x)
self.y = text_box.y
self.yspan = text_box.yspan
def __str__(self):
return "\t".join(self.text)
def __repr__(self):
if self.yspan is None:
repr_yspan = "[ , ] "
else:
repr_yspan = "[{:4d},{:4d}] ".format(self.yspan[0], self.yspan[1])
repr_text = "\t".join(self.text)
return repr_yspan + repr_text
if __name__ == "__main__":
pass