-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
74 lines (69 loc) · 2.27 KB
/
utils.py
File metadata and controls
74 lines (69 loc) · 2.27 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
import json
import numpy as np
import os
import base64
import time
import re
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def extract_answer(response):
try:
position = response.find('\\boxed{')
if position==-1:
return None
num = 1
position += 7
start = position
while num>0:
if response[position]=='{':
num+=1
elif response[position]=='}':
num-=1
position+=1
answer_extracted = response[start:position-1]
return answer_extracted
except:
return None
def write_answer(result_file, result_json):
with open(result_file, 'a') as f:
json_str = json.dumps(result_json)
f.write(json_str + '\n')
f.close()
def latex_matrix_to_list(matrix_str):
try:
matrix_str = re.sub(r"\\begin\{.*?\}", "", matrix_str)
matrix_str = re.sub(r"\\end\{.*?\}", "", matrix_str)
matrix_str = re.sub(r"\{.*?\}", "", matrix_str)
matrix_str = matrix_str.replace("}", "")
matrix_str = matrix_str.replace("]", "")
matrix_str = matrix_str.replace("[", "")
matrix_str = matrix_str.replace("\n", "")
matrix_str = matrix_str.replace(r"\text", "")
matrix_str = matrix_str.replace("json", "")
matrix_str = matrix_str.replace("`", "")
matrix_str = matrix_str.replace("\n", "")
matrix_str = matrix_str.replace("\"", "")
matrix_str = matrix_str.strip()
rows = matrix_str.split("\\")
matrix = []
for row in rows:
try:
row = row.strip()
row = row.replace(',', '&')
if row.startswith('&'):
row = row[1:]
if row.endswith('&'):
row = row[:-1]
split_elements = row.split("&")
elements = []
for num in split_elements:
stripped_num = num.strip()
int_num = int(stripped_num)
elements.append(int_num)
matrix.append(elements)
except:
continue
except:
return None
return matrix