forked from tensorflow/tensor2tensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesc2code.py
More file actions
312 lines (250 loc) · 9.64 KB
/
desc2code.py
File metadata and controls
312 lines (250 loc) · 9.64 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# coding=utf-8
# Copyright 2017 The Tensor2Tensor Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Data generators for the Description2Code OpenAI data-set."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import collections
import os
import random
import re
import zipfile
# Dependency imports
from tensor2tensor.data_generators import generator_utils
from tensor2tensor.data_generators import problem
from tensor2tensor.data_generators import text_encoder
from tensor2tensor.utils import registry
import tensorflow as tf
# End-of-sentence marker.
EOS = text_encoder.EOS_ID
_DATASET_URL = "https://drive.google.com/uc?export=download&id=0Bz3fihKG133ceWNFQTQ5S0xhZUk"
_DATASET_FILENAME = "description2code_current.zip"
_DATASET_PB_PATH = "description2code_current/"
_DESC_DIR_NAME = "description"
_VOCAB_EN_FILENAME = "vocab.endefr"
_RE_CPP_INLINE_COMMENT = re.compile("//.*?\n") # Compiled once
# Constant defined for a language problem
CodingPbConstants = collections.namedtuple("CodingPbConstants", [
"code_dir_name",
"vocab_filename",
"filter_patterns",
"target_space",
])
PB_PY = CodingPbConstants(
code_dir_name="solutions_python",
vocab_filename="vocab.py",
filter_patterns=["#include", "# include", "import java."],
target_space=problem.SpaceID.PY_TOK,
)
PB_CPP = CodingPbConstants(
code_dir_name="solutions_c++",
vocab_filename="vocab.cpp",
filter_patterns=["import java."],
target_space=problem.SpaceID.CPP_TOK,
)
# Struct containing a coding problem (contains the paths to the descriptions
# and code files)
CodingPbInfo = collections.namedtuple("CodingPbInfo", "desc_file, code_files")
class Desc2CodeProblem(problem.Text2TextProblem):
"""Base class for Description2Code problems."""
@property
def is_character_level(self):
return False
@property
def num_shards(self):
return 10
@property
def use_subword_tokenizer(self):
return True
@property
def input_space_id(self):
return problem.SpaceID.EN_TOK
@property
def target_space_id(self):
return self.pb_constants.target_space
@property
def input_vocab_size(self):
return 2**15 # 32k
@property
def target_vocab_size(self):
return 2**12 # 4k
@property
def vocab_input_filename(self):
return "{}.{}".format(_VOCAB_EN_FILENAME, self.input_vocab_size)
@property
def vocab_target_filename(self):
return "{}.{}".format(
self.pb_constants.vocab_filename, self.target_vocab_size)
def preprocess_target(self, target):
"""Apply some preprocessing to the target.
For instance, remove space/tabs.
Args:
target (str): code source content
Returns:
the pre-processed string content
"""
return target
def feature_encoders(self, data_dir):
source_vocab_filename = os.path.join(data_dir, self.vocab_input_filename)
target_vocab_filename = os.path.join(data_dir, self.vocab_target_filename)
source_token = text_encoder.SubwordTextEncoder(source_vocab_filename)
target_token = text_encoder.SubwordTextEncoder(target_vocab_filename)
return {
"inputs": source_token,
"targets": target_token,
}
def generator(self, data_dir, tmp_dir, train):
# Called twice: for train and test
# Get the list of the training samples (coding challenge samples)
samples = list(generator_samples(tmp_dir, self.pb_constants))
# Split between train and dev
# Suffle to get problems from diverse sources (CodeChef and CodeForces) and
# dificulties in each set.
# Need to sort the samples first before shuffling (as walk() isn't
# deterministic)
samples.sort(key=lambda x: x.desc_file) # in-place
rng = random.Random(7531) # Local fixed seed
rng.shuffle(samples) # in-place
# Train: 5019/5228 problems
# Dev: 209/5228 problems
len_samples = len(samples)
split = len_samples // 25
samples = samples[split:] if train else samples[:split]
tf.logging.info("Number of samples for {}: {}/{}".format(
"train" if train else "dev",
len(samples),
len_samples
))
def generator_samples_content(get_source, get_target):
source, target = None, None
# Iterate over the coding samples
for sample in samples:
if get_source:
with tf.gfile.GFile(sample.desc_file, mode="r") as source_file:
source = source_file.read()
if get_target:
# Each challenge can have multiple implementations (or none)
for code_file in sample.code_files:
with tf.gfile.GFile(code_file, mode="r") as target_file:
target = target_file.read()
target = self.preprocess_target(target)
yield source, target
elif sample.code_files: # Only take the source if a target exists
yield source, target
def generator_target():
for _, target in generator_samples_content(False, True):
yield target.strip()
# Generate vocab for both source and target
source_vocab = generator_utils.get_or_generate_vocab(
data_dir, tmp_dir, self.vocab_input_filename, self.input_vocab_size)
target_vocab = generator_utils.get_or_generate_vocab_inner(
data_dir=data_dir,
vocab_filename=self.vocab_target_filename,
vocab_size=self.target_vocab_size,
generator=generator_target(),)
# Yield the training and testing samples
eos_list = [EOS]
for source, target in generator_samples_content(True, True):
source_ints = source_vocab.encode(source.strip()) + eos_list
target_ints = target_vocab.encode(target.strip()) + eos_list
yield {
"inputs": source_ints,
"targets": target_ints,
}
@registry.register_problem
class ProgrammingDesc2codePy(Desc2CodeProblem):
"""Description2Code for python problem."""
@property
def pb_constants(self):
return PB_PY
def preprocess_target(self, target):
"""Simple tab to space replacement."""
return target.replace("\t", " ")
@registry.register_problem
class ProgrammingDesc2codeCpp(Desc2CodeProblem):
"""Description2Code for C++ problem."""
@property
def pb_constants(self):
return PB_CPP
def preprocess_target(self, target):
"""Pre-process Cpp files."""
target = re.sub(_RE_CPP_INLINE_COMMENT, " ", target) # Remove comments
# The regex rule is quite simple, So will fail if a // is inside a string,
# and don't remove /* */ comments
target = " ".join(target.split()) # Normalize all spaces
return target
# Utils functions
def generator_samples(tmp_dir, pb_cst):
"""Generator for the dataset samples.
If not present, download and extract the dataset.
Args:
tmp_dir: path to the directory where to download the dataset.
pb_cst: CodingPbConstants object defining paths
Yields:
A CodingPbInfo object containing the next challenge informations.
"""
# Step1: Download dataset (eventually)
data_zip_path = generator_utils.maybe_download_from_drive(
directory=tmp_dir,
filename=_DATASET_FILENAME,
url=_DATASET_URL,
)
tf.logging.info("Data downloaded in: {}".format(data_zip_path))
# Step2: Extract dataset
# We could deduce _DATASET_PB_PATH from the zip file (instead of
# hardcoded path)
data_rootdir = os.path.join(tmp_dir, _DATASET_PB_PATH)
if not tf.gfile.Exists(data_rootdir):
with zipfile.ZipFile(data_zip_path, "r") as corpus_zip:
corpus_zip.extractall(tmp_dir)
# We could remove the extracted __MACOSX folder
tf.logging.info("Data extracted in: {}".format(tmp_dir))
else:
tf.logging.info("Data already extracted in: {}".format(tmp_dir))
# Step3: Extract the problems list on the extracted folder
def contains_samples(subdir, dirs, files): # pylint: disable=unused-argument
"""Check that the folder contains a problem."""
return (
_DESC_DIR_NAME in dirs and
pb_cst.code_dir_name in dirs
)
def next_sample(subdir, dirs, files): # pylint: disable=unused-argument
"""Return the filenames of the problem."""
# More could be extracted (like the expected inputs/outputs
# pairs, the problem difficulty, the names of the algorithmic techniques
# needed)
desc_file = os.path.join(subdir, _DESC_DIR_NAME, "description.txt")
code_files = []
# As the dataset is noisy, the program deduce the language from the file
# content.
code_pattern = os.path.join(subdir, pb_cst.code_dir_name, "*.txt")
for f in tf.gfile.Glob(code_pattern):
with tf.gfile.GFile(f, mode="r") as target_file:
# Hack to filter C++/Java files. In theory some python comments could
# make the file be concidered as C++ but in practice the chance of
# getting a false negative is low.
content = target_file.read()
if not any(p in content for p in pb_cst.filter_patterns):
code_files.append(f)
return CodingPbInfo(
desc_file=desc_file,
code_files=code_files
)
# The dataset contains problem from two different sources (CodeChef
# and CodeForces). Due to the limited number of samples, all problems from
# both sources are merged
for w in tf.gfile.Walk(data_rootdir):
if contains_samples(*w):
yield next_sample(*w)