BLEU 算法实际上是在判断两个句子的相似程度。要想知道一个句子翻译前后的意思是否一致,直接的方法就是将这个句子的标准人工翻译与机器翻译的结果进行比较。如果它们很相似,那就说明我的翻译很成功。
BLEU 的具体计算方法是以 N-gram 匹配为基础的,并且运用了几何平均值。其具体过程如下:
对于每个句子,要把机器翻译结果里的每个 N-gram 和参考翻译结果里的所有 N-gram 进行对比,接着算出匹配的 N-gram 的数量。
计算每个 N-gram 的权重,比如,BLEU-4 会给四元组的匹配结果赋予更重的权重。
计算 BLEU 得分的几何平均值,也就是将所有权重的 N-gram 匹配数量进行相乘,然后再开 N 次方。
BLEU 分数的范围在 0 到 1 之间。它表示机器翻译结果和参考翻译结果的相似度。一般来讲,BLEU 分数越高,就意味着机器翻译结果越接近参考翻译结果。
需要注意的是,BLEU 分数仅是一种机器翻译评估指标。它不能完全代表机器翻译的质量,因为无法捕捉翻译结果在语法、流畅度以及连贯性等方面的问题。所以,BLEU 分数应当与其他评估指标一同使用,以对机器翻译的质量进行评估。
这是 0.6.2版本
0.8.2就不行了
<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'> <pre><code class="language-python">使用 torchmetrics 库中的 BLEUScore 类。
- 具体的拆分方式是按照空格进行拆分。
#translate_corpus是list
#list中的每个元素是一个句子
#有多个可供选择
reference_corpus 包含两个子列表,一个子列表是['there', 'is', 'a', 'cat', 'on', 'the','mat'],另一个子列表是['a', 'cat', 'is', 'on', 'the','mat']
#reference_corpuss是list
#list中的每个元素是一个句子
bleu_1 等于 BLEUScore,其中 n_gram 为 1,默认情况下 n_gram 等于 4。
bleu_2 = BLEUScore(n_gram=2)
bleu_3 = BLEUScore(n_gram=3)
bleu_4 = BLEUScore(n_gram=4)
调用 bleu_1 函数,并传入 reference_corpus 和 translate_corpus 作为参数,然后进行打印操作。
调用 bleu_2 函数,并传入 reference_corpus 和 translate_corpus 作为参数,然后进行打印操作。
调用 bleu_3 函数,传入 reference_corpus 和 translate_corpus 作为参数,然后进行打印操作。
调用函数 bleu_4,传入 reference_corpus 和 translate_corpus 作为参数,然后进行打印操作。
</code></pre></p>
而如果是一样的话
<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'> <pre><code class="language-python">from torchmetrics import BLEUScore
translate_corpus = ['the cat is on the mat'.split()]
#translate_corpus是list
#list中的每个元素是一个句子
#有多个可供选择
reference_corpus 包含两个子列表,一个子列表是['there is a cat on the mat']分割后的结果,另一个子列表是['a cat is on the mat']分割后的结果。
reference_corpus 包含一个元素,该元素是一个列表,列表中的元素是字符串 'the cat is on the mat' 被分割后的结果,即一个单词列表。
#reference_corpuss是list
#list中的每个元素是一个句子
bleu_1 = BLEUScore(n_gram=1) #默认n_gram=4
bleu_2 = BLEUScore(n_gram=2)
bleu_3 = BLEUScore(n_gram=3)
bleu_4 = BLEUScore(n_gram=4)
print(bleu_1(reference_corpus, translate_corpus))
print(bleu_2(reference_corpus, translate_corpus))
print(bleu_3(reference_corpus, translate_corpus))
print(bleu_4(reference_corpus, translate_corpus))
</code></pre></p>
就全是1
如果是多条文本一起算,那就是
<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'> <pre><code class="language-python">from torchmetrics import BLEUScore
translate_corpus 包含两个部分,一个是 'the cat is on the mat'.split(),另一个也是 'the cat is on the mat'.split()
#translate_corpus是list
#list中的每个元素是一个句子
#有多个可供选择
# reference_corpus = [['there is a cat on the mat'.split(), 'a cat is on the mat'.split()]]
reference_corpus 包含两个部分,一个是['the', 'cat', 'is', 'on', 'the', 'mat'],另一个也是['the', 'cat', 'is', 'on', 'the', 'mat']
#reference_corpuss是list
bleu_1 = BLEUScore(n_gram=1) #默认n_gram=4
bleu_2 = BLEUScore(n_gram=2)
bleu_3 = BLEUScore(n_gram=3)
bleu_4 = BLEUScore(n_gram=4)
print(bleu_1(reference_corpus, translate_corpus))
print(bleu_2(reference_corpus, translate_corpus))
print(bleu_3(reference_corpus, translate_corpus))
print(bleu_4(reference_corpus, translate_corpus))
</code></pre></p>
用.bleu
<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'> <pre><code class="language-python">使用 pycocoevalcap.bleu.bleu 中的 Bleu 类
# 加载参考文本
references = {
}
# 加载生成文本
hypotheses = {
}
# 创建Bleu对象
bleu_scorer = Bleu(n=4)
# 计算BLEU分数
计算出的 bleu 分数,其中一个是 bleu_scores,另一个暂未提及,是通过 bleu_scorer.compute_score 函数,以 references 和 hypotheses 作为参数计算得出的。
# 打印BLEU分数
打印("BLEU 分数:", bleu_scores)
</code></pre></p>
<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'> <pre><code class="language-python">import nltk
使用 nltk 中的 translate.bleu_score 模块的 sentence_bleu 函数。
# 参考翻译结果
ref 等于 ['this', 'is', 'a', 'test'] 。
# 机器翻译结果
'this'、'is'、'a'、'test' 和 'too' 都在 mt 之中。
# 计算BLEU分数
计算得到的分数等于句子的 BLEU 分数,这个 BLEU 分数是通过将参考文本(ref)与机器翻译文本(mt)进行比较计算得出的。
print(score)
</code></pre></p>
浅谈用计算文本BLEU分数 - 云+社区 - 腾讯云 |