我正在尝试使用 NLTK 计算两个字符串之间的 BLEU 分数,如下所示:
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)))
运行它会导致以下错误:
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)
如果我替换hypothesis = ['Dee']
为hypothesis = ['Deee']
,错误消息就会消失。为什么?
我的系统:
- NLTK 版本:3.2.1。
- 蟒蛇 2.7.11 x64