Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bleu Score smoothing function from taking log(0) #2839

Merged
merged 6 commits into from Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Expand Up @@ -280,6 +280,7 @@
- Hiroki Teranishi <https://github.com/chantera>
- Ruben Cartuyvels <https://github.com/rubencart>
- Dalton Pearson <https://github.com/daltonpearson>
- Robby Horvath <https://github.com/robbyhorvath>
- Gavish Poddar <https://github.com/gavishpoddar>
- Saibo Geng <https://github.com/Saibo-creator>
- Ahmet Yildirim <https://github.com/RnDevelover>
Expand Down
10 changes: 10 additions & 0 deletions nltk/test/unit/translate/test_bleu.py
Expand Up @@ -181,6 +181,16 @@ def test_empty_hypothesis(self):
hypothesis = []
assert sentence_bleu(references, hypothesis) == 0

def test_length_one_hypothesis(self):
# Test case where there's hypothesis is of length 1 in Smoothing method 4.
references = ["The candidate has no alignment to any of the references".split()]
hypothesis = ["Foo"]
method4 = SmoothingFunction().method4
try:
sentence_bleu(references, hypothesis, smoothing_function=method4)
except ValueError:
pass # unittest.TestCase.assertWarns is only supported in Python >= 3.2.

def test_empty_references(self):
# Test case where there's reference is empty.
references = [[]]
Expand Down
2 changes: 1 addition & 1 deletion nltk/translate/bleu_score.py
Expand Up @@ -215,7 +215,7 @@ def corpus_bleu(
p_n = smoothing_function(
p_n, references=references, hypothesis=hypothesis, hyp_len=hyp_lengths
)
s = (w_i * math.log(p_i) for w_i, p_i in zip(weights, p_n))
s = (w_i * math.log(p_i) for w_i, p_i in zip(weights, p_n) if p_i > 0)
s = bp * math.exp(math.fsum(s))
return s

Expand Down