-
Notifications
You must be signed in to change notification settings - Fork 7
/
PCRA.py
208 lines (184 loc) · 5.67 KB
/
PCRA.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
import os,sys
import math
import random
import time
def map_add(mp, key1,key2, value):
if (key1 not in mp):
mp[key1] = {}
if (key2 not in mp[key1]):
mp[key1][key2] = 0.0
mp[key1][key2] += value
def map_add1(mp,key):
if (key not in mp):
mp[key] = 0
mp[key]+=1
f = open("data/relation2id.txt","r")
relation2id = {}
id2relation = {}
relation_num = 0
for line in f:
seg = line.strip().split()
relation2id[seg[0]] = int(seg[1])
id2relation[int(seg[1])]=seg[0]
relation_num+=1
for line in f:
seg = line.strip().split()
id2relation[int(seg[1])+relation_num]="~"+seg[0]
f.close()
ok = {}
a ={}
num=0
step=0
f = open("data/train.txt","r")
for line in f:
seg = line.strip().split()
e1 = seg[0]
e2 = seg[1]
rel = seg[2]
if (e1+" "+e2 not in ok):
ok[e1+" "+e2]={}
ok[e1+" "+e2][relation2id[rel]]=1
if (e2+" "+e1 not in ok):
ok[e2+" "+e1]={}
ok[e2+" "+e1][relation2id[rel]+relation_num]=1
if (e1 not in a):
a[e1]={}
if (relation2id[rel] not in a[e1]):
a[e1][relation2id[rel]]={}
a[e1][relation2id[rel]][e2]=1
if (e2 not in a):
a[e2]={}
if ((relation2id[rel]+relation_num) not in a[e2]):
a[e2][relation2id[rel]+relation_num]={}
a[e2][relation2id[rel]+relation_num][e1]=1
f.close()
f = open("data/test.txt","r")
for line in f:
seg = line.strip().split()
if (seg[0]+" "+seg[1] not in ok):
ok[seg[0]+' '+seg[1]]={}
if (seg[1]+" "+seg[0] not in ok):
ok[seg[1]+' '+seg[0]]={}
f.close()
h_e_p = {}
f = open("data/e1_e2_1.txt","r")
print("open e1_e2_1.txt !\n")
for line in f:
seg = line.strip().split()
ok[seg[0]+" "+seg[1]] = {}
ok[seg[1]+" "+seg[0]] = {}
#f.close()
g = open("data/path2.txt","w")
path_dict = {}
path_r_dict = {}
train_path = {}
step = 0
time1= time.time()
path_num = 0
h_e_p={}
for e1 in a:
step+=1
print(step,end = " ")
for rel1 in a[e1]:
e2_set = a[e1][rel1]
for e2 in e2_set:
map_add1(path_dict,str(rel1))
for key in ok[e1+' '+e2]:
map_add1(path_r_dict,str(rel1)+"->"+str(key))
map_add(h_e_p,e1+' '+e2,str(rel1),1.0/len(e2_set))
for rel1 in a[e1]:
e2_set = a[e1][rel1]
for e2 in e2_set:
if (e2 in a):
for rel2 in a[e2]:
e3_set = a[e2][rel2]
for e3 in e3_set:
map_add1(path_dict,str(rel1)+" "+str(rel2))
if (e1+" "+e3 in ok):
for key in ok[e1+' '+e3]:
map_add1(path_r_dict,str(rel1)+" "+str(rel2)+"->"+str(key))
if (e1+" "+e3 in ok):# and h_e_p[e1+' '+e2][str(rel1)]*1.0/len(e3_set)>0.01):
map_add(h_e_p,e1+' '+e3,str(rel1)+' '+str(rel2),h_e_p[e1+' '+e2][str(rel1)]*1.0/len(e3_set))
for e2 in a:
e_1 = e1
e_2 = e2
if (e_1+" "+e_2 in h_e_p):
path_num+=len(h_e_p[e_1+" "+e_2])
bb = {}
aa = {}
g.write(str(e_1)+" "+str(e_2)+"\n")
sum = 0.0
for rel_path in h_e_p[e_1+' '+e_2]:
bb[rel_path] = h_e_p[e_1+' '+e_2][rel_path]
sum += bb[rel_path]
for rel_path in bb:
bb[rel_path]/=sum
if bb[rel_path]>0.01:
aa[rel_path] = bb[rel_path]
g.write(str(len(aa)))
for rel_path in aa:
train_path[rel_path] = 1
g.write(" "+str(len(rel_path.split()))+" "+rel_path+" "+str(aa[rel_path]))
g.write("\n")
print(path_num, time.time()-time1)
sys.stdout.flush()
g.close()
g = open("data/confidence.txt","w")
for rel_path in train_path:
out = []
for i in range(0,relation_num):
if (rel_path in path_dict and rel_path+"->"+str(i) in path_r_dict):
out.append(" "+str(i)+" "+str(path_r_dict[rel_path+"->"+str(i)]*1.0/path_dict[rel_path]))
if (len(out)>0):
g.write(str(len(rel_path.split()))+" "+rel_path+"\n")
g.write(str(len(out)))
for i in range(0,len(out)):
g.write(out[i])
g.write("\n")
g.close()
def work(dir):
f = open("data/"+dir+".txt","r")
g = open("data/"+dir+"_pra.txt","w")
for line in f:
seg = line.strip().split()
e1 = seg[0]
e2 = seg[1]
rel = relation2id[seg[2]]
g.write(str(e1)+" "+str(e2)+' '+str(rel)+"\n")
b = {}
a = {}
if (e1+' '+e2 in h_e_p):
sum = 0.0
for rel_path in h_e_p[e1+' '+e2]:
b[rel_path] = h_e_p[e1+' '+e2][rel_path]
sum += b[rel_path]
for rel_path in b:
b[rel_path]/=sum
if b[rel_path]>0.01:
a[rel_path] = b[rel_path]
g.write(str(len(a)))
for rel_path in a:
g.write(" "+str(len(rel_path.split()))+" "+rel_path+" "+str(a[rel_path]))
g.write("\n")
g.write(str(e2)+" "+str(e1)+' '+str(rel+relation_num)+"\n")
e1 = seg[1]
e2 = seg[0]
b = {}
a = {}
if (e1+' '+e2 in h_e_p):
sum = 0.0
for rel_path in h_e_p[e1+' '+e2]:
b[rel_path] = h_e_p[e1+' '+e2][rel_path]
sum += b[rel_path]
for rel_path in b:
b[rel_path]/=sum
if b[rel_path]>0.01:
a[rel_path] = b[rel_path]
g.write(str(len(a)))
for rel_path in a:
g.write(" "+str(len(rel_path.split()))+" "+rel_path+" "+str(a[rel_path]))
g.write("\n")
f.close()
g.close()
work("train")
work("test")