-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport2json.py
395 lines (332 loc) · 11.2 KB
/
export2json.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
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
# export2json.py
# write algorithm examples to JSON as CompPrehension questions
from itertools import count
from owlready2 import *
import ctrlstrct_run
import trace_gen.styling as styling
import trace_gen.syntax as syntax
from explanations import get_leaf_classes
from trace_gen.txt2algntr import find_by_key_in, find_by_keyval_in
from pprint import pprint
# USER_LANGUAGE = 'en'
USER_LANGUAGE = 'ru'
USER_SYNTAX = 'C'
QUESTION_NAME_PREFIX = '' # default: change nothing
# QUESTION_NAME_PREFIX = '[human]' # mark manual questions
if USER_LANGUAGE == 'ru':
from jena.rusify import replace_in_text as translate_en2ru
# some global setup, once imported from somewhere else
syntax.set_allow_hidden_buttons(False)
# # for buttons "data-tooltip" to translate correctly
# syntax.set_force_button_tooltips_in_english(True)
def export_algtr2dict(alg_tr, onto):
"""onto: modifiable ontology containing schema (TBox)"""
ctrlstrct_run.clear_ontology(onto, keep_tbox=True)
ctrlstrct_run.algorithm_only_to_onto(alg_tr, onto)
statementFacts = []
answerObjects = []
tags = []
concepts = set()
answer_ids = count() # infinite generator
# "owl:NamedIndividual"
# "xsd:int"
# "xsd:string"
# "xsd:boolean"
# objectType
# object
# verb
# subjectType
# subject
### q_text = alg_tr['algorithm']['text']
alg_data = alg_tr['algorithm']
# pprint(alg_data)
algorithm_tags = syntax.algorithm_to_tags(alg_data, USER_LANGUAGE, USER_SYNTAX)
if USER_LANGUAGE == 'ru':
# patch tags containing English
key = "data-tooltip"
for d in find_by_key_in(key, algorithm_tags):
contents = d[key]
assert len(contents) == 1, contents
# print(contents[0], end=' -> ')
contents[0] = translate_en2ru(contents[0])
# print(contents[0])
# pprint(algorithm_tags)
# will be patched in make_answerObject
question_html = styling.to_html(algorithm_tags)
# pprint(question_html)
# algorithm structure
for ind in onto.individuals():
# write type(s)
for class_ in ind.is_a:
statementFacts.append({
'subjectType': "owl:NamedIndividual",
'subject': ind.name,
'verb': "rdf:type",
'objectType': "owl:Class",
'object': class_.name,
})
# write relations
for prop in ind.get_properties():
for subj, value in prop.get_relations():
if ind == subj:
# print(ind, "\t >>>> .%s >>>>\t %s" % (prop.python_name, value))
statementFacts.append({
'subjectType': "owl:NamedIndividual",
'subject': ind.name,
'verb': prop.name,
**({
'objectType': "owl:NamedIndividual",
'object': value.name,
}
if isinstance(value, Thing) else
{
'objectType': type_of(value),
'object': value,
})
})
# expr_values
for ind in onto.expr.instances():
expr_name = ind.stmt_name
values_list = alg_data["expr_values"].get(expr_name, None)
if values_list:
#
# print("values_list:", values_list)
#
statementFacts.append({
'subjectType': "owl:NamedIndividual",
'subject': expr_name,
'verb': "not-for-reasoner:expr_values",
'objectType': "List<boolean>",
'object': ",".join([{True:'1',False:'0'}.get(v, str(v)) for v in values_list]),
})
def make_answerObject(hyperText, phase, id_, concept, answer_id=None):
if answer_id is None:
answer_id = next(answer_ids)
# make simple trace line without "nth time" tail
# strip first word (begin/end/execute) and add phase (started/finished/performed)
view_phase = {
'started': 'began',
'finished': 'ended',
'performed': 'evaluated' if concept == 'expr' else 'executed',
}[phase]
trace_act = hyperText.split(maxsplit=1)[1] + " " + view_phase
# trace_act_hypertext = trace_act
trace_act_hypertext = styling.to_html(styling.prepare_tags_for_line(trace_act))
if USER_LANGUAGE == 'ru':
# print(hyperText, end=' -> ')
hyperText = translate_en2ru(hyperText)
# print(hyperText)
trace_act_hypertext = translate_en2ru(trace_act_hypertext)
# patch ids in HTML
old_info = phase + ":" + str(id_)
new_info = old_info + ":" + trace_act_hypertext
nonlocal question_html
question_html = question_html.replace(old_info, str(answer_id))
### print("domainInfo length:", len(new_info))
return {
"answerId": answer_id,
"hyperText": hyperText,
"domainInfo": new_info,
"isRightCol": False,
"concept": concept,
"responsesLeft": [],
"responsesRight": []
}
# actions to answerObjects
action_classes = [*onto.action.descendants()]
### print(action_classes)
for ind in sorted(onto.action.instances(), key=lambda a: a.name):
if isinstance(ind, onto.algorithm): # or use `ind.is_a`
continue # no buttons for whole algorithm
action_class = [cl for cl in ind.is_a if cl in action_classes]
assert action_class, (ind, ind.is_a, alg_tr)
action_class = next(iter(get_leaf_classes(action_class))) # must exist
# print('action_classes for', ind, ':', [*ind.is_a])
# print('action_classes for', ind, ':', action_class)
concepts.add(action_class.name)
# find (first) dict with `id`
for obj_dict in find_by_keyval_in("id", ind.id, alg_data):
break
### print(obj_dict)
act_name = obj_dict["act_name"]
if isinstance(act_name, dict):
act_name = act_name["en"] # "en" should not be changed here
action_title = act_name
# note: all one-click actions should be listed here! (TODO: add if introduced in future)
if (onto.expr in ind.is_a or
onto.stmt in ind.is_a or
onto['return'] in ind.is_a or
onto['break'] in ind.is_a or
onto['continue'] in ind.is_a
):
answerObjects.append(make_answerObject(
("execute" if onto.stmt in ind.is_a else "evaluate") + " " + action_title,
"performed", ind.id, action_class.name,
))
else:
answerObjects.append(make_answerObject(
("begin") + " " + action_title,
"started", ind.id, action_class.name,
))
answerObjects.append(make_answerObject(
("end") + " " + action_title,
"finished", ind.id, action_class.name,
))
# show concepts
print("\tconcepts:", concepts)
# patch generated html ...
question_html = question_html.replace("<i class=\"play small icon\"></i>", '<img src="https://icons.bootstrap-4.ru/assets/icons/play-fill.svg" alt="Play" width="22">')
question_html = question_html.replace("<i class=\"stop small icon\"></i>", '<img src="https://icons.bootstrap-4.ru/assets/icons/stop-fill.svg" alt="Stop" width="20">')
# data-toggle="tooltip" data-placement="top" title="Tooltip on top"
question_html = question_html.replace("data-tooltip=", 'data-toggle="tooltip" title=')
question_html = question_html.replace("data-position=\"top left\"", 'data-placement="top"')
# # replace answer IDs with their positions among answerObjects
# for i, answerObject in enumerate(answerObjects):
# pattern = "answer_" + answerObject["domainInfo"]
# new_str = "answer_" + str(i)
# question_html = question_html.replace(pattern, new_str)
question_html += STYLE_HEAD
concepts = sorted(concepts)
###
mistakes = find_mistakes_for_task(concepts, alg_data)
print("\tMistakes:", len(mistakes))
# print(mistakes)
return {
"questionType": "ORDERING",
"questionData": {
"questionType": "ORDER",
"questionDomainType": "OrderActs",
"questionName": QUESTION_NAME_PREFIX + alg_tr["algorithm_name"],
"questionText": question_html,
"areAnswersRequireContext": False,
"options": {
"metadata": {},
},
"answerObjects": answerObjects,
"statementFacts": statementFacts,
},
"concepts": [ # ???
"trace",
"mistake",
*concepts,
],
"negativeLaws": [
*mistakes,
],
"tags": [ # ????
"C++",
# *concepts,
# "basics",
# "operators",
# "order",
# "evaluation"
],
"metadata": {},
###>
# "_alg_name": alg_tr["algorithm_name"],
###<
}
def type_of(literal):
if isinstance(literal, bool):
return "xsd:boolean"
if isinstance(literal, str):
return "xsd:string"
# "xsd:int"
return "xsd:" + str(type(literal).__name__)
_MISTAKES_MAP = None
def find_mistakes_for_task(concepts, alg_data=None):
global _MISTAKES_MAP
if not _MISTAKES_MAP:
_MISTAKES_MAP = read_mistakes_map()
concepts = [*concepts, *_analyze_sequences_length(alg_data), *_analyze_alternatives(alg_data)]
mistakes = []
for name, mapping in _MISTAKES_MAP.items():
price = 0
for feature, cost in mapping.items():
if _match_against_features(feature, concepts):
price += cost
if price >= 1:
mistakes.append(name)
pass
return mistakes
def _analyze_sequences_length(alg_data) -> tuple('of features'):
nontrivial_sequence_exists = False
for d in find_by_keyval_in("type", "sequence", alg_data):
if len(d["body"]) > 1:
nontrivial_sequence_exists = True
break
return () if nontrivial_sequence_exists else ('seq-of-1-max', )
def _analyze_alternatives(alg_data) -> tuple('of features'):
if_without_else_exists = False
for d in find_by_keyval_in("type", "alternative", alg_data): # "type": "alternative"
if 'else' not in (b["type"] for b in d["branches"]):
if_without_else_exists = True
break
return ('!else', ) if if_without_else_exists else ()
def _match_against_features(key: str, features: list) -> bool:
if key in features:
return True
if " & " in key:
keys = key.split(" & ")
return all(_match_against_features(k, features) for k in keys)
if key.startswith("*"):
key = key.lstrip("*")
for f in features:
if f.endswith(key):
return True
if key.startswith("!"):
key = key.lstrip("!")
return key not in features
### print(" *** Unknown feature key:", key)
return False
def read_mistakes_map():
S = '\t' # separator
mistakes_mapping = {}
import os, os.path
_dir_path = os.path.dirname(os.path.realpath(__file__)) # dir of current .py file
filepath = os.path.join(_dir_path, 'jena/mistakes-map.txt')
with open(filepath) as f:
for i, line in enumerate(f.readlines()):
if i == 0: continue
if i == 1:
# read useful header
assert line.startswith(S), line
features = line.strip().strip(S).split(S)
continue
# read mistake lines (table body)
line = line.strip().strip(S)
name, *cells = line.strip(S).split(S)
mapping = {f:float(c.strip()) for f, c in zip(features, cells) if c.strip() and c != "-"}
if mapping: # is not empty
mistakes_mapping[name] = mapping
return mistakes_mapping
STYLE_HEAD = '''<style type="text/css" media="screen">
.comp-ph-question-text {
font-family: courier; font-size: 10pt;
}
span.string { color: #555; font-style: italic }
span.atom { color: #f08; font-style: italic; font-weight: bold; }
span.comment { color: #262; font-style: italic; line-height: 1em; }
span.meta { color: #555; font-style: italic; line-height: 1em; }
span.variable { color: #700; text-decoration: underline; }
span.variable-2 { color: #b11; }
span.struct { color: #07c; font-weight: bold; }
span.number { color: #f00; font-weight: bold; }
span.program { color: #f70; font-weight: bold; }
span.function { color: #707; font-weight: bold; }
span.action { color: #077; font-weight: bold; }
span.qualifier { color: #555; }
span.keyword { color: #00a; font-weight: bold; }
span.builtin { color: #30a; }
span.link { color: #762; }
span.warning { background-color: #ff9; }
span.error { background-color: #fdd; }
span.button { background-color: #add; }
span.alg_button { color: #111; cursor: pointer; }
</style>
'''
def main():
# debug it!
print(find_mistakes_for_task(["if", "else"]))
if __name__ == '__main__':
main()