-
Notifications
You must be signed in to change notification settings - Fork 15
/
preprocess.py
executable file
·164 lines (131 loc) · 6.82 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import os
import torch
import config
import pykp.io
from bert_pytorch.dataset import BERTDataset, WordVocab
from pytorch_pretrained_bert.tokenization import BertTokenizer
parser = argparse.ArgumentParser(
description='preprocess.py',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# **Preprocess Options**
parser.add_argument('-dataset_name', required=True,
help="Name of dataset")
parser.add_argument('-source_dataset_dir', required=True,
help="The path to the source data (raw json).")
parser.add_argument('-output_path_prefix', default='data',
help="Output file for the prepared data")
config.preprocess_opts(parser)
opt = parser.parse_args()
opt.output_path_prefix = 'data4'
opt.source_dataset_dir = 'data4'
# input path of each json file
opt.source_train_file = os.path.join(opt.source_dataset_dir, '%s_training.json' % (opt.dataset_name))
opt.source_valid_file = os.path.join(opt.source_dataset_dir, '%s_validation.json' % (opt.dataset_name))
opt.source_test_file = os.path.join(opt.source_dataset_dir, '%s_testing.json' % (opt.dataset_name))
# output path for exporting the processed dataset
opt.output_path = os.path.join(opt.output_path_prefix, opt.dataset_name)
# output path for exporting the processed dataset
opt.subset_output_path = os.path.join(opt.output_path_prefix, opt.dataset_name+'_small')
if not os.path.exists(opt.output_path):
os.makedirs(opt.output_path)
if not os.path.exists(opt.subset_output_path):
os.makedirs(opt.subset_output_path)
def main():
opt.use_bert = False
opt.build_own_vocab = True
opt.useAreadyVocab = True
#opt.src_seq_length_trunc = 510
if opt.use_bert:
bert_model = 'bert-base-uncased'
opt.tokenizer = BertTokenizer.from_pretrained(bert_model)
if opt.dataset_name == 'kp20k':
src_fields = ['title', 'abstract']
trg_fields = ['keyword']
elif opt.dataset_name == 'stackexchange':
src_fields = ['title', 'question']
trg_fields = ['tags']
else:
raise Exception('Unsupported dataset name=%s' % opt.dataset_name)
print("Loading training/validation/test data...")
tokenized_train_pairs = pykp.io.load_src_trgs_pairs(source_json_path=opt.source_train_file,
dataset_name=opt.dataset_name,
src_fields=src_fields,
trg_fields=trg_fields,
opt=opt,
valid_check=True)
tokenized_valid_pairs = pykp.io.load_src_trgs_pairs(source_json_path=opt.source_valid_file,
dataset_name=opt.dataset_name,
src_fields=src_fields,
trg_fields=trg_fields,
opt=opt,
valid_check=False)
tokenized_test_pairs = pykp.io.load_src_trgs_pairs(source_json_path=opt.source_test_file,
dataset_name=opt.dataset_name,
src_fields=src_fields,
trg_fields=trg_fields,
opt=opt,
valid_check=False)
if opt.use_bert and not opt.build_own_vocab:
print("Loading BERT Vocab...")
word2id = opt.tokenizer.vocab
id2word = opt.tokenizer.ids_to_tokens
vocab = None
print('Vocab size = %d' % len(word2id))
elif opt.useAreadyVocab:
vocab = WordVocab.load_vocab("data4/vocab.30")
word2id = vocab.stoi
id2word = vocab.itos
vocab = vocab.freqs
else:
print("Building Vocab...")
word2id, id2word, vocab = pykp.io.build_vocab(tokenized_train_pairs, opt)
print('Vocab size = %d' % len(vocab))
print("Dumping dict to disk")
opt.vocab_path = os.path.join(opt.subset_output_path, opt.dataset_name + '.vocab.pt')
torch.save([word2id, id2word, vocab], open(opt.vocab_path, 'wb'))
opt.vocab_path = os.path.join(opt.output_path, opt.dataset_name + '.vocab.pt')
torch.save([word2id, id2word, vocab], open(opt.vocab_path, 'wb'))
print("Exporting a small dataset to %s (for debugging), "
"size of train/valid/test is 20000" % opt.subset_output_path)
pykp.io.process_and_export_dataset(tokenized_train_pairs[:20000],
word2id, id2word,
opt,
opt.subset_output_path,
dataset_name=opt.dataset_name,
data_type='train')
pykp.io.process_and_export_dataset(tokenized_valid_pairs,
word2id, id2word,
opt,
opt.subset_output_path,
dataset_name=opt.dataset_name,
data_type='valid')
pykp.io.process_and_export_dataset(tokenized_test_pairs,
word2id, id2word,
opt,
opt.subset_output_path,
dataset_name=opt.dataset_name,
data_type='test')
print("Exporting complete dataset to %s" % opt.output_path)
pykp.io.process_and_export_dataset(tokenized_train_pairs,
word2id, id2word,
opt,
opt.output_path,
dataset_name=opt.dataset_name,
data_type='train')
pykp.io.process_and_export_dataset(tokenized_valid_pairs,
word2id, id2word,
opt,
opt.output_path,
dataset_name=opt.dataset_name,
data_type='valid')
pykp.io.process_and_export_dataset(tokenized_test_pairs,
word2id, id2word,
opt,
opt.output_path,
dataset_name=opt.dataset_name,
data_type='test')
if __name__ == "__main__":
main()