metrics.py 6.07 KB
Newer Older
Baber's avatar
Baber committed
1
2
3
4
5
6
7
8
9
import re
import string
from collections import Counter

import jieba
from fuzzywuzzy import fuzz
from rouge import Rouge


Baber's avatar
Baber committed
10
def normalize_answer(s: str) -> str:
Baber's avatar
Baber committed
11
12
13
14
15
    """Lower text and remove punctuation, articles and extra whitespace."""

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

Baber's avatar
Baber committed
16
    def white_space_fix(text):
Baber's avatar
Baber committed
17
18
19
20
21
22
23
24
25
26
27
28
        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))))


Baber's avatar
Baber committed
29
def normalize_zh_answer(s: str) -> str:
Baber's avatar
Baber committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    """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)))


Baber's avatar
Baber committed
46
47
def count_score(predictions: list[str], references: list[str], **kwargs) -> float:
    prediction, ground_truth = predictions[0], references[0]
Baber's avatar
Baber committed
48
49
50
51
52
53
54
55
56
    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)


Baber's avatar
Baber committed
57
58
def retrieval_score(predictions: list[str], references: list[str], **kwargs) -> float:
    prediction, ground_truth = predictions[0], references[0]
Baber's avatar
Baber committed
59
60
61
62
63
64
65
66
67
68
69
70
    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)


Baber's avatar
Baber committed
71
72
73
74
def retrieval_zh_score(
    predictions: list[str], references: list[str], **kwargs
) -> float:
    prediction, ground_truth = predictions[0], references[0]
Baber's avatar
Baber committed
75
76
77
78
79
80
81
82
83
84
85
86
    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)


Baber's avatar
Baber committed
87
88
def code_sim_score(predictions: list[str], references: list[str], **kwargs) -> float:
    prediction, ground_truth = predictions[0], references[0]
Baber's avatar
Baber committed
89
90
91
92
93
94
95
96
97
    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


Baber's avatar
Baber committed
98
99
100
101
def classification_score(
    predictions: list[str], references: list[str], **kwargs
) -> float:
    prediction, ground_truth = predictions[0], references[0]
Baber's avatar
Baber committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    em_match_list = []
    all_classes = kwargs["all_classes"]
    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
    return score


Baber's avatar
Baber committed
117
118
def rouge_score(predictions: list[str], references: list[str], **kwargs) -> float:
    prediction, ground_truth = predictions[0], references[0]
Baber's avatar
Baber committed
119
120
121
122
123
124
125
126
127
    rouge = Rouge()
    try:
        scores = rouge.get_scores([prediction], [ground_truth], avg=True)
        # ruff: noqa
    except:
        return 0.0
    return scores["rouge-l"]["f"]


Baber's avatar
Baber committed
128
129
def rouge_zh_score(predictions: list[str], references: list[str], **kwargs) -> float:
    prediction, ground_truth = predictions[0], references[0]
Baber's avatar
Baber committed
130
131
    prediction = " ".join(list(jieba.cut(prediction, cut_all=False)))
    ground_truth = " ".join(list(jieba.cut(ground_truth, cut_all=False)))
Baber's avatar
Baber committed
132
    score = rouge_score([prediction], [ground_truth])
Baber's avatar
Baber committed
133
134
135
    return score


Baber's avatar
Baber committed
136
def f1_score(predictions: list[str], references: list[str], **kwargs):
Baber's avatar
Baber committed
137
138
139
140
    try:
        prediction, ground_truth = predictions[0], references[0]
    except:
        return 0.0
Baber's avatar
Baber committed
141
142
143
144
145
146
147
148
149
150
    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


Baber's avatar
Baber committed
151
152
153
154
def qa_f1_score(predictions: list[str], references: list[str], **kwargs) -> float:
    prediction, ground_truth = predictions[0], references[0]
    normalized_prediction = normalize_answer(prediction)
    normalized_ground_truth = normalize_answer(ground_truth)
Baber's avatar
Baber committed
155
156
157

    prediction_tokens = normalized_prediction.split()
    ground_truth_tokens = normalized_ground_truth.split()
Baber's avatar
Baber committed
158
159
160
161
162
    try:
        res = f1_score(prediction_tokens, ground_truth_tokens)
    except:
        return 0.0
    return res
Baber's avatar
Baber committed
163
164


Baber's avatar
Baber committed
165
166
def qa_f1_zh_score(predictions: list[str], references: list[str], **kwargs) -> float:
    prediction, ground_truth = predictions[0], references[0]
Baber's avatar
Baber committed
167
168
169
170
171
172
173
    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)