"""Overnight strict scorer (reference implementation; scorer.js on the page gives the same verdicts).

An answer is right when it states most (60%) of the words of the fact's value that the question did not already
give away. Spelling variants count when the first five letters match (offences / offenses). An answer that only
repeats the question cannot score.

    python scorer.py data/news-2026-10-13.jsonl my_answers.jsonl

my_answers.jsonl has one line per question: {"id": 0, "answer": "..."}
"""
import json
import re
import sys


def norm(s):
    return " ".join(re.sub(r"[^0-9a-z]+", " ", str(s).lower()).split())


def new_words(value, question):
    q = set(norm(question).split())
    return [w for w in norm(value).split() if (len(w) > 2 or w.isdigit()) and w not in q]


def answered(answer, value, question):
    vw = new_words(value, question)
    ow = norm(answer).split()
    hit = lambda w: w in ow or (len(w) >= 5 and any(x[:5] == w[:5] for x in ow))  # noqa: E731
    return bool(vw) and sum(hit(w) for w in vw) / len(vw) >= 0.6


if __name__ == "__main__":
    qs = {r["id"]: r for r in map(json.loads, open(sys.argv[1], encoding="utf-8"))}
    got = {r["id"]: r["answer"] for r in map(json.loads, open(sys.argv[2], encoding="utf-8"))}
    right = {i: answered(got.get(i, ""), q["value"], q["question"]) for i, q in qs.items()}
    fair = [i for i, q in qs.items() if q["judged_fair"]]
    print("all questions: %d/%d (%.1f%%)" % (sum(right.values()), len(qs), 100 * sum(right.values()) / len(qs)))
    print("fair questions: %d/%d (%.1f%%)" % (sum(right[i] for i in fair), len(fair),
                                              100 * sum(right[i] for i in fair) / len(fair)))
    print("missing answers: %d" % sum(1 for i in qs if i not in got))
