-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_loader.py
370 lines (324 loc) · 18 KB
/
data_loader.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.preprocessing import normalize
from stanfordcorenlp import StanfordCoreNLP
import os
import collections
from tqdm import tqdm
import string
class Data():
def __init__(self, args):
print('Preparing data...')
self.parse_args(args)
self.load_data()
self.split_data()
self.generate_word_word_semantic_links()
self.generate_word_word_pmi_and_syntactic_links()
self.sample_neighbors()
def parse_args(self, args):
self.dataset_name = args.dataset_name
self.minibatch_size = args.minibatch_size
self.training_ratio = args.training_ratio
self.num_sampled_neighbors = args.num_sampled_neighbors
self.word_word_graph_window_size = args.word_word_graph_window_size
self.word_word_graph_num_neighbors = args.word_word_graph_num_neighbors
self.author_prediction = args.author_prediction
self.word_embedding_model = 'glove'
self.word_embedding_dimension = 300
def load_data(self):
print('Loading data...')
self.documents = self.load_files('./data/' + self.dataset_name + '/contents.txt')
#if not self.author_prediction:
#self.doc_contents_bow = np.loadtxt('./data/' + self.dataset_name + '/contents_bow.txt')
self.num_documents = len(self.documents)
self.citations, self.num_citations = self.symmatrize_links(self.load_files('./data/' + self.dataset_name + '/citations.txt'))
self.pairwise_citations = self.generate_pairwise_links(self.citations)
self.authors = self.load_files('./data/' + self.dataset_name + '/authors.txt')
self.num_authors = len(np.unique([author_id for doc_id in self.authors for author_id in self.authors[doc_id]]))
if self.author_prediction:
self.hide_authors(self.authors)
self.coauthors = self.generate_coauthors(self.authors)
self.venues_available = os.path.isfile('./data/' + self.dataset_name + '/venues.txt')
if self.venues_available:
self.venues = np.reshape(np.loadtxt('./data/' + self.dataset_name + '/venues.txt', dtype=int), [-1, 1])
self.num_venues = int(np.amax(self.venues) + 1)
self.covenues = np.reshape(np.arange(self.num_venues, dtype=int), [-1, 1])
self.labels_available = os.path.isfile('./data/' + self.dataset_name + '/labels.txt')
if self.labels_available:
self.labels = np.loadtxt('./data/' + self.dataset_name + '/labels.txt')
self.num_labels = len(np.unique(self.labels))
self.word_embeddings = np.loadtxt('./data/' + self.dataset_name + '/word_embeddings_' + self.word_embedding_model + '_' + str(self.word_embedding_dimension) + 'd.txt', dtype=float)
self.voc = np.genfromtxt('./data/' + self.dataset_name + '/voc.txt', dtype=str)
self.word2id = {}
for word_id, word in enumerate(self.voc):
self.word2id[word] = word_id
self.num_words = len(self.voc)
self.doc_contents_word_embed = []
for doc_id in range(len(self.documents)):
self.doc_contents_word_embed.append(np.mean(self.word_embeddings[self.documents[doc_id]], axis=0))
self.doc_contents_word_embed = np.array(self.doc_contents_word_embed)
def load_files(self, file_path):
files = collections.defaultdict(list)
with open(file_path) as file:
for row_id, row in enumerate(file):
row = row.split()
row = [int(i) for i in row]
files[row_id] = row
return files
def symmatrize_links(self, links):
symmetric_links_set = collections.defaultdict(set)
for id1 in links:
for id2 in links[id1]:
symmetric_links_set[id1].add(id2)
symmetric_links_set[id2].add(id1)
# symmetric_links_set[id1].add(id1)
# symmetric_links_set[id2].add(id2)
symmetric_links, num_links = collections.defaultdict(list), 0
for id in symmetric_links_set:
symmetric_links[id] = list(symmetric_links_set[id])
num_links += len(symmetric_links_set[id])
num_links -= len(links)
return symmetric_links, num_links
def hide_authors(self, authors):
self.test_doc_author_links = []
doc_author_links = self.generate_pairwise_links(authors)
for author_id in range(self.num_authors):
docs_with_same_author = doc_author_links[doc_author_links[:, 1] == author_id]
if len(docs_with_same_author) < 3:
continue
idx = np.random.choice(len(docs_with_same_author))
doc_id = docs_with_same_author[idx, 0]
if len(self.authors[doc_id]) == 1:
if len(self.citations[doc_id]) > 1:
neighbor_doc_id = np.array(self.citations[doc_id])[self.citations[doc_id] != doc_id][-1]
self.authors[doc_id].append(self.authors[neighbor_doc_id][0])
else:
self.authors[doc_id].append(self.authors[0][0])
self.test_doc_author_links.append([doc_id, author_id])
self.authors[doc_id].remove(author_id)
self.test_doc_author_links = np.array(self.test_doc_author_links)
print('%.4f doc-author links are hidden!' % (len(self.test_doc_author_links) / len(doc_author_links)))
def generate_coauthors(self, authors):
coauthors_set = collections.defaultdict(set)
for doc_id in authors:
for author_id1 in authors[doc_id]:
for author_id2 in authors[doc_id]:
coauthors_set[author_id1].add(author_id2)
coauthors_set[author_id1].add(author_id1)
coauthors_set[author_id2].add(author_id2)
coauthors = collections.defaultdict(list)
for author_id in coauthors_set:
coauthors[author_id] = list(coauthors_set[author_id])
return coauthors
def generate_pairwise_links(self, links):
pairwise_links = []
# for id1 in range(len(links)):
for id1 in links:
for id2 in links[id1]:
pairwise_links.append([id1, id2])
# pairwise_links.append([id2, id1])
# pairwise_links.append([id1, id1])
# pairwise_links.append([id2, id2])
pairwise_links = np.unique(pairwise_links, axis=0)
return pairwise_links
def split_data(self):
print('Splitting data...')
split_idx = int(self.num_documents * self.training_ratio)
self.training_documents, self.test_documents = collections.defaultdict(list), collections.defaultdict(list)
for doc_id in range(self.num_documents):
if doc_id < split_idx:
self.training_documents[doc_id] = self.documents[doc_id]
else:
self.test_documents[doc_id] = self.documents[doc_id]
self.training_citations, self.test_citations, self.inference_citations = collections.defaultdict(list), collections.defaultdict(list), collections.defaultdict(list)
for doc_id1 in self.citations:
if doc_id1 < split_idx:
training_citations_one_doc = []
for doc_id2 in self.citations[doc_id1]:
if doc_id2 < split_idx:
training_citations_one_doc.append(doc_id2)
self.training_citations[doc_id1] = training_citations_one_doc
else:
test_citations_one_doc, inference_citations_one_doc = [], []
for doc_id2 in self.citations[doc_id1]:
if doc_id2 >= split_idx and doc_id1 != doc_id2:
test_citations_one_doc.append(doc_id2)
elif doc_id2 < split_idx or doc_id1 == doc_id2:
inference_citations_one_doc.append(doc_id2)
if len(test_citations_one_doc) > 0:
self.test_citations[doc_id1] = test_citations_one_doc
if len(inference_citations_one_doc) > 0:
self.inference_citations[doc_id1] = inference_citations_one_doc
self.training_pairwise_citations = self.generate_pairwise_links(self.training_citations)
self.test_pairwise_citations = self.generate_pairwise_links(self.test_citations)
np.random.shuffle(self.training_pairwise_citations)
self.num_training_citations, self.num_test_citations = len(self.training_pairwise_citations), len(self.test_pairwise_citations)
if self.venues_available:
self.training_venues, self.test_venues = self.venues[:split_idx], self.venues[split_idx:]
if self.labels_available:
self.training_labels, self.test_labels = self.labels[:split_idx], self.labels[split_idx:]
self.num_training_documents, self.num_test_documents = len(self.training_documents), len(self.test_documents)
def generate_word_word_semantic_links(self):
cos_sim = cosine_similarity(self.word_embeddings, self.word_embeddings)
self.word_word_semantic_links = collections.defaultdict(list)
for word_id, row in enumerate(cos_sim):
self.word_word_semantic_links[word_id] = np.argsort(row)[-self.word_word_graph_num_neighbors:]
self.word_word_semantic_links, _ = self.symmatrize_links(self.word_word_semantic_links)
def generate_word_word_pmi_and_syntactic_links(self):
windows = []
for doc_id in self.training_documents:
doc_length = len(self.training_documents[doc_id])
if doc_length <= self.word_word_graph_window_size:
windows.append(self.training_documents[doc_id])
else:
for start_idx in range(doc_length - self.word_word_graph_window_size + 1):
window = self.training_documents[doc_id][start_idx:start_idx + self.word_word_graph_window_size]
windows.append(window)
print('Generating word-word pmi links...')
word_pair_counts = self.generate_word_word_pmi_links(windows)
print('Generating word-word syntactic links...')
self.generate_word_word_syntactic_links(windows, word_pair_counts)
def generate_word_word_pmi_links(self, windows):
word_counts = {}
for window in windows:
appeared = set()
for i in range(len(window)):
if window[i] in appeared:
continue
if window[i] in word_counts:
word_counts[window[i]] += 1
else:
word_counts[window[i]] = 1
appeared.add(window[i])
word_pair_counts = {}
for window in windows:
for i in range(1, len(window)):
for j in range(0, i):
word_i = window[i]
word_j = window[j]
if word_i == word_j:
continue
word_pair_str = str(word_i) + ',' + str(word_j)
word_pair_str_reversed = str(word_j) + ',' + str(word_i)
if word_pair_str in word_pair_counts:
word_pair_counts[word_pair_str] += 1
elif word_pair_str_reversed in word_pair_counts:
word_pair_counts[word_pair_str_reversed] += 1
else:
word_pair_counts[word_pair_str] = 1
pmi_sim = np.zeros([self.num_words, self.num_words])
num_windows = len(windows)
for word_pair_str in word_pair_counts:
tmp = word_pair_str.split(',')
word_i = int(tmp[0])
word_j = int(tmp[1])
word_pair_count = word_pair_counts[word_pair_str]
word_count_i = word_counts[word_i]
word_count_j = word_counts[word_j]
pmi = np.log((1.0 * word_pair_count / num_windows) / (1.0 * word_count_i * word_count_j / (num_windows * num_windows)))
if pmi <= 0:
continue
pmi_sim[word_i, word_j] = pmi
pmi_sim[word_j, word_i] = pmi
self.word_word_pmi_links = collections.defaultdict(list)
for word_id, row in enumerate(pmi_sim):
self.word_word_pmi_links[word_id] = np.argsort(row)[-self.word_word_graph_num_neighbors:]
self.word_word_pmi_links, _ = self.symmatrize_links(self.word_word_pmi_links)
return word_pair_counts
def generate_word_word_syntactic_links(self, windows, word_pair_counts):
syntactic_links_available = os.path.isfile('./data/' + self.dataset_name + '/word_word_syntactic_links.txt')
if syntactic_links_available:
self.word_word_syntactic_links = self.load_files('./data/' + self.dataset_name + '/word_word_syntactic_links.txt')
return
nlp = StanfordCoreNLP(r'./data/stanford-corenlp-full-2016-10-31', lang='en')
rela_pair_count_str = {}
for window in tqdm(windows):
words = self.voc[window]
words_str = ''
for word in words:
words_str += word
words_str += ' '
words_str = words_str.strip()
words = nlp.word_tokenize(words_str)
# window = window.replace(string.punctuation, ' ')
res = nlp.dependency_parse(words_str)
for tuple in res:
if tuple[0] == 'ROOT':
continue
pair = [words[tuple[1] - 1], words[tuple[2] - 1]]
# rela.append(str(window[tuple[1] - 1]) + ', ' + str(window[tuple[2] - 1]))
if pair[0] == pair[1]:
continue
# if pair[0] in string.punctuation or pair[1] in string.punctuation:
# continue
if pair[0] not in self.word2id or pair[1] not in self.word2id:
continue
#word_pair_str = pair[0] + ',' + pair[1]
word_pair_str = str(self.word2id[pair[0]]) + ',' + str(self.word2id[pair[1]])
word_pair_str_reversed = str(self.word2id[pair[1]]) + ',' + str(self.word2id[pair[0]])
if word_pair_str in rela_pair_count_str:
rela_pair_count_str[word_pair_str] += 1
elif word_pair_str_reversed in rela_pair_count_str:
rela_pair_count_str[word_pair_str_reversed] += 1
else:
rela_pair_count_str[word_pair_str] = 1
# # two orders
# word_pair_str = str(self.word2id[pair[1]]) + ',' + str(self.word2id[pair[0]])
# if word_pair_str in rela_pair_count_str:
# rela_pair_count_str[word_pair_str] += 1
# else:
# rela_pair_count_str[word_pair_str] = 1
syntactic_sim = np.zeros([self.num_words, self.num_words])
for word_pair_str in rela_pair_count_str:
pair = word_pair_str.split(',')
word_i = int(pair[0])
word_j = int(pair[1])
word_pair_count = rela_pair_count_str[word_pair_str]
if word_pair_str not in word_pair_counts:
continue
syntactic_sim[word_i, word_j] = (1.0 * word_pair_count) / (1.0 * word_pair_counts[word_pair_str])
syntactic_sim[word_j, word_i] = (1.0 * word_pair_count) / (1.0 * word_pair_counts[word_pair_str])
self.word_word_syntactic_links = collections.defaultdict(list)
for word_id, row in enumerate(syntactic_sim):
self.word_word_syntactic_links[word_id] = np.argsort(row)[-self.word_word_graph_num_neighbors:]
self.word_word_syntactic_links, _ = self.symmatrize_links(self.word_word_syntactic_links)
with open('./data/' + self.dataset_name + '/word_word_syntactic_links.txt', 'w') as file:
for word_id in range(self.num_words):
words = self.word_word_syntactic_links[word_id]
for w in words:
file.write(str(w))
file.write(' ')
file.write('\n')
with open('./data/' + self.dataset_name + '/word_word_syntactic_counts.txt', 'w') as file:
for word_pair_str in rela_pair_count_str:
pair = word_pair_str.split(',')
word_i = int(pair[0])
word_j = int(pair[1])
word_pair_count = rela_pair_count_str[word_pair_str]
file.write(str(word_i) + ',' + str(word_j))
file.write('\t')
file.write(str(word_pair_count))
file.write('\n')
def sample_neighbors(self):
self.words_semantic_neighbors = self.sample_neighbors_func(self.word_word_semantic_links)
self.words_pmi_neighbors = self.sample_neighbors_func(self.word_word_pmi_links)
self.words_syntactic_neighbors = self.sample_neighbors_func(self.word_word_syntactic_links)
self.documents_neighbors = self.sample_neighbors_func(self.documents)
training_citations_neighbors = self.sample_neighbors_func(self.training_citations)
inference_citations_neighbors = self.sample_neighbors_func(self.inference_citations)
self.citations_neighbors = np.concatenate([training_citations_neighbors, inference_citations_neighbors], axis=0)
self.authors_neighbors = self.sample_neighbors_func(self.authors)
self.coauthors_neighbors = self.sample_neighbors_func(self.coauthors)
if self.venues_available:
self.venues_neighbors = np.tile(self.venues, [1, self.num_sampled_neighbors])
self.covenues_neighbors = np.tile(self.covenues, [1, self.num_sampled_neighbors])
def sample_neighbors_func(self, neighbors):
sampled_neighbors = []
keys = np.sort([key for key in neighbors.keys()])
#for id in range(len(neighbors)):
for id in keys:
replace = len(neighbors[id]) < self.num_sampled_neighbors
neighbor_idx = np.random.choice(len(neighbors[id]), size=self.num_sampled_neighbors, replace=replace)
sampled_neighbors.append([neighbors[id][idx] for idx in neighbor_idx])
sampled_neighbors = np.array(sampled_neighbors)
return sampled_neighbors