|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""BLEU Score.ipynb |
| 3 | +
|
| 4 | +Automatically generated by Colaboratory. |
| 5 | +
|
| 6 | +Original file is located at |
| 7 | + https://colab.research.google.com/drive/1dSsETrstp-EEGMX46nc-m_jw00nzkaNZ |
| 8 | +""" |
| 9 | + |
| 10 | +from nltk.translate.bleu_score import sentence_bleu, corpus_bleu |
| 11 | + |
| 12 | +# Prepare the reference sentences |
| 13 | +reference1 = ['I', 'love', 'eating', 'ice', 'cream'] |
| 14 | +reference2 = ['I', 'enjoy', 'eating', 'ice', 'cream'] |
| 15 | + |
| 16 | +# Prepare the candidate sentence |
| 17 | +translation = ['I', 'love', 'eating', 'ice', 'cream'] |
| 18 | + |
| 19 | +# Calculate the BLEU score for a single sentence |
| 20 | +bleu_score = sentence_bleu([reference1, reference2], translation) |
| 21 | +print("BLEU Score: ", bleu_score) |
| 22 | + |
| 23 | +# Prepare the reference sentences and candidate sentences for multiple translations |
| 24 | +references = [['I', 'love', 'eating', 'ice', 'cream'], ['He', 'enjoys', 'eating', 'cake']] |
| 25 | +translations = [['I', 'love', 'eating', 'ice', 'cream'], ['He', 'likes', 'to', 'eat', 'cake']] |
| 26 | + |
| 27 | +# Create a list of reference lists |
| 28 | +references_list = [[ref] for ref in references] |
| 29 | + |
| 30 | +# Calculate BLEU score for the entire corpus |
| 31 | +bleu_score_corpus = corpus_bleu(references_list, translations) |
| 32 | +print("Corpus BLEU Score: ", bleu_score_corpus) |
| 33 | + |
0 commit comments