Repository navigation
Expand file tree
/
Copy pathHintUtil.py
More file actions
398 lines (330 loc) · 14.9 KB
/
Copy pathHintUtil.py
File metadata and controls
398 lines (330 loc) · 14.9 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
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
__author__ = 'linglin'
"""
Generate the hint id for problem_skill.json and hint image references for hints.json,
according to skill_problem.json as where is
the start & end of hint-img-index of one problem.
"""
from os.path import expanduser
from collections import OrderedDict
import json
import re
import codecs
class HintUtil(object):
def setSettings(self, grade_id=2, section_id="1.1", numbersofHintsperProblem=[3]):
home = expanduser("~")
th_str = "th"
if grade_id == 3 : th_str = "rd"
elif grade_id == 2 : th_str = "nd"
elif grade_id == 1 : th_str = "st"
self.hints_name1 = home + '/Documents/skill_hints.json'
self.hints_name = home + "/mathjoy/app/static/src/data/math/" + str(grade_id) + th_str + "/" + section_id + "/hints.json"
self.problem_name1 = home + "/Documents/skill_problem.json"
self.problem_name = home + "/mathjoy/app/static/src/data/math/" + str(grade_id) + th_str + "/" + section_id + "/skill_problem.json"
self.problem_out = home + "/Documents/skill_problem_out.json"
self.hints_out = home + '/Documents/skill_hints_out.json'
self.hints_infile1 = home + "/Documents/skill_content.txt"
self.hints_infile = home + "/Downloads/Grade " + str(grade_id) + "-Skill " + section_id + ".txt"
self.hintNumPattern=numbersofHintsperProblem
#self.hintNumPattern=[]
#for all problems that have the same number of hints
#self.hintNumPattern.append(numberofHintsperProblem)
#for all problems that have 4, 5, 4, 5 ,... hints
#self.hintNumPattern = [4,4,4,4,4,4,5,4,4,4,4]
#self.hintNumPattern = [3,5,5,6,6,6,6,6,6,6,6]
#special hint number
#self.hintNumPattern = [2,2,2,2,3,2,2,2,3,2] -- g4 chp8.6
#self.hintNumPattern = [6,2,2,2,3,2,2,2,3,2]
#self.hintNumPattern = [3,3,3,4,3,4,3,3,3,4]
self.grade_id = grade_id
self.hintimgs = []
self.loadGradeHintImg(self.grade_id)
self.problem_data=[]
self.hints_data=[]
def readHintContent(self):
with open(self.hints_infile, 'r') as hf:
hint_contents = hf.readlines()
self.hint_array = []
hint_token = " Hint:"
#hintreg = re.compile("[0-9]+[a-z]{2} Hint:")
idx = 0
while idx < len(hint_contents):
hint_line = str(hint_contents[idx]).rstrip('\n')
#print "current_line --- ", hint_line
one_problem_hint = []
hints_dict = {}
while hint_token in hint_line:
idx += 1
one_hint = []
hint_idx = int(hint_line[0:1])
one_hint.append(str(hint_contents[idx]).rstrip('\n'))
#one_hint.append(hint_contents[idx])
one_problem_hint.insert(hint_idx-1, one_hint)
hints_dict[hint_idx] = one_hint
print "add hint --- ", hint_contents[idx]
next_line = str(hint_contents[idx+1]).rstrip('\n')
while hint_token not in next_line and next_line.strip() != "":
idx += 1
one_hint.append(str(hint_contents[idx]).rstrip('\n'))
#one_hint.append(hint_contents[idx])
if idx + 1 < len(hint_contents):
next_line = str(hint_contents[idx+1]).rstrip('\n')
else:
break
idx += 1
hint_line = str(hint_contents[idx]).rstrip('\n')
'''
for one_hint in one_problem_hint:
self.hint_array.append(one_hint)
'''
hints_dict = OrderedDict(sorted(hints_dict.items()))
debug_count = 0
for hintidx, one_hint in hints_dict.iteritems():
self.hint_array.append(one_hint)
debug_count += 1
if debug_count > 0:
print " !!!!!!!!!!!!!!!!!! debug count for hint: ", debug_count
idx += 1
for indx, cur_hint in enumerate(self.hint_array):
print indx, cur_hint
def updateHintContent(self):
if len(self.hint_array) == len(self.hints_data):
for idx, one_hint in enumerate(self.hint_array):
hint_content = " ".join(one_hint)
print idx, " || ", hint_content
self.hints_data[idx]["description"] = hint_content
def updateHintContentWithHintItem(self):
if len(self.hint_array) <= len(self.hints_data):
print "start updating Hint Content !!!!!!!!!"
for idx, one_hint in enumerate(self.hint_array):
hint_content = " ".join(one_hint)
print idx, " || ", hint_content
#self.hints_data[idx]["description"] = hint_content
self.hints_data[idx]["hintItem"][0]["value"] = hint_content
"""
pcount = (idx / 8 + 1) - 1
hintcount = (1+idx) % 8
if hintcount == 0:
hintcount = 8
"""
"""
# 4.2/math-g4-c4-s4-2-p1-h1-img1.png
if hintcount != 2 and hintcount != 4 and hintcount != 6:
img_url = "4.4/math-g4-c4-s4-4-p" + str(pcount) + "-h" + str(hintcount) + "-img1.png"
else:
img_url = ""
print " --- imgurl --- ", img_url
self.hints_data[idx]["hintItem"][1]["value"] = img_url
"""
def loadGradeHintImg(self,gradeid):
if gradeid == 3:
self.hintimgs.append("../../../theme/sea/hint-img-fish-left.png")
self.hintimgs.append("../../../theme/sea/hint-img-dolphin-left.png")
self.hintimgs.append("../../../theme/sea/hint-img-whale-left.png")
self.hintimgs.append("../../../theme/sea/hint-img-crab-left.png")
self.hintimgs.append("../../../theme/sea/hint-img-seahorse-left.png")
self.hintimgs.append("../../../theme/sea/hint-img-octopus-left.png")
return
if gradeid == 2:
self.hintimgs.append("../../theme/safari/hint-img-elephant-left.png")
self.hintimgs.append("../../theme/safari/hint-img-giraffe-left.png")
self.hintimgs.append("../../theme/safari/hint-img-hippopotamus-left.png")
self.hintimgs.append("../../theme/safari/hint-img-zebra-left.png")
self.hintimgs.append("../../theme/safari/hint-img-tiger-left.png")
self.hintimgs.append("../../theme/safari/hint-img-lion-left.png")
return
if gradeid == 4:
self.hintimgs.append("../../../theme/farm/hint-img-cat.png")
self.hintimgs.append("../../../theme/farm/hint-img-cow.png")
self.hintimgs.append("../../../theme/farm/hint-img-dog.png")
self.hintimgs.append("../../../theme/farm/hint-img-duck.png")
self.hintimgs.append("../../../theme/farm/hint-img-horse.png")
self.hintimgs.append("../../../theme/farm/hint-img-pig.png")
self.hintimgs.append("../../../theme/farm/hint-img-rabbit.png")
self.hintimgs.append("../../../theme/farm/hint-img-rooster.png")
self.hintimgs.append("../../../theme/farm/hint-img-sheep.png")
return
if gradeid == 5:
self.hintimgs.append("../../../theme/g5/hint-g5-img1.png")
self.hintimgs.append("../../../theme/g5/hint-g5-img2.png")
self.hintimgs.append("../../../theme/g5/hint-g5-img3.png")
self.hintimgs.append("../../../theme/g5/hint-g5-img4.png")
self.hintimgs.append("../../../theme/g5/hint-g5-img5.png")
self.hintimgs.append("../../../theme/g5/hint-g5-img6.png")
self.hintimgs.append("../../../theme/g5/hint-g5-img7.png")
self.hintimgs.append("../../../theme/g5/hint-g5-img8.png")
return
def getHintsData(self):
hints_str = ""
with open(self.hints_name, 'r') as hf:
hints_str = hf.read()
hints_data = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(hints_str)
print hints_data
self.hints_data = hints_data
return hints_data
def getProblemData(self):
problem_str = ""
with open(self.problem_name, 'r') as pf:
problem_str = pf.read()
problem_data = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(problem_str)
#print problem_data
self.problem_data=problem_data
return problem_data
def updateProblemId(self):
pid=0
for problem in self.problem_data:
pid +=1
problem["problem_id"] = pid
def updateHintId(self):
hid=0
for hint in self.hints_data:
hid +=1
hint["hint_id"] = hid
def updateExerciseImageId(self):
EIid=0
rePrefix = re.compile("[0-9]+.[0-9]+\/math-g[0-9]+-c[0-9]+-s[0-9]+-[0-9]+-p")
reSuffix = re.compile("-img[0-9]+.png")
for problem in self.problem_data:
if problem["assisted_exercise"]==False:
EIid +=1
if "images" in problem:
for img in problem["images"]:
if "image_name" not in img or "image_url" not in img:
continue
img_str=img["image_name"]
mpre = rePrefix.search(img_str)
msuf = reSuffix.search(img_str)
if mpre and msuf:
print mpre.group()
print msuf.group()
img["image_name"] = mpre.group() + str(EIid) + msuf.group()
img["image_url"] = mpre.group() + str(EIid) + msuf.group()
print img["image_url"]
if problem["template_type"] == "rowtype":
for row in problem["problemItems"]:
for part in row:
if "type" in part and part["type"] == "image":
img_str = part["value"]
mpre = rePrefix.search(img_str)
msuf = reSuffix.search(img_str)
if mpre and msuf:
print mpre.group()
print msuf.group()
part["value"] = mpre.group() + str(EIid) + msuf.group()
print part["value"]
else:
for problemItem in problem["problemItems"]:
if "img" in problemItem:
img_str= problemItem["img"]
mpre = rePrefix.search(img_str)
msuf = reSuffix.search(img_str)
if mpre and msuf:
print mpre.group()
print msuf.group()
problemItem["img"] = mpre.group() + str(EIid) + msuf.group()
print problemItem["img"]
if "options" in problemItem:
for optionItem in problemItem["options"]:
if "img" in optionItem:
img_str = optionItem["img"]
mpre = rePrefix.search(img_str)
msuf = reSuffix.search(img_str)
if mpre and msuf:
print mpre.group()
print msuf.group()
optionItem["img"] = mpre.group() + str(EIid) + msuf.group()
print optionItem["img"]
def updateProblemHint(self):
hintid=0
for problem in self.problem_data:
problem_id = int(problem["problem_id"])
print problem_id
hintNumPatternIndex = (problem_id-1) % len(self.hintNumPattern)
print hintNumPatternIndex
hintNum = self.hintNumPattern[hintNumPatternIndex]
hintIds=[]
for i in range(hintNum):
hintid += 1
hintIds.append(hintid)
print hintIds
problem["hintIds"]=hintIds
print problem["hintIds"]
def updateHintIcon(self):
for problem in self.problem_data:
# print problem
hintIds = problem["hintIds"]
print hintIds
hintImgIndex = 0
for hintId in hintIds:
hintIndex = int(hintId) - 1
print self.hints_data[hintIndex]
self.hints_data[hintIndex]["hinticon"] = self.hintimgs[hintImgIndex % len(self.hintimgs)]
print self.hints_data[hintIndex]
hintImgIndex += 1
def writeProblemOut(self):
problem_out_str = json.dumps(self.problem_data, indent=2, separators=(',', ': '))
print problem_out_str
with open(self.problem_out, 'w+') as of:
of.write(problem_out_str)
def writeHintsOut(self):
hints_out_str = json.dumps(self.hints_data, indent=2, separators=(',', ': '))
print hints_out_str
with open(self.hints_out, 'w+') as of:
of.write(hints_out_str)
@staticmethod
def process_updateHintAndInProblem():
hut = HintUtil()
hut.setSettings()
hut.getProblemData()
hut.updateProblemId()
hut.updateProblemHint()
hut.writeProblemOut()
hut.getHintsData()
hut.updateHintId()
hut.updateHintIcon()
hut.writeHintsOut()
@staticmethod
def process_updateHintId():
hut = HintUtil()
hut.setSettings(5,"6.14")
hut.getHintsData()
hut.updateHintId()
hut.writeHintsOut()
@staticmethod
def process_updateProblem():
hut = HintUtil()
hut.setSettings(5,"6.8",[2])
hut.getProblemData()
hut.updateProblemId()
hut.updateExerciseImageId()
hut.updateProblemHint()
hut.writeProblemOut()
@staticmethod
def process_updateHint():
hut = HintUtil()
hut.setSettings(5,"6.10")
hut.getProblemData()
hut.getHintsData()
hut.updateHintId()
hut.updateHintIcon()
hut.writeHintsOut()
@staticmethod
def process_updateHintAndContent():
hut = HintUtil()
hut.setSettings(5, "6.13")
hut.getProblemData()
hut.getHintsData()
hut.updateHintId()
hut.updateHintIcon()
hut.readHintContent()
#hut.updateHintContent()
hut.updateHintContentWithHintItem()
hut.writeHintsOut()
if __name__ == '__main__':
HintUtil.process_updateProblem()
#HintUtil.copyProblemOutToIn()
#HintUtil.process_updateHintAndContent()
#HintUtil.process_updateHint()
#HintUtil.process_updateHintId()
#hutil = HintUtil()
#hutil.setSettings(5, "4.1")
#hutil.readHintContent()