File size: 3,294 Bytes
2e865d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import os
import io
from PIL import Image

from pnglatex import pnglatex
from pdf2image import convert_from_path
import jsonlines
import json

import numpy as np

from pylatex import (
    Alignat,
    Axis,
    Document,
    Figure,
    Math,
    Matrix,
    Plot,
    Section,
    Subsection,
    Tabular,
    TikZ,
)
from pylatex.utils import italic, NoEscape
from pylatex.package import Package

# Question, Answer, Raw_Score, Normalized_Score

def generate_pdf(latex, name, pre=True):
    try:
        doc = Document(default_filepath=name, geometry_options=geometry_options)
        doc.packages.append(Package('amsmath'))
        doc.packages.append(Package('graphicx'))
        if pre:
            doc.append(NoEscape(latex))
        else:
            is_text = True
            while r"\$" in latex:
                pos = latex.index(r"\$")
                if is_text:
                    # print(latex[:pos])
                    doc.append(latex[:pos])
                    is_text = False
                else:
                    # print("$" + latex[:pos] + "$")
                    doc.append(NoEscape("$" + latex[:pos] + "$"))
                    is_text = True
                latex = latex[pos+2:]
            
        doc.generate_pdf(name, clean=True, clean_tex=True, compiler="pdflatex", silent=True)
    except:
        print("pre-ran " + name)

def latex_to_image(latex, name):

    while latex.index('\n') == 0:
        latex = latex[1:]

    generate_pdf(latex, name)
    generate_pdf(latex, name, False)

    images = convert_from_path(name + '.pdf')

    for i in range(len(images)):
        # Save pages as images in the pdf
        images[i].save(name + '_' + str(i) + '.jpg', 'JPEG')

    os.remove(name + ".pdf")

if __name__=="__main__":
    print("STARTED")

    geometry_options = {"tmargin": "1cm", "lmargin": "1cm", "rmargin": "1cm", "bmargin": "1cm"}

    with jsonlines.open('dataset.jsonl') as reader_obj:
        count = 0
        for row in reader_obj.iter(type=dict, skip_invalid=True):
            question_path = "output/" + row["id"]
            if not os.path.exists(question_path): 
                os.makedirs(question_path)

            # # Test first row
            if count >= 27674:

                # print(row)
                # print(row["id"])
                # print(row["score"])
                # print(row["body"])
                # print(row["answers"])
                # print(row["answers"][0]["id"])
                # print(row["answers"][0]["body"])
                # print(row["answers"][0]["score"])
                # print(row["answers"][0]["score"])
                 
                try:
                    latex_to_image(row["body"], question_path + "/question")
                except:
                    print(count, question_path + "/question", " question could not generate")
                for i in range(len(row["answers"])):
                    try:
                        latex_to_image(row["answers"][i]["body"], question_path + "/" + row["answers"][i]["id"])
                    except:
                        print(count, question_path + "/" + row["answers"][i]["id"], " answer could not generate")
                print(count, question_path)
            count += 1
            print(count, end=" ")