Commit 5d809f41 by Dmitrijs Milajevs

An attempt to fix #775 BLEU score returns 1 (perfect match) instead of zero

parent a410b109
...@@ -132,7 +132,13 @@ class BLEU(object): ...@@ -132,7 +132,13 @@ class BLEU(object):
references = [[r.lower() for r in reference] for reference in references] references = [[r.lower() for r in reference] for reference in references]
p_ns = (BLEU.modified_precision(candidate, references, i) for i, _ in enumerate(weights, start=1)) p_ns = (BLEU.modified_precision(candidate, references, i) for i, _ in enumerate(weights, start=1))
s = math.fsum(w * math.log(p_n) for w, p_n in zip(weights, p_ns) if p_n) p_ns_nonzero = list(filter(None, p_ns))
if not p_ns_nonzero:
# There is zero aliment, so the score is 0
return 0
s = math.fsum(w * math.log(p_n) for w, p_n in zip(weights, p_ns_nonzero))
bp = BLEU.brevity_penalty(candidate, references) bp = BLEU.brevity_penalty(candidate, references)
return bp * math.exp(s) return bp * math.exp(s)
......
==========
BLEU tests
==========
>>> from nltk.align.bleu import BLEU
If the candidate has no alignment to any of the references, the BLEU score is
0.
>>> BLEU.compute(
... 'John loves Mary'.split(),
... ['The candidate has no alignment to any of the references'.split()],
... [1],
... )
0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment