forked from jadore801120/attention-is-all-you-need-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
164 lines (134 loc) · 6.3 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
''' Handling the data io '''
import argparse
import torch
import transformer.Constants as Constants
def read_instances_from_file(inst_file, max_sent_len, keep_case=False):
''' Convert file into word seq lists and vocab '''
word_insts = []
trimmed_sent_count = 0
with open(inst_file, encoding='utf-8') as f:
for sent in f:
if not keep_case:
sent = sent.lower()
words = sent.split()
if len(words) > max_sent_len:
trimmed_sent_count += 1
word_inst = words[:max_sent_len]
if word_inst:
word_insts += [[Constants.BOS_WORD] + word_inst + [Constants.EOS_WORD]]
else:
word_insts += [None]
print('[Info] Get {} instances from {}'.format(len(word_insts), inst_file))
if trimmed_sent_count > 0:
print('[Warning] {} instances are trimmed to the max sentence length {}.'
.format(trimmed_sent_count, max_sent_len))
return word_insts
def build_vocab_idx(word_insts, min_word_count):
''' Trim vocab by number of occurence '''
full_vocab = set(w for sent in word_insts for w in sent)
print('[Info] Original Vocabulary size =', len(full_vocab))
word2idx = {
Constants.BOS_WORD: Constants.BOS,
Constants.EOS_WORD: Constants.EOS,
Constants.PAD_WORD: Constants.PAD,
Constants.UNK_WORD: Constants.UNK}
word_count = {w: 0 for w in full_vocab}
for sent in word_insts:
for word in sent:
word_count[word] += 1
ignored_word_count = 0
for word, count in word_count.items():
if word not in word2idx:
if count > min_word_count:
word2idx[word] = len(word2idx)
else:
ignored_word_count += 1
print('[Info] Trimmed vocabulary size = {},'.format(len(word2idx)),
'each with minimum occurrence = {}'.format(min_word_count))
print("[Info] Ignored word count = {}".format(ignored_word_count))
return word2idx
def convert_instance_to_idx_seq(word_insts, word2idx):
'''Word mapping to idx'''
return [[word2idx[w] if w in word2idx else Constants.UNK for w in s] for s in word_insts]
def main():
''' Main function '''
parser = argparse.ArgumentParser()
parser.add_argument('-train_src', required=True)
parser.add_argument('-train_tgt', required=True)
parser.add_argument('-valid_src', required=True)
parser.add_argument('-valid_tgt', required=True)
parser.add_argument('-save_data', required=True)
parser.add_argument('-max_len', '--max_word_seq_len', type=int, default=50)
parser.add_argument('-min_word_count', type=int, default=5)
parser.add_argument('-keep_case', action='store_true')
parser.add_argument('-share_vocab', action='store_true')
parser.add_argument('-vocab', default=None)
opt = parser.parse_args()
opt.max_token_seq_len = opt.max_word_seq_len + 2 # include the <s> and </s>
# Training set
train_src_word_insts = read_instances_from_file(
opt.train_src, opt.max_word_seq_len, opt.keep_case)
train_tgt_word_insts = read_instances_from_file(
opt.train_tgt, opt.max_word_seq_len, opt.keep_case)
if len(train_src_word_insts) != len(train_tgt_word_insts):
print('[Warning] The training instance count is not equal.')
min_inst_count = min(len(train_src_word_insts), len(train_tgt_word_insts))
train_src_word_insts = train_src_word_insts[:min_inst_count]
train_tgt_word_insts = train_tgt_word_insts[:min_inst_count]
#- Remove empty instances
train_src_word_insts, train_tgt_word_insts = list(zip(*[
(s, t) for s, t in zip(train_src_word_insts, train_tgt_word_insts) if s and t]))
# Validation set
valid_src_word_insts = read_instances_from_file(
opt.valid_src, opt.max_word_seq_len, opt.keep_case)
valid_tgt_word_insts = read_instances_from_file(
opt.valid_tgt, opt.max_word_seq_len, opt.keep_case)
if len(valid_src_word_insts) != len(valid_tgt_word_insts):
print('[Warning] The validation instance count is not equal.')
min_inst_count = min(len(valid_src_word_insts), len(valid_tgt_word_insts))
valid_src_word_insts = valid_src_word_insts[:min_inst_count]
valid_tgt_word_insts = valid_tgt_word_insts[:min_inst_count]
#- Remove empty instances
valid_src_word_insts, valid_tgt_word_insts = list(zip(*[
(s, t) for s, t in zip(valid_src_word_insts, valid_tgt_word_insts) if s and t]))
# Build vocabulary
if opt.vocab:
predefined_data = torch.load(opt.vocab)
assert 'dict' in predefined_data
print('[Info] Pre-defined vocabulary found.')
src_word2idx = predefined_data['dict']['src']
tgt_word2idx = predefined_data['dict']['tgt']
else:
if opt.share_vocab:
print('[Info] Build shared vocabulary for source and target.')
word2idx = build_vocab_idx(
train_src_word_insts + train_tgt_word_insts, opt.min_word_count)
src_word2idx = tgt_word2idx = word2idx
else:
print('[Info] Build vocabulary for source.')
src_word2idx = build_vocab_idx(train_src_word_insts, opt.min_word_count)
print('[Info] Build vocabulary for target.')
tgt_word2idx = build_vocab_idx(train_tgt_word_insts, opt.min_word_count)
# word to index
print('[Info] Convert source word instances into sequences of word index.')
train_src_insts = convert_instance_to_idx_seq(train_src_word_insts, src_word2idx)
valid_src_insts = convert_instance_to_idx_seq(valid_src_word_insts, src_word2idx)
print('[Info] Convert target word instances into sequences of word index.')
train_tgt_insts = convert_instance_to_idx_seq(train_tgt_word_insts, tgt_word2idx)
valid_tgt_insts = convert_instance_to_idx_seq(valid_tgt_word_insts, tgt_word2idx)
data = {
'settings': opt,
'dict': {
'src': src_word2idx,
'tgt': tgt_word2idx},
'train': {
'src': train_src_insts,
'tgt': train_tgt_insts},
'valid': {
'src': valid_src_insts,
'tgt': valid_tgt_insts}}
print('[Info] Dumping the processed data to pickle file', opt.save_data)
torch.save(data, opt.save_data)
print('[Info] Finish.')
if __name__ == '__main__':
main()