Datasets:
Download scorer.py from nerology/TitleBench: direct link, hf CLI and curl.
- Browser
- Download file 3.79 kB
-
https://huggingface.co/datasets/nerology/TitleBench/resolve/main/scorer.py
- Command line
-
hf download hf://datasets/nerology/TitleBench/scorer.py
-
curl -L -o scorer.py https://huggingface.co/datasets/nerology/TitleBench/resolve/main/scorer.py
3.79 kB
| #!/usr/bin/env python3 | |
| """TitleBench scorer — self-contained, no external metric libraries. | |
| Usage: | |
| python scorer.py predictions.jsonl | |
| predictions.jsonl: one JSON object per line, in the SAME ORDER as | |
| test.jsonl, each with an "output" field: | |
| {"output": "My Generated Title"} | |
| Prints the full metric table (ROUGE-1/2/L F1, BLEU-4, chrF, EM%, Repet%). | |
| """ | |
| import json | |
| import math | |
| import re | |
| import sys | |
| from collections import Counter | |
| _PUNC = re.compile(r"[^a-z0-9\s]") | |
| def normalize(text: str) -> str: | |
| return " ".join(_PUNC.sub(" ", text.lower()).split()) | |
| def tokens(text: str): | |
| return normalize(text).split() | |
| def _ngrams(toks, n): | |
| return Counter(zip(*[toks[i:] for i in range(n)]) if len(toks) >= n else []) | |
| def rouge_n_f1(g, r, n): | |
| g, r = _ngrams(g, n), _ngrams(r, n) | |
| if not g or not r: | |
| return 0.0 | |
| inter = sum((g & r).values()) | |
| prec = inter / max(1, sum(g.values())) | |
| rec = inter / max(1, sum(r.values())) | |
| return 0.0 if prec + rec == 0 else 2 * prec * rec / (prec + rec) | |
| def rouge_l_f1(g, r): | |
| m, n = len(g), len(r) | |
| if m == 0 or n == 0: | |
| return 0.0 | |
| dp = [[0] * (n + 1) for _ in range(m + 1)] | |
| for i in range(1, m + 1): | |
| for j in range(1, n + 1): | |
| dp[i][j] = dp[i - 1][j - 1] + 1 if g[i - 1] == r[j - 1] else max(dp[i - 1][j], dp[i][j - 1]) | |
| lcs = dp[m][n] | |
| prec, rec = lcs / m, lcs / n | |
| return 0.0 if prec + rec == 0 else 2 * prec * rec / (prec + rec) | |
| def bleu4(g, r): | |
| gl, rl = len(g), len(r) | |
| if gl == 0: | |
| return 0.0 | |
| scores = [] | |
| for n in range(1, 5): | |
| g_, r_ = _ngrams(g, n), _ngrams(r, n) | |
| inter = sum((g_ & r_).values()) | |
| total = sum(g_.values()) | |
| scores.append(inter / total if inter > 0 and total > 0 else (inter + 1) / (total + 1)) | |
| geo = math.exp(sum(math.log(max(s, 1e-9)) for s in scores) / 4) | |
| return geo * min(1.0, math.exp(1 - rl / max(gl, 1))) | |
| def chrf(gen, ref): | |
| t = lambda s: [normalize(s).replace(" ", "")[i:i + 6] for i in range(len(normalize(s).replace(" ", "")) - 5)] | |
| g, r = Counter(t(gen)), Counter(t(ref)) | |
| if not g or not r: | |
| return 0.0 | |
| inter = sum((g & r).values()) | |
| prec, rec = inter / max(1, sum(g.values())), inter / max(1, sum(r.values())) | |
| return 0.0 if prec + rec == 0 else 2 * prec * rec / (prec + rec) | |
| def repetition_rate(g): | |
| if len(g) <= 1: | |
| return 0.0 | |
| seen, reps = set(), 0 | |
| for t in g: | |
| if t in seen: | |
| reps += 1 | |
| seen.add(t) | |
| return reps / len(g) | |
| def main(): | |
| if len(sys.argv) < 2: | |
| print(__doc__) | |
| sys.exit(1) | |
| with open(sys.argv[1]) as f: | |
| preds = [json.loads(l) for l in f if l.strip()] | |
| print(f"scoring {len(preds):,} predictions") | |
| acc = {k: 0.0 for k in | |
| ["rouge1", "rouge2", "rougeL", "bleu4", "chrf", "em", "repet"]} | |
| for p in preds: | |
| g = tokens(p["output"]) | |
| r = tokens(p["target"]) | |
| acc["rouge1"] += rouge_n_f1(g, r, 1) | |
| acc["rouge2"] += rouge_n_f1(g, r, 2) | |
| acc["rougeL"] += rouge_l_f1(g, r) | |
| acc["bleu4"] += bleu4(g, r) | |
| acc["chrf"] += chrf(p["output"], p["target"]) | |
| acc["em"] += 1.0 if normalize(p["output"]) == normalize(p["target"]) else 0.0 | |
| acc["repet"] += repetition_rate(g) | |
| n = len(preds) | |
| print(f"\n{'metric':<10}{'value':>10}") | |
| print("-" * 22) | |
| print(f"{'ROUGE-1':<10}{acc['rouge1']/n:>10.3f}") | |
| print(f"{'ROUGE-2':<10}{acc['rouge2']/n:>10.3f}") | |
| print(f"{'ROUGE-L':<10}{acc['rougeL']/n:>10.3f}") | |
| print(f"{'BLEU-4':<10}{acc['bleu4']/n:>10.3f}") | |
| print(f"{'chrF':<10}{acc['chrf']/n:>10.3f}") | |
| print(f"{'EM%':<10}{100*acc['em']/n:>10.2f}") | |
| print(f"{'Repet%':<10}{100*acc['repet']/n:>10.2f}") | |
| if __name__ == "__main__": | |
| main() | |