-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
666 lines (586 loc) · 16.9 KB
/
utils.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
import json
import chardet
import os
import re
from datasets import load_dataset, concatenate_datasets
from sympy import (
E,
FiniteSet,
I,
Intersection,
Interval,
Matrix,
N,
Union,
pi,
simplify,
sqrt,
)
from sympy.parsing.latex import parse_latex
from sympy.parsing.latex.errors import LaTeXParsingError
from sympy.parsing.sympy_parser import parse_expr
from sympy.utilities.exceptions import SymPyDeprecationWarning
from tqdm import tqdm
from typing import Any, Callable
import pandas as pd
STRIP_STRS = [
":",
".",
"/",
",",
"#",
"?",
"$",
'"',
"'",
# "ки" is the delimeter for Math-Shepherd
"к",
"и",
# LaTeX
"\\(",
"\\)",
"\\[",
"\\]",
]
NO_TRAILING_STRS = ["(", "[", "{", "\\"] + STRIP_STRS
NO_PRECEDING_PUNCS = ["!", ")", "]", "}", "\\\\"] + STRIP_STRS
# Answer prefixes
PRM800K_ANS_PRRFIX = "# Answer"
GSM8K_ANS_PREFIX = "####"
def get_encoding_type(file_path):
with open(file_path, 'rb') as f:
sample = f.read(1024)
cur_encoding = chardet.detect(sample)['encoding']
return cur_encoding
def read_json(file_path):
with open(file_path, 'r', encoding=get_encoding_type(file_path), errors="ignore") as f:
data = json.load(f)
return data
def write_json(data, file_path):
with open(file_path, 'w', encoding='utf-8', errors="ignore") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
def save2jsonl(name, data):
with open(name, "w") as file:
for dict_obj in data:
json_str = json.dumps(dict_obj)
file.write(json_str + "\n")
def mkdir(path):
if not os.path.exists(path):
os.makedirs(path)
def readjsonl2list(name):
data = [] # Create an empty list to store the dictionaries
with open(name, "r") as file:
for line in file:
dict_obj = json.loads(line)
data.append(dict_obj)
return data
def contains_chinese(d):
def is_chinese_char(ch):
return '\u4e00' <= ch <= '\u9fff'
def check(value):
if isinstance(value, str):
return any(is_chinese_char(ch) for ch in value)
elif isinstance(value, dict):
return any(check(v) for v in value.values())
elif isinstance(value, list):
return any(check(item) for item in value)
return False
return check(d)
def extract_code(text):
pattern = r"```(?:python)?(.*?)```"
match = re.search(pattern, text, re.DOTALL)
if match:
return match.group(1).strip()
else:
return None
def norm_str2bool(s: str) -> bool | None:
"""Converts a string representation of a boolean value to its corresponding boolean value."""
# TODO: deal with OL with boolean
if s in ['T', 'Y']:
return True
elif s in ['F', "N"]:
return False
s = str(s).lower().strip().replace("noindent", "").split(" ")
if any(pos in s for pos in ["yes", "true"]):
return True
elif any(neg in s for neg in ["no", "false"]):
return False
else:
return None
def latex2sympy_fix(s: str):
sp_symbol = parse_latex(s)
if "," in s:
first_term = None
try:
first_term = parse_latex(s.split(",")[0])
except Exception:
pass
if sp_symbol == first_term:
raise LaTeXParsingError(f"{s} != {first_term}")
return sp_symbol
def latex2sympy_interval(s: str):
"""Parse LaTeX expression like (-\\infty,0] as SymPy Interval object."""
s = s.replace(" ", "")
if "\\cup" in s:
exps = s.split("\\cup")
intervals = [latex2sympy_interval(exp) for exp in exps]
return Union(*intervals)
if "\\cap" in s:
exps = s.split("\\cap")
intervals = [latex2sympy_interval(exp) for exp in exps]
return Intersection(*intervals)
if s.startswith("\\{") and s.endswith("\\}"):
return FiniteSet(simplify(latex2sympy_fix(s[2:-2])))
elif s.startswith("{") and s.endswith("}"):
return FiniteSet(simplify(latex2sympy_fix(s[1:-1])))
if s.startswith("("):
left_open = True
s = s[1:]
elif s.startswith("\\("):
left_open = True
s = s[2:]
elif s.startswith("["):
left_open = False
s = s[1:]
elif s.startswith("\\["):
left_open = False
s = s[2:]
else:
raise ValueError(f"Invalid interval: {s}")
if s.endswith(")"):
right_open = True
s = s[:-1]
elif s.endswith("\\)"):
right_open = True
s = s[:-2]
elif s.endswith("]"):
right_open = False
s = s[:-1]
elif s.endswith("\\]"):
right_open = False
s = s[:-2]
else:
raise ValueError(f"Invalid interval: {s}")
left, right = s.split(",")
left = simplify(latex2sympy_fix(left))
right = simplify(latex2sympy_fix(right))
if left.is_comparable and right.is_comparable and left >= right:
raise ValueError(f"Invalid interval: {left}, {right}")
interval = Interval(left, right, left_open, right_open)
return interval
PAREN_MAP = {
r"\(": r"\)",
r"\[": r"\]",
r"\{": r"\}",
"(": ")",
"[": "]",
"{": "}",
}
DATETIME_FMTS = [
# Date formats
"%Y-%m-%d",
"%d/%m/%Y",
"%m/%d/%Y",
"%Y/%m/%d",
# Date and time formats
"%Y-%m-%d %H:%M:%S",
"%d/%m/%Y %H:%M:%S",
"%m/%d/%Y %H:%M:%S",
"%Y/%m/%d %H:%M:%S",
"%Y-%m-%d %H:%M",
"%d/%m/%Y %H:%M",
"%m/%d/%Y %H:%M",
"%Y/%m/%d %H:%M",
# Time formats only
"%H:%M:%S",
"%H:%M",
"%I:%M:%S %p",
"%I:%M %p", # 24-hour and 12-hour formats
]
BASIC_FN_NAMES = (
"sin|cos|tan|cot|sec|csc|sinh|cosh|tanh|coth|sech|csch|log|ln|exp|arcsin|arccos|arctan|arcsec|arccsc|arccot|arcsinh|arccosh|arctanh|arcsech|arccsch|arccoth"
).split("|")
UNITS = [
"hour",
"minute",
"min",
"sec",
"s",
"second",
"day",
"week",
"month",
"year",
"meter",
"mile",
"kg",
"mg",
"g",
"t",
"ton",
"nm",
"pm",
"um",
"μm",
"m",
"cm",
"mm",
"dm",
"km",
"kilometer",
"inch",
"feet",
"ft",
"piece",
"bit",
"hz",
"Hz",
"m/s",
"km/s",
"m/(min^2)",
"billion",
"eV",
"V",
"C",
"s",
"rad",
"rad/min",
"in",
"cm^3",
"V/h",
"m^2",
"L/min",
"mi/hr",
"lb",
r"a\.?m\.?",
r"(?<!\\)p\.?m\.?", # 1\pm\sqrt{5}
]
def has_non_ascii(s):
for char in s:
if ord(char) > 127:
return True
return False
def is_querying4set(query):
return "ind the" in query or ("all" in query and "separate" in query)
NDAYS_PER_WEEK = 7
WEEKDAY_ABBRS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
WEEKDAY_FULLS = [
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday",
]
def norm_str2weekday(s: str) -> str | None:
"""Converts a string representation of a weekday to its normalized form. Returns `None` if the input is not a valid weekday"""
s = str(s).lower().strip()
if " " in s: # not a word
return None
for i_day in range(NDAYS_PER_WEEK):
if s.startswith(WEEKDAY_ABBRS[i_day]):
return WEEKDAY_FULLS[i_day].capitalize()
return None
def parse(parser: Callable, s_to_parse: str, parse_errs: list[Exception]) -> Any | None:
try:
return parser(s_to_parse)
except Exception as e:
parse_errs.append(e)
return None
def norm_deg(s: str) -> str:
"""Normalize expressions including degrees, except independent <num>\\circ"""
s = s.replace("rad", "")
s = re.sub(r"^(\d+) ?\^?\\?circ$", r"\1", s)
s = re.sub(r"(\d+) ?\^?\\?circ", r"{\1*\\frac{\\pi}{180}}", s)
return s
def is_set(s: str):
return (
re.search(r"[^a-z]or(x|[^a-z])", s) is not None
or (s.startswith("{") and s.endswith("}"))
or (s.startswith("\\{") and s.endswith("\\}"))
)
def fix_sqrt(
s: str,
) -> str:
"""Fixes the formatting of square root expressions in a given string."""
_s = re.sub(r"\\?sqrt[\(\{\[](\w+)[\)\}\]]", r"\\sqrt{\1}", s)
_s = re.sub(r"\\?sqrt\s*(\d+)", r"\\sqrt{\1}", _s)
return _s
def fix_fracs(s: str) -> str:
"""Fixes the formatting of fractions in a given string."""
substrs = s.split("\\frac")
_s = substrs[0]
if len(substrs) > 1:
substrs = substrs[1:]
for substr in substrs:
_s += "\\frac"
if len(substr) > 0 and substr[0] == "{":
_s += substr
else:
try:
assert len(substr) >= 2
except Exception:
return s
a = substr[0]
b = substr[1]
if b != "{":
if len(substr) > 2:
post_substr = substr[2:]
_s += "{" + a + "}{" + b + "}" + post_substr
else:
_s += "{" + a + "}{" + b + "}"
else:
if len(substr) > 2:
post_substr = substr[2:]
_s += "{" + a + "}" + b + post_substr
else:
_s += "{" + a + "}" + b
return _s
def fix_a_slash_b(s: str) -> str:
"""
Fixes the formatting of fractions in a given string using regular expressions.
"""
# Define a regular expression to match fractions. Here we match two parts: the numerator (a) and the denominator (b).
# The numerator and denominator can be numbers (\d+) or expressions containing sqrt (sqrt\(.*?\)).
# TODO: deal with 1.124/ 2.123
fraction_pattern = r"(\b\d+\..*|sqrt\(.*?\))\/(\d+\..*|sqrt\(.*?\)\b)"
# Use `re.sub` to replace the matched fractions with properly formatted fractions.
result = re.sub(
fraction_pattern, lambda m: f"\\frac{{{m.group(1)}}}{{{m.group(2)}}}", s
)
return result
def fix_inv_func(s: str) -> str:
func_list = "arcsin|arccos|arctan|arcsec|arccsc|arccot|arcsinh|arccosh|arctanh|arcsech|arccsch|arccoth".split("|")
conv_list = "asin|acos|atan|asec|acsc|acot|asinh|acosh|atanh|asech|acsch|acoth".split("|")
for c, f in zip(conv_list, func_list):
s = s.replace(c, f)
return s
STR2NUM = {
"zero": 0,
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
"ten": 10,
"eleven": 11,
"twelve": 12,
"thirteen": 13,
"fourteen": 14,
"fifteen": 15,
"sixteen": 16,
"seventeen": 17,
"eighteen": 18,
"nineteen": 19,
"twenty": 20,
}
def rm_latex_env(s: str, env: str) -> str:
"""Remove LaTeX environment from a string.
Parameters
----------
s : str
The input string.
env : str
The LaTeX environment name to remove.
Returns
-------
str
The string with the specified LaTeX environment removed.
"""
s = s.replace(f"\\begin{{{env}}}", "")
s = s.replace(f"\\end{{{env}}}", "")
return s
LATEX_CMDS = [
"\\textbf",
"\\textit",
"\\textsl",
"\\texttt",
"\\textsc",
"\\textsf",
"\\textrm",
"\\mathrm",
"\\mathbf",
"\\mathit",
"\\mathsf",
"\\mathtt",
"\\mathbb",
"\\mathcal",
"\\mathscr",
"\\mathfrak",
"\\bm",
"\\em",
"\\emph",
"\\underline",
"\\overline",
"\\tiny",
"\\scriptsize",
"\\footnotesize",
"\\small",
"\\normalsize",
"\\large",
"\\Large",
"\\LARGE",
"\\huge",
"\\Huge",
"\\newline",
"\\par",
"\\noindent",
"\\indent",
"\\footnote",
"\\cite",
"\\ref",
"\\label",
"\\textsuperscript",
"\\textsubscript",
"\\text",
"\mbox",
"\\renewcommand{\\arraystretch}",
]
LATEX_FMT_ENVS = [
# Align
"align",
"align*",
"center",
"flushleft",
"flushright",
]
LATEX_LIST_ENVS = [
"itemize",
"enumerate",
"description",
]
SIMPLE_RM_STRS = [
"\n",
"\t",
"approximately",
"'",
'"',
"\\$",
"$",
"¥",
"£",
"€",
"{,}",
"\\!",
"\\,",
"\\:",
"\\;",
"\\quad",
"\\qquad",
"\\space",
"\\thinspace",
"\\medspace",
"\\thickspace",
"~,",
"\\ ",
# Note the order
"\\\\%",
"\\%",
"%",
"\\left",
"\\right",
"^{\\circ}",
"^\\circ",
]
SIMPLE_REPLACE_MAP = {
"∪": "\\cup",
"U": "\\cup",
"π": "\\pi",
"∞": "\\infty",
"∈": "\\in",
"∩": "\\cap",
"−": "-",
"\\item": ",",
"and": ",",
";": ",",
"infinity": "\\infty",
"+\\infty": "\\infty",
"tfrac": "frac",
"dfrac": "frac",
"\\approx": "=",
"\\times": "*",
"\\cdot": "*",
"{.": "{0.", # "{0." equivalent to "{."
" .": " 0.", # " 0." equivalent to " ."
":": "/", # Ratio like 3:2
}
def make_prompt(prompt_dict, data):
prompt_prefix_multiple = (
"The following is an undergraduate-level mathematical problem in {subject}. You need to solve the problem by completing all placeholders [ANS].\n\n"
"This problem involves {num_of_answers} placeholders [ANS] to be completed. Their answer types are, in order, {answer_type_description}.\n\n"
"Problem:\n{problem}\n\n"
'All mathematical formulas and symbols you output should be represented with LaTeX. Please end your response with: "The final answers are \\boxed{ANSWER}", where ANSWER should be the sequence of your final answers, separated by commas.'
)
prompt_prefix_single = (
"The following is an undergraduate-level mathematical problem in {subject}. You need to solve the problem by completing all placeholders [ANS].\n\n"
"This problem involves only one placeholders [ANS] to be completed. The answer type is {answer_type_description}.\n\n"
"Problem:\n{problem}\n\n"
'All mathematical formulas and symbols you output should be represented with LaTeX. Please end your response with: "The final answer is \\boxed{ANSWER}", where ANSWER should be your final answer.'
)
type2descriptions = {
"UOL": 'an unordered list of answers surrounded by parentheses with any answer types, for example, (1, x^2, True), where "unordered list" means changing the order of elements results in the same answer',
"OL": 'an ordered list of answers surrounded by parentheses with any answer types, for example, (1, x^2, True), where "ordered list" means changing the order of elements results in different answers',
"INT": 'a range inteval',
"TF": 'either True or False',
"EX": 'an expression',
"EQ": 'an equation',
"MCS": "one option of a multiple choice question with options {options}",
"MCM": "more than one option concatenated without space or commas of a multiple choice question with options {options}, for example: BD",
"NV": "a numerical value without units",
"OE": "a word, phrase, term or string that satisfies the requirements of the problem"
}
for item in data:
item['prompt'] = prompt_dict['sys_prompt'] + prompt_dict['query_prompt']
if len(item['answer']) == 1:
desc = type2descriptions[item['answer_type'][0]]
if item['answer_type'][0] in ['MCS', 'MCM']:
desc = desc.format(options=item['options'][0])
item['prompt'] += prompt_prefix_single.format(subject=item['subject'], answer_type_description=desc, problem=item['problem'], ANSWER="{ANSWER}")
else:
desc = ""
for i, ty in enumerate(item['answer_type']):
if i == 0:
desc += type2descriptions[ty]
else:
desc += ", " + type2descriptions[ty]
if ty in ['MCS', 'MCM']:
desc = desc.format(options=item['options'][i])
item['prompt'] += prompt_prefix_multiple.format(subject=item['subject'], num_of_answers=len(item['answer']), answer_type_description=desc, problem=item['problem'], ANSWER="{ANSWER}")
item['prompt'] += prompt_dict['prompt_after_query'] + prompt_dict['resp_prompt'] + prompt_dict['prompt_before_resp']
return data
def last_boxed_only_string(string):
idx = string.rfind("\\boxed")
if idx < 0:
idx = string.rfind("\\fbox")
if idx < 0:
return None
i = idx
right_brace_idx = None
num_left_braces_open = 0
while i < len(string):
if string[i] == "{":
num_left_braces_open += 1
if string[i] == "}":
num_left_braces_open -= 1
if num_left_braces_open == 0:
right_brace_idx = i
break
i += 1
if right_brace_idx == None:
retval = None
else:
retval = string[idx:right_brace_idx + 1]
return retval
def remove_boxed(s):
left = "\\boxed{"
try:
assert s[:len(left)] == left
assert s[-1] == "}"
return s[len(left):-1]
except:
return None