-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
265 lines (222 loc) · 8.74 KB
/
preprocessing.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
# coding=utf-8
from __future__ import absolute_import
from tqdm import tqdm
import multiprocessing as mp
import pandas as pd
import numpy as np
import pyemblib2
import scipy
import time
import sys
import os
from io import open
u'''
preprocessing.py
Preprocessing methods for cuto.py.
'''
#========1=========2=========3=========4=========5=========6=========7==
def check_valid_dir(some_dir):
if not os.path.isdir(some_dir):
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print u""
print u"DIES IST EIN UNGÜLTIGES VERZEICHNIS!!!!"
print u""
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
exit()
#========1=========2=========3=========4=========5=========6=========7==
def check_valid_file(some_file):
if not os.path.isfile(some_file):
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print u""
print u"DIES IST KEIN GÜLTIGER SPEICHERORT FÜR DATEIEN!!!!"
print u""
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print u"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
exit()
#========1=========2=========3=========4=========5=========6=========7==
def loadGloveModel(gloveFile):
print u"Loading Glove Model"
f = open(gloveFile,u'r')
model = {}
for line in f:
splitLine = line.split()
word = splitLine[0]
embedding = np.array([float(val) for val in splitLine[1:]])
model[word] = embedding
print u"Done.",len(model),u" words loaded!"
return model
#========1=========2=========3=========4=========5=========6=========7==
# pass None to vocab to use use entire embedding
# RETURNS: [numpy matrix of word vectors, df of the labels]
def get_embedding_dict(emb_path, emb_format, first_n, vocab):
print u"Preprocessing. "
file_name_length = len(emb_path)
extension = os.path.basename(emb_path).split(u'.')[-1]
# Decide if it's a binary or text embedding file, and read in
# the embedding as a dict object, where the keys are the tokens
# (strings), and the values are the components of the corresponding
# vectors (floats).
embedding = {}
read_mode = None
if first_n == 0 or emb_format == pyemblib2.Format.Glove:
print u"No value passed for first_n or feature not supported. "
first_n = None
if extension == u'bin':
read_mode = pyemblib2.Mode.Binary
binary = True
print u"binary reac."
elif extension == u'txt':
read_mode = pyemblib2.Mode.Text
binary = False
print u"text read."
else:
print u"Unsupported embedding mode. "
exit()
u'''
if emb_format == pyemblib2.Format.Glove:
embedding = loadGloveModel(emb_path)
'''
if first_n:
embedding = pyemblib2.read( emb_path,
format=emb_format,
mode=read_mode,
first_n=first_n,
replace_errors=True,
skip_parsing_errors=True,
)
else:
embedding = pyemblib2.read( emb_path,
format=emb_format,
mode=read_mode,
replace_errors=True,
skip_parsing_errors=True,
)
return embedding
#========1=========2=========3=========4=========5=========6=========7==
# pass None to vocab to use use entire embedding
# RETURNS: [numpy matrix of word vectors, df of the labels]
def process_embedding(emb_path, emb_format, first_n, vocab):
print u"Preprocessing. "
file_name_length = len(emb_path)
extension = os.path.basename(emb_path).split(u'.')[-1]
# Decide if it's a binary or text embedding file, and read in
# the embedding as a dict object, where the keys are the tokens
# (strings), and the values are the components of the corresponding
# vectors (floats).
embedding = {}
read_mode = None
if first_n == 0 or emb_format == pyemblib2.Format.Glove:
print u"No value passed for first_n or feature not supported. "
first_n = None
if extension == u'bin':
read_mode = pyemblib2.Mode.Binary
binary = True
print u"binary reac."
elif extension == u'txt':
read_mode = pyemblib2.Mode.Text
binary = False
print u"text read."
else:
print u"Unsupported embedding mode. "
exit()
u'''
if emb_format == pyemblib2.Format.Glove:
embedding = loadGloveModel(emb_path)
'''
if first_n:
embedding = pyemblib2.read( emb_path,
format=emb_format,
mode=read_mode,
first_n=first_n,
replace_errors=True,
skip_parsing_errors=True,
)
else:
embedding = pyemblib2.read( emb_path,
format=emb_format,
mode=read_mode,
replace_errors=True,
skip_parsing_errors=True,
)
# take a subset of the vocab
new_embedding = {}
if (vocab != None):
for word in vocab:
if word in embedding:
vector = embedding[word]
new_embedding.update({word:vector})
embedding = new_embedding
# convert embedding to pandas dataframe
# "words_with_friends" is the column label for the vectors
# this df has shape [num_inputs,2] since the vectors are all in 1
# column as length d lists
emb_array = np.array(embedding.items())
print u"numpy"
sys.stdout.flush()
label_array = np.array([ row[0] for row in emb_array.tolist() ])
#print(label_array[0:10])
sys.stdout.flush()
vectors_matrix = np.array([ row[1:] for row in emb_array.tolist() ])
vectors_matrix = np.array([ row[0] for row in vectors_matrix ])
#print(vectors_matrix[0:10])
sys.stdout.flush()
u'''
emb_df = pd.Series(embedding, name="words_with_friends")
# print(emb_df.head(10))
# reset the index of the dataframe
emb_df = emb_df.reset_index()
# print(emb_df.head(10))
# matrix of just the vectors
emb_matrix = emb_df.words_with_friends.values.tolist()
# print(emb_matrix[0:10])
# dataframe of just the vectors
vectors_df = pd.DataFrame(emb_matrix,index=emb_df.index)
# print(vectors_df.head(10))
# numpy matrix of just the vectors
vectors_matrix = vectors_df.as_matrix()
# print(vectors_matrix[0:10])
'''
return vectors_matrix, label_array
#========1=========2=========3=========4=========5=========6=========7==
# pass None to vocab to use use entire embedding
# DOES: Saves the first n words in a new embedding file
def subset_embedding(emb_path, first_n, vocab):
print u"Preprocessing. "
file_name_length = len(emb_path)
last_char = emb_path[file_name_length - 1]
# Decide if it's a binary or text embedding file, and read in
# the embedding as a dict object, where the keys are the tokens
# (strings), and the values are the components of the corresponding
# vectors (floats).
embedding = {}
if (last_char == u'n'):
embedding = pyemblib2.read(emb_path,
mode=pyemblib2.Mode.Binary,
first_n=first_n)
elif (last_char == u't'):
embedding = pyemblib2.read(emb_path,
mode=pyemblib2.Mode.Text,
first_n=first_n)
else:
print u"Unsupported embedding format. "
exit()
# make sure it has a valid file extension
extension = emb_path[file_name_length - 4:file_name_length]
if extension != u".txt" and extension != u".bin":
print u"Invalid file path. "
exit()
# get the emb_path without the file extension
path_no_ext = emb_path[0:file_name_length - 4]
new_path = path_no_ext + u"_SUBSET.txt"
# write to text embedding file
pyemblib2.write(embedding,
new_path,
mode=pyemblib2.Mode.Text)
return