forked from thunlp/FewRel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_split.py
More file actions
44 lines (33 loc) · 1.11 KB
/
data_split.py
File metadata and controls
44 lines (33 loc) · 1.11 KB
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
import os
import random
from shutil import copyfile
import json
random.seed(123)
ROOT_PATH = './data/'
k = 5
target_path = './data/wiki_5_splits/'
'''
Splits the training set to 5 folds.
In each split, the held out set is used for test.
'''
path = os.path.join(ROOT_PATH, 'train_wiki' + '.json')
data = json.load(open(path, 'r'))
relations = list(data.keys())
num_relations = len(relations)
rels_per_split = round(num_relations / k)
random.shuffle(relations)
for i in range(k):
split_val_rels = relations[i*rels_per_split: (i+1)*rels_per_split]
split_train = {}
split_val = {}
for rel, examples in data.items():
if rel in split_val_rels:
split_val[rel] = examples
else:
split_train[rel] = examples
print(f"split {i}: train: {len(split_val.keys())}, test: {len(split_train.keys())}")
os.makedirs(os.path.join(target_path, str(i)), exist_ok=True)
with open(os.path.join(target_path, str(i), 'train.json'), 'w') as f:
json.dump(split_train, f)
with open(os.path.join(target_path, str(i), 'val.json'), 'w') as f:
json.dump(split_val, f)