-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate_rules.py
291 lines (253 loc) · 8.85 KB
/
generate_rules.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
import numpy as np
import os
import argparse
#parse the argument
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, required=True,
help='Name of dataset')
parser.add_argument('--confidence', type=float, default=0.7,
help='confidence threshold')
args = parser.parse_args()
CONFIDENCE = args.confidence
test_graph = []
test_fact = []
G1 = set()
G2 = set()
dataset = 'data/{}/test/'.format(args.dataset)
save_path = 'data/rule/{}'.format(args.dataset)
if not os.path.exists(save_path):
os.makedirs(save_path)
relations_TG1 = set()
relations_TG2 = set()
relations_G1 = set()
relations_G2 = set()
for line in open('{}/test-graph.txt'.format(dataset)):
t = line.strip().split('\t')
if t[1] != '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>':
test_graph.append(t)
if (t[0], t[1], t[2]) not in G1:
G1.add((t[0], t[1], t[2]))
if (t[0], t[1], t[2]) not in G2:
G2.add((t[0], t[1], t[2]))
if t[1] != '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>':
relations_TG1.add(t[1])
relations_G1.add(t[1])
relations_G2.add(t[1])
for line in open('{}/test-fact.txt'.format(dataset)):
t = line.strip().split('\t')
if t[1] != '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>':
test_fact.append(t)
if (t[0], t[1], t[2]) not in G2:
G2.add((t[0], t[1], t[2]))
if t[1] != '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>':
relations_TG2.add(t[1])
relations_G2.add(t[1])
relations_TG1 = list(relations_TG1)
relations_TG2 = list(relations_TG2)
relations_G1 = list(relations_G1)
relations_G2 = list(relations_G2)
pairs_for_relation_G1 = dict([(i,set()) for i in relations_G1])
pairs_for_relation_G2 = dict([(i,set()) for i in relations_G2])
for t in G1:
pairs_for_relation_G1[t[1]].add((t[0],t[2]))
for t in G2:
pairs_for_relation_G2[t[1]].add((t[0],t[2]))
# R(x,y) --> S(x,y)
import numpy as np
f = open('{}/pattern1.txt'.format(save_path),"w+")
for i in range(len(relations_TG2)):
s = relations_TG2[i]
for j in range(len(relations_TG1)):
r = relations_TG1[j]
if s != r:
#computing N
N = len(pairs_for_relation_G1[r])
M = 0
for pair in pairs_for_relation_G1[r]:
if pair in pairs_for_relation_G2[s]:
M += 1
confidence = M / N
if confidence >= CONFIDENCE:
f.write('{}\t{}\t{}\n'.format(str(M/N), r, s))
f.close()
# R(x,y) --> R(y,x)
f = open('{}/pattern3.txt'.format(save_path),"w+")
for i in range(len(relations_TG2)):
s = relations_TG2[i]
for j in range(len(relations_TG1)):
r = relations_TG1[j]
if s == r:
N = len(pairs_for_relation_G1[r])
M = 0
for pair in pairs_for_relation_G1[r]:
if (pair[1], pair[0]) in pairs_for_relation_G2[s]:
M += 1
confidence = M / N
if confidence >= CONFIDENCE:
f.write('{}\t{}\n'.format(str(confidence), r))
f.close()
# R(x,y), S(y,z) -> T(x,z)
rs_list = []
for r in relations_TG1:
for s in relations_TG1:
rs_list.append((r,s))
def compute_N_for_rs(r, s, G):
curr_N = 0
curr_XZ = set()
for p1 in pairs_for_relation_G1[r]:
for p2 in pairs_for_relation_G1[s]:
if p1[1] == p2[0]:
if (p1[0], p2[1]) not in curr_XZ:
curr_N += 1
curr_XZ.add((p1[0], p2[1]))
return curr_N, curr_XZ
import time
time1 = time.time()
N_for_rs = dict([(i,0) for i in rs_list])
XZ_for_rs = dict([(i,set()) for i in rs_list])
i = 0
for r in relations_TG1:
i += 1
for s in relations_TG1:
N, XZ = compute_N_for_rs(r, s, G1)
N_for_rs[(r,s)] = N
XZ_for_rs[(r,s)] = XZ
import numpy as np
f = open('{}/pattern4.txt'.format(save_path),"w+")
for i in range(len(relations_TG2)):
t = relations_TG2[i]
for j in range(len(relations_TG1)):
r = relations_TG1[j]
for k in range(len(relations_TG1)):
s = relations_TG1[k]
#computing N
N = N_for_rs[(r,s)]
XZ = XZ_for_rs[(r,s)]
M = 0
for pair in pairs_for_relation_G2[t]:
if pair in XZ:
M += 1
if N == 0:
continue
confidence = M / N
if confidence >= CONFIDENCE:
f.write('{}\t{}\t{}\t{}\n'.format(str(M/N), r,s,t))
f.close()
rs_list = []
for r in relations_TG1:
for s in relations_TG1:
rs_list.append((r,s))
def compute_N_for_rs_same(r, s, G):
curr_N = 0
curr_XY = set()
for p1 in pairs_for_relation_G1[r]:
for p2 in pairs_for_relation_G1[s]:
if p1[0] == p2[0]:
if p1[1] == p2[1]:
if (p1[0], p1[1]) not in curr_XY:
curr_N += 1
curr_XY.add((p1[0], p1[1]))
return curr_N, curr_XY
import time
time1 = time.time()
N_for_rs_same = dict([(i,0) for i in rs_list])
XY_for_rs_same = dict([(i,set()) for i in rs_list])
i = 0
for r in relations_TG1:
i += 1
for s in relations_TG1:
N, XY = compute_N_for_rs_same(r, s, G1)
N_for_rs_same[(r,s)] = N
XY_for_rs_same[(r,s)] = XY
#R(x,y) , S(x,y) -> T(x,y)
f = open('{}/pattern5.txt'.format(save_path),"w+")
for i in range(len(relations_TG2)):
t = relations_TG2[i]
for j in range(len(relations_TG1)):
r = relations_TG1[j]
for k in range(len(relations_TG1)):
s = relations_TG1[k]
if s !=r and s != t and r != t:
N = N_for_rs_same[(r,s)]
XY = XY_for_rs_same[(r,s)]
M = 0
for pair in pairs_for_relation_G2[t]:
if pair in XY:
M += 1
if N == 0:
continue
confidence = M / N
if confidence >= CONFIDENCE:
f.write('{}\t{}\t{}\t{}\n'.format(str(M/N), r,s,t))
f.close()
types_G1 = set()
types_G2 = set()
types_TG1 = set()
types_TG2 = set()
G1_type_triples = set()
G2_type_triples = set()
for line in open('{}/test-graph.txt'.format(dataset)):
t = line.strip().split('\t')
if t[1] == '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>':
types_G1.add(t[2])
types_G2.add(t[2])
types_TG1.add(t[2])
G1_type_triples.add((t[0], t[1], t[2]))
G2_type_triples.add((t[0], t[1], t[2]))
for line in open('{}/test-fact.txt'.format(dataset)):
t = line.strip().split('\t')
if t[1] == '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>':
types_G2.add(t[2])
types_TG2.add(t[2])
G2_type_triples.add((t[0], t[1], t[2]))
# A(x) -> B(x)
#Type(a, A) -> Type(a, B)
f = open('{}/pattern2.txt'.format(save_path), "w+")
entities_for_type_G1 = dict([(i,set()) for i in types_G1])
for t in G1_type_triples:
if t[1] == '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>':
entities_for_type_G1[t[2]].add(t[0])
entities_for_type_G2 = dict([(i,set()) for i in types_G2])
for t in G2_type_triples:
if t[1] == '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>':
entities_for_type_G2[t[2]].add(t[0])
for b in types_TG2:
for a in types_TG1:
if a != b:
M = 0
N = len(entities_for_type_G1[a])
for entity in entities_for_type_G2[b]:
if entity in entities_for_type_G1[a]:
M += 1
if N == 0:
continue
confidence = M / N
if confidence >= CONFIDENCE:
f.write('{}\t{}\t{}\n'.format(str(M/N), a,b))
f.close()
# A1(x), A2(x) -> B(x)
f = open('{}/pattern6.txt'.format(save_path),"w+")
entities_for_type_G1 = dict([(i,set()) for i in types_G1])
for t in G1_type_triples:
if t[1] == '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>':
entities_for_type_G1[t[2]].add(t[0])
entities_for_type_G2 = dict([(i,set()) for i in types_G2])
for t in G2_type_triples:
if t[1] == '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>':
entities_for_type_G2[t[2]].add(t[0])
for b in types_TG2:
for a1 in types_TG1:
for a2 in types_TG1:
if a1 != a2 and a1 != b and a2!=b:
M = 0
entities_a1_a2 = entities_for_type_G1[a1] & entities_for_type_G1[a2]
N = len(entities_a1_a2)
for entity in entities_for_type_G2[b]:
if entity in entities_a1_a2:
M += 1
if N == 0:
continue
confidence = M / N
if confidence >= CONFIDENCE:
f.write('{}\t{}\t{}\t{}\n'.format(str(M/N), a1, a2, b))
f.close()