-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess_data.py
265 lines (207 loc) · 8.56 KB
/
preprocess_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, random
import os
# sys.setdefaultencoding() does not exist, here!
#reload(sys) # Reload does the trick!
#sys.setdefaultencoding('UTF8')
# TREC
# After running this code, we can confirm that the collection has a
# split of 5435/50/2189 documents for train/validation/test respectively,
# and a total of 6 different categories.
from nltk import word_tokenize
from nltk.corpus import reuters
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.stem.porter import PorterStemmer
import re, random, pickle
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer
from gensim import corpora, models, similarities
from os.path import expanduser
from collections import defaultdict
import codecs
import csv
from nltk.tokenize import RegexpTokenizer
import numpy as np
import tensorflow as tf
import argparse
from sklearn.utils import shuffle
import model.data
seed = 42
np.random.seed(seed)
tf.set_random_seed(seed)
csv.field_size_limit(2**28)
tokenizer = RegexpTokenizer(r'\w+')
cachedStopWords = []
with open("english_stopwords.txt", "r") as f:
cachedStopWords.extend([line.strip() for line in f.readlines()])
def tokens(text):
return [w.lower() for w in text.split()]
def counts_to_sequence(counts):
seq = []
for i in range(len(counts)):
seq.extend([i] * int(counts[i]))
return seq
def log_counts(ids, vocab_size):
counts = np.bincount(ids, minlength=vocab_size)
return np.floor(0.5 + np.log(counts + 1))
def preprocess(text, vocab_to_id, dataset_type):
ids = [vocab_to_id.get(x) for x in tokens(text) if not (vocab_to_id.get(x) is None)]
if dataset_type == "docnade":
counts = log_counts(ids, len(vocab_to_id))
sequence = counts_to_sequence(counts)
else:
sequence = ids
if len(sequence) == 0:
return None
else:
return ' '.join([str(x) for x in sequence])
def TF(docs, max_features=2000):
cv = CountVectorizer(tokenizer=tokens, min_df=1, max_df=1.0, max_features=max_features, encoding='utf-8', decode_error='ignore')
cv.fit(docs)
return cv
def load_file(filename):
"""
Read the tab delimited file containing the labels and the docs.
"""
labels = []
docs = []
with open(filename) as f:
for line in f:
content = line.split('\t')
if len(content) > 2:
print('incorrect read')
exit()
if len(content[1]) == 0: continue
docs.append(str(content[1]).strip('\r').strip('\n').strip('\r\n'))
labels.append(content[0])
return docs, labels
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def main(args):
args.split_train_val = str2bool(args.split_train_val)
doc_train_filename = args.training_file
doc_val_filename = args.validation_file
doc_test_filename = args.test_file
train_csv_filename = os.path.join(args.data_output, "training.csv")
val_csv_filename = os.path.join(args.data_output, "validation.csv")
test_csv_filename = os.path.join(args.data_output, "test.csv")
if not os.path.exists(args.data_output):
os.makedirs(args.data_output)
docnade_vocabulary = args.vocab_size
docnade_vocab_filename = os.path.join(args.data_output, "vocab_docnade.vocab")
lstm_vocab_filename = os.path.join(args.data_output, "vocab_lstm.vocab")
mapping_dict_filename = os.path.join(args.data_output, "mapping_dict.pkl")
train_docs, train_docs_labels = load_file(doc_train_filename)
test_docs, test_docs_labels = load_file(doc_test_filename)
#if not args.split_train_val:
val_docs, val_docs_labels = load_file(doc_val_filename)
print(np.unique(train_docs_labels))
train_docs, train_docs_labels = shuffle(train_docs, train_docs_labels, random_state=123)
val_docs, val_docs_labels = shuffle(val_docs, val_docs_labels, random_state=123)
#test_docs, test_docs_labels = shuffle(test_docs, test_docs_labels, random_state=123)
###########################################################################
# Prepare CSV file
new_train_docs = []
with open(train_csv_filename, 'w', newline='') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',')
for doc, label in zip(train_docs, train_docs_labels):
new_doc_tokens = tokens(str(doc).lower().strip())
new_doc_tokens = [token for token in new_doc_tokens if not token in cachedStopWords]
new_doc = ' '.join(new_doc_tokens)
li = [str(label).lower().strip(), str(new_doc).lower().strip()]
filewriter.writerow(li)
new_train_docs.append(str(new_doc).lower().strip())
new_val_docs = []
with open(val_csv_filename, 'w', newline='') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',')
for doc, label in zip(val_docs, val_docs_labels):
new_doc_tokens = tokens(str(doc).lower().strip())
new_doc_tokens = [token for token in new_doc_tokens if not token in cachedStopWords]
new_doc = ' '.join(new_doc_tokens)
li = [str(label).lower().strip(), str(new_doc).lower().strip()]
filewriter.writerow(li)
new_val_docs.append(str(new_doc).lower().strip())
new_test_docs = []
with open(test_csv_filename, 'w', newline='') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',')
for doc, label in zip(test_docs, test_docs_labels):
new_doc_tokens = tokens(str(doc).lower().strip())
new_doc_tokens = [token for token in new_doc_tokens if not token in cachedStopWords]
new_doc = ' '.join(new_doc_tokens)
li = [str(label).lower().strip(), str(new_doc).lower().strip()]
filewriter.writerow(li)
new_test_docs.append(str(new_doc).lower().strip())
total_docs = []
total_docs.extend(new_train_docs)
total_docs.extend(new_val_docs)
#total_docs.extend(new_test_docs)
# Saving docnade vocabulary
#representer = TF(total_docs, max_features=docnade_vocabulary)
representer = TF(total_docs, max_features=None)
vocab_dict_docnade = representer.get_feature_names()
with open(docnade_vocab_filename, "w") as f:
f.write('\n'.join(vocab_dict_docnade))
# Preparing CSV files for DocNADE Tensorflow
data = model.data.Dataset(args.data_output)
with open(docnade_vocab_filename, 'r') as f:
vocab = [w.strip() for w in f.readlines()]
vocab_to_id = dict(zip(vocab, range(len(vocab))))
if not os.path.isdir(args.data_output):
os.mkdir(args.data_output)
labels = {}
removed_indices = {"training":[], "test":[], "validation":[]}
for collection in data.collections:
output_path = os.path.join(args.data_output, '{}_docnade.csv'.format(collection))
with open(output_path, 'w', newline='') as f:
#with open(output_path, 'w') as f:
w = csv.writer(f, delimiter=',')
count = -1
for y, x in data.rows(collection, num_epochs=1):
count += 1
try:
pre = preprocess(x, vocab_to_id, "docnade")
if pre is None:
removed_indices[str(collection).lower()].append(count)
continue
if ':' in y:
temp_labels = y.split(':')
new_label = []
for label in temp_labels:
if label not in labels:
labels[label] = len(labels)
new_label.append(str(labels[label]))
temp_label = ':'.join(new_label)
w.writerow((temp_label, pre))
else:
if y not in labels:
labels[y] = len(labels)
w.writerow((labels[y], pre))
except:
import pdb; pdb.set_trace()
with open(os.path.join(args.data_output, 'labels.txt'), 'w') as f:
f.write('\n'.join([k for k in sorted(labels, key=labels.get)]))
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--training-file', type=str, required=True,
help='path to validation text file')
parser.add_argument('--validation-file', type=str, required=True,
help='path to validation text file')
parser.add_argument('--test-file', type=str, required=True,
help='path to validation text file')
parser.add_argument('--data-output', type=str, required=True,
help='path to data output directory')
parser.add_argument('--vocab-size', type=int, default=2000,
help='the vocab size')
parser.add_argument('--split-train-val', type=str, default="False",
help='whether to do train-val split')
parser.add_argument('--split-num', type=int, default=50,
help='number of documents in validation set')
return parser.parse_args()
if __name__ == '__main__':
main(parse_args())