forked from HKUST-KnowComp/semihin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatareader.py
555 lines (480 loc) · 18.5 KB
/
datareader.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import csv
from hin import HIN
import misc
class DataReaderWang:
''' Data reader for Chenguang Wang's 20NG and RCV1 data
'''
def __init__(self, path='20news.hin',scope=[], data_set='20ng'):
# Initial
self.path = path
self.scope = scope
self.data_set = data_set
# Attributes Declaration
self.Ids = {}
self.DocIds = {}
self.DocLabels = {}
self.Links = []
self.RelationIds = {}
self.NodeTypes = {}
self.TypeIds = {}
self.WordIds = {}
self.WordDFs = {}
self.WordCount = {}
self.EntityDFs = {}
# Default Values
self.TypeIds['document'] = 0
self.TypeIds['word'] = 1
self.RelationIds['document_word'] = 0
self.RelationIds['_document_word'] = 1
def readfile(self):
if self.data_set == '20ng':
from misc import word_valid
else:
from misc_gcat import word_valid
f = open(self.path)
self.Ids = {}
self.DocIds = {}
scope_flag = False
for line in f.readlines():
sp = line.split()
# Document Header
if line.startswith('doc'):
doc_name = sp[0][4:]
doc_type = sp[2]
# Constrain the data to be in 3 classes
if doc_type in self.scope:
scope_flag = True
doc_id = len(self.Ids)
self.Ids[doc_name] = doc_id
self.NodeTypes[doc_id] = self.TypeIds['document']
self.DocIds[doc_name] = doc_id
self.DocLabels[doc_id] = doc_type
else:
scope_flag = False
# Skip the line that is out the scope
elif not scope_flag:
continue
elif line.startswith('----sentence'):
sp = line[20:].lower().split()
for w in sp:
if word_valid(w):
if w not in self.Ids:
# word serial
word_id = len(self.Ids)
self.Ids[w] = word_id
self.WordIds[w] = word_id
self.NodeTypes[word_id] = self.TypeIds['word']
else:
word_id = self.Ids[w]
# word count
self.WordCount[word_id] = self.WordCount.get(word_id,0) + 1
# word DFs
if word_id not in self.WordDFs:
self.WordDFs[word_id] = set()
if doc_id not in self.WordDFs[word_id]:
self.WordDFs[word_id].add(doc_id)
# DWD links
self.Links.append((doc_id, self.RelationIds['document_word'], word_id))
# linkage from KB
elif line.startswith('\t\t'):
head = sp[0]
tail = sp[2]
relation = sp[1]
# register Ids
if head not in self.Ids:
self.Ids[head] = len(self.Ids)
head_id = self.Ids[head]
if tail not in self.Ids:
self.Ids[tail] = len(self.Ids)
tail_id = self.Ids[tail]
if relation not in self.RelationIds:
self.RelationIds[relation] = len(self.RelationIds)
self.RelationIds['_' + relation] = len(self.RelationIds)
relation_id = self.RelationIds[relation]
# add relation links
self.Links.append((head_id, relation_id, tail_id))
# named entities
elif line.startswith('\t'):
# register Ids
entity = sp[0]
entity_type = sp[1]
if entity not in self.Ids:
self.Ids[entity] = len(self.Ids)
entity_id = self.Ids[entity]
if entity_type not in self.TypeIds:
self.TypeIds[entity_type] = len(self.TypeIds)
entity_typeId = self.TypeIds[entity_type]
self.NodeTypes[entity_id] = entity_typeId
# entity DFs
if entity_id not in self.EntityDFs:
self.EntityDFs[entity_id] = set()
if doc_id not in self.EntityDFs[entity_id]:
self.EntityDFs[entity_id].add(doc_id)
# Document-Entity links
relation = 'document_' + entity_type
if relation not in self.RelationIds:
self.RelationIds[relation] = len(self.RelationIds)
self.RelationIds['_' + relation] = len(self.RelationIds)
relation_id = self.RelationIds[relation]
self.Links.append((doc_id, relation_id, entity_id))
# calculate word DF
df = {}
for word_id in self.WordDFs:
df[word_id] = len(self.WordDFs[word_id])
self.WordDFs = df
# calculate entity DF
df = {}
for entity_id in self.EntityDFs:
df[entity_id] = len(self.EntityDFs[entity_id])
self.EntityDFs = df
print 'word max count %d' % max(self.WordCount, key =self.WordCount.get)
print 'word max DF %d' % max(df, key=df.get)
def getlabel(self):
return self.DocLabels
def getHIN(self):
hin = HIN()
hin.Ids = self.Ids
hin.DocIds = self.DocIds
hin.DocLabels = self.DocLabels
hin.Links = self.Links
hin.RelationIds = self.RelationIds
hin.NodeTypes = self.NodeTypes
hin.TypeIds = self.TypeIds
hin.WordIds = self.WordIds
hin.WordDFs = self.WordDFs
hin.WordCount = self.WordCount
hin.EntityDFs = self.EntityDFs
return hin
class YAGOReader:
def __init__(self,path='/data/YAGO/yagoFacts.tsv'):
self.path = path
# define tsv dialect
self.tsvDialect = csv.Dialect()
self.tsvDialect.delimiter = '\t'
self.tsvDialect.delimiter = '"'
# Attributes Declaration
self.Ids = {}
self.Links = []
self.RelationIds = {}
self.NodeTypes = {}
self.TypeIds = {}
def readfile(self):
tsvfile = open(self.path)
reader = csv.reader(tsvfile,dialect=self.tsvDialect)
for line in reader:
head = line[1]
relation = line[2]
tail = line[3]
if head not in self.Ids:
head_id = len(self.Ids)
self.Ids[head] = head_id
else:
head_id = self.Ids[head]
if tail not in self.Ids:
tail_id = len(self.Ids)
self.Ids[tail] = tail_id
else:
tail_id = self.Ids[tail]
if relation not in self.RelationIds:
relation_id = len(self.RelationIds)
self.RelationIds[relation] = relation_id
else:
relation_id = self.RelationIds[relation]
self.Links.append((head_id,relation_id,tail_id))
def getHIN(self):
hin = HIN()
hin.Ids = self.Ids
hin.RelationIds = self.RelationIds
hin.Links = self.Links
return hin
class FB15KReader:
def __init__(self,path='/home/data/corpora/FB15K/'):
self.path = path
# define tsv dialect
self.tsvDialect = csv.Dialect()
self.tsvDialect.delimiter = '\t'
# self.tsvDialect.delimiter = '"'
# Attributes Declaration
self.Ids = {}
self.Links = []
self.TestLinks = []
self.ValidLinks = []
self.RelationIds = {}
self.NodeTypes = {}
self.TypeIds = {}
def readfile(self):
train = open(self.path + 'train.txt')
valid = open(self.path + 'valid.txt')
test = open(self.path + 'test.txt')
trainReader = csv.reader(train,dialect=self.tsvDialect)
validReader = csv.reader(valid,dialect=self.tsvDialect)
testReader = csv.reader(test,dialect=self.tsvDialect)
for line in trainReader:
head = line[0]
relation = line[1]
tail = line[2]
if head not in self.Ids:
head_id = len(self.Ids)
self.Ids[head] = head_id
else:
head_id = self.Ids[head]
if tail not in self.Ids:
tail_id = len(self.Ids)
self.Ids[tail] = tail_id
else:
tail_id = self.Ids[tail]
if relation not in self.RelationIds:
relation_id = len(self.RelationIds)
self.RelationIds[relation] = relation_id
else:
relation_id = self.RelationIds[relation]
self.Links.append((head_id,relation_id,tail_id))
for line in validReader:
head = line[0]
relation = line[1]
tail = line[2]
if head not in self.Ids:
head_id = len(self.Ids)
self.Ids[head] = head_id
else:
head_id = self.Ids[head]
if tail not in self.Ids:
tail_id = len(self.Ids)
self.Ids[tail] = tail_id
else:
tail_id = self.Ids[tail]
if relation not in self.RelationIds:
relation_id = len(self.RelationIds)
self.RelationIds[relation] = relation_id
else:
relation_id = self.RelationIds[relation]
self.ValidLinks.append((head_id,relation_id,tail_id))
for line in testReader:
head = line[0]
relation = line[1]
tail = line[2]
if head not in self.Ids:
head_id = len(self.Ids)
self.Ids[head] = head_id
else:
head_id = self.Ids[head]
if tail not in self.Ids:
tail_id = len(self.Ids)
self.Ids[tail] = tail_id
else:
tail_id = self.Ids[tail]
if relation not in self.RelationIds:
relation_id = len(self.RelationIds)
self.RelationIds[relation] = relation_id
else:
relation_id = self.RelationIds[relation]
self.TestLinks.append((head_id,relation_id,tail_id))
def getHIN(self):
hin = HIN()
hin.Ids = self.Ids
hin.RelationIds = self.RelationIds
hin.Links = self.Links
hin.ValidLinks= self.ValidLinks
hin.TestLinks = self.TestLinks
return hin
# TODO : for NELL data set, understand their type system.
# Done : NELL have no type system, the semantic is stored in links
class NELLReader:
def __init__(self,path='/data/NELL/NELL.csv'):
self.path = path
# Attributes Declaration
self.Ids = {}
self.Links = []
self.RelationIds = {}
self.NodeTypes = {}
self.TypeIds = {}
def readfile(self):
csvfile = open(self.path)
reader = csv.DictReader(csvfile)
for line in reader:
head = line['Entity']
relation = line['Relation']
tail = line['Value']
if head not in self.Ids:
head_id = len(self.Ids)
self.Ids[head] = head_id
else:
head_id = self.Ids[head]
if tail not in self.Ids:
tail_id = len(self.Ids)
self.Ids[tail] = tail_id
else:
tail_id = self.Ids[tail]
if relation not in self.RelationIds:
relation_id = len(self.RelationIds)
self.RelationIds[relation] = relation_id
else:
relation_id = self.RelationIds[relation]
self.Links.append((head_id, relation_id, tail_id))
def getHIN(self):
hin = HIN()
hin.Ids = self.Ids
hin.RelationIds = self.RelationIds
hin.Links = self.Links
# Read DBLP data from Yizhou Sun
class DBLPReader:
def __init__(self,path='/home/data/corpora/DBLP/'):
self.path = path
# Attributes Declaration
self.Ids = {}
self.Links = []
self.RelationIds = {}
self.NodeTypes = {}
self.TypeIds = {}
# Special : Old - New Id projection from file
self.AuthorOldId = {}
self.ConfOldId = {}
self.TermOldId = {}
# Labels for semi-supervised classification
self.AuthorLabels = {}
self.ConfLabels = {}
# Fixed types
self.TypeIds['word'] = 0
# Fixed Relations
if 'author' not in self.TypeIds:
self.TypeIds['author'] = len(self.TypeIds)
if 'conference' not in self.TypeIds:
self.TypeIds['conference'] = len(self.TypeIds)
if 'term' not in self.TypeIds:
self.TypeIds['term'] = len(self.TypeIds)
if 'author_author' not in self.RelationIds:
self.RelationIds['author_author'] = len(self.RelationIds)
if 'author_term' not in self.RelationIds:
self.RelationIds['author_term'] = len(self.RelationIds)
if 'conference_author' not in self.RelationIds:
self.RelationIds['conference_author'] = len(self.RelationIds)
if 'conference_term' not in self.RelationIds:
self.RelationIds['conference_term'] = len(self.RelationIds)
def readfile(self):
authorDictFile = open(self.path + 'author_dict.txt')
confDictFile = open(self.path + 'conf_dict.txt')
termDictFile = open(self.path + 'term_dict.txt')
# read and convert Ids
for line in authorDictFile.readlines():
sp = line.split('\t')
author = sp[1]
author_old_id = int(sp[0])
author_id = len(self.Ids)
self.Ids[author] = author_id
self.AuthorOldId[author_old_id] = author_id
self.NodeTypes[author_id] = self.TypeIds['author']
for line in confDictFile.readlines():
sp = line.split('\t')
conf = sp[1]
conf_old_id = int(sp[0])
conf_id = len(self.Ids)
self.Ids[conf] = conf_id
self.ConfOldId[conf_old_id] = conf_id
self.NodeTypes[conf_id] = self.TypeIds['conference']
for line in termDictFile.readlines():
sp = line.split('\t')
term = sp[1]
term_old_id = int(sp[0])
term_id = len(self.Ids)
self.Ids[term] = term_id
self.TermOldId[term_old_id] = term_id
self.NodeTypes[term_id] = self.TypeIds['term']
# read
AAFile = open(self.path + 'AA.txt')
ATFile = open(self.path + 'AT.txt')
CAFile = open(self.path + 'CA.txt')
CTFile = open(self.path + 'CT.txt')
for line in AAFile.readlines():
sp = line.split()
head = int(sp[0])
tail = int(sp[1])
weight = int(sp[2])
head_id = self.AuthorOldId[head]
tail_id = self.AuthorOldId[tail]
relation_id = self.RelationIds['author_author']
self.Links.append((head_id, relation_id, tail_id, weight))
for line in ATFile.readlines():
sp = line.split()
head = int(sp[0])
tail = int(sp[1])
weight = int(sp[2])
head_id = self.AuthorOldId[head]
tail_id = self.TermOldId[tail]
relation_id = self.RelationIds['author_term']
self.Links.append((head_id, relation_id, tail_id, weight))
for line in CAFile.readlines():
sp = line.split()
head = int(sp[0])
tail = int(sp[1])
weight = int(sp[2])
head_id = self.ConfOldId[head]
tail_id = self.AuthorOldId[tail]
relation_id = self.RelationIds['conference_author']
self.Links.append((head_id, relation_id, tail_id, weight))
for line in CTFile.readlines():
sp = line.split()
head = int(sp[0])
tail = int(sp[1])
weight = int(sp[2])
head_id = self.ConfOldId[head]
tail_id = self.TermOldId[tail]
relation_id = self.RelationIds['conference_term']
self.Links.append((head_id, relation_id, tail_id, weight))
authorLabelFile = open(self.path + 'author_label.txt')
confLabelFile = open(self.path + 'conf_label.txt')
for line in authorLabelFile.readlines():
sp = line.split()
author = int(sp[0])
label = int(sp[1])
self.AuthorLabels[self.AuthorOldId[author]] = label
for line in confLabelFile.readlines():
sp = line.split()
conf = int(sp[0])
label = int(sp[2])
self.ConfLabels[self.ConfOldId[conf]] = label
def getHIN(self):
hin = HIN()
hin.Ids = self.Ids
hin.RelationIds = self.RelationIds
hin.Links = self.Links
hin.NodeTypes = self.NodeTypes
hin.TypeIds = self.TypeIds
hin.AuthorLabels = self.AuthorLabels
hin.ConfLabels = self.ConfLabels
return hin
# Read four_area data from Yizhou Sun
class FourAreaReader:
def __init__(self,path='/home/data/corpora/four_area/'):
self.path = path
# Attributes Declaration
self.Ids = {}
self.Links = []
self.RelationIds = {}
self.NodeTypes = {}
self.TypeIds = {}
# Special : Old - New Id projection from file
self.AuthorOldId = {}
self.ConfOldId = {}
self.TermOldId = {}
# Labels for semi-supervised classification
self.AuthorLabel = {}
self.ConfLabel = {}
# Fixed types
self.TypeIds['word'] = 0
# Fixed Relations
if 'author' not in self.TypeIds:
self.TypeIds['author'] = len(self.TypeIds)
if 'conference' not in self.TypeIds:
self.TypeIds['conference'] = len(self.TypeIds)
if 'term' not in self.TypeIds:
self.TypeIds['term'] = len(self.TypeIds)
if 'author_author' not in self.RelationIds:
self.RelationIds['author_author'] = len(self.RelationIds)
if 'author_term' not in self.RelationIds:
self.RelationIds['author_term'] = len(self.RelationIds)
if 'conference_author' not in self.RelationIds:
self.RelationIds['conference_author'] = len(self.RelationIds)
if 'conference_term' not in self.RelationIds:
self.RelationIds['conference_term'] = len(self.RelationIds)
def readfile(self):
pass