"""Score classify.py on the test results: every homepage should be a success, and
every random missing path should not be. The site's fingerprint comes from a second
random URL (calibrate.json), never the one being classified.
Run with: python3 evaluate.py"""
import json
from collections import Counter

from classify import classify

R = json.load(open("results.json"))
C = {c["domain"]: c["second"] for c in json.load(open("calibrate.json"))}
ok = [r for r in R if "error" not in r["home"] and r["home"]["status"] == 200 and "error" not in r["missing"]]


def fingerprint(r):
    s = C.get(r["domain"])
    return s if s and "error" not in s else None


print(len(ok), "sites")
print("Status code only, missing pages:", dict(Counter("success" if r["missing"]["status"] == 200 else "not a success" for r in ok)))
for kind, url in [("home", lambda r: r["home"]["final_url"]), ("missing", lambda r: r["missing_url"])]:
    print(f"Classifier, {kind}:", dict(Counter(classify(r[kind], url(r), r["home"], fingerprint(r)) for r in ok)))
soft = [r for r in ok if r["missing"]["status"] == 200]
print("Classifier, the", len(soft), "missing pages that returned 200:",
      dict(Counter(classify(r["missing"], r["missing_url"], r["home"], fingerprint(r)) for r in soft)))
for r in ok:
    h = classify(r["home"], r["home"]["final_url"], r["home"], fingerprint(r))
    if h != "success":
        print("  homepage as", h, "|", r["domain"], "|", r["home"]["title"][:60], "|", r["home"]["words"])
