# Re-score the two prediction files with a lenient match, as in the article:
# double and single quotes alike, numbers with or without quotes, ASC dropped,
# spaces and letter case ignored. What is left are differences of logic.
# usage: python score.py preds_base.json preds_tuned.json
import json, re, sys

def lenient(s):
    s = s.strip().strip("`").strip().rstrip(";").strip().replace('"', "'")
    s = re.sub(r"'(-?\d+(?:\.\d+)?)'", r"\1", s)
    s = re.sub(r"\s+asc\b", "", s, flags=re.I)
    s = re.sub(r"\s+", " ", s).lower()
    return re.sub(r"\s*([(),=<>])\s*", r"\1", s)

for path in sys.argv[1:]:
    preds = json.load(open(path))
    ok = sum(lenient(p["pred"]) == lenient(p["ref"]) for p in preds)
    print(f"{path}: {ok}/{len(preds)} lenient match")
