-
Notifications
You must be signed in to change notification settings - Fork 0
/
intra_data_split.py
213 lines (187 loc) · 7.37 KB
/
intra_data_split.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
import random
random.seed(42)
data_dir = "data/"
in_dir = data_dir + "outputs-all/"
min_source_vocab = 3 # cut off source vocabulary at 10 token occurrences
min_target_vocab = 3 # cut off target vocabulary at 10 token occurrences
minibatchMaxSize = 10000 # cut off files with less than x characters
family = "angular" # select project family for intra-setting
include_JS = False
file_count = 0
project_sizes = []
for project in os.listdir(in_dir):
if "DefinitelyTyped" in project:
continue
if os.stat(in_dir + "/" + project).st_size == 0:
continue
file_count += 1
source_tokens = []
target_tokens = []
# count source tokens of each project with same suffix
with open(in_dir + "/" + project, "r", encoding="utf-8") as f:
content = [line.strip() for line in f]
for ix, line in enumerate(content): # iterate over each line in the current sourcefile
if len(line) == 0:
continue
parts = line.split("\t") # split in source and target tokens
if len(parts) < 2:
continue
source_tokens += parts[0].split(' ')
target_tokens += parts[1].split(' ')
if source_tokens[1] == "'js'" and not include_JS:
continue
if len(source_tokens) != len(target_tokens):
continue
suffix = project.split("__")[0].lower()
proj_tuple = (suffix, len(source_tokens))
for tuple in project_sizes:
if tuple[0] == suffix:
size = len(source_tokens) + tuple[1]
proj_tuple = (suffix, size)
project_sizes.pop()
project_sizes.append(proj_tuple)
big10 = []
for tuple in project_sizes:
if len(big10) < 10:
big10.append(tuple)
else:
big10 = sorted(big10, key=lambda tuple: tuple[1])
if big10[0][1] < tuple[1]:
big10[0] = tuple
# Write biggest 10 files in data/biggest10.txt
with open(data_dir + "biggest10.txt", "w+") as f:
for project in big10:
f.write(project[0])
f.write("\t")
f.write(str(project[1]))
f.write("\n")
tenth = file_count // 10
indices = list(range(file_count))
random.shuffle(indices)
train_indices = indices[:(8 * len(indices)) // 10]
valid_indices = indices[(8 * len(indices)) // 10:(9 * len(indices)) // 10]
test_indices = indices[(9 * len(indices)) // 10:]
train_sources = []
train_targets = []
valid_sources = []
valid_targets = []
test_sources = []
test_targets = []
# collect all source code lines of the selected project family
content = []
for project in os.listdir(in_dir):
if "DefinitelyTyped" in project:
continue
if os.stat(in_dir + "/" + project).st_size == 0:
continue
if not project.lower().startswith(family):
continue
with open(in_dir + "/" + project, "r", encoding="utf-8") as f:
content = content + [line.strip() for line in f] # each line corresponds to one sourcefile
# split dataset into training, validation and test set
for ix, line in enumerate(content): # iterate over each sourcode-line in the project
if len(line) == 0:
continue
parts = line.split("\t") # split in source and target tokens
if len(parts) < 2:
continue
source_tokens = ["<s>"] + parts[0].split(' ') + ["</s>"]
target_tokens = ["O"] + parts[1].split(' ') + ["O"]
if source_tokens[1] == "'js'" and not include_JS:
continue
if len(source_tokens) != len(target_tokens):
print("Different lengths at line %d!" % ix)
print("%d, %d" % (len(source_tokens), len(target_tokens)))
break
if len(source_tokens) > minibatchMaxSize:
continue
# split source files into training/validation/test
if ix in train_indices:
train_sources.append(source_tokens)
train_targets.append(target_tokens)
elif ix in valid_indices:
valid_sources.append(source_tokens)
valid_targets.append(target_tokens)
elif ix in test_indices:
with open("data/intra_test_code-" + family + ".txt", 'a+') as t_code:
t_code.write(" ".join(source_tokens))
t_code.write("\n")
test_sources.append(source_tokens)
test_targets.append(target_tokens)
print("Statistics for project family: " + family)
print("Train files: %d" % len(train_sources))
print("Validation files: %d" % len(valid_sources))
print("Test files: %d" % len(test_sources))
# Vocabularies
# count the occurrence of each source and target token
source_counts = dict()
target_counts = dict()
for source in train_sources:
for t in source:
source_counts[t] = source_counts.get(t, 0) + 1
for target in train_targets:
for t in target:
target_counts[t] = target_counts.get(t, 0) + 1
# include source tokens until count < threshold
source_words = sorted(source_counts.items(), key=lambda x: x[1], reverse=True)
source_cutoff = 0
for ix, (_, count) in enumerate(source_words):
source_cutoff = ix
if count < min_source_vocab:
break
source_words = source_words[:source_cutoff]
source_word_vocab = set([word for word, _ in source_words])
if "<s>" not in source_word_vocab:
source_words.append(("<s>", 0))
source_word_vocab.add("<s>")
if "</s>" not in source_word_vocab:
source_words.append(("</s>", 0))
source_word_vocab.add("</s>")
source_words.append(("_UNKNOWN_", 0))
source_word_vocab.add("_UNKNOWN_")
# include target tokens until count < threshold
target_words = sorted(target_counts.items(), key=lambda x: x[1], reverse=True)
target_cutoff = 0
for ix, (_, count) in enumerate(target_words):
target_cutoff = ix
if count < min_target_vocab:
break
target_words = target_words[:target_cutoff]
target_word_vocab = set([word for word, _ in target_words])
with open(data_dir + "intra_source_wl-" + family, "w", encoding="utf-8") as out:
for name, count in source_words:
out.write(name)
out.write("\n")
with open(data_dir + "intra_target_wl-" + family, "w", encoding="utf-8") as out:
for name, count in target_words:
out.write(name)
out.write("\n")
print("Size of source vocab: %d" % len(source_words))
print("Size of target vocab: %d" % len(target_words))
# Output files
print("Writing train/valid/test files")
def write(wfile, sources, targets):
with open(wfile, "w", encoding="utf-8") as f:
token_count = 0
for i in range(len(sources)):
source = sources[i]
target = targets[i]
source_tokens = [token if token in source_word_vocab else '_UNKNOWN_' for token in source]
target_tokens = [token if token in target_word_vocab else '$any$' for token in target]
if len(source_tokens) != len(target_tokens):
print("Different lengths at line %d!" % ix)
print("%d, %d" % (len(source_tokens), len(target_tokens)))
token_count += len(source_tokens)
f.write(" ".join(source_tokens))
f.write("\t")
f.write(" ".join(target_tokens))
f.write("\n")
return token_count
train_file = data_dir + "intra_train-" + family + ".txt"
valid_file = data_dir + "intra_valid-" + family + ".txt"
test_file = data_dir + "intra_test-" + family + ".txt"
train_tokens = write(train_file, train_sources, train_targets)
valid_tokens = write(valid_file, valid_sources, valid_targets)
test_tokens = write(test_file, test_sources, test_targets)
print("Overall tokens: %d train, %d valid and %d test" % (train_tokens, valid_tokens, test_tokens))