-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinfer-lr-only.py
executable file
·165 lines (135 loc) · 6.07 KB
/
infer-lr-only.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
import os
import pickle
import argparse
from objects.KG import KG
from objects.KGs import KGs
from ea.model import EntityAlignmentModel
from config import Config
argparser = argparse.ArgumentParser()
argparser.add_argument('--bottomk', action='store_true')
argparser.add_argument('--label_refine_test', action='store_true')
argparser.add_argument('--topk_match', type=int, default=10)
argparser.add_argument('--query_scheme', type=str, default="by_degree")
argparser.add_argument('--dataset', type=str, default="EN_FR_15K")
argparser.add_argument('--iter', type=int, default=3)
argparser.add_argument('--tpr', type=float, default=0.95)
argparser.add_argument('--simulate', action='store_true', help="simulate the label annotation process, used only in case studies or have no access to llm api, by default False")
argparser.add_argument('--budget', type=float, default=0.1, help="ratio of the number of inserted pairs to the number of entities in KG1")
args = argparser.parse_args()
Config.init_with_attr = False
print(f"init_with_attr: {Config.init_with_attr}")
if args.label_refine_test:
Config.print_during_exp['paris'] = True
Config.simulate = args.simulate
def construct_kg(path_r, path_a=None, sep='\t', name=None):
kg = KG(name=name)
if path_a is not None:
with open(path_r, "r", encoding="utf-8") as f:
for line in f.readlines():
if len(line.strip()) == 0:
continue
params = str.strip(line).split(sep=sep)
if len(params) != 3:
print(line)
continue
h, r, t = params[0].strip(), params[1].strip(), params[2].strip()
kg.insert_relation_tuple(h, r, t)
with open(path_a, "r", encoding="utf-8") as f:
for line in f.readlines():
if len(line.strip()) == 0:
continue
params = str.strip(line).split(sep=sep)
if len(params) != 3:
print(line)
continue
# assert len(params) == 3
e, a, v = params[0].strip(), params[1].strip(), params[2].strip()
kg.insert_attribute_tuple(e, a, v)
else:
with open(path_r, "r", encoding="utf-8") as f:
prev_line = ""
for line in f.readlines():
params = line.strip().split(sep)
if len(params) != 3 or len(prev_line) == 0:
prev_line += "\n" if len(line.strip()) == 0 else line.strip()
continue
prev_params = prev_line.strip().split(sep)
e, a, v = prev_params[0].strip(), prev_params[1].strip(), prev_params[2].strip()
prev_line = "".join(line)
if len(e) == 0 or len(a) == 0 or len(v) == 0:
print("Exception: " + e)
continue
if v.__contains__("http"):
kg.insert_relation_tuple(e, a, v)
else:
kg.insert_attribute_tuple(e, a, v)
kg.init()
kg.print_kg_info()
return kg
def get_top_match(f_path):
top_match = pickle.load(open(f_path, "rb"))
top_match = {k: {x[0] for x in v} for k, v in top_match.items()}
print(f"loaded top match from {f_path}, one example of top match k-v: {list(top_match.items())[0]}")
return top_match
def construct_kgs(dataset_dir, name="KGs", load_chk=None):
path_r_1 = os.path.join(dataset_dir, "rel_triples_1")
path_a_1 = os.path.join(dataset_dir, "attr_triples_1")
path_r_2 = os.path.join(dataset_dir, "rel_triples_2")
path_a_2 = os.path.join(dataset_dir, "attr_triples_2")
kg1 = construct_kg(path_r_1, path_a_1, name=str(name + "-KG1"))
kg2 = construct_kg(path_r_2, path_a_2, name=str(name + "-KG2"))
kgs = KGs(kg1=kg1, kg2=kg2, ground_truth_path=os.path.join(dataset_path, "ent_links"))
# load the previously saved PRASE model
if load_chk is not None:
kgs.util.load_params(load_chk)
topk_match_path = os.path.join(dataset_path, f"top{args.topk_match}_match.dict")
topk_match = get_top_match(topk_match_path)
kgs.topk_match = topk_match
return kgs
def align(kgs):
iter = 1
tpr = args.tpr
pairPerIter = int(len(kgs.kg_l.entity_set) * args.budget)
for i in range(iter):
print(f"Inserting {pairPerIter} pairs in iteration {i}...")
kgs.generate_labels(budget=pairPerIter, tpr=tpr)
# label refine
kgs.run()
if args.label_refine_test:
# if this is a label refine test, then only show one run of label refine and stop
exit(0)
# evaluate the quality of refined labels
kgs.util.test_refined_alignments(kgs.gold_result, kgs.refined_alignments)
# use refined labels to train EA model
data, bias = kgs.util.generate_input_for_emb_model()
print(f"bias: {bias}")
if i == 0:
ea_model = EntityAlignmentModel(data)
else:
train_pair= data[-2]
ea_model.update_data(train_pair)
new_pairs = ea_model.train(epoch=20)
# feed the inferred pairs into label refiner, for the next iteration
kgs.inject_ea_inferred_pairs(new_pairs, bias[0])
# continue training the EA model
for i in range(5):
kgs.run()
kgs.util.test_refined_alignments(kgs.gold_result, kgs.refined_alignments)
data, bias = kgs.util.generate_input_for_emb_model()
train_pair= data[-2]
ea_model.update_data(train_pair)
new_pairs = ea_model.train(epoch=5)
kgs.inject_ea_inferred_pairs(new_pairs, bias[0])
ea_model.fine_tune()
if __name__ == '__main__':
print(f"\nExp config:\n {Config()}\n")
base, _ = os.path.split(os.path.abspath(__file__))
dataset_name = args.dataset
dataset_path = os.path.join(os.path.join(base, "data"), dataset_name)
print("Construct KGs...")
kgs = construct_kgs(dataset_dir=dataset_path, name=dataset_name, load_chk=None)
kgs.set_worker_num(6)
kgs.set_iteration(10)
if args.label_refine_test:
kgs.set_iteration(20)
align(kgs=kgs)