import os
import tempfile
import json
from PYEVALB import scorer

def load_lines(fl):
    with open(fl,'r') as f:
        lines = [line.strip() for line in f.readlines()]
    return lines


#out = '/afs/inf.ed.ac.uk/group/project/prosody/prosody_nlp/code/self_attn_speech_parser/output/turn_opensmile'
out = '/afs/inf.ed.ac.uk/group/project/prosody/prosody_nlp/code/self_attn_speech_parser/output/turn_pipeline'
data = '/afs/inf.ed.ac.uk/group/project/prosody/prosody_nlp/data/input_features/turn_opensmile/'
#data = '/afs/inf.ed.ac.uk/group/project/prosody/prosody_nlp/data/input_features/turn_pipeline'

id_file = os.path.join(data,'turn_dev.ids')
gold_file = os.path.join(data,'turn_dev.trees')
#pred_file = os.path.join(out,'turn_sp_parse_72240_dummy_penalty.5_dev=86.36.pt_dev_predicted.txt')
pred_file = os.path.join(out,'sp_seg_dev=89.84.pt.silver.parsed.turns')
subset_file = '/afs/inf.ed.ac.uk/group/project/prosody/asr_preps/ranking_split_turns.json'

subset_ids = json.load(open(subset_file,'r'))['dev']
all_ids = load_lines(id_file)
all_golds = load_lines(gold_file)
all_preds = load_lines(pred_file)

id2gold = dict(zip(all_ids,all_golds))
id2pred = dict(zip(all_ids,all_preds))

sub_golds = [id2gold[idnum] for idnum in subset_ids if idnum in id2gold]
sub_preds = [id2pred[idnum] for idnum in subset_ids if idnum in id2gold ]

temp_dir = tempfile.TemporaryDirectory(prefix="evalb-")
gold_path = os.path.join(temp_dir.name, "gold.txt")
predicted_path = os.path.join(temp_dir.name, "predicted.txt")
output_path = os.path.join(temp_dir.name, "out.txt")

with open(gold_path,'w') as f:
    for gold in sub_golds:
        f.write(gold)
        f.write('\n')

with open(predicted_path,'w') as f:
    for pred in sub_preds:
        f.write(pred)
        f.write('\n')

    
        
scr = scorer.Scorer()
scr.evalb(gold_path,predicted_path,output_path)
import pdb;pdb.set_trace()
temp_dir.cleanup()

