import os
import re
import json
import numpy as np
import pandas as pd
from scipy import stats

if __name__ == "__main__":
    
    # 4.1
    # results = {"Synthesis": [], "BioEng": [], "Ecology": [], "Genetics": [], "Medical": []}
    # folder_path = "data/files_for_expert/original_protocol/"
    # for subject in results:
    #     file_path = f"data/Dataset/{subject}/testset_DSL.json"
    #     with open(file_path, 'r', encoding='utf-8') as f:
    #         data = json.load(f)
        
    #     for a in data:            
    #         results[subject].append(len(a))
    # for dataset in results:
    #     print(dataset)
    #     print(sum(results[dataset]), len(results[dataset]))

    #4.3
    df = pd.read_csv('evaluation_result/stage_1.csv')
    methods = df["method"].unique()
    print(methods)
    for i in range(len(methods)):
        for j in range(i + 1, len(methods)):
            method1 = methods[i]
            method2 = methods[j]

            ndf = df[df["method"] == method1]
            ndf = ndf[ndf["evaluation"] == "BLEU-1"]
            list1 = list(ndf["value"])

            ndf = df[df["method"] == method2]
            ndf = ndf[ndf["evaluation"] == "BLEU-1"]
            list2 = list(ndf["value"])

            t_statistic, p_value = stats.ttest_ind(list1, list2)

            print(method1, method2)
            print(f"t({len(list1) + len(list2) - 2}) = {t_statistic:.2f}, p = {p_value}")
    
    df = pd.read_csv('evaluation_result/stage_2.csv')
    methods = df["method"].unique()
    print(methods)
    for i in range(len(methods)):
        for j in range(i + 1, len(methods)):
            method1 = methods[i]
            method2 = methods[j]

            ndf = df[df["method"] == method1]
            ndf = ndf[ndf["evaluation"] == "BLEU-1"]
            list1 = list(ndf["value"])

            ndf = df[df["method"] == method2]
            ndf = ndf[ndf["evaluation"] == "BLEU-1"]
            list2 = list(ndf["value"])

            t_statistic, p_value = stats.ttest_ind(list1, list2)

            print(method1, method2)
            print(f"t({len(list1) + len(list2) - 2}) = {t_statistic:.2f}, p = {p_value}")
    
    df = pd.read_csv('evaluation_result/stage_3.csv')
    methods = df["method"].unique()
    print(methods)
    for i in range(len(methods)):
        for j in range(i + 1, len(methods)):
            method1 = methods[i]
            method2 = methods[j]

            ndf = df[df["method"] == method1]
            ndf = ndf[ndf["evaluation"] == "BLEU-1"]
            list1 = list(ndf["value"])

            ndf = df[df["method"] == method2]
            ndf = ndf[ndf["evaluation"] == "BLEU-1"]
            list2 = list(ndf["value"])

            t_statistic, p_value = stats.ttest_ind(list1, list2)

            print(method1, method2)
            print(f"t({len(list1) + len(list2) - 2}) = {t_statistic:.2f}, p = {p_value}")
            