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

使用NLTK的RIBES比对两个相同句子报除零错误是什么原因?

报错原因
  • 核心原因是调用sentence_ribes接口时的传参格式不符合要求。
    NLTK的sentence_ribes第一个入参为参考译文集合,需要传入由所有参考译文的单词列表组成的嵌套列表,即使只有1条参考译文也需要套一层列表。你直接把单条参考的单词列表ref1a传给了第一个参数,函数会把ref1a里的每个单词识别为一条独立的参考译文,单条参考长度仅为1。
  • 计算Kendall Tau系数时,序列长度小于2的情况下,总可能配对数的计算公式为n*(n-1)/2,当n=1时计算结果为0,直接触发除零错误。
修正方案

把参考译文套一层列表再传入即可,修正后代码如下:

from nltk.translate.ribes_score import sentence_ribes

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']
# 参考译文集合套一层列表
ribes_score = sentence_ribes([ref1a], hyp1)

print(ribes_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 05:57:04