tzvc commited on
Commit
3ef0fca
1 Parent(s): 7c424a9

chunk dataset script

Browse files
Files changed (1) hide show
  1. chunk-dataset.py +40 -0
chunk-dataset.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import shutil
4
+
5
+ def file_exists(file_path):
6
+ return os.path.isfile(file_path)
7
+
8
+ def read_jsonl(file_path):
9
+ data = []
10
+ with open(file_path, 'r') as f:
11
+ for line in f:
12
+ data.append(json.loads(line))
13
+ return data
14
+
15
+ if __name__ == "__main__":
16
+ count = 0
17
+ chunk_nb = 0
18
+ data = read_jsonl("./data/metadata.jsonl")
19
+ os.mkdir("./data/" + f"chunk_{chunk_nb}/")
20
+ print(len(data))
21
+ for d in data:
22
+ # check if file exist
23
+ if not file_exists("./data/" + d.get("file_name")):
24
+ print("File not found: " + d.get("file_name"))
25
+ continue
26
+ new_path = "./data/" + f"chunk_{chunk_nb}/" + d.get("file_name")
27
+ shutil.copyfile("./data/" + d.get("file_name"), new_path)
28
+ # update file name in JSONL recor
29
+ d["file_name"] = f"./chunk_{chunk_nb}/" + d.get("file_name")
30
+ count += 1
31
+ if count >= 10:
32
+ chunk_nb += 1
33
+ count = 0
34
+ os.mkdir("./data/" + f"chunk_{chunk_nb}/")
35
+ # write to new JSONL file
36
+ with open("./data/metadata-chunked.jsonl", "a") as f:
37
+ f.write(json.dumps(d) + "\n")
38
+ # print(d.get("file_name"))
39
+ print(len(data))
40
+ print(count)