-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_to_file.py
69 lines (54 loc) · 1.96 KB
/
generate_to_file.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
"""Example of how to write generated questions to text files.
Given an output directory, this will create the following subdirectories:
* train-easy
* train-medium
* train-hard
* interpolate
* extrapolate
and populate each of these directories with a text file for each of the module,
where the text file contains lines alternating between the question and the
answer.
Passing --train_split=False will create a single output directory 'train' for
training data.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
# Dependency imports
from absl import app
from absl import flags
from absl import logging
import generate
import six
from six.moves import range
FLAGS = flags.FLAGS
flags.DEFINE_string('output_dir', None, 'Where to write output text')
flags.DEFINE_boolean('train_split', False,
'Whether to split training data by difficulty')
flags.mark_flag_as_required('output_dir')
def main(unused_argv):
generate.init_modules(FLAGS.train_split)
output_dir = os.path.expanduser(FLAGS.output_dir)
if os.path.exists(output_dir):
logging.fatal('output dir %s already exists', output_dir)
logging.info('Writing to %s', output_dir)
os.makedirs(output_dir)
for regime, flat_modules in six.iteritems(generate.filtered_modules):
regime_dir = os.path.join(output_dir, regime)
os.mkdir(regime_dir)
per_module = generate.counts[regime]
for module_name, module in six.iteritems(flat_modules):
path = os.path.join(regime_dir, module_name + '.txt')
with open(path, 'w') as text_file:
for _ in range(per_module):
try:
problem, _ = generate.sample_from_module(module)
except Exception as e:
print(e)
continue
text_file.write(str(problem.question) + '\n')
text_file.write(str(problem.answer) + '\n')
logging.info('Written %s', path)
if __name__ == '__main__':
app.run(main)