metrics.py 9.55 KB
Newer Older
Baber Abbasi's avatar
Baber Abbasi committed
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
# MIT License
#
# Copyright (c) 2023 THU-KEG & Zhipu AI
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import re
import string
from collections import Counter
26
from typing import Union
Baber Abbasi's avatar
Baber Abbasi committed
27
28
29
30
31
32
33

try:
    import jieba
    from fuzzywuzzy import fuzz
    from rouge import Rouge
except ImportError:
    raise ImportError(
wackey's avatar
wackey committed
34
        'Please install the required dependencies for this task with `pip install lm_eval["longbench"] or `pip install jieba fuzzywuzzy rouge`'
Baber Abbasi's avatar
Baber Abbasi committed
35
36
    )

37
# taken and slightly modified from https://github.com/THUDM/LongBench
Baber Abbasi's avatar
Baber Abbasi committed
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


def normalize_answer(s: str) -> str:
    """Lower text and remove punctuation, articles and extra whitespace."""

    def remove_articles(text):
        return re.sub(r"\b(a|an|the)\b", " ", text)

    def white_space_fix(text):
        return " ".join(text.split())

    def remove_punc(text):
        exclude = set(string.punctuation)
        return "".join(ch for ch in text if ch not in exclude)

    def lower(text):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(s))))


def normalize_zh_answer(s: str) -> str:
    """Lower text and remove punctuation, extra whitespace."""

    def white_space_fix(text):
        return "".join(text.split())

    def remove_punc(text):
        cn_punctuation = "!?。。"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏."
        all_punctuation = set(string.punctuation + cn_punctuation)
        return "".join(ch for ch in text if ch not in all_punctuation)

    def lower(text):
        return text.lower()

    return white_space_fix(remove_punc(lower(s)))


76
def count_score(prediction: str, ground_truth: str, **kwargs):
Baber Abbasi's avatar
Baber Abbasi committed
77
78
79
80
81
82
83
84
85
    numbers = re.findall(r"\d+", prediction)
    right_num = 0
    for number in numbers:
        if str(number) == str(ground_truth):
            right_num += 1
    final_score = 0.0 if len(numbers) == 0 else right_num / len(numbers)
    return float(final_score)


86
87
88
89
90
91
92
93
94
95
def get_count_score(doc: dict, results: list[str], **kwargs):
    output = 0.0
    prediction = results[0].strip()
    for ground_truth in doc["answers"]:
        score = count_score(prediction, ground_truth)
        output = max(score, output)
    return {"count_score": output}


def retrieval_score(prediction: str, ground_truth: str, **kwargs):
Baber Abbasi's avatar
Baber Abbasi committed
96
97
98
99
100
101
102
103
104
105
106
107
    pattern = r"Paragraph (\d+)"
    matches = re.findall(pattern, ground_truth)
    ground_truth_id = matches[0]
    numbers = re.findall(r"\d+", prediction)
    right_num = 0
    for number in numbers:
        if str(number) == str(ground_truth_id):
            right_num += 1
    final_score = 0.0 if len(numbers) == 0 else right_num / len(numbers)
    return float(final_score)


108
109
110
111
112
113
114
115
116
117
def get_retrieval_score(doc: dict, results: list[str], **kwargs):
    output = 0.0
    prediction = results[0].strip()
    for ground_truth in doc["answers"]:
        score = retrieval_score(prediction, ground_truth)
        output = max(score, output)
    return {"retrieval_score": output}


def retrieval_zh_score(prediction: str, ground_truth: str, **kwargs):
Baber Abbasi's avatar
Baber Abbasi committed
118
119
120
121
122
123
124
125
126
127
128
129
    pattern = r"段落(\d+)"
    matches = re.findall(pattern, ground_truth)
    ground_truth_id = matches[0]
    numbers = re.findall(r"\d+", prediction)
    right_num = 0
    for number in numbers:
        if str(number) == str(ground_truth_id):
            right_num += 1
    final_score = 0.0 if len(numbers) == 0 else right_num / len(numbers)
    return float(final_score)


130
131
132
133
134
135
136
137
138
139
def get_retrieval_zh_score(doc: dict, results: list[str], **kwargs):
    output = 0.0
    prediction = results[0].strip()
    for ground_truth in doc["answers"]:
        score = retrieval_zh_score(prediction, ground_truth)
        output = max(score, output)
    return {"retrieval_zh_score": output}


def code_sim_score(prediction: str, ground_truth: str, **kwargs):
Baber Abbasi's avatar
Baber Abbasi committed
140
141
142
143
144
145
146
147
148
    all_lines = prediction.lstrip("\n").split("\n")
    prediction = ""
    for line in all_lines:
        if ("`" not in line) and ("#" not in line) and ("//" not in line):
            prediction = line
            break
    return fuzz.ratio(prediction, ground_truth) / 100


149
150
151
152
153
154
155
156
157
158
def get_code_sim_score(doc: dict, results: list[str], **kwargs):
    output = 0.0
    prediction = results[0]  ## important! do not strip the prediction!
    for ground_truth in doc["answers"]:
        score = code_sim_score(prediction, ground_truth)
        output = max(score, output)
    return {"code_sim_score": output}


def classification_score(prediction: str, ground_truth: str, **kwargs):
Baber Abbasi's avatar
Baber Abbasi committed
159
    em_match_list = []
160
    all_classes = kwargs["all_classes"]
Baber Abbasi's avatar
Baber Abbasi committed
161
162
163
164
165
166
167
168
169
170
    for class_name in all_classes:
        if class_name in prediction:
            em_match_list.append(class_name)
    for match_term in em_match_list:
        if match_term in ground_truth and match_term != ground_truth:
            em_match_list.remove(match_term)
    if ground_truth in em_match_list:
        score = 1.0 / len(em_match_list)
    else:
        score = 0.0
171
172
173
174
175
176
177
178
179
180
181
182
    return score


def get_classification_score(doc: dict, results: list[str]) -> dict:
    output = 0.0
    prediction = results[0].strip()
    for ground_truth in doc["answers"]:
        score = classification_score(
            prediction, ground_truth, all_classes=doc["all_classes"]
        )
        output = max(score, output)
    return {"classification_score": output}
Baber Abbasi's avatar
Baber Abbasi committed
183
184


185
def rouge_score(predictions: str, ground_truth: str, **kwargs) -> float:
Baber Abbasi's avatar
Baber Abbasi committed
186
187
188
    global rouge
    if "rouge" not in globals():
        rouge = Rouge()
Baber Abbasi's avatar
Baber Abbasi committed
189
    try:
190
        scores = rouge.get_scores([predictions], [ground_truth], avg=True)
Baber Abbasi's avatar
Baber Abbasi committed
191
192
193
194
195
196
        # ruff: noqa
    except:
        return 0.0
    return scores["rouge-l"]["f"]


197
198
199
200
201
202
203
204
205
206
def get_rouge_score(doc: dict, results: list[str], **kwargs):
    output = 0.0
    prediction = results[0].strip()
    for ground_truth in doc["answers"]:
        score = rouge_score(prediction, ground_truth)
        output = max(score, output)
    return {"rouge_score": output}


def rouge_zh_score(prediction: str, ground_truth: str, **kwargs):
Baber Abbasi's avatar
Baber Abbasi committed
207
208
    prediction = " ".join(list(jieba.cut(prediction, cut_all=False)))
    ground_truth = " ".join(list(jieba.cut(ground_truth, cut_all=False)))
209
    score = rouge_score(prediction, ground_truth)
Baber Abbasi's avatar
Baber Abbasi committed
210
211
212
    return score


213
214
215
216
217
218
219
220
221
222
def get_rouge_zh_score(doc, results, **kwargs):
    output = 0.0
    prediction = results[0].strip()
    for ground_truth in doc["answers"]:
        score = rouge_zh_score(prediction, ground_truth)
        output = max(score, output)
    return {"rouge_zh_score": output}


def f1_score(prediction: Union[str, list], ground_truth: Union[str, list], **kwargs):
Baber Abbasi's avatar
Baber Abbasi committed
223
224
225
226
227
228
229
230
231
232
    common = Counter(prediction) & Counter(ground_truth)
    num_same = sum(common.values())
    if num_same == 0:
        return 0
    precision = 1.0 * num_same / len(prediction)
    recall = 1.0 * num_same / len(ground_truth)
    f1 = (2 * precision * recall) / (precision + recall)
    return f1


233
234
235
236
237
238
239
240
241
242
def get_f1_score(doc: dict, results: list[str], **kwargs):
    output = 0.0
    prediction = results[0].strip()
    for ground_truth in doc["answers"]:
        score = f1_score(prediction, ground_truth)
        output = max(score, output)
    return {"f1_score": output}


def qa_f1_score(prediction: str, ground_truth: str, **kwargs):
Baber Abbasi's avatar
Baber Abbasi committed
243
244
245
246
247
    normalized_prediction = normalize_answer(prediction)
    normalized_ground_truth = normalize_answer(ground_truth)

    prediction_tokens = normalized_prediction.split()
    ground_truth_tokens = normalized_ground_truth.split()
248
    return f1_score(prediction_tokens, ground_truth_tokens)
Baber Abbasi's avatar
Baber Abbasi committed
249
250


251
def qa_f1_zh_score(prediction: str, ground_truth: str, **kwargs):
Baber Abbasi's avatar
Baber Abbasi committed
252
253
254
255
256
257
258
    prediction_tokens = list(jieba.cut(prediction, cut_all=False))
    ground_truth_tokens = list(jieba.cut(ground_truth, cut_all=False))
    prediction_tokens = [normalize_zh_answer(token) for token in prediction_tokens]
    ground_truth_tokens = [normalize_zh_answer(token) for token in ground_truth_tokens]
    prediction_tokens = [token for token in prediction_tokens if len(token) > 0]
    ground_truth_tokens = [token for token in ground_truth_tokens if len(token) > 0]
    return f1_score(prediction_tokens, ground_truth_tokens)
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276


def get_qa_f1_score(doc: dict, results: list[str], **kwargs):
    output = 0.0
    prediction = results[0].strip()
    for ground_truth in doc["answers"]:
        score = qa_f1_score(prediction, ground_truth)
        output = max(score, output)
    return {"qa_f1_score": output}


def get_qa_f1_zh_score(doc: dict, results: list[str], **kwargs):
    output = 0.0
    prediction = results[0].strip()
    for ground_truth in doc["answers"]:
        score = qa_f1_zh_score(prediction, ground_truth)
        output = max(score, output)
    return {"qa_f1_zh_score": output}