Shortcuts

BleuMetric

class BleuMetric(split_fn=<function BleuMetric.<lambda>>, ngram=4, smooth='no_smooth', average='macro', gather_result=True)[源代码]

计算 BLEU 得分.

\[\text{BLEU} = b_{p} \cdot \exp \left( \sum_{n=1}^{N} w_{n} \: \log p_{n} \right)\]

这里的 \(N\)n-gram 的阶数, \(b_{p}\) 是句子的简洁性惩罚, \(w_{n}\) 是加起来为一的正权重, 而 \(p_{n}\) 是修改过的 n-gram 精确度.

更多的细节可以查看 Papineni et al. 2002.

另外, 关于平滑 (smoothing) 的技术可以查看 Chen et al. 2014

result 中需要包含 predtarget 字段,例如:

result = {
    "pred": ["the the the the the the the", "cat cat cat cat cat cat cat"],
    "target": [["the cat is on the mat", "there is a cat on the mat"], 
    ["the cat is on the mat", "there is a cat on the mat"]]
}
参数:
  • ngram (int, default: 4) – n-gram 的阶数

  • smooth (str, default: 'no_smooth') – 是否使用平滑技术。可选值为 no_smooth, smooth1, nltk_smooth2smooth2. 默认为 no_smooth

  • average (str, default: 'macro') – 指定使用哪种类型的平均值 (宏平均或微平均)。更多细节可以查看 https://www.nltk.org/_modules/nltk/translate/bleu_score.html

  • gather_result (bool, default: True) – 是否对 DP 中不同进程的结果进行聚合。默认为 True

  • split_fn (callable, default: <function BleuMetric.<lambda> at 0x7f06c7bf5af0>) – 用于分割字符串的函数。默认以空格分割

例子:

from collie.metrics import BleuMetric

m = BleuMetric(ngram=4, smooth="smooth1")

result = {
    "pred": ["the the the the the the the"],
    "target": [["the cat is on the mat", "there is a cat on the mat"]]
}

m.update(result)

print(m.get_metric())