Switch to unified view

a b/utils/pycocoevalcap/bleu/bleu.py
1
#!/usr/bin/env python
2
#
3
# File Name : bleu.py
4
#
5
# Description : Wrapper for BLEU scorer.
6
#
7
# Creation Date : 06-01-2015
8
# Last Modified : Thu 19 Mar 2015 09:13:28 PM PDT
9
# Authors : Hao Fang <hfang@uw.edu> and Tsung-Yi Lin <tl483@cornell.edu>
10
11
# Last modified : Wed 22 May 2019 08:10:00 PM EDT
12
# By Sabarish Sivanath
13
# To support Python 3
14
15
from .bleu_scorer import BleuScorer
16
17
18
class Bleu:
19
    def __init__(self, n=4):
20
        # default compute Blue score up to 4
21
        self._n = n
22
        self._hypo_for_image = {}
23
        self.ref_for_image = {}
24
25
    def compute_score(self, gts, res, score_option = 'closest', verbose = 1):
26
        '''
27
        Inputs:
28
            gts - ground truths
29
            res - predictions
30
            score_option - {shortest, closest, average}
31
            verbose - 1 or 0
32
        Outputs:
33
            Blue scores
34
        '''
35
        assert(gts.keys() == res.keys())
36
        imgIds = gts.keys()
37
38
        bleu_scorer = BleuScorer(n=self._n)
39
        for id in imgIds:
40
            hypo = res[id]
41
            ref = gts[id]
42
43
            # Sanity check.
44
            assert(type(hypo) is list)
45
            assert(len(hypo) == 1)
46
            assert(type(ref) is list)
47
            #assert(len(ref) >= 1)
48
49
            bleu_scorer += (hypo[0], ref)
50
51
        score, scores = bleu_scorer.compute_score(option = score_option, verbose =verbose)
52
53
        # return (bleu, bleu_info)
54
        return score, scores
55
56
    def method(self):
57
        return "Bleu"