You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用NLTK的GLEU比对完全相同句子未得到1.0分的原因是什么

NLTK GLEU分数异常问题解决

核心错误原因

  • 函数传参不符合要求:sentence_gleu第一个入参要求为多参考句子的集合列表,即使只有1条参考句子,也需要将分词后的参考句子外层额外套一层列表。
  • 原代码直接传入了单条参考句子的分词列表,函数会误将列表内的每个单词识别为一条独立的参考句子,匹配度极低,因此输出异常低分。

修正后代码

from nltk.translate.gleu_score import sentence_gleu

hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', 'ensures', 'that', 'the', 'military', 'always', 'obeys', 'the', 'commands', 'of', 'the', 'party']
# 单参考句子外层增加列表嵌套
ref1a = [['It', 'is', 'a', 'guide', 'to', 'action', 'which', 'ensures', 'that', 'the', 'military', 'always', 'obeys', 'the', 'commands', 'of', 'the', 'party']]

gleu_score = sentence_gleu(ref1a, hyp1)
print(gleu_score)

运行结果

修正后输出为1.0,符合满分预期。

内容的提问来源于stack exchange,提问作者slow_war

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 06:24:03