-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data_Preprocess.py
286 lines (229 loc) · 9.91 KB
/
Data_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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from terminaltables import AsciiTable
import numpy as np
import pickle
from collections import defaultdict
import sys, re
import pandas as pd
from gensim.models import word2vec
from os.path import join, exists, split
import os
from nltk.tokenize import word_tokenize
from collections import Counter
import itertools
import time
def build_data_structure(input_files):
"""
Loads data and split into 10 folds.
"""
revs = []
train_file = input_files[0]
test_file = input_files[1]
vocab = defaultdict(float)
with open(train_file, "r") as f:
for line in f:
rev = []
temp_rev = []
rev.append(line.strip()) # remove the "\n" at the end of each line
temp_rev.append(rev[0].split('\t')[0])
temp_rev.append(rev[0].split('\t')[1])
orig_rev = clean_str(temp_rev[1])
words = set(orig_rev.split())
for word in words:
vocab[word] += 1
rev_data = {"y":int(temp_rev[0]),
"text": orig_rev,
"num_words": len(orig_rev.split())}
revs.append(rev_data)
with open(test_file, "r") as f:
for line in f:
rev = []
temp_rev = []
rev.append(line.strip()) # remove the "\n" at the end of each line
temp_rev.append(rev[0].split('\t')[0])
temp_rev.append(rev[0].split('\t')[1])
orig_rev = clean_str(temp_rev[1])
words = set(orig_rev.split())
for word in words:
vocab[word] += 1
rev_data = {"y":int(temp_rev[0]),
"text": orig_rev,
"num_words": len(orig_rev.split())}
revs.append(rev_data)
splited = [s["text"].strip() for s in revs]
tokenized_sentences = [word_tokenize(s) for s in splited]
labels = [L["y"] for L in revs ]
return revs, vocab, tokenized_sentences, labels
def review_data_structure(input_files):
"""
Loads data and construct the proper data structure
"""
revs = []
file = input_files
vocab = defaultdict(float)
labels = []
i = 1
with open(file, "r") as f:
rows_no = len(f.readlines())
f.seek(0)
for line in f:
percent = float((i*100) / rows_no)
hashes = '#' * int(round(percent/2))
spaces = ' ' * (len(hashes)- 50)
sys.stdout.write("\rLoading data: [{0}] {1}%".format(hashes + spaces, int(round(percent))))
rev = []
temp_rev = []
rev.append(line.strip()) # remove the "\n" at the end of each line
temp_rev.append(rev[0].split('\t')[0])
temp_rev.append(rev[0].split('\t')[1])
orig_rev = clean_str(temp_rev[1])
words = set(orig_rev.split())
for word in words:
vocab[word] += 1
labels.append(int(temp_rev[0]))
# if temp_rev[0] == '0':
# labels.append([1,0])
# elif temp_rev[0] == '1':
# labels.append([0,1])
rev_data = {"y":int(temp_rev[0]),
"text": orig_rev,
"num_words": len(orig_rev.split())}
revs.append(rev_data)
sys.stdout.flush()
i += 1
print("\t{0} loaded\n".format(f.name))
# Split by words
splited = [s["text"].strip() for s in revs]
tokenized_sentences = [word_tokenize(s) for s in splited]
return revs, vocab, tokenized_sentences, labels
def clean_str(string):
"""
Tokenization/string cleaning/lower cased for datasets.
"""
string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
string = re.sub(r"\'s", " \'s", string)
string = re.sub(r"\'ve", " \'ve", string)
string = re.sub(r"n\'t", " n\'t", string)
string = re.sub(r"\'re", " \'re", string)
string = re.sub(r"\'d", " \'d", string)
string = re.sub(r"\'ll", " \'ll", string)
string = re.sub(r",", " , ", string)
string = re.sub(r"!", " ! ", string)
string = re.sub(r"\(", " \( ", string)
string = re.sub(r"\)", " \) ", string)
string = re.sub(r"\?", " \? ", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip().lower()
def rev_count(reviews):
pos_rev = 0
neg_rev = 0
for item in reviews:
if item["y"] == 0:
neg_rev += 1
else:
pos_rev += 1
return (pos_rev, neg_rev)
def sentence_padding(tokenized_sentences, max_length, padding_sign = "<PAD/>"):
"""
Pads all sentences to the same length. The length is defined by the longest sentence.
Returns padded sentences.
"""
padded_sentences = []
for i in range(len(tokenized_sentences)):
percent = float((i * 100) / len(tokenized_sentences))
hashes = '#' * int(round(percent / 2))
spaces = ' ' * (len(hashes) - 50)
sys.stdout.write("\rPadding sentences: [{0}] {1}%".format(hashes + spaces, int(round(percent))))
sentence = tokenized_sentences[i]
num_padding = max_length - len(sentence)
new_sentence = sentence + [padding_sign] * num_padding
padded_sentences.append(new_sentence)
sys.stdout.flush()
print("\tsentences are padded\n")
return padded_sentences
def dictionary_maker(sentences):
"""
Builds a vocabulary mapping from word to index based on the sentences.
Returns vocabulary mapping and inverse vocabulary mapping.
"""
# Build vocabulary
word_counts = Counter(itertools.chain(*sentences))
# Mapping from index to word
vocabulary_inv = [x[0] for x in word_counts.most_common()]
# Mapping from word to index
vocabulary = {x: i for i, x in enumerate(vocabulary_inv)}
return vocabulary, vocabulary_inv
def build_matrices(sentences, labels, vocabulary):
"""
Maps sentences and labels to vectors based on a vocabulary.
"""
x = np.array([[vocabulary[word] for word in sentence] for sentence in sentences])
y = np.array(labels)
return x, y
def load_data():
input_files = ["Input_Files\\train.txt", "Input_Files\\test.txt"]
"""
revs: A list of dictionaries in following format: [{'text': 'review', 'y': label, 'num_words': number of words in review},...]
vocab: A dictionary in following format: {'word': frequency,...}
tokenized_sentences: A 2D list in following format: [[tokenized_sentences_1],...]
labels: A list with represent label of reviews respectively
"""
# train_revs, train_vocab, train_tokenized_sentences, train_labels = review_data_structure(input_files[0])
# test_revs, test_vocab, test_tokenized_sentences, test_labels = review_data_structure(input_files[1])
# train_max_sentence_lenght = np.max(pd.DataFrame(train_revs)["num_words"]) # Get the max length sentence
# test_max_sentence_lenght = np.max(pd.DataFrame(test_revs)["num_words"]) # Get the max length sentence
revs, vocab, tokenized_sentences, labels = build_data_structure(input_files)
max_sentence_lenght = np.max(pd.DataFrame(revs)["num_words"]) # Get the max length sentence
table_data = []
table_data.append(['Data Statistics', 'Values'])
table_data.append(['Number of sentences', str(len(revs))])
table_data.append(['Vocabulary size', str(len(vocab))])
table_data.append(['Max sentence length', str(max_sentence_lenght)])
table_data.append(['Positive reviews', str(rev_count(revs)[0])])
table_data.append(['Negative reviews', str(rev_count(revs)[1])])
train_table = AsciiTable(table_data)
print(train_table.table)
# train_table_data = []
# train_table_data.append(['Training Data Statistics', 'Values'])
# train_table_data.append(['Number of sentences', str(len(train_revs))])
# train_table_data.append(['Vocabulary size', str(len(train_vocab))])
# train_table_data.append(['Max sentence length', str(train_max_sentence_lenght)])
# train_table_data.append(['Positive reviews', str(rev_count(train_revs)[0])])
# train_table_data.append(['Negative reviews', str(rev_count(train_revs)[1])])
# train_table = AsciiTable(train_table_data)
# print(train_table.table)
#
# test_table_data = []
# test_table_data.append(['Test Data Statistics', 'Values'])
# test_table_data.append(['Number of sentences', str(len(test_revs))])
# test_table_data.append(['Vocabulary size', str(len(test_vocab))])
# test_table_data.append(['Max sentence length', str(test_max_sentence_lenght)])
# test_table_data.append(['Positive reviews', str(rev_count(test_revs)[0])])
# test_table_data.append(['Negative reviews', str(rev_count(test_revs)[1])])
# test_table = AsciiTable(test_table_data)
# print(test_table.table)
"""
sentences_padded: A 2D list in following format: [[tokenized_sentences_1+paddings],...], max value: 3256, min value: 0
"""
# train_sentences_padded = sentence_padding(train_tokenized_sentences, train_max_sentence_lenght)
# test_sentences_padded = sentence_padding(test_tokenized_sentences, test_max_sentence_lenght)
#
sentences_padded = sentence_padding(tokenized_sentences, max_sentence_lenght)
#
# train_vocabulary, train_vocabulary_inv = dictionary_maker(train_sentences_padded)
# test_vocabulary, test_vocabulary_inv = dictionary_maker(test_sentences_padded)
#
vocabulary, vocabulary_inv = dictionary_maker(sentences_padded)
#
# X_train, y_train = build_matrices(train_sentences_padded, train_labels, train_vocabulary)
# X_test, y_test = build_matrices(test_sentences_padded, test_labels, test_vocabulary)
#
X, Y = build_matrices(sentences_padded, labels, vocabulary)
#
# return (X_train, y_train, train_vocabulary, train_vocabulary_inv), (X_test, y_test, test_vocabulary, test_vocabulary_inv)
return X, Y, vocabulary_inv, vocabulary
if __name__ =="__main__":
X, Y, vocabulary_inv, vocabulary = load_data()
print(len(X))
print(len(Y))
print(len(vocabulary_inv))
print(len(vocabulary))