-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathcalculate_metrics.py
More file actions
43 lines (32 loc) · 1.23 KB
/
calculate_metrics.py
File metadata and controls
43 lines (32 loc) · 1.23 KB
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
# SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import re
import pandas as pd
def string_match_part(preds, refs):
score = (
sum([max([1.0 if r.lower() in pred.lower() else 0.0 for r in ref]) for pred, ref in zip(preds, refs)])
/ len(preds)
* 100
)
return round(score, 2)
def string_match_all(preds, refs):
score = (
sum(
[sum([1.0 if r.lower() in pred.lower() else 0.0 for r in ref]) / len(ref) for pred, ref in zip(preds, refs)]
)
/ len(preds)
* 100
)
return round(score, 2)
def calculate_metrics(df: pd.DataFrame) -> dict:
scores = {}
np_pattern = re.compile(r"[\x00-\x1f]")
df["predicted_answer"] = df["predicted_answer"].apply(lambda x: np_pattern.sub("", x.strip()).strip())
for task, df_task in df.groupby("task"):
task_category = task.split("_")[0]
metric_fn = string_match_part if task_category == "qa" else string_match_all
preds = df_task["predicted_answer"].tolist()
refs = df_task["answer"].tolist()
score = metric_fn(preds, refs)
scores[task] = {"string_match": score}
return scores