-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
two changes: 1) a python script to combine multiple training corpus i…
…nto one; 2) push Colibrow's PR (#8). Thanks, Colibrow!
- Loading branch information
Yundi Qian
committed
Jun 16, 2021
1 parent
345011d
commit b235539
Showing
2 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# coding=utf-8 | ||
# Copyright 2020 Google LLC | ||
# | ||
# 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. | ||
|
||
r"""Combine multiple training corpus into a single training corpus. | ||
Usage: we'd like to combine training corpus corpus1 and corpus2 into | ||
combinedcorpus; we first structure the files as follows: | ||
combinedcorpus | ||
combinedcorpus/corpus1 | ||
combinedcorpus/corpus2 | ||
Running this script with | ||
python3 compiler_opt/tools/combine_training_corpus.py \ | ||
--root_dir=$PATH_TO_combinedcorpus | ||
generates combinedcorpus/module_path file. In this way corpus1 and | ||
corpus2 are combined into combinedcorpus. | ||
""" | ||
|
||
import os | ||
|
||
from absl import app | ||
from absl import flags | ||
from absl import logging | ||
|
||
import tensorflow as tf | ||
|
||
flags.DEFINE_string('root_dir', '', 'root dir of module paths to combine.') | ||
|
||
FLAGS = flags.FLAGS | ||
|
||
_FILE_NAME = 'module_paths' | ||
|
||
|
||
def main(argv): | ||
if len(argv) > 1: | ||
raise app.UsageError('Too many command-line arguments.') | ||
|
||
module_names = [] | ||
|
||
for sub_dir in tf.io.gfile.listdir(FLAGS.root_dir): | ||
path = os.path.join(FLAGS.root_dir, sub_dir, _FILE_NAME) | ||
|
||
logging.info('processing %s', path) | ||
|
||
if not tf.io.gfile.exists(path): | ||
logging.error('%s does not exist.', path) | ||
continue | ||
|
||
with tf.io.gfile.GFile(path, 'r') as f: | ||
module_names.extend( | ||
[os.path.join(sub_dir, name.rstrip('\n')) for name in f]) | ||
|
||
with tf.io.gfile.GFile(os.path.join(FLAGS.root_dir, _FILE_NAME), 'w') as f: | ||
for module in module_names: | ||
f.write(module + '\n') | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters