python 代码如下
from nltk.translate.bleu_score import sentence_bleu
reference = [['a', 'close', 'up', 'picture', 'of', 'a', 'brown', "bear's", 'face'], ['A', 'large', 'bear', 'that', 'is', 'sitting', 'on', 'grass', ''], ['Closeup', 'of', 'a', 'brown', 'bear', 'sitting', 'in', 'a', 'grassy', 'area'], ['The', 'large', 'brown', 'bear', 'has', 'a', 'black', 'nose'], ['A', 'big', 'burly', 'grizzly', 'bear', 'is', 'show', 'with', 'grass', 'in', 'the', 'background']]
candidate = ['a', 'large', 'brown', 'bear', 'standing', 'on', 'top', 'of', 'a', 'grass', 'covered', 'field']
print ('Individual 1-gram: %f' % sentence_bleu (reference, candidate, weights=(1, 0, 0, 0)))
print ('Individual 2-gram: %f' % sentence_bleu (reference, candidate, weights=(0, 1, 0, 0)))
print ('Individual 3-gram: %f' % sentence_bleu (reference, candidate, weights=(0, 0, 1, 0)))
print ('Individual 4-gram: %f' % sentence_bleu (reference, candidate, weights=(0, 0, 0, 1)))
得到输出为
UserWarning:
Corpus/Sentence contains 0 counts of 4-gram overlaps.
BLEU scores might be undesirable; use SmoothingFunction ().
Individual 1-gram: 0.666667
Individual 2-gram: 0.272727
Individual 3-gram: 0.100000
Individual 4-gram: 1.000000
我不懂这里 4-gram 为什么会报错,并且是否 1-gram 得分就是 BLEU-1 得分,4-gram 就是 BLEU-4 得分呢?
希望懂的人能帮帮我,谢谢大家啦!
提问人 StormshadowRay,2018-4-22 21:23:50