-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreward.py
More file actions
executable file
·165 lines (142 loc) · 5.56 KB
/
reward.py
File metadata and controls
executable file
·165 lines (142 loc) · 5.56 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
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
'''
reward for TQA task: WTQ, HiTab
'''
import re
import json
import string
PATTERN = re.compile(r'^<think>.*?</think>\s*<answer>.*?</answer>$', re.DOTALL)
ANSWER_BLOCK_PATTERN = re.compile(r'<answer>(.*?)</answer>', re.DOTALL)
STRICT_ANSWER_PATTERN = re.compile(r'```json\s*(\{\s*\"answer\"\s*:\s*(?:\[[\s\S]*?\])\s*\})\s*```')
ANSWER_PATTERN_1 = re.compile(r'```json\s*(\{\s*\"answer\"\s*:\s*(?:\[[\s\S]*?\]|\"[\s\S]*?\"|[-+]?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?)\s*\})\s*```')
ANSWER_PATTERN_2 = re.compile(r'(\{\s*\"answer\"\s*:\s*(?:\[[\s\S]*?\]|\"[\s\S]*?\"|[-+]?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?)\s*\})')
NUMBER_PATTERN = re.compile(r'^[+-]?(?:(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$')
def normalize_string(s):
if isinstance(s, str):
# 移除非字母数字字符(保留数字和字母)
s = re.sub(r'[^a-zA-Z0-9]', '', s)
return s.lower()
return s
def parse_json(answer):
try:
data = json.loads(answer)
if not isinstance(data, dict) or "answer" not in data:
return None
if isinstance(data["answer"], list):
return data["answer"]
else:
return [data["answer"]]
except json.JSONDecodeError:
return None
def extract_answer_pattern(predict_str):
answer_match = ANSWER_PATTERN_1.search(predict_str)
if answer_match is not None:
answer = answer_match.group(1).strip()
return parse_json(answer)
answer_match = ANSWER_PATTERN_2.search(predict_str)
if answer_match is not None:
answer = answer_match.group(1).strip()
return parse_json(answer)
return None
def extract_answer(predict_str):
answer_block_match = ANSWER_BLOCK_PATTERN.search(predict_str)
if answer_block_match is not None:
answer = extract_answer_pattern(answer_block_match.group(1))
if answer is not None:
return answer
answer = extract_answer_pattern(predict_str)
if answer is not None:
return answer
return None
def normalize_answer(answer):
normalized_answer = []
for x in answer:
if isinstance(x, int) or isinstance(x, float):
normalized_answer.append(float(x))
elif isinstance(x, str):
if NUMBER_PATTERN.match(x):
try:
normalized_answer.append(float(x.replace(',', '')))
except ValueError:
normalized_answer.append(normalize_string(x))
else:
normalized_answer.append(normalize_string(x))
else:
return []
return normalized_answer
# for instruct model
def format_check(predict_str):
if PATTERN.fullmatch(predict_str):
for tag in ["<think>", "</think>", "<answer>", "</answer>"]:
if predict_str.count(tag) != 1:
return False
answer_block_match = ANSWER_BLOCK_PATTERN.search(predict_str).group(1)
answer_match = STRICT_ANSWER_PATTERN.search(answer_block_match)
if answer_match is not None:
answer = answer_match.group(1).strip()
final_answer = parse_json(answer)
if final_answer is None:
return False
return True
return False
def compute_score(data_source, solution_str, ground_truth, extra_info):
predict_str = solution_str
answer = extract_answer(predict_str)
if answer is None:
return {
"score": 0.0,
"format_score": 0.0,
"partial_score": 0.0,
"accurate_score": 0.0,
}
normalized_answer = normalize_answer(answer)
if len(normalized_answer) == 0 or len(normalized_answer) > 100:
return {
"score": 0.0,
"format_score": 0.0,
"partial_score": 0.0,
"accurate_score": 0.0,
}
normalized_ground_truth = normalize_answer(ground_truth)
format_score = 1.0 if format_check(predict_str) else 0.0
partial_score = 0.0
accurate_score = 0.0
# 检查部分匹配:只要有一个元素匹配就得分
for a in normalized_answer:
for b in normalized_ground_truth:
# 数字比较
if isinstance(a, float) and isinstance(b, float):
if abs(a - b) < 1e-2:
partial_score = 1.0
break
# 字符串比较(已规范化)
elif isinstance(a, str) and isinstance(b, str):
if a == b:
partial_score = 1.0
break
if partial_score == 1.0:
break
# 检查完全匹配:忽略顺序,只要元素集合相同就得分
if len(normalized_answer) == len(normalized_ground_truth):
# 创建答案和真实值的集合(考虑浮点数精度)
answer_set = set()
truth_set = set()
for a in normalized_answer:
if isinstance(a, float):
# 将浮点数转换为字符串,保留两位小数,以避免浮点数精度问题
answer_set.add(f"{a:.2f}")
else:
answer_set.add(str(a))
for b in normalized_ground_truth:
if isinstance(b, float):
truth_set.add(f"{b:.2f}")
else:
truth_set.add(str(b))
if answer_set == truth_set:
accurate_score = 1.0
total_score = 0.2 * format_score + 0.3 * partial_score + 0.5 * accurate_score
return {
"score": total_score,
"format_score": format_score,
"partial_score": partial_score,
"accurate_score": accurate_score,
}