"ZeroDivisionError: Fraction(0, 0)" when computing the BLEU with NLTK

610 views Asked by At

I am trying to compute the BLEU score between two strings using NLTK as follows:

from nltk import bleu_score
reference = ['The moon is very bright']
hypothesis = ['Dee']
print('bleu_score.corpus_bleu(reference, hypothesis): {0}'.
      format(bleu_score.corpus_bleu(reference, hypothesis)))

Running it causes the following error:

Traceback (most recent call last):
  File "C:\Users\Francky\Documents\GitHub\nlp\tests\SEbleu.py", line 28, in <module>
    format(bleu_score.corpus_bleu(reference, hypothesis)))
  File "C:\Anaconda\lib\site-packages\nltk\translate\bleu_score.py", line 146, in corpus_bleu
    p_i = modified_precision(references, hypothesis, i)
  File "C:\Anaconda\lib\site-packages\nltk\translate\bleu_score.py", line 287, in modified_precision
    return Fraction(numerator, denominator, _normalize=False)  
  File "C:\Anaconda\lib\site-packages\nltk\compat.py", line 700, in __new__
    cls = super(Fraction, cls).__new__(cls, numerator, denominator)
  File "C:\Anaconda\lib\fractions.py", line 162, in __new__
    raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
ZeroDivisionError: Fraction(0, 0)

If I replace hypothesis = ['Dee'] with hypothesis = ['Deee'], the error message disappears. Why?

My system:

  • NLTK version: 3.2.1.
  • python 2.7.11 x64
0

There are 0 answers