<jupyter_start><jupyter_text>Length 1 Rules<jupyter_code>dict_1=utils.get_r_e1e2_dict(mapped_train_data)<jupyter_output><empty_output><jupyter_text>Length 2 Rules<jupyter_code>index_head=utils.get_head_index(mapped_train_data)
dict_2=utils.get_r1r2_e1e2_dict(mapped_train_data,index_head)<jupyter_output><empty_output><jupyter_text>Length 3 Rules<jupyter_code>## Gives entity in path for given relation and body
re2_e1=utils.get_re2_e1_dict(mapped_train_data)
e1r_e2=utils.get_e1r_e2_dict(mapped_train_data)

set_mapped_train_data=utils.get_set_mapped_train_data(mapped_train_data)
def solve_3(fact,body,e1r_e2,re2_e1,set_mapped_train_data):
    r1=body[0]
    r2=body[1]
    r3=body[2]
    e1=fact[0]
    e4=fact[2]
    
    key1=(e1,r1)
    key2=(r3,e4)
    
    if key1 not in e1r_e2:
        return ("",-1)
    if key2 not in re2_e1:
        return ("",-1)
    list1=e1r_e2[key1]
    list2=re2_e1[key2]
    
    for e2 in list1:
        for e3 in list2:
            if (e2,r2,e3) in set_mapped_train_data:
                return (body,(e2,e3))
    
    return ("",-1)
    
def get_explanation(fact,relation_to_body,dict_1,dict_2,e1r_e2,re2_e1,set_mapped_train_data):
    pair=(fact[0],fact[2])
    rel=fact[1]
    
    r1=0
    r2=0
    r3=0
#     print(r1,r2,r3)
    if rel not in relation_to_body:
        return ("",-1)
#     print("Exists")
    bodies=relation_to_body[rel]
    for body in bodies:
        if isinstance(body,int):
            r1+=1
            if pair not in dict_1[body]:
                continue
            return (body,-1)
        else:
            if len(body)==2:
                r2+=1
                if pair not in dict_2[body]:
                    continue
                return (body,dict_2[body][pair])
            else:
                if len(body)==3:
                    r3+=1
                    temp=solve_3(fact,body,e1r_e2,re2_e1,set_mapped_train_data)
                    if(temp[0]!=""):
                        return temp
    
    return ("",-1)
# get_explanation((453,37,82),relation_to_body,dict_1,dict_2)
#triplet_file="/home/cse/btech/cs1150210/scratch/BTP/Interpretable-KBC/logs/fb15k/turk_test_hits10_not_hits1/small_id.txt"
# triplet_file="/home/yatin/hpcscratch/Aman_BTP/Interpretable-KBC-tlp/data/fb15k/test/test_hits_1_ordered_x.txt"
triplet_file="/home/yatin/hpcscratch_copy/texkbc/Interpretable-KBC-tlp/data/fb15k/test/test_hits1_x.txt"
data=utils.read_data(triplet_file)
np_arr=np.array(utils.map_data(data,dump=dump)).astype(np.int32)
# np_arr=np.loadtxt(triplet_file)
arr=[]
coun=0
for line in np_arr:
    fact=(int(line[0]),int(line[1]),int(line[2]))
    arr.append(get_explanation(fact,relation_to_body,dict_1,dict_2,e1r_e2,re2_e1,set_mapped_train_data))
    coun+=1

# print(arr)
count=0
for x in arr:
    if x[0]!="":
        count+=1
print(count)
print(arr[0:100])
# path=os.path.join(mining_dir,"explanation_test_hits10_not_hits1.pkl")
path=os.path.join(mining_dir,"explanation_test.pkl")
utils.dump_pickle(arr,path)
print(len(np_arr),len(arr))
lol=utils.load_pickle(path)
print(len(lol))
print(lol[0:100])
# -----------------------------------------------------------
# Mayank
# To do analysis of rules1 vs rules2 vs rules3
relation_to_body_1={}
relation_to_body_1=add_relation_body(rules_1,relation_to_body_1)

relation_to_body_2={}
relation_to_body_2=add_relation_body(rules_2,relation_to_body_2)

relation_to_body_3={}
relation_to_body_3=add_relation_body(rules_3,relation_to_body_3)
arr_1=[]
coun_1=0
arr_2=[]
coun_2=0
arr_3=[]
coun_3=0
for line in np_arr:
    fact=(int(line[0]),int(line[1]),int(line[2]))
    arr_1.append(get_explanation(fact,relation_to_body_1,dict_1,dict_2,e1r_e2,re2_e1,set_mapped_train_data))
    coun_1+=1
    arr_2.append(get_explanation(fact,relation_to_body_2,dict_1,dict_2,e1r_e2,re2_e1,set_mapped_train_data))
    coun_2+=1
    arr_3.append(get_explanation(fact,relation_to_body_3,dict_1,dict_2,e1r_e2,re2_e1,set_mapped_train_data))
    coun_3+=1
path=os.path.join(mining_dir,"explanation_test_1.pkl")
utils.dump_pickle(arr_1,path)
path=os.path.join(mining_dir,"explanation_test_2.pkl")
utils.dump_pickle(arr_2,path)
path=os.path.join(mining_dir,"explanation_test_3.pkl")
utils.dump_pickle(arr_3,path)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup