BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Evaluation metrics14 of 33 in topic

165. Compute ROUGE-L F1 from the longest common subsequence

MediumEvaluation metrics

Why it matters Summarisation evals and many LLM-as-product dashboards still log ROUGE-L.

ROUGE-L compares a generated summary to a reference by their longest common subsequence of tokens.

Write rouge_l(cand, ref), both token lists. Let L be the LCS length. Precision P = L / len(cand), recall R = L / len(ref), return F1 = 2PR / (P + R), or 0.0 if L is 0.

Functiondef rouge_l(cand, ref)

Example 1

rouge_l(["the", "cat", "sat"], ["the", "cat", "on", "mat"]) has L 2, P 2/3, R 1/2, F1 about 0.5714.

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

Dynamic programming table of size (len(cand) + 1) by (len(ref) + 1).

Hint 2

Classic LCS DP.

Hint 3

Match: diagonal + 1. Mismatch: max of up and left.

Common mistakes
  • Using the diagonal on a mismatch.
  • Computing F1 with lengths swapped.
Understand the idea

ROUGE-L measures how much of a reference text a summary keeps in the same order. It uses the longest common subsequence (LCS): the longest list of tokens that appear in both texts in order, with gaps allowed.

Worked example

cand = [a, b, c, d], ref = [a, c, d, e, f]. The LCS is [a, c, d], length 3. Precision P = 3 / 4 = 0.75 and recall R = 3 / 5 = 0.6. F1 = 2 * 0.75 * 0.6 / (0.75 + 0.6) = 0.9 / 1.35 = about 0.667.

fill a dynamic programming table where dp[i][j] = dp[i-1][j-1] + 1 if the tokens match, else max(dp[i-1][j], dp[i][j-1])

L is the bottom-right value. P = L / len(cand), R = L / len(ref), F1 = 2PR / (P + R), and return 0.0 if L is 0. On a mismatch carry the best of skipping a token from either side; carrying the diagonal gives a substring count instead. Used in: summarisation benchmarks and quick automatic checks of LLM output against a reference.

Topics
Evaluation metrics
Similar questions
  • Compute ROC AUC by counting ranked pairsMedium
  • Grade a search ranking with NDCG at kMedium
  • Compute macro and micro F1 for a multi-class classifierMedium
  • Measure rater agreement with Cohen's kappaMedium
All Evaluation metrics problems
Report
165 / 903
Python 3solution.pyPython idle
1
2
3
4
def rouge_l(cand, ref):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS