Code for How to Calculate the BLEU Score in Python Tutorial


View on Github

bleu_score.py

# -*- coding: utf-8 -*-
"""BLEU Score.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1dSsETrstp-EEGMX46nc-m_jw00nzkaNZ
"""

from nltk.translate.bleu_score import sentence_bleu, corpus_bleu

# Prepare the reference sentences
reference1 = ['I', 'love', 'eating', 'ice', 'cream']
reference2 = ['I', 'enjoy', 'eating', 'ice', 'cream']

# Prepare the candidate sentence
translation = ['I', 'love', 'eating', 'ice', 'cream']

# Calculate the BLEU score for a single sentence
bleu_score = sentence_bleu([reference1, reference2], translation)
print("BLEU Score: ", bleu_score)

# Prepare the reference sentences and candidate sentences for multiple translations
references = [['I', 'love', 'eating', 'ice', 'cream'], ['He', 'enjoys', 'eating', 'cake']]
translations = [['I', 'love', 'eating', 'ice', 'cream'], ['He', 'likes', 'to', 'eat', 'cake']]
 
# Create a list of reference lists
references_list = [[ref] for ref in references]
 
# Calculate BLEU score for the entire corpus
bleu_score_corpus = corpus_bleu(references_list, translations)
print("Corpus BLEU Score: ", bleu_score_corpus)