ROUGE-1, ROUGE-2 and ROUGE-L From Scratch

Quick Overview

Implement ROUGE-1, ROUGE-2 and ROUGE-L from scratch in Python, returning precision, recall and F1 for each pair and a macro-averaged pandas table. Tests clipped n-gram counting, longest common subsequence length, tokenization and zero-length edge cases.

ROUGE-1, ROUGE-2 and ROUGE-L From Scratch

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `tokenize`, `rouge_n(candidate, reference, n)`, `rouge_l(candidate, reference)` and `rouge_scores(candidates, references)`. The pair functions return a `(precision, recall, f1)` tuple of floats. `rouge_scores` macro-averages these over all pairs into a `pandas` table. The functions must follow these rules: - `tokenize` lowercases the text, then keeps runs of `[a-z0-9]+`. Punctuation and whitespace are dropped. - ROUGE-N measures overlap with clipped counts `min(cand_count, ref_count)`. Precision divides by the candidate n-gram total, recall by the reference total, and F1 is their harmonic mean. `n < 1` raises `ValueError`. - ROUGE-L uses the length of the token LCS (non-contiguous) as the overlap. The denominators are the candidate and reference token counts. - Any zero denominator, including text shorter than `n`, gives `0.0` for that score. If precision + recall is 0, F1 is `0.0`. None of these cases raise. - `rouge_scores` returns a DataFrame with columns `metric, precision, recall, f1` and rows `rouge1`, `rouge2`, `rougeL` in that order. Mismatched lengths or empty input raise `ValueError`. Example: ```python rouge_n("the the the the", "the cat", 1) # (0.25, 0.5, 0.3333...) clipped overlap = 1 rouge_l("a b c d", "b x d a y") # (0.5, 0.4, 0.4444...) LCS = [b, d] rouge_n("hello", "hello world", 2) # (0.0, 0.0, 0.0) ``` ```hint One helper for the three numbers ROUGE-N and ROUGE-L differ only in how they compute the overlap and the two totals. Put the precision, recall, F1 and zero-denominator rules in one function that both can call. ``` ```hint Memory for the LCS Each row of the LCS table reads only the row above it, so you do not need the full grid to get the length. ``` ### What a Strong Answer Covers - Tokenization that makes case and punctuation irrelevant to the scores. - Clipped n-gram counting, including text shorter than `n`. - An LCS length computation, with its time and memory cost. - The zero-denominator and zero-F1 rules shared by both metrics. - Input validation and the exact row and column layout of the macro-averaged table. ### Follow-up Questions - How would you score a candidate against multiple references? - How does ROUGE-Lsum differ from sentence-level ROUGE-L? - Why might your scores differ from the `rouge-score` package?

Overview: Implement ROUGE-1, ROUGE-2 and ROUGE-L from scratch in Python, returning precision, recall and F1 for each pair and a macro-averaged pandas table. Tests clipped n-gram counting, longest common subsequence length, tokenization and zero-length edge cases.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement tokenize, rouge_n(candidate, reference, n), rouge_l(candidate, reference) and rouge_scores(candidates, references). The pair functions return a (precision, recall, f1) tuple of floats. rouge_scores macro-averages these over all pairs into a pandas table.

The functions must follow these rules:

  • tokenize lowercases the text, then keeps runs of [a-z0-9]+ . Punctuation and whitespace are dropped.
  • ROUGE-N measures overlap with clipped counts min(cand_count, ref_count) . Precision divides by the candidate n-gram total, recall by the reference total, and F1 is their harmonic mean. n < 1 raises ValueError .
  • ROUGE-L uses the length of the token LCS (non-contiguous) as the overlap. The denominators are the candidate and reference token counts.
  • Any zero denominator, including text shorter than n , gives 0.0 for that score. If precision + recall is 0, F1 is 0.0 . None of these cases raise.
  • rouge_scores returns a DataFrame with columns metric, precision, recall, f1 and rows rouge1 , rouge2 , rougeL in that order. Mismatched lengths or empty input raise ValueError .

Example:

rouge_n("the the the the", "the cat", 1)  # (0.25, 0.5, 0.3333...)  clipped overlap = 1
rouge_l("a b c d", "b x d a y")          # (0.5, 0.4, 0.4444...)  LCS = [b, d]
rouge_n("hello", "hello world", 2)       # (0.0, 0.0, 0.0)

What a Strong Answer Covers Guidance

  • Tokenization that makes case and punctuation irrelevant to the scores.
  • Clipped n-gram counting, including text shorter than n .
  • An LCS length computation, with its time and memory cost.
  • The zero-denominator and zero-F1 rules shared by both metrics.
  • Input validation and the exact row and column layout of the macro-averaged table.

Follow-up Questions Guidance

  • How would you score a candidate against multiple references?
  • How does ROUGE-Lsum differ from sentence-level ROUGE-L?
  • Why might your scores differ from the rouge-score package?