PIXELBANKv9.1.0
Menu

Implement the ROUGE-N metric for evaluating text summarization.

ROUGE-N measures n-gram overlap between a generated summary and a reference summary.

ROUGE-N=∑n-gram∈refmin⁡(countgen(n-gram),countref(n-gram))∑n-gram∈refcountref(n-gram)\text{ROUGE-N} = \frac{\sum_{\text{n-gram} \in \text{ref}} \min(\text{count}_{\text{gen}}(\text{n-gram}), \text{count}_{\text{ref}}(\text{n-gram}))}{\sum_{\text{n-gram} \in \text{ref}} \text{count}_{\text{ref}}(\text{n-gram})}

In simpler terms: sum of clipped overlapping n-gram counts divided by total reference n-gram count.

Input format:

  • Line 1: N (n-gram size, integer)
  • Line 2: Generated summary (space-separated lowercase words)
  • Line 3: Reference summary (space-separated lowercase words)

Output: ROUGE-N recall score rounded to 4 decimal places.

Example:

Input:
1
the cat sat on the mat
the cat sat on a rug
Output:
0.6667
Reasoning:

ROUGE-1 (unigram):

Reference unigrams: the(1), cat(1), sat(1), on(1), a(1), rug(1) => total = 6 Generated unigrams: the(2), cat(1), sat(1), on(1), mat(1)

Overlap (clipped by ref count):

  • the: min(2, 1) = 1
  • cat: min(1, 1) = 1
  • sat: min(1, 1) = 1
  • on: min(1, 1) = 1
  • a: min(0, 1) = 0
  • rug: min(0, 1) = 0

Sum of clipped: 4 ROUGE-1 = 4/6 = 0.6667

Constraints:

  • N-grams are word-level
  • Clip generated counts to reference counts
  • ROUGE is recall-oriented (denominator = reference n-gram count)
  • If reference has no n-grams (too short), output 0.0
  • Round to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
ROUGE Score - Medium | PixelBank