-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprocess_data.py
226 lines (168 loc) · 7.3 KB
/
process_data.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import pickle as pkl
import numpy as np
from collections import defaultdict
# Source file
TRAIN_SRC = 'data/xinyadu_data/src-train.txt'
TRAIN_TGT = 'data/xinyadu_data/tgt-train.txt'
DEV_SRC = 'data/xinyadu_data/src-dev.txt'
DEV_TGT = 'data/xinyadu_data/tgt-dev.txt'
TEST_SRC = 'data/xinyadu_data/src-test.txt'
TEST_TGT = 'data/xinyadu_data/tgt-test.txt'
# Target file
sentence_outfile_train = 'data/processed/train_sentence.npy'
question_outfile_train = 'data/processed/train_question.npy'
sentence_outfile_dev = 'data/processed/dev_sentence.npy'
question_outfile_dev = 'data/processed/dev_question.npy'
sentence_outfile_test = 'data/processed/test_sentence.npy'
question_outfile_test = 'data/processed/test_question.npy'
maxlen_s_train = 60
maxlen_q_train = 30
maxlen_s_dev = 60
maxlen_q_dev = 25
maxlen_s_test = 60
maxlen_q_test = 25
dic_size = 34000
vocab_dir = 'data/processed/vocab_xinyadu.dic'
# Read data and Check max-length >>>
# Train data
with open(TRAIN_SRC) as f:
sentence_train = [line.split() for line in f.readlines()]
with open(TRAIN_TGT) as f:
question_train = [line.split() for line in f.readlines()]
maxlen = max([len(sentence) for sentence in sentence_train])
print 'train sentences max length : %d '%maxlen
maxlen = max([len(sentence) for sentence in question_train])
print 'train questions max length : %d \n'%maxlen
# Dev data
with open(DEV_SRC) as f:
sentence_dev = [line.split() for line in f.readlines()]
with open(DEV_TGT) as f:
question_dev = [line.split() for line in f.readlines()]
maxlen = max([len(sentence) for sentence in sentence_dev])
print 'dev sentences max length : %d '%maxlen
maxlen = max([len(sentence) for sentence in question_dev])
print 'dev questions max length : %d \n'%maxlen
# Test data
with open(TEST_SRC) as f:
sentence_test = [line.split() for line in f.readlines()]
with open(TEST_TGT) as f:
question_test = [line.split() for line in f.readlines()]
maxlen = max([len(sentence) for sentence in sentence_test])
print 'test sentences max length : %d '%maxlen
maxlen = max([len(sentence) for sentence in question_test])
print 'test questions max length : %d \n'%maxlen
# <<< Read data and Check max-length
# >>> Filtering data with max-length
print 'restrict max length of train_sentence as %d'%maxlen_s_train
print 'restrict max length of train_question as %d'%maxlen_q_train
print 'restrict max length of dev_sentence as %d'%maxlen_s_dev
print 'restrict max length of dev_question as %d'%maxlen_q_dev
print 'restrict max length of test_sentence as %d'%maxlen_s_test
print 'restrict max length of test_question as %d\n'%maxlen_q_test
def filter_with_maxlen(maxlen_s, maxlen_q, sentence, question):
# Filtering with maxlen(sentence)
temp_sentence = list()
temp_question = list()
for i, line in enumerate(sentence):
if (len(line) <= maxlen_s):
temp_sentence.append(line)
temp_question.append(question[i])
# Filtering with maxlen(question)
filtered_sentence = list()
filtered_question = list()
for i, line in enumerate(temp_question):
if len(line) <= maxlen_q:
filtered_sentence.append(temp_sentence[i])
filtered_question.append(line)
return filtered_sentence, filtered_question
# Train data
filtered_sentence_train, filtered_question_train = filter_with_maxlen(
maxlen_s_train, maxlen_q_train, sentence_train, question_train)
# Dev data
filtered_sentence_dev, filtered_question_dev = filter_with_maxlen(
maxlen_s_dev, maxlen_q_dev, sentence_dev, question_dev)
# Test data
filtered_sentence_test, filtered_question_test = filter_with_maxlen(
maxlen_s_test, maxlen_q_test, sentence_test, question_test)
# Save filtered data
with open('data/processed/sentence_train.txt', 'w') as f:
for line in filtered_sentence_train:
f.write(' '.join(line) + '\n')
with open('data/processed/question_train.txt', 'w') as f:
for line in filtered_question_train:
f.write(' '.join(line) + '\n')
with open('data/processed/sentence_dev.txt', 'w') as f:
for line in filtered_sentence_dev:
f.write(' '.join(line) + '\n')
with open('data/processed/question_dev.txt', 'w') as f:
for line in filtered_question_dev:
f.write(' '.join(line) + '\n')
with open('data/processed/sentence_test.txt', 'w') as f:
for line in filtered_sentence_test:
f.write(' '.join(line) + '\n')
with open('data/processed/question_test.txt', 'w') as f:
for line in filtered_question_test:
f.write(' '.join(line) + '\n')
# <<< Filtering data with max-length
# Make vocab with filtered sentences and questions(train) >>>
all_sentence = filtered_sentence_train + filtered_question_train
# Make vocab with word frequency
wordcount = defaultdict(int)
for sentence in all_sentence:
for word in sentence:
wordcount[word]+=1
sorted_wordlist = [(k, wordcount[k]) for k in sorted(wordcount, key=wordcount.get, reverse=True)]
print 'resize dictionary with %d most frequent words...'%dic_size
resized_dic = dict(sorted_wordlist[:(dic_size-4)])
word2idx = dict()
word2idx['<PAD>'] = 0
word2idx['<GO>'] = 1
word2idx['<EOS>'] = 2
word2idx['<UNK>'] = 3
idx = 4
for word in resized_dic:
word2idx[word] = idx
idx += 1
# Save dic
print 'save Dic File...'
with open(vocab_dir, 'w') as f:
pkl.dump(word2idx, f)
# <<< Make vocab with filtered sentences and questions(train)
# Process data with vocab >>>
def process(data, vocab, maxlen, if_go = False):
if if_go:
maxlen = maxlen + 2 # include <GO> and <EOS>
processed_data = list()
length_data = list()
for line in data:
processed_data.append([])
if if_go:
processed_data[-1].append(word2idx['<GO>'])
for word in line:
if word in word2idx:
processed_data[-1].append(word2idx[word])
else:
processed_data[-1].append(word2idx['<UNK>'])
if if_go:
processed_data[-1].append(word2idx['<EOS>'])
length_data.append(len(processed_data[-1]))
processed_data[-1] = processed_data[-1] + [word2idx['<PAD>']] * (maxlen - len(processed_data[-1]))
return processed_data, length_data
# Train data
processed_sentence_train, length_sentence_train = process(filtered_sentence_train, word2idx, maxlen_s_train, if_go = False)
processed_question_train, length_question_train = process(filtered_question_train, word2idx, maxlen_q_train, if_go = True)
# Eval data
processed_sentence_dev, length_sentence_dev = process(filtered_sentence_dev, word2idx, maxlen_s_dev, if_go = False)
processed_question_dev, length_question_dev = process(filtered_question_dev, word2idx, maxlen_q_dev, if_go = True)
# Test data
processed_sentence_test, length_sentence_test = process(filtered_sentence_test, word2idx, maxlen_s_test, if_go = False)
processed_question_test, length_question_test = process(filtered_question_test, word2idx, maxlen_q_test, if_go = True)
print 'Processing Complete'
# <<< Process data with vocab
np.save(sentence_outfile_train, processed_sentence_train)
np.save(question_outfile_train, processed_question_train)
np.save(sentence_outfile_dev, processed_sentence_dev)
np.save(question_outfile_dev, processed_question_dev)
np.save(sentence_outfile_test, processed_sentence_test)
np.save(question_outfile_test, processed_question_test)
print 'Saving Complete'