import os

test_number="test1"
dir_in = ".\results\result_T\TERB_Train"

list_path_in = []

def get_all_path(dir_in, list_path):
    for root, dirs, files in os.walk(dir_in):
        for filename in files:
            path_file = root + "\\" + filename
            list_path.append(path_file)

get_all_path(dir_in, list_path_in)
#print(list_path_in)

def get_right_answer(list_path_in):
    '''
    Generate the truth answer of each test file

    :param list_path_in: the list of the path of the answer generated by Bi-LSTM
    :return: dict_testNum_ansList: {"number(file number 1-5): the list of is right answer"}
    '''
    dict_testNum_ansList = {}
    for path_in in list_path_in:
        list_right_answer = []

        number = path_in.split("\\")[6][-1]
        test_number = "test"+number

        path_answer_bf = ".\data\Test_network\Test_Temporal\\"+test_number+"\dev\\Temporal.Beforeafter"
        with open(path_answer_bf, "r") as r_f:
            all = r_f.readlines()
            list_right_answer.extend(["0"]*len(all))
       # print(len(list_right_answer))

        path_answer_de = ".\data\Test_network\Test_Temporal\\" + test_number + "\dev\\Temporal.DuringEquals"
        with open(path_answer_de, "r") as r_f:
            all = r_f.readlines()
            list_right_answer.extend(["1"]*len(all))
        #print(len(list_right_answer))

        dict_testNum_ansList.update({number: list_right_answer})
    return dict_testNum_ansList

dict_testNum_ansList = get_right_answer(list_path_in)

def eval(list_path_in, dict_ans):
    for path_in in list_path_in:
        print(path_in)

        list_show_answer_b = []
        list_show_answer_e = []

        TP_b = 0
        FP_b = 0
        P_b = 0
        R_b = 0
        F_b = 0

        TP_e = 0
        FP_e = 0
        P_e = 0
        R_e = 0
        F_e = 0

        number = path_in.split("\\")[6][-1]
        answerList = dict_ans[number]
        index_bound = answerList.count("0")-1

        list_is_equal = []

        judged_ansList = []
        with open(path_in, "r") as r_f:
            judged_ansList_want = r_f.readlines()
            for i in judged_ansList_want:
                judged_ansList.append(i.strip("\n"))

        #print(judged_ansList)
        for i in range(len(answerList)):
            if judged_ansList[i] == answerList[i]:
                list_is_equal.append(1)
            else:
                list_is_equal.append(0)

        list_equal_before = list_is_equal[0: index_bound+1]
        list_equal_during = list_is_equal[index_bound+1:]

        TP_b = list_equal_before.count(1)
        # print(TP_b)
        FP_b = list_equal_during.count(0)
        # print(FP_b)

        P_b = 0
        if TP_b + FP_b > 0:
            P_b = TP_b / (TP_b + FP_b)
        R_b = TP_b / (index_bound + 1)
        F_b = 0
        if P_b>0:
            F_b = 2/(1/P_b+1/R_b)
        list_show_answer_b.append(P_b)
        list_show_answer_b.append(R_b)
        list_show_answer_b.append(F_b)

        TP_e = list_equal_during.count(1)
        # print(TP_e)
        FP_e = list_equal_before.count(0)
        # print(FP_e)

        P_e = 0
        if TP_e + FP_e > 0:
            P_e = TP_e / (TP_e + FP_e)
        R_e = TP_e / (len(answerList) - index_bound - 1)
        F_e = 0
        if P_e > 0:
            F_e = 2 / (1 / P_e + 1 / R_e)
        list_show_answer_e.append(P_e)
        list_show_answer_e.append(R_e)
        list_show_answer_e.append(F_e)

        MacroP = (P_b + P_e)/2
        MacroR = (R_b + R_e)/2
        MacroF_avg = (F_b+F_e)/2
        MacroF = 0
        if MacroP > 0:
            MacroF = 2 / (1 / MacroP + 1 / MacroR )

        print("%-20s\t%-20s\t%-20s\t%-20s\t" % ("Temporal.Beforeafter", list_show_answer_b[0], list_show_answer_b[1], list_show_answer_b[2]))
        print("%-20s\t%-20s\t%-20s\t%-20s\t" % ("Temporal.DuringEquals", list_show_answer_e[0], list_show_answer_e[1], list_show_answer_e[2]))
        print("%-20s\t%-20s\t%-20s\t%-20s\t%-20s\t" % ("Macro", MacroP, MacroR, MacroF_avg, MacroF))


eval(list_path_in, dict_testNum_ansList)
