forked from hadifar/stc_clustering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
242 lines (186 loc) · 7.58 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
# -*- coding: utf-8 -*-
import os
from collections import Counter
import nltk
import numpy as np
import scipy.io
from sklearn.decomposition import PCA, TruncatedSVD
from sklearn.preprocessing import MinMaxScaler
def load_stackoverflow(data_path='data/stackoverflow/'):
# load SO embedding
with open(data_path + 'vocab_withIdx.dic', 'r') as inp_indx, \
open(data_path + 'vocab_emb_Word2vec_48_index.dic', 'r') as inp_dic, \
open(data_path + 'vocab_emb_Word2vec_48.vec') as inp_vec:
pair_dic = inp_indx.readlines()
word_index = {}
for pair in pair_dic:
word, index = pair.replace('\n', '').split('\t')
word_index[word] = index
index_word = {v: k for k, v in word_index.items()}
del pair_dic
emb_index = inp_dic.readlines()
emb_vec = inp_vec.readlines()
word_vectors = {}
for index, vec in zip(emb_index, emb_vec):
word = index_word[index.replace('\n', '')]
word_vectors[word] = np.array(list((map(float, vec.split()))))
del emb_index
del emb_vec
with open(data_path + 'title_StackOverflow.txt', 'r') as inp_txt:
all_lines = inp_txt.readlines()[:-1]
text_file = " ".join([" ".join(nltk.word_tokenize(c)) for c in all_lines])
word_count = Counter(text_file.split())
total_count = sum(word_count.values())
unigram = {}
for item in word_count.items():
unigram[item[0]] = item[1] / total_count
all_vector_representation = np.zeros(shape=(20000, 48))
for i, line in enumerate(all_lines):
word_sentence = nltk.word_tokenize(line)
sent_rep = np.zeros(shape=[48, ])
j = 0
for word in word_sentence:
try:
wv = word_vectors[word]
j = j + 1
except KeyError:
continue
weight = 0.1 / (0.1 + unigram[word])
sent_rep += wv * weight
if j != 0:
all_vector_representation[i] = sent_rep / j
else:
all_vector_representation[i] = sent_rep
pca = PCA(n_components=1)
pca.fit(all_vector_representation)
pca = pca.components_
XX1 = all_vector_representation - all_vector_representation.dot(pca.transpose()) * pca
XX = XX1
scaler = MinMaxScaler()
XX = scaler.fit_transform(XX)
with open(data_path + 'label_StackOverflow.txt') as label_file:
y = np.array(list((map(int, label_file.readlines()))))
print(y.dtype)
return XX, y
def load_search_snippet2(data_path='data/SearchSnippets/new/'):
mat = scipy.io.loadmat(data_path + 'SearchSnippets-STC2.mat')
emb_index = np.squeeze(mat['vocab_emb_Word2vec_48_index'])
emb_vec = mat['vocab_emb_Word2vec_48']
y = np.squeeze(mat['labels_All'])
del mat
rand_seed = 0
# load SO embedding
with open(data_path + 'SearchSnippets_vocab2idx.dic', 'r') as inp_indx:
pair_dic = inp_indx.readlines()
word_index = {}
for pair in pair_dic:
word, index = pair.replace('\n', '').split('\t')
word_index[word] = index
index_word = {v: k for k, v in word_index.items()}
del pair_dic
word_vectors = {}
for index, vec in zip(emb_index, emb_vec.T):
word = index_word[str(index)]
word_vectors[word] = vec
del emb_index
del emb_vec
with open(data_path + 'SearchSnippets.txt', 'r') as inp_txt:
all_lines = inp_txt.readlines()[:-1]
all_lines = [line for line in all_lines]
text_file = " ".join([" ".join(nltk.word_tokenize(c)) for c in all_lines])
word_count = Counter(text_file.split())
total_count = sum(word_count.values())
unigram = {}
for item in word_count.items():
unigram[item[0]] = item[1] / total_count
all_vector_representation = np.zeros(shape=(12340, 48))
for i, line in enumerate(all_lines):
word_sentence = nltk.word_tokenize(line)
sent_rep = np.zeros(shape=[48, ])
j = 0
for word in word_sentence:
try:
wv = word_vectors[word]
j = j + 1
except KeyError:
continue
weight = 0.1 / (0.1 + unigram[word])
sent_rep += wv * weight
if j != 0:
all_vector_representation[i] = sent_rep / j
else:
all_vector_representation[i] = sent_rep
svd = TruncatedSVD(n_components=1, n_iter=20)
svd.fit(all_vector_representation)
svd = svd.components_
XX = all_vector_representation - all_vector_representation.dot(svd.transpose()) * svd
scaler = MinMaxScaler()
XX = scaler.fit_transform(XX)
return XX, y
def load_biomedical(data_path='data/Biomedical/'):
mat = scipy.io.loadmat(data_path + 'Biomedical-STC2.mat')
emb_index = np.squeeze(mat['vocab_emb_Word2vec_48_index'])
emb_vec = mat['vocab_emb_Word2vec_48']
y = np.squeeze(mat['labels_All'])
del mat
rand_seed = 0
# load SO embedding
with open(data_path + 'Biomedical_vocab2idx.dic', 'r') as inp_indx:
# open(data_path + 'vocab_emb_Word2vec_48_index.dic', 'r') as inp_dic, \
# open(data_path + 'vocab_emb_Word2vec_48.vec') as inp_vec:
pair_dic = inp_indx.readlines()
word_index = {}
for pair in pair_dic:
word, index = pair.replace('\n', '').split('\t')
word_index[word] = index
index_word = {v: k for k, v in word_index.items()}
del pair_dic
word_vectors = {}
for index, vec in zip(emb_index, emb_vec.T):
word = index_word[str(index)]
word_vectors[word] = vec
del emb_index
del emb_vec
with open(data_path + 'Biomedical.txt', 'r') as inp_txt:
all_lines = inp_txt.readlines()[:-1]
# print(sum([len(line.split()) for line in all_lines])/20000) #avg length
text_file = " ".join([" ".join(nltk.word_tokenize(c)) for c in all_lines])
word_count = Counter(text_file.split())
total_count = sum(word_count.values())
unigram = {}
for item in word_count.items():
unigram[item[0]] = item[1] / total_count
all_vector_representation = np.zeros(shape=(20000, 48))
for i, line in enumerate(all_lines):
word_sentence = nltk.word_tokenize(line)
sent_rep = np.zeros(shape=[48, ])
j = 0
for word in word_sentence:
try:
wv = word_vectors[word]
j = j + 1
except KeyError:
continue
weight = 0.1 / (0.1 + unigram[word])
sent_rep += wv * weight
if j != 0:
all_vector_representation[i] = sent_rep / j
else:
all_vector_representation[i] = sent_rep
svd = TruncatedSVD(n_components=1, random_state=rand_seed, n_iter=20)
svd.fit(all_vector_representation)
svd = svd.components_
XX = all_vector_representation - all_vector_representation.dot(svd.transpose()) * svd
scaler = MinMaxScaler()
XX = scaler.fit_transform(XX)
return XX, y
def load_data(dataset_name):
print('load data')
if dataset_name == 'stackoverflow':
return load_stackoverflow()
elif dataset_name == 'biomedical':
return load_biomedical()
elif dataset_name == 'search_snippets':
return load_search_snippet2()
else:
raise Exception('dataset not found...')