class GetMetricEmbedding: def __init__(self): self.graph = Graph() self.graph.e2idx_file = "../../MyData/DBO/All/entity2id.txt" self.graph.r2idx_file = "../../MyData/DBO/All/relation2id.txt" self.graph.triple2idx_file = "../../MyData/DBO/All/triple2id.txt" self.graph.load_data() self.graph.load_er_embedding() def split_cons(self, cons, direction): if direction == "left": first_r_name, first_e_name = cons.split() else: first_e_name, first_r_name = cons.split() return first_e_name, first_r_name def get_metric(self, r_name1, direction1, r_name2, direction2): example_file_path = "./TwoCons_eval/{}_{}/2consQandA.txt".format(r_name1.replace("dbo:", ""), r_name2.replace("dbo:", "")) metric_file = "./TwoCons_eval/{}_{}/metric.txt".format(r_name1.replace("dbo:", ""), r_name2.replace("dbo:", "")) first_e_idx_list = [] first_r_idx_list = [] second_e_idx_list = [] second_r_idx_list = [] answer_idx_list = [] with open(example_file_path, 'r', encoding="UTF-8") as f: all_lines = f.readlines() cnt = 0 while True: if cnt >= len(all_lines) or all_lines[cnt].strip() == "": break first_e_name, first_r_name = self.split_cons(all_lines[cnt], direction1) first_e_idx_list.append(self.graph.e_name2id[first_e_name]) first_r_idx_list.append(self.graph.r_name2id[first_r_name]) cnt += 1 second_e_name, second_r_name = self.split_cons(all_lines[cnt], direction2) second_e_idx_list.append(self.graph.e_name2id[second_e_name]) second_r_idx_list.append(self.graph.r_name2id[second_r_name]) cnt += 1 answer_name = all_lines[cnt].split() answer_idx_list.append([self.graph.e_name2id[name] for name in answer_name]) cnt += 1 start_time = time.time() map_r, mrr_r, hits10_r, mean_rank = self.graph.ekg.get_map_mrr_hits10_meanRank_embed_2cons( first_e_idx_list, first_r_idx_list, direction1, second_e_idx_list, second_r_idx_list, direction2, answer_idx_list) end_time = time.time() with open(metric_file, 'w', encoding="UTF-8") as f: f.write("map:{}\n".format(map_r)) f.write("mrr:{}\n".format(mrr_r)) f.write("hits10:{}\n".format(hits10_r)) f.write("mean_rank:{}\n".format(mean_rank)) f.write("average_time:{}\n".format((end_time - start_time) / len(first_e_idx_list)))
t_idx_list.append(e_idx_list[1:]) return h_idx_list, t_idx_list search_folder = "../../MyData/EmbedDBO/" graph = Graph() graph.e2idx_file = search_folder + "entity2id.txt" graph.r2idx_file = search_folder + "relation2id.txt" graph.triple2idx_file = search_folder + "triple2id.txt" if transe_embed is True: transe_embed = False graph.load_data() graph.load_er_embedding() r_name_list = [] for sparql in eaqs: sp = SparqlParser(sparql=sparql) sp.parse_sparql() for relation in sp.r_name_list: r_name_list.append(relation) r_name_list = list(set(r_name_list)) final_map = 0.0 final_hits10 = 0.0 final_mrr = 0.0 final_time = 0