-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderAndSaveQuestion
85 lines (72 loc) · 2.65 KB
/
RenderAndSaveQuestion
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
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import pymongo
import matplotlib
matplotlib.use('TkAgg')
import pylab
import matplotlib.pyplot as plt
mongo_connection = pymongo.MongoClient("mongodb://localhost")
db = mongo_connection.mathpix
questions = db['questions']
errors =db['errors']
direcotry = '/Users/lingaraj/Desktop/questionRenders/'
#questionText -> A combination of latex and questiontext which needs to be converted and saved as an Image
def renderAndSaveFileUsingPyLab(questionId,questionText):
fig = pylab.figure()
text = fig.text(0, 0, questionText)
# Saving the figure will render the text.
dpi = 300
fig.savefig('formula.png', dpi=dpi)
# Now we can work with text's bounding box.
bbox = text.get_window_extent()
width, height = bbox.size / float(dpi) + 0.005
# Adjust the figure size so it can hold the entire text.
fig.set_size_inches((width, height))
# Adjust text's vertical position.
dy = (bbox.ymin / float(dpi)) / height
text.set_position((0, -dy))
# Save the adjusted text.
fig.savefig(direcotry+questionId+'.png', dpi=dpi)
pass
def renderAndSaveFile(questionId,questionText):
plt.title(questionText,fontsize=20)
#plt.text(questionText)
plt.box(on=False)
#plt.text(10, 10, questionText, fontsize=8)
ax = plt.gca()
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
plt.draw()
plt.savefig(direcotry+questionId+'.png',dpi=900)
pass
def updateError(question_id):
try:
cursor = errors.find_one({'questionId',question_id})
if cursor is None:
errors.insert_one({'questionId',question_id})
print('Question Id marked as error:',question_id)
except Exception as e:
print('problem inserting errors',type(e),e)
pass
def readAndRenderQuestionsCollection():
try:
cursor = questions.find({})
errorIds = []
count = 1
for doc in cursor:
print ('processing record',count)
question_id = doc['questionId']
try:
question_text = doc['questionText']
renderAndSaveFileUsingPyLab(question_id,question_text)
except Exception as e:
updateError(question_id)
errorIds.append(question_id)
print('Problem with Question Text',type(e),e)
#renderAndSaveFile(question_id,question_text)
count = count + 1
except Exception as e:
print('Error reading data from collection:',type(e),e)
pass
if __name__ == '__main__':
readAndRenderQuestionsCollection()