forked from ndnlp/transformers_without_tears
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_data.py
More file actions
94 lines (81 loc) · 2.93 KB
/
Copy pathsplit_data.py
File metadata and controls
94 lines (81 loc) · 2.93 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
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
import argparse
import sys
import shutil
import os
import math
import all_constants as ac
# Takes a directory containing the original data, and splits it up into different versions,
# with the source side data truncated different amounts.
# To be used with preprocessing_splits in Darcey's experiments.
# The original data directory should have a structure like this:
# de_en
# dev.de
# dev.en
# test.de
# test.en
# train.de
# train.en
# You would pass in the de_en directory here (not its parent directory as with the original scripts).
# The truncated data directory will end up looking like this:
# de_en_trunc
# de0_en
# dev.de0
# dev.en
# test.de0
# test.en
# train.de0
# train.en
# de10_en
# ...
# ...
# de100_en
# ...
# Here you would pass in de_en_trunc as the argument.
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--orig-data-dir', type=str, required=True,
help='directory containing the original (un-truncated) data')
parser.add_argument('--trunc-data-dir', type=str, required=True,
help='directory to put the new, truncated data')
parser.add_argument('--source', type=str, required=True,
help='source language, e.g. de')
parser.add_argument('--target', type=str, required=True,
help='target language, e.g. en')
parser.add_argument('--split-increment', type=int, required=True,
help='percent increment for the source side splits (e.g. 5 for 5%)')
return parser
if __name__ == '__main__':
# get and process args
args = get_parser().parse_args()
orig_dir = args.orig_data_dir
trunc_dir = args.trunc_data_dir
src = args.source
tgt = args.target
inc = args.split_increment
# make the directories for the splits
if not os.path.exists(trunc_dir):
os.makedirs(trunc_dir)
dirs = {}
for i in range(0,101,inc):
dir_i = os.path.join(trunc_dir, f'{src}{i}_{tgt}')
dirs[i] = dir_i
os.mkdir(dir_i)
# copy all the target data over
for mode in [ac.TRAIN, ac.DEV, ac.TEST]:
orig_file = os.path.join(orig_dir, f'{mode}.{tgt}')
for i in range(0,101,inc):
shutil.copy(orig_file, dirs[i])
# read in the source data, do the splits
for mode in [ac.TRAIN, ac.DEV, ac.TEST]:
orig_file = os.path.join(orig_dir, f'{mode}.{src}')
with open(orig_file) as f:
orig_data = f.readlines()
for i in range(0,101,inc):
new_file = os.path.join(dirs[i], f'{mode}.{src}{i}')
with open(new_file, 'w') as f:
for orig_line in orig_data:
toks = orig_line.split()
percent = float(i)/100
keep = math.ceil(percent * len(toks))
new_line = ' '.join(toks[:keep])
f.write(new_line + '\n')