Skip to content

Commit

Permalink
Fix Bleu Score smoothing function from taking log(0) (#2839)
Browse files Browse the repository at this point in the history
* Add conditional to prevent log(0)

* Add name to AUTHORS.md

* Add smoothin 4 test

Co-authored-by: Steven Bird <stevenbird1@gmail.com>
  • Loading branch information
Robby Horvath and stevenbird committed Oct 14, 2021
1 parent e7a1f31 commit c692b0d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
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

0 comments on commit c692b0d

Please sign in to comment.