我遵循bleu评分的原始代码如下:
from nltk.translate.bleu_score import sentence_bleu
reference = [['this', 'is', 'a', 'test'], ['this', 'is' 'test']]
candidate = ['this', 'is', 'a', 'test']
score = sentence_bleu(reference, candidate)
print(score)
代码运行良好。但是我试图通过导入csv文件作为以下代码来更改reference
和{
但它遇到了一个错误:
Traceback (most recent call last):
File "nltk-bleu-score.py", line 56, in <module>
score = sentence_bleu(reference, candidate)
File "C:\Users\Fachri\Anaconda3\lib\site-packages\nltk\translate\bleu_score.py", line 89, in sentence_bleu
emulate_multibleu)
File "C:\Users\Fachri\Anaconda3\lib\site-packages\nltk\translate\bleu_score.py", line 162, in corpus_bleu
p_i = modified_precision(references, hypothesis, i)
File "C:\Users\Fachri\Anaconda3\lib\site-packages\nltk\translate\bleu_score.py", line 292, in modified_precision
counts = Counter(ngrams(hypothesis, n)) if len(hypothesis) >= n else Counter()
File "C:\Users\Fachri\Anaconda3\lib\collections\__init__.py", line 535, in __init__
self.update(*args, **kwds)
File "C:\Users\Fachri\Anaconda3\lib\collections\__init__.py", line 622, in update
_count_elements(self, iterable)
TypeError: unhashable type: 'list'
然后,我检查reference
和{list
我搞不清是什么让这些名单与众不同。在
reference
和“candidate”的列表显示如下
Opening references file...
[['two', 'airplanes', 'are', 'waiting', 'on', 'the', 'tarmac'], ['Two', 'airplanes', 'parked', 'at', 'the', 'airport', '.']]
Opening candidates file...
[['An', 'airplane', 'sitting', 'on', 'the', 'tarmac', 'at', 'an', 'airport', 'with', 'another', 'plane', 'in', 'the', 'background', '.']]
hypothesis
的预期类型是list(str)
,来自{a1}:candidate
是一个list(list(str))
,可以这样计算bleu分:输出
^{pr2}$相关问题 更多 >
编程相关推荐