Commit 33355f4f by Steven Bird

Merge pull request #782 from dimazest/bleu_no_aligment_fix

Bleu no aligment fix
parents 085399ea 5d809f41
......@@ -132,7 +132,13 @@ class BLEU(object):
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))
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)
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