![](https://crypto4nerd.com/wp-content/uploads/2023/10/0GDa14E97Lmcoimm2-1024x1552.jpeg)
Calculating BLEU Score with NLTK
Now, let’s calculate BLEU scores for a sample sentence. Assume we have a reference sentence “this is a simple example,” and a candidate sentence “this is an example.” We’ll calculate both the sentence-level BLEU score and the corpus-level BLEU score.
python
from nltk.translate.bleu_score import sentence_bleu, corpus_bleu# Reference and candidate sentences
reference = “this is a simple example”.split()
candidate = “this is an example”.split()
# Calculate BLEU score for a single sentence
bleu_score = sentence_bleu([reference], candidate)
print(“BLEU Score (sentence):”, bleu_score)
# Calculate BLEU score for a corpus (list of candidate sentences)
corpus_reference = [[“this is a simple example”.split()]]
corpus_candidate = [“this is an example”.split()]
corpus_bleu_score = corpus_bleu(corpus_reference, corpus_candidate)
print(“BLEU Score (corpus):”, corpus_bleu_score)
In this code, we calculate the BLEU score for a single sentence using sentence_bleu
and for a corpus of sentences using corpus_bleu
. You can replace the reference and candidate sentences with your own data for evaluation.