-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_freq_rank_2.jl
174 lines (135 loc) · 4.38 KB
/
sim_freq_rank_2.jl
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
using DataStructures
# Predict the arg1 and arg2 using only the frequency feature in train data
# 1. Get the frequency distribution from train data
freq_dist_set_l = Dict{Int64,Int64}()
freq_dist_set_r = Dict{Int64,Int64}()
fd_l = Dict{Int64,Dict{Int64,Int64}}()
fd_l_o = Dict{Int64,OrderedDict{Int64,Int64}}()
fd_l_gp_keys = Dict{Int64,Array{Int64}}()
fd_r = Dict{Int64,Dict{Int64,Int64}}()
fd_r_o = Dict{Int64,OrderedDict{Int64,Int64}}()
fd_r_gp_keys = Dict{Int64,Array{Int64}}()
train_file = "/u/wujieche/Projects/OpenKE/data/RV15M/train2id.txt"
test_file = "/u/wujieche/Projects/OpenKE/data/RV15M/test2id.txt"
open(train_file) do f
n = parse(Int,readline(f))
for i in 1:n
i%10^6==0?println(i):0
line = readline(f)
h,t,r = [parse(Int64,str) for str in split(line,"\t")]
# Statistics left
if !haskey(freq_dist_set_l,h)
freq_dist_set_l[h] = 0
end
freq_dist_set_l[h] += 1
# Precisely corresponde to relation Left
if !haskey(fd_l,r)
fd_l[r]=Dict()
end
if !haskey(fd_l[r],h)
fd_l[r][h]=0
end
fd_l[r][h]+=1
# Statistics right
if !haskey(freq_dist_set_r,t)
freq_dist_set_r[t] = 0
end
freq_dist_set_r[t] += 1
# Precisely corresponde to relation Right
if !haskey(fd_r,r)
fd_r[r]=Dict()
end
if !haskey(fd_r[r],t)
fd_r[r][t]=0
end
fd_r[r][t]+=1
end
end
for r in keys(fd_l)
fd_l_o[r]=OrderedDict(sort(collect(fd_l[r]), by=x->x[2], rev=true))
fd_l_gp_keys[r]=collect(keys(fd_l_o[r]))
end
for r in keys(fd_r)
fd_r_o[r]=OrderedDict(sort(collect(fd_r[r]), by=x->x[2], rev=true))
fd_r_gp_keys[r]=collect(keys(fd_r_o[r]))
end
fd_set_l = OrderedDict(sort(collect(freq_dist_set_l), by=x->x[2], rev=true))
fd_set_r = OrderedDict(sort(collect(freq_dist_set_r), by=x->x[2], rev=true))
max_ent_l = length(fd_set_l)
max_ent_r = length(fd_set_r)
fd_keys_l = collect(keys(fd_set_l))
fd_keys_r = collect(keys(fd_set_r))
function get_index(od_k,v)
n = findfirst(od_k,v)
return n
end
# 2. Apply the prediction
tic()
cand_l = Dict{Int64,Array{Int64,1}}()
cand_r = Dict{Int64,Array{Int64,1}}()
open(test_file) do f
n = parse(Int,readline(f))
global result_list = Array{Int64,2}(n,5)
for i in 1:n
global fd_set_l
println(i)
line = readline(f)
h,t,r = [parse(Int64,str) for str in split(line,"\t")]
# Prediction Arg1
mr_l = 0
# If the relation is in the relation grouped table
if haskey(fd_l_o,r)
# Look for h in the relation grouped table
mr_l = get_index(fd_l_gp_keys[r],h)
# If the head is NOT in the relation grouped table
if mr_l == 0
if !haskey(cand_l,r)
cand_l[r] = setdiff(fd_keys_l, fd_l_gp_keys[r])
end
end
end
# If the head is NOT in the relation grouped table
if mr_l==0
if !haskey(cand_l,r)
cand_l[r] = fd_keys_l
end
# Look for h in the frequence table
mr_l = get_index(cand_l[r],h)
end
# If h NOT in the frequence table
if mr_l==0
mr_l = max_ent_l +1
end
# Prediction Arg2
mr_r = 0
if haskey(fd_r_o,r)
mr_r = get_index(fd_r_gp_keys[r],t)
if mr_r == 0
if !haskey(cand_r,r)
cand_r[r] = setdiff(fd_keys_r, fd_r_gp_keys[r])
end
end
end
if mr_r==0
if !haskey(cand_r,r)
cand_r[r] = fd_keys_r
end
mr_r = get_index(cand_r[r],t)
end
if mr_r==0
mr_r = max_ent_r +1
end
# Add arg1 and arg2 pred rank to list
result_list[i,:] = [h t r mr_l mr_r]
end
end
toc()
using DataFrames
df = DataFrame(result_list)
names!(df, [:arg1,:arg2,:rel,:mr_l,:mr_r])
# println("Total arg1 = $(length(fd_set_l))")
# println("Total arg2 = $(length(fd_set_r))")
println("MR arg1 = $(mean(df[:mr_l]))")
println("MR arg2 = $(mean(df[:mr_r]))")
import CSV
CSV.write(open("./df_sim_freq_gp_rel.csv","w"),df)