Skip to content

Commit

Permalink
Merge pull request #122 from sbintuitions/update_bleu
Browse files Browse the repository at this point in the history
Set `effective_order=True` for computing sentence-level bleu
  • Loading branch information
ryokan0123 authored Jan 16, 2025
2 parents 8ead327 + 6970109 commit fe065b6
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions flexeval/core/metric/bleu.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class BLEU(Metric):
"""

def __init__(self, tokenize_option: str | None = None) -> None:
self._bleu = sacrebleu.metrics.BLEU(tokenize=tokenize_option)
self._corpus_bleu = sacrebleu.metrics.BLEU(tokenize=tokenize_option)
# For sentence BLEU, we need to set `effective_order=True` as recommended by sacrebleu.
self._sentence_bleu = sacrebleu.metrics.BLEU(tokenize=tokenize_option, effective_order=True)

def evaluate(
self,
Expand All @@ -60,16 +62,16 @@ def evaluate(
set_of_references.append("")
references_for_sacrebleu.append(set_of_references)

bleu = self._bleu.corpus_score([o.strip() for o in lm_outputs], references_for_sacrebleu)
bleu = self._corpus_bleu.corpus_score([o.strip() for o in lm_outputs], references_for_sacrebleu)
sentence_bleu_list = [
self._bleu.sentence_score(o.strip(), refs) for o, refs in zip(lm_outputs, references_list)
self._sentence_bleu.sentence_score(o.strip(), refs) for o, refs in zip(lm_outputs, references_list)
]

return MetricResult(
{
"bleu_score": bleu.score / 100,
"bleu_bp": bleu.bp,
"bleu_signature": self._bleu.get_signature(),
"bleu_signature": self._corpus_bleu.get_signature(),
},
instance_details=[{"bleu_score": b.score / 100, "bleu_bp": b.bp} for b in sentence_bleu_list],
)

0 comments on commit fe065b6

Please sign in to comment.