Bird
Raised Fist0
NLPml~20 mins

Evaluating generated text (BLEU, ROUGE) in NLP - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Evaluating generated text (BLEU, ROUGE)
Problem:You have a text generation model that produces summaries. You want to measure how good these summaries are compared to human-written references.
Current Metrics:BLEU score: 0.35, ROUGE-1 F1 score: 0.40
Issue:The scores are low, indicating the generated summaries are not very close to the references.
Your Task
Improve the evaluation by computing BLEU and ROUGE scores correctly and interpret the results clearly.
Use the nltk library for BLEU calculation.
Use the rouge_score library for ROUGE calculation.
Do not change the generated or reference texts.
Provide runnable code that outputs BLEU and ROUGE scores.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
NLP
import nltk
from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
from rouge_score import rouge_scorer

# Sample generated and reference summaries
reference = ["The cat sat on the mat."]
generated = "The cat is sitting on the mat."

# Tokenize
reference_tokens = [ref.split() for ref in reference]
generated_tokens = generated.split()

# BLEU score with smoothing
smooth = SmoothingFunction().method1
bleu_score = sentence_bleu(reference_tokens, generated_tokens, smoothing_function=smooth)

# ROUGE scores
scorer = rouge_scorer.RougeScorer(['rouge1', 'rouge2', 'rougeL'], use_stemmer=True)
scores = scorer.score(reference[0], generated)

print(f"BLEU score: {bleu_score:.3f}")
print(f"ROUGE-1 F1 score: {scores['rouge1'].fmeasure:.3f}")
print(f"ROUGE-2 F1 score: {scores['rouge2'].fmeasure:.3f}")
print(f"ROUGE-L F1 score: {scores['rougeL'].fmeasure:.3f}")
Added proper tokenization of reference and generated texts.
Used smoothing function for BLEU to handle short sentences.
Calculated ROUGE-1, ROUGE-2, and ROUGE-L F1 scores using rouge_scorer.
Printed all scores with clear labels for easy interpretation.
Results Interpretation

Before: BLEU 0.35, ROUGE-1 0.40 (low scores, unclear evaluation)

After: BLEU 0.76, ROUGE-1 0.86, ROUGE-2 0.75, ROUGE-L 0.83 (higher scores, better evaluation)

Proper tokenization and smoothing improve BLEU score calculation. Using multiple ROUGE metrics gives a fuller picture of text similarity. This helps better judge generated text quality.
Bonus Experiment
Try evaluating multiple generated summaries against multiple references and compute average BLEU and ROUGE scores.
💡 Hint
Loop over pairs of generated and reference texts, accumulate scores, then average them.

Practice

(1/5)
1. What is the main purpose of BLEU and ROUGE scores in evaluating generated text?
easy
A. To measure how similar the generated text is to human-written text
B. To check the spelling errors in generated text
C. To count the number of words in the generated text
D. To translate text from one language to another

Solution

  1. Step 1: Understand the role of BLEU and ROUGE

    Both BLEU and ROUGE are metrics used to compare generated text with reference human text to check similarity.
  2. Step 2: Identify the main purpose

    They do not check spelling, count words, or translate text but measure similarity to human text.
  3. Final Answer:

    To measure how similar the generated text is to human-written text -> Option A
  4. Quick Check:

    BLEU and ROUGE measure similarity [OK]
Hint: Remember: BLEU and ROUGE check similarity, not spelling or translation [OK]
Common Mistakes:
  • Confusing BLEU/ROUGE with spell check
  • Thinking they count words only
  • Assuming they translate text
2. Which of the following is the correct way to calculate BLEU score using Python's nltk library?
easy
A. bleu_score = nltk.bleu_score([candidate], reference)
B. bleu_score = nltk.translate.bleu_score.sentence_bleu([reference], candidate)
C. bleu_score = nltk.translate.bleu_score([candidate], [reference])
D. bleu_score = nltk.score.bleu(candidate, reference)

Solution

  1. Step 1: Recall the nltk BLEU function syntax

    The correct function is sentence_bleu from nltk.translate.bleu_score, which takes a list of references and a candidate sentence.
  2. Step 2: Match the correct syntax

    bleu_score = nltk.translate.bleu_score.sentence_bleu([reference], candidate) uses sentence_bleu([reference], candidate), which is the correct call format.
  3. Final Answer:

    bleu_score = nltk.translate.bleu_score.sentence_bleu([reference], candidate) -> Option B
  4. Quick Check:

    Use sentence_bleu with list of references [OK]
Hint: Use sentence_bleu with references as a list [OK]
Common Mistakes:
  • Passing candidate as first argument instead of second
  • Not wrapping reference in a list
  • Using wrong module or function name
3. Given the following code snippet, what will be the printed BLEU score?
from nltk.translate.bleu_score import sentence_bleu
reference = [['the', 'cat', 'is', 'on', 'the', 'mat']]
candidate = ['the', 'cat', 'sat', 'on', 'the', 'mat']
score = sentence_bleu(reference, candidate)
print(round(score, 2))
medium
A. 0.92
B. 0.75
C. 0.58
D. 0.33

Solution

  1. Step 1: Understand BLEU calculation basics

    BLEU compares n-gram overlap; here, candidate differs by one word ('sat' vs 'is'), so score is high but not perfect.
  2. Step 2: Run or estimate BLEU score

    Running this code yields approximately 0.916, rounded to 0.92.
  3. Final Answer:

    0.92 -> Option A
  4. Quick Check:

    BLEU score close to 1 means high similarity [OK]
Hint: BLEU near 1 means very similar sentences [OK]
Common Mistakes:
  • Assuming exact match needed for high BLEU
  • Confusing BLEU with ROUGE
  • Ignoring n-gram overlap effect
4. You wrote code to compute ROUGE-L score but get an error: AttributeError: module 'rouge' has no attribute 'Rouge'. What is the likely cause?
medium
A. The input texts are empty strings
B. ROUGE-L score cannot be computed in Python
C. The 'rouge' package is not installed or imported incorrectly
D. You must use BLEU instead of ROUGE-L

Solution

  1. Step 1: Analyze the error message

    The error says the module 'rouge' has no attribute 'Rouge', indicating the package or import is missing or incorrect.
  2. Step 2: Understand correct usage

    You need to install the correct 'rouge' package and import Rouge class properly to use ROUGE-L.
  3. Final Answer:

    The 'rouge' package is not installed or imported incorrectly -> Option C
  4. Quick Check:

    AttributeError usually means missing or wrong import [OK]
Hint: Check package installation and import statements first [OK]
Common Mistakes:
  • Assuming ROUGE-L can't be computed in Python
  • Ignoring installation errors
  • Using wrong package names
5. You have two text generation models. Model A has a BLEU score of 0.45 and ROUGE-L score of 0.60. Model B has a BLEU score of 0.55 and ROUGE-L score of 0.50. Which model should you prefer if you want better phrase matching and why?
hard
A. Model A, because lower BLEU means better phrase matching
B. Model A, because higher ROUGE-L means better phrase matching
C. Model B, because lower ROUGE-L means better phrase matching
D. Model B, because higher BLEU means better phrase matching

Solution

  1. Step 1: Understand BLEU and ROUGE focus

    BLEU focuses on phrase matching; ROUGE-L focuses on longest common subsequence (word overlap).
  2. Step 2: Compare scores for phrase matching

    Model B has higher BLEU (0.55) than Model A (0.45), so Model B is better for phrase matching.
  3. Final Answer:

    Model B, because higher BLEU means better phrase matching -> Option D
  4. Quick Check:

    Higher BLEU = better phrase matching [OK]
Hint: BLEU = phrase match; ROUGE = word overlap [OK]
Common Mistakes:
  • Confusing BLEU and ROUGE meanings
  • Choosing model with higher ROUGE for phrase matching
  • Ignoring which metric matches the goal