File size: 1,598 Bytes
97b4774
 
eb3a90a
15e460d
97b4774
 
 
 
 
 
 
 
 
 
59eebb1
97b4774
 
 
eb3a90a
2379bb1
 
 
 
97b4774
15e460d
a1d1610
97b4774
 
 
 
59eebb1
97b4774
 
a1d1610
 
 
 
97b4774
 
2b03d4d
 
 
97b4774
 
 
fcc2240
97b4774
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Copied from https://github.com/huggingface/datasets/blob/76bb45964df1e62d1411b0a9e9fc673e9a791c9a/metrics/sacrebleu/sacrebleu.py

from copy import deepcopy
from sacrebleu.metrics import BLEU


def compute_bleu(
    predictions,
    references,
    smooth_method="exp",
    smooth_value=None,
    force=False,
    lowercase=False,
    tokenize=None,
    effective_order=False,
):
    references_per_prediction = len(references[0])
    if any(len(refs) != references_per_prediction for refs in references):
        references = deepcopy(references)
        max_references_per_prediction = max(len(refs) for refs in references)
        for refs in references:
            refs.extend([None] * (max_references_per_prediction - len(refs)))

    transformed_references = [[refs[i] for refs in references] for i in range(references_per_prediction)]

    bleu = BLEU(
        smooth_method=smooth_method,
        smooth_value=smooth_value,
        force=force,
        lowercase=lowercase,
        effective_order=effective_order,
        **(dict(tokenize=tokenize) if tokenize else {}),
    )
    output = bleu.corpus_score(
        predictions,
        transformed_references,
    )
    output_dict = {
        "score": output.score,
        **{f"counts-{i+1}": round(p, 4) for i, p in enumerate(output.counts)},
        **{f"totals-{i+1}": round(p, 4) for i, p in enumerate(output.totals)},
        **{f"precision-{i+1}": round(p, 4) for i, p in enumerate(output.precisions)},
        "bp": output.bp,
        "sys_len": output.sys_len,
        "ref_len": output.ref_len,
    }
    return output_dict