-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess_data.py
170 lines (137 loc) · 5.88 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
import numpy as np
import os
import nltk
nltk.download('punkt')
import itertools
import io
########### One Hot Features ###########
## create directory to store preprocessed data
if(not os.path.isdir('preprocessed_data')):
os.mkdir('preprocessed_data')
# get all of the training reviews (including unlabeled reviews)
train_directory = './aclImdb/train/'
pos_filenames = os.listdir(train_directory + 'pos/')
neg_filenames = os.listdir(train_directory + 'neg/')
unsup_filenames = os.listdir(train_directory + 'unsup/')
pos_filenames = [train_directory+'pos/'+filename for filename in pos_filenames]
neg_filenames = [train_directory+'neg/'+filename for filename in neg_filenames]
unsup_filenames = [train_directory+'unsup/'+filename for filename in unsup_filenames]
# the first 12500 are positive reviews, the next 12500 are negative
# reviews, and the remaining are unlabeled reviews
filenames = pos_filenames + neg_filenames + unsup_filenames
count = 0
x_train = []
for filename in filenames:
with io.open(filename,'r',encoding='utf-8') as f:
line = f.readlines()[0]
line = line.replace('<br />',' ') # remove unwanted text
line = line.replace('\x96',' ') # remove unwanted text
line = nltk.word_tokenize(line) # make everything lowercase
line = [w.lower() for w in line]
x_train.append(line)
count += 1
# print(count)
# get all of the test reviews
test_directory = './aclImdb/test/'
pos_filenames = os.listdir(test_directory + 'pos/')
neg_filenames = os.listdir(test_directory + 'neg/')
pos_filenames = [test_directory+'pos/'+filename for filename in pos_filenames]
neg_filenames = [test_directory+'neg/'+filename for filename in neg_filenames]
filenames = pos_filenames+neg_filenames
count = 0
x_test = []
for filename in filenames:
with io.open(filename,'r',encoding='utf-8') as f:
line = f.readlines()[0]
line = line.replace('<br />',' ')
line = line.replace('\x96',' ')
line = nltk.word_tokenize(line)
line = [w.lower() for w in line]
x_test.append(line)
count += 1
# print(count)
# print some statistics
# number of tokens per review
no_of_tokens = []
for tokens in x_train:
no_of_tokens.append(len(tokens))
no_of_tokens = np.asarray(no_of_tokens)
print('Total: ', np.sum(no_of_tokens), ' Min: ', np.min(no_of_tokens), ' Max: ', np.max(no_of_tokens), ' Mean: ', np.mean(no_of_tokens), ' Std: ', np.std(no_of_tokens))
# word_to_id and id_to_word associate an id to every unique token in the training data
all_tokens = itertools.chain.from_iterable(x_train)
word_to_id = {token: idx for idx, token in enumerate(set(all_tokens))} # convert to set to make token unique
all_tokens = itertools.chain.from_iterable(x_train)
id_to_word = [token for idx, token in enumerate(set(all_tokens))]
id_to_word = np.asarray(id_to_word)
# let's sort the indices by word frequency instead of random
x_train_token_ids = [[word_to_id[token] for token in x] for x in x_train]
count = np.zeros(id_to_word.shape)
for x in x_train_token_ids:
for token in x:
count[token] += 1
indices = np.argsort(-count)
# id_to_word in order from most frequent tokens to the least frequent
id_to_word = id_to_word[indices]
# count simply contains the number of occurrences for each token
count = count[indices]
# Over 80% (~160k) of the unique tokens occur between 1 and 10 times
hist = np.histogram(count,bins=[1,10,100,1000,10000])
print(hist)
for i in range(10):
print(id_to_word[i],count[i])
# recreate word_to_id based on sorted list (id_to_word)
word_to_id = {token: idx for idx, token in enumerate(id_to_word)}
# assign -1 if token doesn't appear in our dictionary
# add +1 to all token ids, we went to reserve id=0 for an unknown token
# dict.get(key, -1) return value is the key exists, return -1 if key does not exist
x_train_token_ids = [[word_to_id.get(token,-1)+1 for token in x] for x in x_train]
x_test_token_ids = [[word_to_id.get(token,-1)+1 for token in x] for x in x_test]
## save dictionary
np.save('preprocessed_data/imdb_dictionary.npy',np.asarray(id_to_word))
## save training data to single text file
with io.open('preprocessed_data/imdb_train.txt','w',encoding='utf-8') as f:
for tokens in x_train_token_ids:
for token in tokens:
f.write("%i " % token)
f.write("\n")
## save test data to single text file
with io.open('preprocessed_data/imdb_test.txt','w',encoding='utf-8') as f:
for tokens in x_test_token_ids:
for token in tokens:
f.write("%i " % token)
f.write("\n")
########### GloVe Features ###########
glove_filename = './glove.840B.300d.txt'
with io.open(glove_filename,'r',encoding='utf-8') as f:
lines = f.readlines()
glove_dictionary = []
glove_embeddings = []
count = 0
for line in lines:
line = line.strip()
line = line.split(' ')
glove_dictionary.append(line[0])
embedding = np.asarray(line[1:],dtype=np.float)
glove_embeddings.append(embedding)
count+=1
if(count>=100000):
break
glove_dictionary = np.asarray(glove_dictionary)
glove_embeddings = np.asarray(glove_embeddings)
# added a vector of zeros for the unknown tokens
glove_embeddings = np.concatenate((np.zeros((1,300)),glove_embeddings))
word_to_id = {token: idx for idx, token in enumerate(glove_dictionary)}
x_train_token_ids = [[word_to_id.get(token,-1)+1 for token in x] for x in x_train]
x_test_token_ids = [[word_to_id.get(token,-1)+1 for token in x] for x in x_test]
np.save('preprocessed_data/glove_dictionary.npy',glove_dictionary)
np.save('preprocessed_data/glove_embeddings.npy',glove_embeddings)
with io.open('preprocessed_data/imdb_train_glove.txt','w',encoding='utf-8') as f:
for tokens in x_train_token_ids:
for token in tokens:
f.write("%i " % token)
f.write("\n")
with io.open('preprocessed_data/imdb_test_glove.txt','w',encoding='utf-8') as f:
for tokens in x_test_token_ids:
for token in tokens:
f.write("%i " % token)
f.write("\n")