方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Build a reference-based BLEU/ROUGE scorer

Build a Reference-Based BLEU/ROUGE Scorer

Reference-based metrics compare generated text to a known-good reference. They are the cheapest automated evaluation when you have ground-truth answers.

What to build

  1. rouge1_f1(prediction: str, reference: str) -> float — Tokenize by whitespace+lowercase. Compute precision, recall, and F1 on token set overlap. Return F1 (0.0-1.0).

  2. bleu1(prediction: str, reference: str) -> float — Unigram BLEU: fraction of prediction tokens that appear in the reference, multiplied by a brevity penalty min(1.0, exp(1 - len(ref_tokens) / len(pred_tokens))) when prediction is shorter.

  3. score_dataset(cases: list[dict]) -> dict — Each case has "prediction" and "reference". Return {"avg_rouge1_f1": float, "avg_bleu1": float, "n": int}.

Constraints

  • Standard library only (no nltk, no evaluate).
  • Empty strings: return 0.0 without crashing.

Evaluation / medium / Step 19 of 36

Practice stage

Evaluation and review loops

Hint

Separate the scoring logic from the interpretation logic. Your goal is not just a number; it is a useful next action.

Success criteria
  • - Produces a useful signal, not decorative output
  • - Makes regression review easier
  • - Would support a benchmark or observability loop
Review checklist
  • - Would this output help decide what to fix next?
  • - Are important failure modes visible?
  • - Does the score hide any ambiguity I should record?

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.