-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_raw_text.py
92 lines (78 loc) · 2.71 KB
/
extract_raw_text.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
# coding=utf-8
# Copyright 2019 The Google UDA Team 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.
"""Extract raw text for back translation.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import google_type_annotations
from __future__ import print_function
import os
from absl import app
from absl import flags
import tensorflow as tf
from utils import raw_data_utils
from absl import app
from absl import flags
FLAGS = flags.FLAGS
flags.DEFINE_bool(
"separate_doc_by_newline", False, "")
flags.DEFINE_string(
"output_data_dir",
None, "")
flags.DEFINE_string(
"sub_set",
"unsup_in", "")
flags.DEFINE_string(
"task_name",
"IMDB", "")
flags.DEFINE_string(
"raw_data_dir",
"IMDB", "")
def dump_raw_examples(examples, separate_doc_by_newline):
"""dump raw examples."""
tf.logging.info("dumpping raw examples")
text_path = os.path.join(FLAGS.output_data_dir, "text.txt")
label_path = os.path.join(FLAGS.output_data_dir, "label.txt")
with tf.gfile.Open(text_path, "w") as text_ouf:
with tf.gfile.Open(label_path, "w") as label_ouf:
for example in examples:
text_a = example.text_a
text_b = example.text_b
label = example.label
text_ouf.write(text_a + "\n")
if text_b is not None:
text_ouf.write(text_b + "\n")
if separate_doc_by_newline:
text_ouf.write("\n")
label_ouf.write(label + "\n")
tf.logging.info("finished dumpping raw examples")
def main(argv):
processor = raw_data_utils.get_processor(FLAGS.task_name)
tf.logging.info("loading examples")
FLAGS.output_data_dir = os.path.join(
FLAGS.output_data_dir, FLAGS.sub_set)
if not tf.gfile.Exists(FLAGS.output_data_dir):
tf.gfile.MakeDirs(FLAGS.output_data_dir)
if FLAGS.sub_set == "train":
examples = processor.get_train_examples(FLAGS.raw_data_dir)
elif FLAGS.sub_set.startswith("unsup"):
examples = processor.get_unsup_examples(FLAGS.raw_data_dir, FLAGS.sub_set)
else:
assert False
tf.logging.info("finished loading examples")
tf.logging.info("examples num: {:d}".format(len(examples)))
dump_raw_examples(examples, FLAGS.separate_doc_by_newline)
if __name__ == '__main__':
app.run(main)