v9.1.1
Menu

ROUGE-1 Score Calculator

easyFREEText Generation & AdvancedNLP 3: Advanced

Calculate the ROUGE-1 F1 score between a generated summary and a reference summary.

ROUGE-1 measures unigram overlap. A word that appears aa times in the generated summary and bb times in the reference contributes min⁡(a,b)\min(a, b) matching unigrams:

  • Precision = (matching unigrams) / (unigrams in generated)
  • Recall = (matching unigrams) / (unigrams in reference)
  • F1 = 2 * Precision * Recall / (Precision + Recall)

All comparisons are case-insensitive. Round the F1 score to 4 decimal places.

Input:

  • gen: the generated summary (a string)
  • ref: the reference summary (a string)

Return the F1 score as a float.

Example

Input
the cat sat on the mat
the cat is on the mat
Output
0.8333
Reasoning
  • Inputs: gen is the generated summary and ref the reference; the unigrams are each text's whitespace-separated words, compared ignoring case. Both sample sentences have 6 words.
  • Word counts: generated has the ×2, cat, sat, on, mat; reference has the ×2, cat, is, on, mat.
  • Matches are clipped: each word counts min⁡(a,b)\min(a, b) times, where aa and bb are its counts in the two texts. the gives min⁡(2,2)=2\min(2,2)=2; cat, on, mat give 1 each; sat and is give 0. Total 2+1+1+1=52+1+1+1=5.
  • Scores: P=5/6P = 5/6, R=5/6R = 5/6, so F1=2⋅56⋅5656+56=56≈0.8333F_1 = \frac{2 \cdot \frac{5}{6} \cdot \frac{5}{6}}{\frac{5}{6}+\frac{5}{6}} = \frac{5}{6} \approx 0.8333, the output 0.8333.
  • Notice: when P=RP = R, F1F_1 equals that shared value. Clipping stops repetition from inflating the score: generated "The THE the" against reference "the" has only 1 match, so P=1/3P = 1/3, R=1R = 1 and F1=0.5F_1 = 0.5. With no shared words (0.0) both are 0.

Constraints

  • Case-insensitive comparison
  • Use word-level unigrams (split on whitespace)
  • If precision + recall = 0, F1 = 0.0
  • Round F1 to 4 decimal places

More from NLP 3: Advanced

solution.py

Test results

Sign in to run your code against the test cases.