-
Notifications
You must be signed in to change notification settings - Fork 38
/
preprocess.py
executable file
·150 lines (126 loc) · 6.74 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
"""
Preprocess the data for training/validating/testing
Be advised to run the remove_duplicates.py on raw json files (remove overlapping docs between train and valid/test)
before running this preprocess
"""
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import os
import torch
import config
import pykp.io
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()
# 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():
if opt.dataset_name == 'kp20k':
src_fields = ['title', 'abstract']
trg_fields = ['keyword']
valid_check=True
elif opt.dataset_name == 'stackexchange':
src_fields = ['title', 'question']
trg_fields = ['tags']
valid_check=True
elif opt.dataset_name == 'twacg':
src_fields = ['observation']
trg_fields = ['admissible_commands']
valid_check=False
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=valid_check)
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=valid_check)
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=valid_check)
print("Building Vocab...")
word2id, id2word, vocab = pykp.io.build_vocab(tokenized_train_pairs, opt)
print('Vocab size = %d' % len(vocab))
if opt.vocab_size > len(vocab):
opt.vocab_size = len(vocab)
print('Reset vocab size to %d' % opt.vocab_size)
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',
include_original=True)
pykp.io.process_and_export_dataset(tokenized_test_pairs,
word2id, id2word,
opt,
opt.subset_output_path,
dataset_name=opt.dataset_name,
data_type='test',
include_original=True)
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',
include_original=True)
pykp.io.process_and_export_dataset(tokenized_test_pairs,
word2id, id2word,
opt,
opt.output_path,
dataset_name=opt.dataset_name,
data_type='test',
include_original=True)
if __name__ == "__main__":
main()