Skip to content

Commit 8f21ae7

Browse files
committed
add bleu score tutorial
1 parent 891eeb3 commit 8f21ae7

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5959
- [Named Entity Recognition using Transformers and Spacy in Python](https://www.thepythoncode.com/article/named-entity-recognition-using-transformers-and-spacy). ([code](machine-learning/nlp/named-entity-recognition))
6060
- [Tokenization, Stemming, and Lemmatization in Python](https://www.thepythoncode.com/article/tokenization-stemming-and-lemmatization-in-python). ([code](machine-learning/nlp/tokenization-stemming-lemmatization))
6161
- [How to Fine Tune BERT for Semantic Textual Similarity using Transformers in Python](https://www.thepythoncode.com/article/finetune-bert-for-semantic-textual-similarity-in-python). ([code](machine-learning/nlp/semantic-textual-similarity))
62+
- [How to Calculate the BLEU Score in Python](https://www.thepythoncode.com/article/bleu-score-in-python). ([code](machine-learning/nlp/bleu-score))
6263
- ### [Computer Vision](https://www.thepythoncode.com/topic/computer-vision)
6364
- [How to Detect Human Faces in Python using OpenCV](https://www.thepythoncode.com/article/detect-faces-opencv-python). ([code](machine-learning/face_detection))
6465
- [How to Make an Image Classifier in Python using TensorFlow and Keras](https://www.thepythoncode.com/article/image-classification-keras-python). ([code](machine-learning/image-classifier))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Calculate the BLEU Score in Python](https://www.thepythoncode.com/article/bleu-score-in-python)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ntlk

0 commit comments

Comments
 (0)