2021SC@SDUSC
我们继续分析evaluate()函数的剩余的关键代码。
final_scores = self.score(ref, hypo)
这里的final_scores保存的是最终的评价得分,根据上篇博客提到的三种评价方法。这里调用了score函数,它是Evaluate的类函数,代码如下:
def score(self, ref, hypo):
final_scores = {}
for scorer, method in self.scorers:
score, scores = scorer.compute_score(ref, hypo)
if type(score) == list:
for m, s in zip(method, score):
final_scores[m] = s
else:
final_scores[method] = score
return final_scores
参数ref和hypo是两个集合,循环进行评价打分,调用compute_score函数,在三个评价方法里都有compute_score,让我们先看BLEU:
def compute_score(self, gts, res):
bleu_scorer = BleuScorer(n=self._n)
for id in sorted(gts.keys())