python - 我的 Bleu 分数与 nltk bleu 分数不同
问题描述
我试图从头开始计算 bleu 分数。
import numpy as np
reference = 'hello how are you i am good here'
output = 'hello baby are you i am fine here'
# calculate Brevity penalty
BP = 0
if len(reference) < len(output):
BP = 1
else:
BP = np.exp(1-(len(reference)/len(output)))
def Bleu(ref, pred):
count = []
clip_count = []
for i in range(1, len(pred)):
clp = 0
cp = 0
start = set()
for j in range(len(pred)):
if j+i >len(pred):
continue
goal = pred[j:i+j]
sum = ''
for k in goal:
sum += k+' '
final = sum[:-1]
cp += 1
if final in ref:
if final in start:
continue
else:
clp += 1
start.add(final)
clip_count.append(clp)
count.append(cp)
return clip_count, count
clip, count = Bleu(reference, output.split())
pn = sum(np.divide(clip, count))
bleu = np.exp((1/len(clip)) * pn) * BP
print(bleu)
nltk python Bleu分数的输出
import nltk
t = 'hello how are you i am good here'
m = 'hello baby are you i am fine here'
hypothesis = m.split()
reference = t.split()
#there may be several references
BLEUscore = nltk.translate.bleu_score.sentence_bleu([reference], hypothesis)
print(BLEUscore)
我的问题是:
Q1。两个 bleu 分数不匹配,是什么错误?有人请帮帮我
Q2。如果我们尝试计算 Bleu 分数,则 bleu 分数的值将始终大于 1,因为 bleu 分数的公式为
Bleu score-> exp( 1/n * sum(precision n-gram) ) * Brevity_Penalty
如果 x 为 +ve,则指数函数 (e^x) 始终大于 1,并且精度 n-gram 的值始终为正。
那为什么一般文件都说 bleu score value 应该在 0 到 1 之间???
解决方案
推荐阅读
- sql - 为什么在数据库中收集统计数据称为资源消耗活动?
- flutter - 如何指定测试窗口?
- jquery - 如何将 bootstrap-colorpicker 附加到我的 laravel/blade 页面?
- css - 响应性- 如何使用 CSS 控制 PC 显示的比例和布局?
- swift - 快速为倒计时计时器制作一个重置按钮
- javascript - 打印时 PDF 不完整
- ios - 自定义 UITableViewCell 中的 TableView 未针对该自定义 cellviewtype 的所有单元格出现
- javascript - 解决 karma 中源文件的依赖关系解析
- kotlin - 如何模拟调用 kotlin.system.exitProcess
- reactjs - 强制 React 在一些视觉上重要的状态变化上快速重新渲染