-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema-html-renderer.lisp
409 lines (359 loc) · 11.8 KB
/
schema-html-renderer.lisp
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
; (format t "<! --- ")
; (setf *random-state* (make-random-state t))
(load "ll-load.lisp")
(ll-load "ll-util.lisp")
(ll-load "schema-util.lisp")
(ll-load "verbalize-schemas.lisp")
; (format t " --- !>~%")
; (defparameter *USE-GPT* nil)
(defparameter *USE-GPT* t)
(defparameter *ALLOWED-SECTIONS* '(
:Roles
:Steps
:Goals
))
(defparameter *SCHEMA-WEBPAGE-TEMPLATE* (join-str-list *NEWLINE-STR* '(
"<html>"
"<head>"
"<script>"
"</script>"
"<style>"
"body {"
" background-color: #66666600;"
"}"
"p {"
" margin: 8px;"
"}"
".sec {"
"float: left;"
"background: #dfd8cc;"
"border-radius: 5px;"
"margin: 5px;"
"}"
".schema {"
" font-family: monospace;"
" font-size: 18px;"
" line-height: 0.5;"
" border: 2px solid black;"
" border-radius: 10px;"
" display: inline-block;"
" padding: 10px;"
" background: #EFE7DB;"
" flex: 1 1 auto;"
"}"
".epi-schema {"
" font-weight: bold;"
" font-size: 18px;"
"}"
".section-title {"
" font-weight: bold;"
" font-size: 18px;"
"}"
".el-prop {"
" font-size: 14px;"
"}"
".nonfluent-id {"
" font-weight: bold;"
" color: #BB0000;"
"}"
".fluent-id {"
" font-weight: bold;"
" color: #0000FF;"
"}"
".charstar {"
" font-weight: bold;"
"}"
".variable {"
" font-weight: bold;"
"}"
".eng {"
" font-family: serif;"
" font-size: 16px;"
" border-radius: 2px;"
" border: 1px dotted black;"
" color: #000000;"
; " text-shadow: 1px 0px 0px gray, -1px 0px 0px gray, 0px 1px 0px gray, 0px -1px 0px gray;"
" background: rgba(255,255,160,0.5);"
" padding: 4px;"
" margin: 4px;"
; " font-style: italic;"
; " font-weight: bold;"
"}"
".step {"
" border: 1px dotted black;"
" border-radius: 4px;"
" display: inline-block;"
" margin-left: 80px;"
" margin-top: 10px;"
" padding-bottom: 4px;"
; " background: rgba(128,128,128,0.);"
"}"
"</style>"
"<title>Schema</title>"
"</head>"
"<body>"
"~a"
"</body>"
"</html>"
)))
(ldefun rel-lum (r g b)
(+
(/ (* 0.2126 r) 255)
(/ (* 0.7152 g) 255)
(/ (* 0.0722 b) 255))
)
(ldefun raw-rand-color ()
(list
(random 256)
(random 256)
(random 256))
)
(ldefun rand-color ()
(block outer
(loop for i from 1 to 50 do (block inner
(setf cand (raw-rand-color))
(if (good-lum cand)
(return-from outer cand))
))
(return-from outer (raw-rand-color))
)
)
(ldefun pointwise-cost (c1 c2)
(sqrt (reduce #'+ (loop for i from 0 to (- (length c1) 1)
collect (expt (- (nth i c1) (nth i c2)) 2))))
)
(ldefun cost (c colors)
(pointwise-cost c
(min-all colors (lambda (x)
(pointwise-cost c x))))
)
(ldefun good-lum (c)
(and
(<= (apply #'rel-lum c) 0.4)
(>= (apply #'rel-lum c) 0.1))
)
(ldefun children (c)
(block outer
(setf new-cs (list
(list (min 255 (+ 1 (car c))) (second c) (third c))
(list (max 0 (- 1 (car c))) (second c) (third c))
(list (car c) (min 255 (+ 1 (second c))) (third c))
(list (car c) (max 0 (- 1 (second c))) (third c))
(list (car c) (second c) (min 255 (+ 1 (third c))))
(list (car c) (second c) (max 0 (- 1 (third c))))
c))
(setf new-cs (loop for nc in new-cs
if (good-lum nc)
collect nc))
(return-from outer new-cs)
)
)
(ldefun best-child (c colors)
(max-all (children c)
(lambda (x) (cost x
(loop for y in colors
if (not (equal y c))
collect y))))
)
(ldefun n-colors (n)
(block outer
(setf colors (loop for i from 1 to n
collect (rand-color)))
(if (equal n 1)
(return-from outer colors))
(setf cost-window (list))
(loop for i from 1 to 50000 do (block inner
(setf colors (loop for c in colors
collect (best-child c colors)))
(setf total-cost (reduce #'+ (loop for x in colors
collect (cost x (loop for y in colors
if (not (equal x y)) collect y)))))
(setf cost-window (append cost-window (list total-cost)))
(if (>= (length cost-window) 5)
(setf cost-window (subseq cost-window
(- (length cost-window) 5)
(length cost-window))))
(if (and
(equal 5 (length cost-window))
(< (abs (- (car (last cost-window)) (car cost-window))) 0.001))
; then
(return-from outer colors))
))
(return-from outer colors)
)
)
(ldefun prop-html (prop var-color-map)
(let ((var-color-map (if (null var-color-map) (make-hash-table :test #'equal) var-color-map)))
(cond
((not (null (gethash prop var-color-map)))
; then
(let ((col (gethash prop var-color-map)))
(format nil "<span style='border: 1px dotted black; border-radius: 2px; background: rgba(~d, ~d, ~d, 0.5);'>~a</span>" (car col) (second col) (third col) (if (listp prop)
; then
(concat-strs "("
(join-str-list " "
(loop for e in prop
collect (prop-html e var-color-map))) ")")
; else
prop))))
((listp prop)
; then
(concat-strs
"("
(join-str-list " "
(loop for e in prop
collect (prop-html e var-color-map)))
")"
))
((lex-verb? prop)
; then
(format nil "<span style='color: #0000FF; font-weight: bold;'>~a</span>" prop))
(t
(format nil "~a" prop)))
))
(ldefun mk-var-color-map (vars)
(block outer
(setf var-colors (n-colors (length vars)))
(setf var-color-map (mk-hashtable
(loop for i from 0 to (- (length vars) 1)
collect (list (nth i vars) (nth i var-colors)))))
(return-from outer var-color-map)
)
)
(ldefun schema-html (schema)
(block outer
(setf verbal-steps (mapcar #'cdr (cdr (car (loop for sec in (verbalize-schema schema)
if (and (listp sec) (equal (car sec) 'STEPS.))
collect sec)))))
(setf verbal-goals nil)
(handler-case
(setf verbal-goals (mapcar #'cdr (cdr (car (loop for sec in (verbalize-schema schema)
if (and (listp sec) (equal (car sec) 'GOALS.))
collect sec)))))
(error () nil))
(setf verbal-preconds nil)
(handler-case
(setf verbal-preconds (mapcar #'cdr (cdr (car (loop for sec in (verbalize-schema schema)
if (and (listp sec) (equal (car sec) 'PRECONDS.))
collect sec)))))
(error () nil))
(setf verbal-preconds nil)
(handler-case
(setf verbal-postconds (mapcar #'cdr (cdr (car (loop for sec in (verbalize-schema schema)
if (and (listp sec) (equal (car sec) 'EFFECTS.))
collect sec)))))
(error () nil))
;(setf verbal-roles (cddr (car (loop for sec in (verbalize-schema schema)
;if (and (listp sec) (equal (car sec) 'ADDITIONAL) (equal (second sec) 'ROLES.))
;collect sec))))
; (setf vars (dedupe (get-elements-pred schema #'varp)))
(setf vars (dedupe (mapcar #'car (mapcar #'second
(section-formulas (get-section schema ':Roles))))))
(setf var-color-map (mk-var-color-map vars))
(setf buf (list))
(setf buf (append buf (list
"<div class='schema'>")))
(setf buf (append buf (list "<div style='float: top; margin-bottom: 20px;'>")))
(setf buf (append buf (list
"<span class='epi-schema'>Learned Schema</span>")))
(setf buf (append buf (list
(format nil "<span class='el-prop'>~a</span>" (prop-html (car (schema-header schema)) var-color-map)))))
(setf buf (append buf (list
"<span class='charstar'>**</span>")))
(setf buf (append buf (list
(format nil "<span class='fluent-id'>~a</span>" (third (schema-header schema))))))
(setf buf (append buf (list "</div>")))
(loop for sec in (nonmeta-sections schema) do (block sec-block
(setf sec-buf (list))
(if (not (contains *ALLOWED-SECTIONS* (section-name sec)))
(return-from sec-block))
(if (equal (section-name sec) ':Episode-relations)
(return-from sec-block))
(setf fluent (equal (section-type sec) 'FLUENT))
(setf css-class (if fluent "fluent-id" "nonfluent-id"))
(setf sec-buf (append sec-buf (list
(format nil "<p style='margin-left: 40px;'><span class='section-title'>~a</span></p>" (section-name sec)))))
(loop for phi-pair in (section-formulas sec)
for i from 0 do (block phi
; Add the verbalization
(if (and (contains *ALLOWED-SECTIONS* (section-name sec)) (equal (section-name sec) ':Steps))
(setf sec-buf (append sec-buf (list
(format nil "<p style='padding-top: 10px; margin-left: 40px;'><span class='eng'>~a</span></p>"
(if *USE-GPT*
(gpt-reverbalize (join-str-list " " (mapcar (lambda (x) (format nil "~a" x)) (nth i verbal-steps))))
(nth i verbal-steps))
)))))
(if (and (contains *ALLOWED-SECTIONS* (section-name sec)) (equal (section-name sec) ':Goals))
(setf sec-buf (append sec-buf (list
(format nil "<p style='padding-top: 10px; margin-left: 40px;'><span class='eng'>~a</span></p>"
(if *USE-GPT*
(gpt-reverbalize (join-str-list " " (mapcar (lambda (x) (format nil "~a" x)) (nth i verbal-goals))))
(nth i verbal-goals))
)))))
(if (and (contains *ALLOWED-SECTIONS* (section-name sec)) (equal (section-name sec) ':Preconds))
(setf sec-buf (append sec-buf (list
(format nil "<p style='padding-top: 10px; margin-left: 40px;'><span class='eng'>~a</span></p>"
(if *USE-GPT*
(gpt-reverbalize (join-str-list " " (mapcar (lambda (x) (format nil "~a" x)) (nth i verbal-preconds))))
(nth i verbal-preconds))
)))))
(if (and (contains *ALLOWED-SECTIONS* (section-name sec)) (equal (section-name sec) ':Postconds))
(setf sec-buf (append sec-buf (list
(format nil "<p style='padding-top: 10px; margin-left: 40px;'><span class='eng'>~a</span></p>"
(if *USE-GPT*
(gpt-reverbalize (join-str-list " " (mapcar (lambda (x) (format nil "~a" x)) (nth i verbal-postconds))))
(nth i verbal-postconds))
)))))
; Start the step div
(setf sec-buf (append sec-buf (list "<div class='step'>")))
; Add the EL prop
(setf sec-buf (append sec-buf (list
(format nil "<p><span class='el-prop'>~a</span></p>" (prop-html (second phi-pair) var-color-map)))))
; End the step div
(setf sec-buf (append sec-buf (list "</div>")))
; Add a line break
(setf sec-buf (append sec-buf (list "<br /><br />")))
))
(setf buf (append buf (list
(concat-strs "<div class='sec'>"
(join-str-list " " sec-buf)
"</div>"))))
))
(setf buf (append buf (list
"</div>")))
(return-from outer (join-str-list *NEWLINE-STR* buf))
)
)
(ldefun ngram-html (ngram ngram-page-fn ngram-schemas)
(format nil "<form method='get' action='~a'><button type='submit'><div class='eng' style='display: inline-block;'>~a</div></button></form>"
ngram-page-fn
(join-str-list "<br />"
(append
(loop for e in ngram collect (format nil "~a" e))
(list (format nil "<br /><span style='font-weight: bold; text-align: center;'>~d occurrences</span>" (length ngram-schemas))))))
)
(ldefun schema-webpage-html (schemas)
(format nil *SCHEMA-WEBPAGE-TEMPLATE*
(join-str-list "" ;"<br /><br />"
(append
; (list (format nil "<form method='get' action='index.html'><button type='submit'><div class='eng' style='display: inline-block;'>Return</div></button></form><br />"))
(list "<div style='display: flex; width: fit-content; justify-content: space-between; flex-wrap: wrap;'>")
(mapcar #'schema-html schemas)
(list "</div>"))))
)
(ldefun ngram-webpage-html (ngrams)
(format nil *SCHEMA-WEBPAGE-TEMPLATE*
(join-str-list *NEWLINE-STR* (list
"<div style='display: flex; justify-content: center; margin-bottom: 10px;'><div style='display: inline; font-size: 24px; border: 2px solid black; border-radius: 3px; background: #DDDDDD; padding: 5px;'>Click on any n-gram to view its contained schemas!</div></div>"
"<div style='display: flex; justify-content: space-between; flex-wrap: wrap;'>"
(join-str-list "<br /><br />"
(loop for i from 1 to (max-all (mapcar (lambda (x) (length (caar x))) ngrams)) append (append
(list (format nil "<div style='display: flex; justify-content: center; margin-bottom: 10px;'><div style='display: inline; font-size: 24px; border: 2px solid black; border-radius: 3px; background: #DDDDDD; padding: 5px;'>~d-grams</div></div>" i))
(loop for ng in ngrams
if (equal (length (caar ng)) i)
collect (ngram-html (caar ng) (second ng) (third ng))))))
"</div>")))
)
; (load "tests/interesting-nesl-compos.lisp")
; (setf *NESL-COMPOS* (shuffle *NESL-COMPOS*))
; (format t "~a~%" (schema-webpage-html (subseq *NESL-COMPOS* 0 20)))