Professional Documents
Culture Documents
Bleu
Bleu
Where:
BP (Brevity Penalty) is a penalty term that adjusts the score
for translations that are shorter than the reference
translations. It is calculated as min(1, (reference_length /
translated_length)), where reference_length is the total
number of words in the reference translations, and
translated_length is the total number of words in the
machine-generated translation.
pn is the precision of n-grams, which is calculated as the
number of n-grams that appear in both the machine-
generated translation and the reference translations
divided by the total number of n-grams in the machine-
generated translation.
Sample code
import openai
from nltk.translate.bleu_score import sentence_bleu,
SmoothingFunction
def generate_translation(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful
assistant."},
{"role": "user", "content": prompt},
]
)
return response['choices'][0]['message']['content'].strip()
def main():
# Example source sentences
reference = [
'this is a dog'.split(),
'it is dog'.split(),
'dog it is'.split(),
'a dog, it is'.split()
]
if __name__ == "__main__":
main()
Output:
Links:
1. https://github.com/y33-j3T/Coursera-Deep-Learning/
blob/master/Natural%20Language%20Processing
%20with%20Attention%20Models/Week%201%20-
%20Neural%20Machine%20Translation/
C4_W1_Ungraded_Lab_Bleu_Score.ipynb
2. https://machinelearningmastery.com/calculate-bleu-
score-for-text-python/
3.