Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/evidently/metrics/bleu_metric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# src/evidently/metrics/bleu_metric.py
"""
BLEU Metric for evaluating candidate sentences against reference sentences.
Beginner-friendly contribution for Hacktoberfest.
"""

from nltk.translate.bleu_score import sentence_bleu

class BleuMetric:
def __init__(self):
"""Initialize the BLEU metric class."""
pass

def evaluate(self, reference, candidate):
"""
Evaluate the BLEU score.

Parameters:
reference (str): The reference sentence.
candidate (str): The candidate sentence to evaluate.

Returns:
float: BLEU score between 0 and 1.
"""
# Convert sentences to lists of words
reference_words = reference.split()
candidate_words = candidate.split()

# Compute BLEU score
score = sentence_bleu([reference_words], candidate_words)

return score


# Example usage (for maintainers/tests)
if __name__ == "__main__":
reference_sentence = "The cat is on the mat"
candidate_sentence = "The cat is on the mat"

bleu = BleuMetric()
print("BLEU Score:", bleu.evaluate(reference_sentence, candidate_sentence))