-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitting_data.py
More file actions
65 lines (59 loc) · 2.39 KB
/
splitting_data.py
File metadata and controls
65 lines (59 loc) · 2.39 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import sys
def split(file, train_file, test_file, val_file, proportions):
proportions = [int(i) / 100 for i in proportions]
with open(file, encoding="utf-8") as f:
for id, line in enumerate(f):
pass
print("length of the whole file:", id)
len_train = id * proportions[0]
len_test = id * proportions[1]
len_val = id * proportions[2]
print("approx. length of the splits:", len_train, len_test, len_val)
with open(file, encoding="utf-8") as f:
for id, line in enumerate(f):
if id <= len_train:
flag = "training"
with open(train_file, "a", encoding="utf-8") as train:
train.writelines(line)
elif id > len_train and id <= (len_train+len_test):
if flag == "training":
if line == "*\n":
flag = "testing"
with open(train_file, "a", encoding="utf-8") as train:
train.writelines(line)
else:
with open(train_file, "a", encoding="utf-8") as train:
train.writelines(line)
else:
with open(test_file, "a", encoding="utf-8") as test:
test.writelines(line)
elif id > (len_train+len_test):
if flag == "testing":
if line == "*\n":
flag = "validation"
with open(test_file, "a", encoding="utf-8") as test:
test.writelines(line)
else:
with open(test_file, "a", encoding="utf-8") as test:
test.writelines(line)
else:
with open(val_file, "a", encoding="utf-8") as val:
val.writelines(line)
return train_file, test_file, val_file
def main():
file = sys.argv[1]
train_file = sys.argv[2]
test_file = sys.argv[3]
val_file = sys.argv[4]
proportions = sys.argv[5]
#file = "sample.tsv"
proportions = proportions.split(":")
with open(train_file, "w", encoding="utf-8"):
pass
with open(test_file, "w", encoding="utf-8"):
pass
with open(val_file, "w", encoding="utf-8"):
pass
split(file, train_file, test_file, val_file, proportions)
if __name__=="__main__":
main()