-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
35 lines (30 loc) · 968 Bytes
/
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
import json
import numpy as np
import re
from copy import deepcopy
def read_jsonl(path: str):
with open(path, "r", encoding='utf-8') as fh:
return [json.loads(line) for line in fh.readlines() if line]
def extract_nums(s):
s = s.replace(",", "")
nums = re.findall(r"[+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?", s)
return_list = []
for i in range(len(nums)):
try:
return_list.append(eval(nums[i].strip().lstrip(" 0")))
except:
pass
return return_list
def find_formula(step):
assert step.count("<<") == step.count(">>") == 1
left, right = step.find("<<")+2, step.find(">>")
return step[left: right]
def extract_answer(completion):
ANS_RE = re.compile(r"#### (\-?[0-9\.\,]+)")
match = ANS_RE.search(completion)
if match:
match_str = match.group(1).strip()
match_str = match_str.replace(",", "")
return match_str
else:
assert False