import json
import os
import pandas as pd

ans_path = "./data/glue/RTE/dev.tsv"
with open(ans_path) as f:
	data = [x.split("\t") for x in f.readlines()][1:]
df = pd.DataFrame(data,columns = ['idx','sentence1','sentence2','label'])#['bool','id1','id2','sentence1','sentence2'])
print([len(d) for d in df])
#df['label'] = #["equivalent" if a == 1 else "not_equivalent" for a in df["bool"]]
ans = [x.strip() for x in list(df["label"])]
s1 = list(df["sentence1"])
s2 = list(df["sentence2"])
#GOOD
good = {k:0 for k in range(len(ans))}
for seed in range(1,6):
	pred_path = f"./results/finetune/distil_main/bertxclip/Epoch01_XATTNBERT/RTE/lr-1e-4seed-{seed}/predict_results_rte.txt"
	with open(pred_path) as f:
		dfgood = pd.read_csv(pred_path,sep = "\t")
	print(len(df))
	predg = dfgood["prediction"]
	
	for i in range(len(ans)):
		print(ans[i],predg[i],ans[i] == predg[i])
		if ans[i] != predg[i]:
			pass#print(i)
		else:
			good[i] += 1
#print(good)
#s()
#BAD
bad = {k:0 for k in range(len(ans))}
for i in range(1,6):
	pred_path2 = f"./results/finetune/bert-base-uncased/RTE/lr-1e-4-seed-{seed}/predict_results_rte.txt"



	with open(pred_path2) as f:
		dfbad = pd.read_csv(pred_path2,sep = "\t")

	predb = dfbad["prediction"]
	for i in range(len(ans)):
		if ans[i] != predb[i]:
			pass#print(i)
		else:
			bad[i] += 1
diff = [(n,good[i]-bad[i]) for n,i in enumerate(range(len(ans)))]
diff = sorted(diff,key = lambda x : x[1],reverse = True)
print(diff)
advantage = 0
compare = {}
compare["len"] = {"pos":[],"neg":[]}
import benepar
import nltk
from nltk import word_tokenize
benepar.download("benepar_en3")
parser = benepar.Parser("benepar_en3")
import json
import coco-text
ct = coco_text.COCO_Text("/mnt/HDD/research/taiwanai/xattnbertfast/data/cocotext.v2.json")
#with open("/mnt/HDD/research/taiwanai/xattnbertfast/data/cocotext.v2.json","r") as f:
	data = json.load(f)
print(data)
s()
for i in range(len(diff)):
	advantage += diff[i][1]
	print("**",diff[i][1])
	print(s1[diff[i][0]])
	input_sentence = benepar.InputSentence(
    words=word_tokenize(s1[diff[i][0]]),
    #space_after=[True],
    #tags=['``', 'VB', 'RB', '.', "''"],
    #escaped_words=['``', 'Fly', 'safely', '.', "''"],
	)
	tree = parser.parse(input_sentence)
	print(tree)
	s()
	if diff[i][1] > 0:
		compare["len"]["pos"].append(len(s1[diff[i][0]].split()))
	print(s2[diff[i][0]])
	if diff[i][1] <= 0:
		compare["len"]["neg"].append(len(s1[diff[i][0]].split()))
	print(ans[diff[i][0]])
print(advantage)

print(sum(compare["len"]["pos"])/len(compare["len"]["pos"]))
print(sum(compare["len"]["neg"])/len(compare["len"]["neg"]))

# print(diff)
# for i in range(-1,-6,-1):
# 	print("**",diff[i][1])
# 	print(s1[diff[i][0]])
# 	print(s2[diff[i][0]])
# 	print(ans[diff[i][0]])
# g2b = 0
# b2g = 0
# for i in range(len(ans)):
# 	if ans[i] != predb[i] and ans[i] == predg[i]:
# 		b2g += 1
# 	if ans[i] != predg[i] and ans[i] == predb[i]:
# 		g2b += 1
# 	else:
# 		pass
# print("b2g : ",b2g/len(ans))
# print("g2b : ",g2b/len(ans))
