-
Notifications
You must be signed in to change notification settings - Fork 0
/
printing.py
99 lines (84 loc) · 2.64 KB
/
printing.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
import json
class HumanEval:
def __init__(self, lines):
self.lines = lines
self.problems = {
"task_id": "dummy",
"prompt": "dummy",
"entry_point": "dummy",
"canonical_solution": "dummy",
"test": "dummy",
}
self.samples = {
"task_id": "dummy",
"completion": "dummy"
}
self.idx = 0
def fetch_id(self, start: int):
self.problems["task_id"] = 'HumanEval/{start}'.format(start=start)
def fetch_prompt(self):
prompt = ''
for i, line in enumerate(self.lines):
if (line != '\n'):
prompt += line
self.idx += 1
else:
prompt += '\n'
self.idx += 1
break
self.problems["prompt"] = prompt
def fetch_entry(self):
entry_point = ''
temp = list(self.lines[self.idx])[:-1]
for item in temp:
entry_point += item
temp.clear
self.problems["entry_point"] = entry_point
self.idx += 1
def fetch_canonical(self):
canonical = ''
while (self.lines[self.idx] != '\n'):
canonical += self.lines[self.idx]
self.idx += 1
canonical += '\n'
self.idx += 1
self.problems["canonical_solution"] = canonical
def fetch_test(self):
test = ''
while (self.idx != len(self.lines)):
test += self.lines[self.idx]
self.idx += 1
self.problems["test"] = test
def fetch_samples(self):
self.samples["task_id"] = self.problems["task_id"]
self.samples["completion"] = self.problems["canonical_solution"]
def make_problems(self, file_path: str):
with open(file_path, 'w') as f:
json.dump(self.problems, f, ensure_ascii=False)
def make_samples(self, file_path: str):
with open(file_path, 'w') as f:
json.dump(self.samples, f, ensure_ascii=False)
def main(lines: list):
a = HumanEval(lines)
# renew start parameter
a.fetch_id(start=164)
a.fetch_prompt()
a.fetch_entry()
a.fetch_canonical()
a.fetch_test()
a.fetch_samples()
problems = './data/problems.json'
samples = './data/samples.json'
a.make_problems(problems)
a.make_samples(samples)
if __name__ == "__main__":
# preprocessing
file = open('./input.txt', 'r', encoding='utf-8')
lines = file.readlines()
# convert " into \"
for idx, line in enumerate(lines):
temp = line
lines[idx] = temp
# add \n\n to last line
lines[-1] += '\n\n'
main(lines)