File size: 1,133 Bytes
c550325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Score a run file against human.jsonl or generated.jsonl.
# A run file is JSONL, one line per question: {"qid": "h0000", "ranking": ["<module_id>", ...]} (best first).
# Rank only modules from the question's own book (corpus.jsonl). Questions missing from the run count as wrong.
#   python score.py human.jsonl run.jsonl
import json, sys
def load(p):
    return [json.loads(l) for l in open(p, encoding="utf-8")]
def score(qs, runs):
    rank = {r["qid"]: r["ranking"] for r in runs}
    hit = lambda q, k: q["gold"] in rank.get(q["qid"], [])[:k]
    out = {}
    groups = {"all": qs}
    if "lo_tagged" in qs[0]:
        groups |= {"LO-tagged": [q for q in qs if q["lo_tagged"]], "untagged": [q for q in qs if not q["lo_tagged"]]}
    for name, g in groups.items():
        out[name] = {"n": len(g), "R@1": round(100 * sum(hit(q, 1) for q in g) / len(g), 1),
                     "R@5": round(100 * sum(hit(q, 5) for q in g) / len(g), 1)}
    return out
if __name__ == "__main__":
    for k, v in score(load(sys.argv[1]), load(sys.argv[2])).items():
        print(f"{k:10} n={v['n']:5}  R@1 {v['R@1']:5.1f}  R@5 {v['R@5']:5.1f}")