-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.py
286 lines (175 loc) · 6.18 KB
/
tester.py
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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from pprint import pprint
# import shlex
import sys
from PIL import Image, ImageDraw, ImageFont
from draw import TextDraw, LineDraw
from node import Node, connect_categories_and_words
class Test:
def __init__(self):
None
def TextDrawMath ():
h = "Hello"
td = TextDraw(h)
td.display()
print(20 * '---')
lines = ["Yes", "Indeed", "This is a big long sentence which is very big and which is also very long"]
coord = (100, 50)
W, H = 1000, 1000
i = Image.new("RGBA",(W,H),"white") # random
for line in lines:
td = TextDraw(line)
td.display()
print('Coord:')
print(coord)
print('Top left:')
print(td.get_top_left(coord))
print('Below:')
print(td.get_coord_below(coord))
print('After drawing:')
pprint(td.draw(i, coord))
print(20 * '---')
TextDraw('\n'.join(lines))
def TextDrawMultiline (image, coord):
lines = ["Yes", "Indeed", "This is a big long sentence which is very big and which is also very long"]
s = '\n'.join(lines)
td = TextDraw(s)
new_coord = td.draw(image, coord)
pprint(new_coord)
return new_coord
def TextDrawSeparateLines (image, coord):
lines = ["Yes", "Indeed", "This is a big long sentence which is very big and which is also very long"]
# s = '\n'.join(lines)
for line in lines:
td = TextDraw(line)
coord = td.draw(image, coord)
pprint(coord)
return coord
def LineDrawMath ():
print("hey")
coord = (100, 50)
W, H = 1000, 1000
i = Image.new("RGBA",(W,H),"white") # random
delta_x, delta_y = (50, 100)
line = LineDraw(coord, delta_x, delta_y)
line.display()
def DrawLine (image, coord, d_x, d_y):
line = LineDraw(coord, d_x, d_y)
return line.draw_line(image)
def DrawTriangle (image, coord, d_x, d_y):
line = LineDraw(coord, d_x, d_y)
return line.draw_triangle(image)
def BuildNodes ():
# not a nested structure, properly
# it's just a dict
sen = 'Jamie was wondering about the dog'.split(' ')
cats = ['N', 'I', 'V', 'P', 'D', 'N']
# todo: join them
sen_with_cats = []
for i in range(len(sen)):
sen_with_cats.append(cats[i])
sen_with_cats.append(sen[i])
pprint(sen_with_cats)
nodes = {}
node_names = ['a0', 'a1', 'b0', 'b1', 'c0', 'c1', 'd0', 'd1', 'e0', 'e1', 'f0', 'f1']
prev = None
sen_backwards = list(reversed(sen_with_cats))
print(sen)
print(sen_backwards)
for index, name in enumerate(node_names):
if prev:
nodes[name] = Node(sen_backwards[index], [prev])
else:
nodes[name] = Node(sen_backwards[index], [])
prev = nodes[name]
print("prev is type " + str(type(prev)))
print(type(nodes['a0']))
return nodes
def DrawNodeBaseZero (image, coord):
# the tree is a Node of nodes
# base case: node with no children
# nodes = BuildNodes()
nodes = Test.BuildNodes()
node = nodes['a0']
result = node.draw_node(image, coord)
return result
def DrawNodeBaseOne (image, coord):
# base case: node with one simple child
nodes = Test.BuildNodes()
node = nodes['a1']
result = node.draw_node(image, coord)
return result
def DrawNodeBaseTriangleZero (image, coord):
# base case: triangle node with zero children
result = coord
return result
def DrawNodeBaseTriangleOne (image, coord):
# seems to work
# base case: triangle node with one child
nodes = Test.BuildNodes()
node = nodes['a1']
node.is_triangle = True
result = node.draw_node(image, coord)
return result
def DrawNodeBaseTriangleMany (image, coord):
# base case: triangle node with many children
nodes = Test.BuildNodes()
node = nodes['e1']
node.is_triangle = True
result = node.draw_node(image, coord)
return result
# seemingly not working
# it just triangles the text and the text of the child immediately below it, not all children
# ...which maybe makes sense
def DrawNodeComplexA (image, coord):
# tree consists of complex children
nodes = Test.BuildNodes()
node = nodes['e1']
result = node.draw_node(image, coord)
return result
def SampleNodes ():
a = Node("I")
a1 = Node("D", [a])
a0 = Node("DP", [a1])
b = Node("love_i")
b0 = Node("I", [b])
c = Node("t_i")
c0 = Node("V", [c])
d = Node("my")
d0 = Node("D", [d])
e = Node("dog")
e0 = Node("N", [e])
iv = Node("DP", [d0, e0])
iii = Node("VP", [c0, iv])
ii = Node("I'", [b0, iii]) # empty for now, fix later
i = Node("IP", [a0, ii])
return i
def DrawNodeComplexB (image, coord):
# tree consists of complex children
root = Test.SampleNodes()
result = root.draw_node(image, coord)
return result
def main() -> int:
W, H = 2500, 1000
i = Image.new("RGBA",(W,H),"white") # random
# Test.LineDrawMath()
coord = (W/2, 50)
#
# Test.DrawLine(i, coord, 300, 100)
# out = Test.DrawLine(i, coord, 50, 200)
# out = Test.DrawTriangle(i, out, 300, 100)
# out = Test.TextDrawSeparateLines(i, out)
# i.show()
#
#
# Test.DrawNodeBaseZero(i, coord)
# Test.DrawNodeBaseOne(i, coord)
# Test.DrawNodeBaseTriangleOne(i, coord)
# not sure if this passes or not
# Test.DrawNodeBaseTriangleMany(i, coord)
#
# Test.DrawNodeComplexA(i, coord)
Test.DrawNodeComplexB(i, coord)
i.show()
return 0
if __name__ == '__main__':
sys.exit(main()) # next section explains the use of sys.exit