forked from geffy/tffm
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreader.py
More file actions
322 lines (265 loc) · 10.9 KB
/
reader.py
File metadata and controls
322 lines (265 loc) · 10.9 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import csv
import pandas as pd
import scipy.sparse as sp
import sys
import numpy as np
WORD_META_ID = 0
#TOPIC_META_ID = 1
#POS_META_ID = 2
#YEAR_META_ID = 3
#PRODUCT_META_ID = 4
v_words = [
'prime',
'minister'
]
'''v_topics = [
'AID',
'BOMB',
'CRIM',
'DEF',
'DIP',
'DIS',
'ENT',
'LAW',
'LIF',
'POL',
'SCI',
'WEA'
]
v_pos = [
'NN',
'NNS',
'NNP',
'NNPS',
'RBR',
'RBS',
'VB',
'VBD',
'VBP',
'VBZ',
'VBN',
'JJ',
'JJR',
'JJS'
]
v_year = [
'2003',
'2004',
'2005',
'2006',
'2007',
'2008',
'2009',
'2010',
'2011',
'2012',
'2013',
'2014',
'2015'
]
v_product = [
'G',
'SPO'
]'''
def save_sparse_csr(filename, array):
np.savez(filename, data = array.data ,indices=array.indices,
indptr =array.indptr, shape=array.shape )
def load_sparse_csr(filename):
loader = np.load(filename)
return sp.csr_matrix(( loader['data'], loader['indices'], loader['indptr']),
shape = loader['shape'])
class Reader():
def __init__(self,word_meta2_id_file,cooccur_file,metadata_path):
print("step 1 : load vocabulary and write metadata")
self.vocab_size, unique_meta, self.meta_vector, word_meta2_id_dict, self.words = self.load_vocabulary_and_write_metadata(word_meta2_id_file,metadata_path)
self.len_unique_meta = len(unique_meta)
print("step 2 : load cooccurencies")
try:
# try to load serialized coocurrencies
filename = cooccur_file+"_X.npz"
self.X = load_sparse_csr(filename)
self.X_ids = []
self.X_weights = []
for m in range(len(unique_meta)):
filename = cooccur_file+"_X_ids_"+str(m)+".npz"
self.X_ids.append(load_sparse_csr(filename))
filename = cooccur_file+"_X_weights_"+str(m)+".npz"
self.X_weights.append(load_sparse_csr(filename))
filename = cooccur_file+"_Y.npy"
self.Y = np.load(filename)
print("\n\t ... succesfully loaded serialized coocurrencies")
except Exception as inst:
print(str(type(inst)) + "\n" + str(inst.args) + "\n" + str(inst))
# read coocurrencies from file
print("\n\t ... read coocurrencies from file")
self.X, self.X_ids, self.X_weights, self.Y = self.load_cooccurencies(cooccur_file, self.vocab_size, self.meta_vector, unique_meta)
self.valid_examples_words = self.loadValidSet(word_meta2_id_dict)
def load_vocabulary_and_write_metadata(self,word_meta2_id_file,metadata_path):
# LOAD VOCABULARY
word_meta2_id_df = pd.read_csv(word_meta2_id_file, names=['word','id','meta'], quotechar='"')
word_meta2_id_df['word'] = word_meta2_id_df['word'].apply(lambda x: str(x).replace('"', ''))
word_meta2_id_df = word_meta2_id_df.set_index(['word','meta'])
word_meta2_id_df.loc[:,'id'] += 1 # increase the id by one to reserve element 0 for NULL
word_meta2_id_dict = word_meta2_id_df.T.to_dict('records')[0]
word_meta2_id_dict[('NULL',-1)] = 0 # add NULL
vocab_size = len(word_meta2_id_dict)
# WRITE METADATA
metadata_file = open(metadata_path, 'w')
metadata_file.write('Name\tMeta\n')
for k,v in sorted(word_meta2_id_dict.items(), key=lambda x: x[1]):
metadata_file.write(str(k[0])+'\t'+str(k[1])+'\n')
metadata_file.close()
words = ['NULL'] + [w[0] for w in word_meta2_id_df.index]
aux_meta = [w[1] for w in word_meta2_id_df.index]
unique_meta = sorted(list(set(aux_meta)))
meta_vector = [-1] + aux_meta
return vocab_size, unique_meta, meta_vector, word_meta2_id_dict, words
def load_cooccurencies(self,cooccur_file, vocab_size, meta_vector, unique_meta):
# LOAD COOCCURRENCIES
print("\tstep 1 : LOAD COOCCURRENCIES")
rows = []
cols = []
data = []
y = []
with open(cooccur_file, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
row_index = 0
for row in reader:
'''if row_index > 100:
break'''
# print(row_index, " ",row)
current_y = float(row[0])
y.append(current_y)
for j in range(1,len(row)):
_split = row[j].split(':')
col_index = int(_split[0]) + 1 # increase the id by one to reserve element 0 for NULL
value = float(_split[1])
rows.append(row_index)
cols.append(col_index)
data.append(value)
row_index+=1
X = sp.csr_matrix((data, (rows, cols)), shape=(row_index, vocab_size))
Y = np.array(y)
# LOAD FEATURES
# one for each meta
print("\tstep 2 : LOAD FEATURES")
X_weights = []
X_ids = []
for m in range(len(unique_meta)):
rows = []
cols = []
sp_weights = []
sp_ids = []
max_arbitrary_col_index = 0
with open(cooccur_file, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
row_index = 0
for row in reader:
'''if row_index > 100:
break'''
# print(row_index, " ",row)
current_y = float(row[0])
y.append(current_y)
arbitrary_col_index = 0
for j in range(1,len(row)):
_split = row[j].split(':')
embedding_index = int(_split[0]) + 1 # increase the id by one to reserve element 0 for NULL
value = float(_split[1])
if (meta_vector[embedding_index] == unique_meta[m]):
rows.append(row_index)
cols.append(arbitrary_col_index)
sp_weights.append(value)
sp_ids.append(embedding_index)
arbitrary_col_index += 1
#print(arbitrary_col_index)
if arbitrary_col_index>max_arbitrary_col_index:
max_arbitrary_col_index = arbitrary_col_index
'''# raw fix for pos and words
# ------------------------------------
if ( arbitrary_col_index == 1 and (unique_meta[m] == WORD_META_ID or unique_meta[m] == POS_META_ID)):
print("ERROR a")
sys.exit(-1)
rows.append(row_index)
cols.append(arbitrary_col_index)
old_weight = sp_weights[-1]
# print("meta: {}, old_weight : {}".format(m,old_weight))
old_weight = 1.
sp_weights[-1] = old_weight
old_id = sp_ids[-1]
sp_weights.append(old_weight)
sp_ids.append(old_id)
arbitrary_col_index += 1
if arbitrary_col_index<max_arbitrary_col_index:
print("ERROR b")
sys.exit(-1)
assert(arbitrary_col_index==2)
# ------------------------------------ '''
row_index+=1
assert(row_index == len(Y))
sparse_weight = sp.csr_matrix((sp_weights, (rows, cols)), shape=(row_index, max_arbitrary_col_index))
sparse_indices = sp.csr_matrix((sp_ids, (rows, cols)), shape=(row_index, max_arbitrary_col_index))
X_weights.append(sparse_weight)
X_ids.append(sparse_indices)
# validity check
# print(X_ids[1])
'''for m in range(len(unique_meta)):
cx = X_ids[m].tocoo()
check = {}
for i,j,v in zip(cx.row, cx.col, cx.data):
if i in check:
check[i]+=1
else:
check[i]=1
for (k,v) in check.items():
if (v!=1):
print("{} - {}".format(k,v))
sys.exit(-1)
print("\tvalidity check passed")'''
# step 3 : SERIALIZE MATRICES
print("\tstep 3 : SERIALIZE MATRICES")
filename = cooccur_file+"_X"
save_sparse_csr(filename, X)
for m in range(len(unique_meta)):
filename = cooccur_file+"_X_ids_"+str(m)
save_sparse_csr(filename, X_ids[m])
filename = cooccur_file+"_X_weights_"+str(m)
save_sparse_csr(filename, X_weights[m])
filename = cooccur_file+"_Y"
np.save(filename, Y)
return X, X_ids, X_weights, Y
def loadValidSet(self, word_meta2_id_dict):
global v_words
global v_topics
global v_pos
global v_year
global v_product
valid_set_word = set()
valid_set_pos = set()
try :
valid_examples_words = [word_meta2_id_dict[(w,WORD_META_ID)] for w in v_words]
valid_set_word = set(valid_examples_words)
except :
print(" WARNING: unable to load words ! ")
'''try :
valid_examples_topics = [word_meta2_id_dict[(w,TOPIC_META_ID)] for w in v_topics]
valid_set = valid_set | set(valid_examples_topics)
except :
print(" WARNING: unable to load valid TOPICS ! ")'''
#try :
# valid_examples_pos = [word_meta2_id_dict[(w,POS_META_ID)] for w in v_pos]
# valid_set_pos = set(valid_examples_pos)
#except :
# print(" WARNING: unable to load valid POS ! ")
'''try :
valid_examples_year = [word_meta2_id_dict[(w,YEAR_META_ID)] for w in v_year]
valid_set = valid_set | set(valid_examples_year)
except :
print(" WARNING: unable to load valid YEAR ! ")
try :
valid_examples_products = [word_meta2_id_dict[(w,PRODUCT_META_ID)] for w in v_product]
valid_set = valid_set | set(valid_examples_products)
except :
print(" WARNING: unable to load valid PRODUCTS ! ")'''
valid_examples_words = list(valid_set_word)
#valid_examples_pos= list(valid_set_pos)
return valid_examples_words #,valid_examples_pos