File size: 10,208 Bytes
417f39c
 
cb18316
80d7919
64096c9
7004eb5
64096c9
faf189f
417f39c
 
 
 
 
 
 
 
 
 
 
 
 
bac1189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c418edf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417f39c
 
 
7004eb5
417f39c
7004eb5
417f39c
7004eb5
bac1189
7004eb5
bac1189
7004eb5
bac1189
7004eb5
edd993d
7004eb5
417f39c
 
 
bac1189
417f39c
 
 
 
 
c418edf
417f39c
bac1189
417f39c
 
 
 
bac1189
 
 
 
 
 
 
 
edd993d
 
 
c418edf
80d7919
c418edf
 
 
 
 
64096c9
7004eb5
9ddbb93
 
 
 
 
 
bac1189
417f39c
 
64096c9
7004eb5
64096c9
7004eb5
64096c9
 
 
c418edf
553023f
417f39c
fff21b1
417f39c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64096c9
 
 
 
 
 
 
e315877
cb18316
 
7004eb5
64096c9
 
faf189f
64096c9
417f39c
cb18316
 
 
 
 
 
 
faf189f
 
 
 
 
 
 
 
64096c9
cb18316
faf189f
 
 
 
417f39c
64096c9
 
1b5f36c
 
faf189f
1b5f36c
 
 
faf189f
 
 
 
1b5f36c
c418edf
64096c9
cb18316
417f39c
 
80d7919
64096c9
cb18316
80d7919
c418edf
64096c9
 
 
 
 
 
 
 
 
 
 
faf189f
64096c9
c418edf
64096c9
 
cb18316
64096c9
 
80d7919
c418edf
 
 
 
80d7919
64096c9
 
c418edf
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import datasets
import evaluate
import nltk
import numpy as np
import spacy
import torch
from alignscore import AlignScore
from transformers import AutoTokenizer

_CITATION = """\
@inproceedings{lin-2004-rouge,
    title = "{ROUGE}: A Package for Automatic Evaluation of Summaries",
    author = "Lin, Chin-Yew",
    booktitle = "Text Summarization Branches Out",
    month = jul,
    year = "2004",
    address = "Barcelona, Spain",
    publisher = "Association for Computational Linguistics",
    url = "https://www.aclweb.org/anthology/W04-1013",
    pages = "74--81",
}
\
@INPROCEEDINGS{Papineni02bleu:a,
    author = {Kishore Papineni and Salim Roukos and Todd Ward and Wei-jing Zhu},
    title = {BLEU: a Method for Automatic Evaluation of Machine Translation},
    booktitle = {},
    year = {2002},
    pages = {311--318}
}
@inproceedings{lin-och-2004-orange,
    title = "{ORANGE}: a Method for Evaluating Automatic Evaluation Metrics for Machine Translation",
    author = "Lin, Chin-Yew  and
      Och, Franz Josef",
    booktitle = "{COLING} 2004: Proceedings of the 20th International Conference on Computational Linguistics",
    month = "aug 23{--}aug 27",
    year = "2004",
    address = "Geneva, Switzerland",
    publisher = "COLING",
    url = "https://www.aclweb.org/anthology/C04-1072",
    pages = "501--507",
\
@inproceedings{bert-score,
  title={BERTScore: Evaluating Text Generation with BERT},
  author={Tianyi Zhang* and Varsha Kishore* and Felix Wu* and Kilian Q. Weinberger and Yoav Artzi},
  booktitle={International Conference on Learning Representations},
  year={2020},
  url={https://openreview.net/forum?id=SkeHuCVFDr}
\
@inproceedings{bleurt,
  title={BLEURT: Learning Robust Metrics for Text Generation},
  author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh},
  booktitle={ACL},
  year={2020},
  url={https://arxiv.org/abs/2004.04696}
}
"""

_DESCRIPTION = """\
This evaluator computes multiple metrics to assess the quality of generated text. These metrics are the following:

- **ROUGE**: a set of metrics and a software package used for evaluating automatic summarization and machine translation software in natural language processing. The metrics compare an automatically produced summary or translation against a reference or a set of references (human-produced) summary or translation. Note that ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters. This metrics is a wrapper around Google Research reimplementation of ROUGE: https://github.com/google-research/google-research/tree/master/rouge

- **BLEU**: evaluates the quality of text which has been machine-translated from one natural language to another. Quality is considered to be the correspondence between a machine's output and that of a human: "the closer a machine translation is to a professional human translation, the better it is" this is the central idea behind BLEU. BLEU was one of the first metrics to claim a high correlation with human judgements of quality, and remains one of the most popular automated and inexpensive metrics. Scores are calculated for individual translated segments—generally sentences—by comparing them with a set of good quality reference translations. Those scores are then averaged over the whole corpus to reach an estimate of the translation's overall quality. Neither intelligibility nor grammatical correctness are not taken into account.

- **Exact Match**: rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.

- **BERTScore**: leverages the pre-trained contextual embeddings from BERT and matches words in candidate and reference sentences by cosine similarity. It has been shown to correlate with human judgment on sentence-level and system-level evaluation. Moreover, BERTScore computes precision, recall, and F1 measure, which can be useful for evaluating different language generation tasks. See the project's README at https://github.com/Tiiiger/bert_score#readme for more information.

- **AlignScore**: evaluates whether all the information in a piece of text *b* is contained in another piece of text *a* and *b* does not contradict *a*, by leveraging an information alignment function learnt through RoBERTa models. See https://github.com/yuh-zha/AlignScore for more information.

- **ChrF and ChrF++**: are two MT evaluation metrics. They both use the F-score statistic for character n-gram matches, and ChrF++ adds word n-grams as well which correlates more strongly with direct assessment. We use the implementation that is already present in sacrebleu.
"""

_KWARGS_DESCRIPTION = """
Calculates average rouge and bleu scores for a list of hypotheses and references
Args:
    predictions: list of predictions to score. Each prediction
        should be a string with tokens separated by spaces.
    references: list of reference for each prediction. Each
        reference should be a string with tokens separated by spaces.

Returns:
ROUGE:{
    rouge1: rouge_1 (precision, recall, f1),
    rouge2: rouge_2 (precision, recall, f1),
    rougeL: rouge_l (precision, recall, f1),
    rougeLsum: rouge_lsum (precision, recall, f1)
},
BLEU:{
    'bleu': bleu score,
    'precisions': geometric mean of n-gram precisions,
    'brevity_penalty': brevity penalty,
    'length_ratio': ratio of lengths,
    'translation_length': translation_length,
    'reference_length': reference_length
},
EXACT_MATCH:{
    "exact_match": exact_match rate. Possible values are between 0.0 and 1.0, inclusive.
},
BERT_SCORE:{
    "precision": Precision.
    "recall": Recall.
    "f1": F1 score.
    "hashcode": Hashcode of the library.
},
AlignScore:{
    "score": mean align-scores using roberta-large as scorer
},
CHRF:{
    'score' (float): The chrF (chrF++) score,
    'char_order' (int): The character n-gram order,
    'word_order' (int): The word n-gram order. If equals to 2, the metric is referred to as chrF++,
    'beta' (int): Determine the importance of recall w.r.t precision
}
"""

ALIGNSCORE_ARGS = {
    "model": "roberta-large",
    "batch_size": 32,
    "ckpt_path": "https://huggingface.co/yzha/AlignScore/resolve/main/AlignScore-large.ckpt",
    "evaluation_mode": "nli_sp",
}


class GenerationEvaluator(evaluate.Metric):
    def _info(self):
        return evaluate.MetricInfo(
            description=_DESCRIPTION,
            citation=_CITATION,
            inputs_description=_KWARGS_DESCRIPTION,
            features=datasets.Features(
                {
                    "predictions": datasets.Value("string"),
                    "references": datasets.Value("string"),
                }
            ),
            codebase_urls=[
                "https://github.com/google-research/google-research/tree/master/rouge"
            ],
            reference_urls=[
                "https://en.wikipedia.org/wiki/ROUGE_(metric)",
                "https://github.com/google-research/google-research/tree/master/rouge",
            ],
        )

    def _download_and_prepare(self, dl_manager):
        # Download Spacy en_core_web_sm model for AlignScore
        try:
            spacy.load("en_core_web_sm")
        except OSError:
            spacy.cli.download("en_core_web_sm")

        # Download punkt for AlignScore
        nltk.download("punkt_tab")

        # Download AlignScore model and move to GPU if possible
        model_path = dl_manager.download(ALIGNSCORE_ARGS["ckpt_path"])
        ALIGNSCORE_ARGS["ckpt_path"] = model_path
        ALIGNSCORE_ARGS["device"] = "cuda:0" if torch.cuda.is_available() else "cpu"
        self.align_scorer = AlignScore(**ALIGNSCORE_ARGS)

        # Prepare scorers
        self.rouge_scorer = evaluate.load("rouge")
        self.bleu_scorer = evaluate.load("bleu")
        self.exact_match_scorer = evaluate.load("exact_match")
        self.bert_scorer = evaluate.load("bertscore")
        self.chrf_scorer = evaluate.load("chrf")

    def _compute(self, predictions, references, **eval_kwargs):
        tokenizer_name = eval_kwargs.pop("tokenizer_name", None)
        tokenizer = None

        if tokenizer_name is not None:
            tks = AutoTokenizer.from_pretrained(tokenizer_name)
            tokenizer = tks.tokenize

        # Compute ROUGE
        rouge_results = self.rouge_scorer.compute(
            predictions=predictions,
            references=references,
            tokenizer=tokenizer,
            **eval_kwargs
        )

        # Compute BLEU
        if tokenizer is None:
            bleu_results = self.bleu_scorer.compute(
                predictions=predictions, references=references, **eval_kwargs
            )
        else:
            bleu_results = self.bleu_scorer.compute(
                predictions=predictions,
                references=references,
                tokenizer=tokenizer,
                **eval_kwargs
            )

        # Compute Exact Match
        exact_match_results = self.exact_match_scorer.compute(
            predictions=predictions, references=references
        )

        # Compute BERTScore
        bert_score_results = self.bert_scorer.compute(
            predictions=predictions, references=references, lang="en"
        )

        mean_precision = np.mean(bert_score_results["precision"])
        mean_recall = np.mean(bert_score_results["recall"])
        mean_f1 = np.mean(bert_score_results["f1"])

        bert_score_results["precision"] = round(mean_precision, 4)
        bert_score_results["recall"] = round(mean_recall, 4)
        bert_score_results["f1"] = round(mean_f1, 4)

        # Compute AlignScore
        align_score = round(
            np.mean(self.align_scorer.score(contexts=references, claims=predictions)),
            4,
        )

        # Compute CHRF
        chrf_results = self.chrf_scorer.compute(
            predictions=predictions, references=references
        )

        return {
            "ROUGE": rouge_results,
            "BLEU": bleu_results,
            "EXACT_MATCH": exact_match_results,
            "BERT_SCORE": bert_score_results,
            "CHRF": chrf_results,
            "ALIGN_SCORE": align_score,
        }