使用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
相关产品推荐
相关产品推荐

