AI Engineer Portal
Your personal operating system for career transition.
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
-
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). -
bleu1(prediction: str, reference: str) -> float— Unigram BLEU: fraction of prediction tokens that appear in the reference, multiplied by a brevity penaltymin(1.0, exp(1 - len(ref_tokens) / len(pred_tokens)))when prediction is shorter. -
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, noevaluate). - Empty strings: return 0.0 without crashing.
Evaluation / medium / Step 19 of 36
Evaluation and review loops
Separate the scoring logic from the interpretation logic. Your goal is not just a number; it is a useful next action.
- - Produces a useful signal, not decorative output
- - Makes regression review easier
- - Would support a benchmark or observability loop
- - 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.