问题描述
我很好奇是否有人熟悉使用 NLTK's BLEU score calculation 和 SacreBLEU library 之间的区别。
特别是,我使用了两个库的句子 bleu 分数,在整个数据集上取平均值。两者给出了不同的结果:
>>> from nltk.translate import bleu_score
>>> from sacrebleu import sentence_bleu
>>> print(len(predictions))
256
>>> print(len(targets))
256
>>> prediction = "this is the first: the world's the world's the world's the \
... world's the world's the world's the world's the world's the world's the world \
... of the world of the world'"
...
>>> target = "al gore: so the alliance for climate change has launched two campaigns."
>>> print(bleu_score.sentence_bleu([target],prediction))
0.05422283394039736
>>> print(sentence_bleu(prediction,[target]).score)
0.0
>>> print(sacrebleu.corpus_bleu(predictions,[targets]).score)
0.678758518214081
>>> print(bleu_score.corpus_bleu([targets],[predictions]))
0
如您所见,存在许多令人困惑的不一致之处。我的 bleu 分数不可能是 67.8%,但也不应该是 0%(有很多重叠的 n-gram,比如“the”)。
如果有人能对此有所了解,我将不胜感激。谢谢。
解决方法
NLTK 和 SacreBLEU 使用不同的标记化规则,主要在于它们如何处理标点符号。 NLTK 使用自己的标记化,而 SacreBLEU 复制了 2002 年的原始 Perl 实现。NLTK 中的标记化规则可能更详细,但它们使数量与原始实现无法比拟。
你从 SacreBLEU 得到的语料库 BLEU 不是 67.8%,而是 0.67%——来自 SacreBLEU 的数字已经乘以 100,与 NLTK 不同。所以,我不会说分数之间存在巨大差异。
句子级别的 BLEU 可以使用不同的 smoothing techniques,以确保即使 3-gram 的 4-gram 精度为零,分数也会获得合理的值。但是,请注意,BLEU 作为句子级指标非常不可靠。