-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
40 lines (37 loc) · 1.18 KB
/
preprocess.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
import os
import json
import ast
for root, dirs, files in os.walk("./python/final/jsonl"):
for name in files:
data = []
file_name = os.path.join(root, name)
with open(file_name, "r") as f:
cleaned_name = os.path.splitext(file_name)[0] + "_cleaned" + os.path.splitext(file_name)[1]
for line in f:
line = line.strip()
js = json.loads(line)
try:
if 'code' in js:
code = js['code']
else:
code = js['function']
parsed = ast.parse(code)
except SyntaxError:
pass
else:
data.append(line + '\n')
os.remove(file_name)
last_line = data.pop(-1)
data.append(last_line.strip('\n'))
with open(cleaned_name, "w") as g:
g.writelines(data)
total_data = ''
for file_name in os.listdir("./python/final/jsonl/train"):
file_name = os.path.join("./python/final/jsonl/train", file_name)
with open(file_name, 'r') as f:
data = f.read()
total_data += data
total_data += '\n'
os.remove(file_name)
with open("./python/final/jsonl/train/python_train_cleaned.jsonl", 'w') as g:
g.write(total_data.strip('\n'))