Registry / testing / rouge-score

rouge-score

JSON →
library0.1.2pypypi✓ verified 26d ago

The `rouge-score` library is a pure Python implementation of the ROUGE-1.5.5 evaluation metric, designed to closely replicate the results of the original Perl script. It provides functionalities for calculating ROUGE-N, ROUGE-L (sentence-level and summary-level), text normalization, and optional Porter stemming. The library is currently at version 0.1.2, released in July 2022, and while the version updates are infrequent, it remains an actively used and stable package maintained by Google for evaluating text generation tasks like summarization.

pip install rouge-score
INSTALL
IMPORT
SIG · ROUGE-SCORE
R
rouge-score
testingpythonv0.1.2
Install
6.7s avg
Import
1090ms
Disk
113MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 1.104s · 111.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 6.7s · import 1.076s · 108MB
113MB installed
● package 113MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RougeScorer
from rouge_score import rouge_scorer

This example demonstrates how to initialize `RougeScorer` for common ROUGE types (ROUGE-1, ROUGE-2, ROUGE-L, ROUGE-Lsum) with stemming enabled. It then computes and prints the precision, recall, and F1-score for a given reference and candidate text. Ensure `nltk` data (like 'punkt' and 'wordnet') is available if `use_stemmer=True`.

from rouge_score import rouge_scorer # Initialize the scorer with desired ROUGE types and optional stemming scorer = rouge_scorer.RougeScorer(['rouge1', 'rouge2', 'rougeL', 'rougeLsum'], use_stemmer=True) # Define the reference (target) and candidate (prediction) summaries reference_summary = "The quick brown fox jumps over the lazy dog. It's a sunny day." candidate_summary = "A quick brown fox leaps over a sleeping dog. The weather is nice." # Calculate scores scores = scorer.score(reference_summary, candidate_summary) # Print the results for each ROUGE type (precision, recall, f-measure) for key, value in scores.items(): print(f"{key}:") print(f" Precision: {value.precision:.4f}") print(f" Recall: {value.recall:.4f}") print(f" F1 Score: {value.fmeasure:.4f}")
Debug
Known issues
gotchaBeware of inconsistent ROUGE implementations across different Python packages. Many ROUGE libraries exist, and not all adhere strictly to the ROUGE-1.5.5 standard or produce identical results, leading to irreproducible and incomparable evaluation scores. `rouge-score` aims to replicate the Perl script's behavior, but results might differ from other Python wrappers or custom implementations.
fix
Always specify the exact ROUGE package and version used in research or production. When comparing results, ensure the same ROUGE implementation, configuration, and preprocessing steps are applied.
affects: All versions
gotchaROUGE metrics primarily evaluate lexical overlap (n-gram matching) and inherently suffer from 'semantic blindness'. They do not fully capture semantic meaning, logical coherence, factual correctness, or fluency. Systems can achieve high ROUGE scores by repeating phrases or using similar vocabulary without truly understanding the content, and conversely, well-phrased paraphrases might get lower scores.
fix
Complement ROUGE scores with other evaluation metrics that assess semantic similarity (e.g., BERTScore), human evaluations for quality assessment, and qualitative analysis to gain a comprehensive understanding of text generation quality.
affects: All versions
gotchaThe `rouge-score` library distinguishes between two flavors of ROUGE-L: `rougeL` (sentence-level LCS) and `rougeLsum` (summary-level union-LCS). The choice depends on whether newlines in your text should be treated as sentence boundaries for LCS computation. Misunderstanding this distinction can lead to different and potentially incorrect evaluation results for multi-sentence summaries.
fix
Carefully select between `rougeL` and `rougeLsum` based on your data's structure and the desired evaluation behavior. `rougeLsum` is often preferred for multi-sentence summaries where newlines delineate sentences.
affects: All versions
gotchaThe `rouge-score` library supports optional Porter stemming via `use_stemmer=True` but explicitly *does not* include stopword removal. This differs from some configurations of the original Perl ROUGE script and other Python ROUGE implementations. If you rely on stopword removal for specific tasks, this needs to be handled externally.
fix
If stopword removal is desired, implement it as a preprocessing step on both reference and candidate texts before passing them to `RougeScorer`. Be mindful of how this might affect comparability with other ROUGE setups.
affects: All versions
gotchaROUGE scores are highly dependent on the quality and number of human-written reference summaries. Different reference summaries for the same source text can lead to significantly varying ROUGE scores, even if all references are of high quality, reflecting the subjective nature of summarization. This variability can make it difficult to objectively compare models.
fix
Use multiple, diverse human reference summaries if possible. Acknowledge and report the dependency on references, and consider sensitivity analysis to understand how scores change with different reference sets. Focus on relative improvements rather than absolute scores.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'rouge_score'
The `rouge-score` library has not been installed in your Python environment.
fix
pip install rouge-score
ImportError: cannot import name 'RougeScorer' from 'rouge_score'
The `RougeScorer` class is located within the `rouge_scorer` module inside the `rouge_score` package, not directly available from the top-level package import.
fix
from rouge_score.rouge_scorer import RougeScorer
AttributeError: 'list' object has no attribute 'split'
The `score` method of `RougeScorer` expects the `target` argument to be a single string, but it received a list of strings instead, leading to an attempt to call `split()` on a list.
fix
Ensure the `target` argument is a single string. The `predictions` argument can be a list of strings, but `target` must be singular.
```python
from rouge_score.rouge_scorer import RougeScorer
scorer = RougeScorer(['rouge1'], use_stemmer=True)
target_text = "This is the reference summary."
predictions_list = ["This is a candidate summary.", "Another candidate."]
scores = scorer.score(target_text, predictions_list)
```
LookupError: Resource punkt not found.
When `use_stemmer=True` is enabled in `RougeScorer`, the `nltk` library requires the `punkt` tokenizer data, which has not been downloaded.
fix
Download the `punkt` resource using NLTK's downloader before initializing RougeScorer.
```python
import nltk
nltk.download('punkt')

from rouge_score.rouge_scorer import RougeScorer
scorer = RougeScorer(['rouge1'], use_stemmer=True)
```
Upgrade
Version history
0.1.2latest on PyPI · released Jul 22, 2022
Audit
Dependencies
sixrequiredCompatibility utilities.
nltkrequiredUsed for tokenization and Porter stemming when `use_stemmer=True`.
absl-pyrequiredAbseil Python Common Libraries, for logging and utilities.
numpyrequiredNumerical operations.
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources