首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Pandas中运行NLTK sentence_bleu

在Pandas中运行NLTK sentence_bleu
EN

Stack Overflow用户
提问于 2018-11-28 07:26:05
回答 1查看 405关注 0票数 1

我正在尝试将sentence_bleu应用于Pandas中的一个专栏,以评估一些机器翻译的质量。但是它输出的分数是不正确的。有人能看到我的错误吗?

代码语言:javascript
复制
import pandas as pd
from nltk.translate.bleu_score import sentence_bleu

translations = {
    'reference': [['this', 'is', 'a', 'test'],['this', 'is', 'a', 'test'],['this', 'is', 'a', 'test']],
    'candidate': [['this', 'is', 'a', 'test'],['this', 'is', 'not','a', 'quiz'],['I', 'like', 'kitties', '.']]
}
df = pd.DataFrame(translations)

df['BLEU'] = df.apply(lambda row: sentence_bleu(row['reference'],row['candidate']), axis=1)
df

它输出以下内容:

代码语言:javascript
复制
Index   reference   candidate   BLEU
0   [this, is, a, test] [this, is, a, test] 1.288230e-231
1   [this, is, a, test] [this, is, not, a, quiz]    1.218332e-231
2   [this, is, a, test] [I, like, kitties, .]   0.000000e+00

行0应等于1.0,行1应小于1.0。大概在0.9左右。我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2019-06-15 20:12:26

您当前正在比较列表中的字符串。由于这些字符串只包含单个单词,因此得分将所有n>1的n-gram直接评分为0。

相反,您希望引用是['this is a test'] (基本事实引用的列表),候选者是'this is a test' (单个候选者)。

代码语言:javascript
复制
from nltk.translate.bleu_score import sentence_bleu

translations = {
    'reference': [['this is a test'],['this is a test'],['this is a test']],
    'candidate': ['this is a test','this is not a test','I like kitties']
}
df = pd.DataFrame(translations)

df['BLEU'] = df.apply(lambda row: sentence_bleu(row['reference'],row['candidate']), axis=1)
df

其结果是:

代码语言:javascript
复制
          reference           candidate           BLEU
0  [this is a test]      this is a test   1.000000e+00
1  [this is a test]  this is not a test   7.037906e-01
2  [this is a test]      I like kitties  6.830097e-155
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53509788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档