-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpreprocess.lua
More file actions
113 lines (91 loc) · 4.28 KB
/
Copy pathpreprocess.lua
File metadata and controls
113 lines (91 loc) · 4.28 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
require('onmt.init')
local cmd = onmt.utils.ExtendedCmdLine.new('preprocess.lua')
-- First argument define the dataType: bitext/monotext - default is bitext.
local dataType = cmd.getArgument(arg, '-data_type') or 'bitext'
-- Options declaration
local options = {
{'-data_type', 'bitext', [[Type of text to preprocess. Use 'monotext' for monolingual text.
This option impacts all options choices.]],
{enum={'bitext','monotext'}}},
{'-save_data', '', [[Output file for the prepared data]],
{valid=onmt.utils.ExtendedCmdLine.nonEmpty}}
}
cmd:setCmdLineOptions(options, 'Preprocess')
onmt.data.Preprocessor.declareOpts(cmd, dataType)
local otherOptions = {
{'-seed', 3425, [[Random seed]],
{valid=onmt.utils.ExtendedCmdLine.isUInt()}},
{'-report_every', 100000, [[Report status every this many sentences]],
{valid=onmt.utils.ExtendedCmdLine.isUInt()}}
}
cmd:setCmdLineOptions(otherOptions, 'Other')
onmt.utils.Logger.declareOpts(cmd)
local opt = cmd:parse(arg)
local function isValid(sent, maxSeqLength)
return #sent > 0 and #sent <= maxSeqLength
end
local function main()
torch.manualSeed(opt.seed)
_G.logger = onmt.utils.Logger.new(opt.log_file, opt.disable_logs, opt.log_level)
local Vocabulary = onmt.data.Vocabulary
local Preprocessor = onmt.data.Preprocessor.new(opt, dataType)
local data = { dataType=dataType }
data.dicts = {}
data.dicts.src = Vocabulary.init('train',
opt.train_src or opt.train,
opt.src_vocab or opt.vocab,
opt.src_vocab_size or opt.vocab_size,
opt.features_vocabs_prefix,
function(s) return isValid(s, opt.src_seq_length or opt.seq_length) end)
if dataType ~= 'monotext' then
data.dicts.tgt = Vocabulary.init('target',
opt.train_tgt,
opt.tgt_vocab,
opt.tgt_vocab_size,
opt.features_vocabs_prefix,
function(s) return isValid(s, opt.tgt_seq_length) end)
end
_G.logger:info('Preparing training data...')
data.train = {}
if dataType == 'monotext' then
data.train.src = Preprocessor:makeMonolingualData(opt.train, data.dicts.src, isValid)
else
data.train.src, data.train.tgt = Preprocessor:makeBilingualData(opt.train_src, opt.train_tgt,
data.dicts.src, data.dicts.tgt,
isValid)
end
_G.logger:info('')
_G.logger:info('Preparing validation data...')
data.valid = {}
if dataType == 'monotext' then
data.valid.src = Preprocessor:makeMonolingualData(opt.valid, data.dicts.src, isValid)
else
data.valid.src, data.valid.tgt = Preprocessor:makeBilingualData(opt.valid_src, opt.valid_tgt,
data.dicts.src, data.dicts.tgt,
isValid)
end
_G.logger:info('')
if dataType == 'monotext' then
if opt.vocab:len() == 0 then
Vocabulary.save('source', data.dicts.src.words, opt.save_data .. '.dict')
end
if opt.features_vocabs_prefix:len() == 0 then
Vocabulary.saveFeatures('source', data.dicts.src.features, opt.save_data)
end
else
if opt.src_vocab:len() == 0 then
Vocabulary.save('source', data.dicts.src.words, opt.save_data .. '.src.dict')
end
if opt.tgt_vocab:len() == 0 then
Vocabulary.save('target', data.dicts.tgt.words, opt.save_data .. '.tgt.dict')
end
if opt.features_vocabs_prefix:len() == 0 then
Vocabulary.saveFeatures('source', data.dicts.src.features, opt.save_data)
Vocabulary.saveFeatures('target', data.dicts.tgt.features, opt.save_data)
end
end
_G.logger:info('Saving data to \'' .. opt.save_data .. '-train.t7\'...')
torch.save(opt.save_data .. '-train.t7', data, 'binary', false)
_G.logger:shutDown()
end
main()