forked from BUPT-GAMMA/HGAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_data.py
301 lines (246 loc) · 9.07 KB
/
build_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
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
#!/user/bin/env python
# -*- coding: utf-8 -*-
import networkx
# import matplotlib.pyplot as plt
import json, os
import pickle
import gensim
import numpy as np
from tqdm import tqdm
from scipy import sparse as spr
from utils import sample
DATASETS = 'example'
rootpath = './'
outpath = rootpath + 'model/data/{}/'.format(DATASETS)
datapath = rootpath + 'data/{}/'.format(DATASETS)
SIM = 0.5
LP = 0.75
RHO = 0.3
TopK_for_Topics = 2
def cnt_nodes(g):
text_nodes, entity_nodes, topic_nodes = set(), set(), set()
for i in g.nodes():
if i.isdigit():
text_nodes.add(i)
elif i[:6] == 'topic_':
topic_nodes.add(i)
else:
entity_nodes.add(i)
print("# text_nodes: {} # entity_nodes: {} # topic_nodes: {}".format(
len(text_nodes), len(entity_nodes), len(topic_nodes)))
return text_nodes, entity_nodes, topic_nodes
with open(datapath+'model_network_sampled.pkl', 'rb') as f:
g = pickle.load(f)
text_nodes, entity_nodes, topic_nodes = cnt_nodes(g)
with open(datapath+"features_BOW.pkl", 'rb') as f:
features_BOW = pickle.load(f)
with open(datapath+"features_TFIDF.pkl", 'rb') as f:
features_TFIDF = pickle.load(f)
with open(datapath+"features_index.pkl", 'rb') as f:
features_index_BOWTFIDF = pickle.load(f)
with open(datapath+"features_entity_descBOW.pkl", 'rb') as f:
features_entity_BOW = pickle.load(f)
with open(datapath+"features_entity_descTFIDF.pkl", 'rb') as f:
features_entity_TFIDF = pickle.load(f)
with open(datapath+"features_entity_index_desc.pkl", 'rb') as f:
features_entity_index_desc = pickle.load(f)
feature = features_TFIDF
features_index = features_index_BOWTFIDF
entityF = features_entity_TFIDF
features_entity_index = features_entity_index_desc
textShape = feature.shape
entityShape = entityF.shape
print("Shape of text feature:",textShape, 'Shape of entity feature:', entityShape)
# 删掉没有特征的实体
notinind = set()
entitySet = set(entity_nodes)
print(len(entitySet))
for i in entitySet:
if i not in features_entity_index:
notinind.add(i)
print(len(g.nodes()), len(notinind))
g.remove_nodes_from(notinind)
entitySet = entitySet - notinind
print(len(entitySet), len(features_entity_index))
N = len(g.nodes())
print(len(g.nodes()), len(g.edges()))
text_nodes, entity_nodes, topic_nodes = cnt_nodes(g)
# 删掉一些边
cnt = 0
nodes = g.nodes()
print(len(g.edges()))
for node in tqdm(nodes):
try:
cache = [j for j in g[node]
if ('sim' in g[node][j] and g[node][j]['sim'] < SIM) # 0.5
or ('link_probability' in g[node][j] and g[node][j]['link_probability'] <= LP)
or ('rho' in g[node][j] and g[node][j]['rho'] < RHO)
]
if len(cache) != 0:
g.remove_edges_from( [(node, i) for i in cache] )
cnt += len( cache )
except:
print(g[node])
break
print(len(g.edges()), cnt)
# 删掉孤立点(实体)
delete = [n for n in g.nodes() if len(g[n]) == 0 and n not in text_nodes]
print("Num of 孤立点:", len(delete))
g.remove_nodes_from(delete)
train, vali, test, alltext = sample(datapath, DATASETS)
# topic
with open(datapath + 'topic_word_distribution.pkl', 'rb') as f:
topic_word = pickle.load(f)
with open(datapath + 'doc_topic_distribution.pkl', 'rb') as f:
doc_topic = pickle.load(f)
with open(datapath + 'doc_index_LDA.pkl', 'rb') as f:
doc_idx_list = pickle.load(f)
topic_num = topic_word.shape[0]
topics = []
for i in range(topic_num):
topicName = 'topic_' + str(i)
topics.append(topicName)
def naive_arg_topK(matrix, K, axis=0):
"""
perform topK based on np.argsort
:param matrix: to be sorted
:param K: select and sort the top K items
:param axis: dimension to be sorted.
:return:
"""
full_sort = np.argsort(-matrix, axis=axis)
return full_sort.take(np.arange(K), axis=axis)
topK_topics = naive_arg_topK(doc_topic, TopK_for_Topics, axis=1)
for i in range(topK_topics.shape[0]):
for j in range(TopK_for_Topics):
g.add_edge(doc_idx_list[i], topics[topK_topics[i, j]])
print("gnodes:", len(g.nodes()), "gedges:", len(g.edges()))
# build Edges data
import collections
cnt = 0
nodes = g.nodes()
graphdict = collections.defaultdict(list)
for node in tqdm(nodes):
try:
cache = [j for j in g[node]
if ('sim' in g[node][j] and g[node][j]['sim'] >= SIM) or ('sim' not in g[node][j]) # 0.5
if ('link_probability' in g[node][j] and g[node][j]['link_probability'] > LP) or ('link_probability' not in g[node][j])
if ('rho' in g[node][j] and g[node][j]['rho'] > RHO) or ('rho' not in g[node][j])
]
if len(cache) != 0:
graphdict[ node ] = cache
cnt += len( cache )
except:
print(g[node])
break
print('edges: ', cnt)
def normalizeF(mx):
sup = np.absolute(mx).max()
if sup == 0:
return mx
return mx / sup
text_nodes, entity_nodes, topic_nodes = cnt_nodes(g)
mapindex = dict()
cnt = 0
for i in text_nodes|entity_nodes|topic_nodes:
mapindex[i] = cnt
cnt += 1
print(len(g.nodes()), len(mapindex))
if not os.path.exists(outpath):
os.makedirs(outpath)
# build feature data
gnodes = set(g.nodes())
print(gnodes, mapindex)
with open(outpath + 'train.map', 'w') as f:
f.write('\n'.join([str(mapindex[i]) for i in train if i in gnodes]))
with open(outpath + 'vali.map', 'w') as f:
f.write('\n'.join([str(mapindex[i]) for i in vali if i in gnodes]))
with open(outpath + 'test.map', 'w') as f:
f.write('\n'.join([str(mapindex[i]) for i in test if i in gnodes]))
flag_zero = False
input_type = 'text&entity&topic2hgcn'
if input_type == 'text&entity&topic2hgcn':
node_with_feature = set()
DEBUG = False
# text node
content = dict()
for i in tqdm(range(textShape[0])):
ind = features_index[i]
if (ind) not in text_nodes:
continue
content[ind] = feature[i, :].toarray()[0].tolist() #
if DEBUG:
content[ind] = feature[i, :10].toarray()[0].tolist()
if flag_zero:
entityFlen = entityShape[1]
content[ind] += [0] * (entityFlen + topic_word.shape[1])
with open(datapath + '{}.txt'.format(DATASETS), 'r') as f:
for line in tqdm(f):
ind, cat = line.strip('\n').split('\t')[:2]
ind = (ind)
if ind not in text_nodes:
continue
content[ind] += [cat]
alllen = len(content[ind])
with open(outpath + '{}.content.text'.format(DATASETS), 'w') as f:
for ind in tqdm(content):
f.write(str(mapindex[ind]) + '\t' + '\t'.join(map(str, content[ind])) + '\n')
node_with_feature.add(ind)
cache = len(content)
print("共{}个文本".format(len(content)))
# entity node
content = dict()
for i in tqdm(range(entityShape[0])):
name = features_entity_index[i]
if name not in entity_nodes:
continue
content[name] = entityF[i, :].toarray()[0].tolist() + ['entity']
if flag_zero:
content[name] = [0] * textShape[1] + content[name] + [0] * topic_word.shape[1] + ['entity']
with open(outpath + '{}.content.entity'.format(DATASETS), 'w') as f:
for ind in tqdm(content):
f.write(str(mapindex[ind]) + '\t' + '\t'.join(map(str, content[ind])) + '\n')
node_with_feature.add(ind)
cache += len(content)
print("共{}个实体".format(len(content)))
# topic node
content = dict()
for i in range(topic_num):
# zero_num = textShape[1] + entityFlen - topic_num
topicName = topics[i]
if topicName not in topic_nodes:
continue
one_hot = [0] * topic_num
one_hot[i] = 1
content[topicName] = one_hot
content[topicName] = topic_word[i].tolist() + ['topic']
if flag_zero:
zero_num = textShape[1] + entityFlen
content[topicName] = [0] * zero_num + content[topicName] + ['topic']
with open(outpath + '{}.content.topic'.format(DATASETS), 'w') as f:
for ind in tqdm(content):
f.write(str(mapindex[ind]) + '\t' + '\t'.join(map(str, content[ind])) + '\n')
node_with_feature.add(ind)
cache += len(content)
print("共{}个主题".format(len(content)))
print(cache, len(mapindex))
print("nodes with features:", len(node_with_feature))
# save mappings
with open(outpath+'mapindex.txt', 'w') as f:
for i in mapindex:
f.write("{}\t{}\n".format(i, mapindex[i]))
# save adj matrix
with open(outpath+'{}.cites'.format(DATASETS), 'w') as f:
doneSet = set()
nodeSet = set()
for node in graphdict:
for i in graphdict[node]:
if (node, i) not in doneSet:
f.write( str(mapindex[node])+'\t'+str(mapindex[i])+'\n' )
doneSet.add( (i, node) )
doneSet.add( (node, i) )
nodeSet.add( node )
nodeSet.add( i )
for i in tqdm(range(len(mapindex))):
f.write(str(i)+'\t'+str(i)+'\n')
print('Num of nodes with edges: ', len(nodeSet))