forked from OpenNMT/OpenNMT-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
executable file
·94 lines (76 loc) · 3.33 KB
/
preprocess.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import codecs
import torch
import onmt
import onmt.IO
import opts
parser = argparse.ArgumentParser(
description='preprocess.py',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
opts.add_md_help_argument(parser)
# **Preprocess Options**
parser.add_argument('-config', help="Read options from this file")
parser.add_argument('-data_type', default="text",
help="Type of the source input. Options are [text|img].")
parser.add_argument('-data_img_dir', default=".",
help="Location of source images")
parser.add_argument('-train_src', required=True,
help="Path to the training source data")
parser.add_argument('-train_tgt', required=True,
help="Path to the training target data")
parser.add_argument('-valid_src', required=True,
help="Path to the validation source data")
parser.add_argument('-valid_tgt', required=True,
help="Path to the validation target data")
parser.add_argument('-save_data', required=True,
help="Output file for the prepared data")
parser.add_argument('-src_vocab',
help="Path to an existing source vocabulary")
parser.add_argument('-tgt_vocab',
help="Path to an existing target vocabulary")
parser.add_argument('-features_vocabs_prefix', type=str, default='',
help="Path prefix to existing features vocabularies")
parser.add_argument('-seed', type=int, default=3435,
help="Random seed")
parser.add_argument('-report_every', type=int, default=100000,
help="Report status every this many sentences")
opts.preprocess_opts(parser)
opt = parser.parse_args()
torch.manual_seed(opt.seed)
def main():
print('Preparing training ...')
with codecs.open(opt.train_src, "r", "utf-8") as src_file:
src_line = src_file.readline().strip().split()
_, _, n_src_features = onmt.IO.extract_features(src_line)
with codecs.open(opt.train_tgt, "r", "utf-8") as tgt_file:
tgt_line = tgt_file.readline().strip().split()
_, _, n_tgt_features = onmt.IO.extract_features(tgt_line)
fields = onmt.IO.get_fields(n_src_features, n_tgt_features)
print("Building Training...")
train = onmt.IO.ONMTDataset(
opt.train_src, opt.train_tgt, fields,
opt.src_seq_length, opt.tgt_seq_length,
src_seq_length_trunc=opt.src_seq_length_trunc,
tgt_seq_length_trunc=opt.tgt_seq_length_trunc,
dynamic_dict=opt.dynamic_dict)
print("Building Vocab...")
onmt.IO.build_vocab(train, opt)
print("Building Valid...")
valid = onmt.IO.ONMTDataset(
opt.valid_src, opt.valid_tgt, fields,
opt.src_seq_length, opt.tgt_seq_length,
src_seq_length_trunc=opt.src_seq_length_trunc,
tgt_seq_length_trunc=opt.tgt_seq_length_trunc,
dynamic_dict=opt.dynamic_dict)
print("Saving train/valid/fields")
# Can't save fields, so remove/reconstruct at training time.
torch.save(onmt.IO.save_vocab(fields),
open(opt.save_data + '.vocab.pt', 'wb'))
train.fields = []
valid.fields = []
torch.save(train, open(opt.save_data + '.train.pt', 'wb'))
torch.save(valid, open(opt.save_data + '.valid.pt', 'wb'))
if __name__ == "__main__":
main()