-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprepare_train.py
193 lines (170 loc) · 5.12 KB
/
prepare_train.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
import json
import argparse
import os
from collections import defaultdict
import random
from tqdm import tqdm
# P->P and P<-P
def no_intermediate_node(dataset, doc2text, docs, metadata):
meta2doc = defaultdict(set)
doc2meta = {}
with open(f'{dataset}/{dataset}_train.json') as fin:
for idx, line in enumerate(tqdm(fin)):
data = json.loads(line)
doc = data['paper']
metas = data[metadata]
if not isinstance(metas, list):
metas = [metas]
for meta in metas:
meta2doc[meta].add(doc)
doc2meta[doc] = set(metas)
with open(f'{dataset}_input/dataset.txt', 'w') as fout:
for idx, doc in enumerate(tqdm(doc2meta)):
# sample positive
dps = [x for x in doc2meta[doc] if x in doc2text]
if len(dps) == 0:
continue
dp = random.choice(dps)
# sample negative
while True:
dn = random.choice(docs)
if dn != doc and dn != dp:
break
fout.write(f'1\t{doc2text[doc]}\t{doc2text[dp]}\n')
fout.write(f'0\t{doc2text[doc]}\t{doc2text[dn]}\n')
# PAP, PVP, P->P<-P, and P<-P->P
def one_intermediate_node(dataset, doc2text, docs, metadata):
meta2doc = defaultdict(set)
doc2meta = {}
with open(f'{dataset}/{dataset}_train.json') as fin:
for idx, line in enumerate(tqdm(fin)):
data = json.loads(line)
doc = data['paper']
metas = data[metadata]
if not isinstance(metas, list):
metas = [metas]
for meta in metas:
meta2doc[meta].add(doc)
doc2meta[doc] = set(metas)
with open(f'{dataset}_input/dataset.txt', 'w') as fout:
for idx, doc in enumerate(tqdm(doc2meta)):
# sample positive
metas = doc2meta[doc]
dps = []
for meta in metas:
candidates = list(meta2doc[meta])
if len(candidates) > 1:
while True:
dp = random.choice(candidates)
if dp != doc:
dps.append(dp)
break
if len(dps) == 0:
continue
dp = random.choice(dps)
# sample negative
while True:
dn = random.choice(docs)
if dn != doc and dn != dp:
break
fout.write(f'1\t{doc2text[doc]}\t{doc2text[dp]}\n')
fout.write(f'0\t{doc2text[doc]}\t{doc2text[dn]}\n')
# P(AA)P, P(AV)P, P->(PP)<-P, and P<-(PP)->P
def two_intermediate_node(dataset, doc2text, docs, metadata1, metadata2):
meta12doc = defaultdict(set)
doc2meta1 = {}
doc2meta2 = {}
with open(f'{dataset}/{dataset}_train.json') as fin:
for idx, line in enumerate(tqdm(fin)):
data = json.loads(line)
doc = data['paper']
meta1s = data[metadata1]
if not isinstance(meta1s, list):
meta1s = [meta1s]
for meta1 in meta1s:
meta12doc[meta1].add(doc)
doc2meta1[doc] = set(meta1s)
meta2s = data[metadata2]
if not isinstance(meta2s, list):
meta2s = [meta2s]
doc2meta2[doc] = set(meta2s)
with open(f'{dataset}_input/dataset.txt', 'w') as fout:
for idx, doc in enumerate(tqdm(doc2meta1)):
# sample positive
meta1s = doc2meta1[doc]
dps = []
for meta1 in meta1s:
candidates = []
for d_cand in list(meta12doc[meta1]):
if d_cand == doc:
continue
meta_intersec = doc2meta2[doc].intersection(doc2meta2[d_cand])
if metadata1 != metadata2:
if len(meta_intersec) >= 1:
candidates.append(d_cand)
else:
if len(meta_intersec) >= 2:
candidates.append(d_cand)
if len(candidates) > 1:
while True:
dp = random.choice(candidates)
if dp != doc:
dps.append(dp)
break
if len(dps) == 0:
continue
dp = random.choice(dps)
# sample negative
while True:
dn = random.choice(docs)
if dn != doc and dn != dp:
break
fout.write(f'1\t{doc2text[doc]}\t{doc2text[dp]}\n')
fout.write(f'0\t{doc2text[doc]}\t{doc2text[dn]}\n')
parser = argparse.ArgumentParser(description='main', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--dataset', default='MAG', type=str)
parser.add_argument('--metagraph', default='PRP', type=str)
args = parser.parse_args()
dataset = args.dataset
metagraph = args.metagraph
doc2text = {}
docs = []
with open(f'{dataset}/{dataset}_train.json') as fin:
for idx, line in enumerate(tqdm(fin)):
data = json.loads(line)
doc = data['paper']
text = data['text'].replace('_', ' ')
doc2text[doc] = text
docs.append(doc)
# P->P
if metagraph == 'PR':
no_intermediate_node(dataset, doc2text, docs, 'reference')
# P<-P
elif metagraph == 'PC':
no_intermediate_node(dataset, doc2text, docs, 'citation')
# PAP
elif metagraph == 'PAP':
one_intermediate_node(dataset, doc2text, docs, 'author')
# PVP
elif metagraph == 'PVP':
one_intermediate_node(dataset, doc2text, docs, 'venue')
# P->P<-P
elif metagraph == 'PRP':
one_intermediate_node(dataset, doc2text, docs, 'reference')
# P<-P->P
elif metagraph == 'PCP':
one_intermediate_node(dataset, doc2text, docs, 'citation')
# P(AA)P
elif metagraph == 'PAAP':
two_intermediate_node(dataset, doc2text, docs, 'author', 'author')
# P(AV)P
elif metagraph == 'PAVP':
two_intermediate_node(dataset, doc2text, docs, 'author', 'venue')
# P->(PP)<-P
elif metagraph == 'PRRP':
two_intermediate_node(dataset, doc2text, docs, 'reference', 'reference')
# P<-(PP)->P
elif metagraph == 'PCCP':
two_intermediate_node(dataset, doc2text, docs, 'citation', 'citation')
else:
print('Wrong Meta-path/Meta-graph!!')