import sys
from collections import *

files=[("base","results/marvin_results_base.txt"),("large","results/marvin_results_large.txt")]

if "with_only_prefix" in sys.argv:
    files += [("base_only_prefix","results/marvin_results_base_only_prefix.txt"),("large_only_prefix","results/marvin_results_large_only_prefix.txt")]

if "no_split" in sys.argv:
    files.append(("openai_gpt", "results/marvin_results_openai_gpt_no_split.txt"))
elif "use_postfix" in sys.argv:
    files.append(("openai_gpt", "results/marvin_results_openai_gpt_use_postfix.txt"))
else:
    files.append(("openai_gpt", "results/marvin_results_openai_gpt.txt"))


by_model={}
conditions=set()
for title,fname in files:
    lines = open(fname)
    results=defaultdict(Counter)
    by_model[title]=results
    skipped = set()
    for line in lines:
        if line.startswith("Better speed"): continue
        if line.startswith("skipping"):
            skipped.add(line.split()[1])
            next(lines)
            continue
        res,c1,c2,w1,w2,s = line.split(None, 5)
        c1 = c1.replace("inanim","anim")
        conditions.add(c1)
        results[c1][res]+=1

print("skipped:",skipped)

if "with_only_prefix" in sys.argv:
    print("condition & base & large & base_only_prefix & large_only_prefix & openai_gpt & count bert & count openai_gpt \\\\")
else:
    print("condition & base & large & openai_gpt & count bert & count openai_gpt \\\\")

for cond in conditions:
    rb = by_model['base'][cond]
    rl = by_model['large'][cond]
    ro = by_model['openai_gpt'][cond]
    if sum(ro.values())==0:
        so = "-"
    else:
        so = "%.2f" % (ro['True']/(ro['True']+ro['False']))
    sb = "%.2f" % (rb['True']/(rb['True']+rb['False']))
    sl = "%.2f" % (rl['True']/(rl['True']+rl['False']))
    if "with_only_prefix" in sys.argv:
        rbp = by_model['base_only_prefix'][cond]
        rlp = by_model['large_only_prefix'][cond]
        sbp = "%.2f" % (rbp['True']/(rbp['True']+rbp['False']))
        slp = "%.2f" % (rlp['True']/(rlp['True']+rlp['False']))
        print(" & ".join(map(str,[cond, sb, sl, sbp, slp, so, sum(rb.values()), sum(ro.values())])),"\\\\")
    else:
        print(" & ".join(map(str,[cond, sb, sl, so, sum(rb.values()), sum(ro.values())])),"\\\\")
