#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import re
def check_percent_in_process(data: dict) -> list[bool]:
"""
判断每个 process 元素是否包含百分号 %
:param data: 包含 'process' 字段的字典
:return: 每个元素是否包含百分号的布尔列表
"""
process_list = data.get("process", [])
return ['%' in step for step in process_list]
def extract_boxed_answer(text: str) -> str:
# 匹配最后一个 \boxed{...} 中的内容
matches = re.findall(r'\\boxed\{(.*?)\}', text)
return matches[-1] if matches else None
def process_jsonl(input_path: str, output_path: str):
with open(input_path, 'r', encoding='utf-8') as infile, \
open(output_path, 'w', encoding='utf-8') as outfile:
for line in infile:
data = json.loads(line)
distill_text = data.get("distill_answer", "")
infer_answer = extract_boxed_answer(distill_text).replace('\\%', '%')
try:
if check_percent_in_process(data):
if infer_answer[-1] == '%':
infer_answer = float(infer_answer[:-1])
else:
infer_answer = float(infer_answer) * 100
else:
if infer_answer[-1] == '%':
infer_answer = float(infer_answer[:-1]) * 100
except:
infer_answer = None
if infer_answer:
data["infer_answer"] = infer_answer
json.dump(data, outfile, ensure_ascii=False)
outfile.write('\n')
def main():
# 示例调用:
process_jsonl("data/temp.jsonl", "data/a0_infer_answer.jsonl")
if __name__=='__main__':
main()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
def split_jsonl_by_result(
input_path: str,
equal_output_path: str,
unequal_output_path: str,
threshold: float = 1e-3
):
"""
读取 JSONL 文件,比较 result 与 infer_answer 是否相等(相对误差小于阈值),
并将结果分别保存到两个 JSONL 文件中。
参数:
- input_path: 输入 JSONL 文件路径
- equal_output_path: 相等结果保存路径
- unequal_output_path: 不相等结果保存路径
- threshold: 相对误差阈值,默认 1e-3
"""
equal_count = 0
unequal_count = 0
with open(equal_output_path, 'w', encoding='utf-8') as eq_out, \
open(unequal_output_path, 'w', encoding='utf-8') as neq_out, \
open(input_path, 'r', encoding='utf-8') as f:
for line in f:
try:
data = json.loads(line)
result = data.get('result')
infer_answer = data.get('infer_answer')
if isinstance(result, (int, float)) and isinstance(infer_answer, (int, float)):
relative_error = abs(result - infer_answer) / abs(result)
if relative_error < threshold:
eq_out.write(json.dumps(data, ensure_ascii=False) + '\n')
equal_count += 1
else:
neq_out.write(json.dumps(data, ensure_ascii=False) + '\n')
unequal_count += 1
else:
# 数据格式不正确,默认写入不相等文件
neq_out.write(json.dumps(data, ensure_ascii=False) + '\n')
unequal_count += 1
except json.JSONDecodeError:
print("跳过无法解析的行:", line.strip())
continue
print(f"处理完成:相等记录 {equal_count} 条,不相等记录 {unequal_count} 条。")
def main():
split_jsonl_by_result(
input_path='data/a0_infer_answer.jsonl',
equal_output_path='data/a1_equal.jsonl',
unequal_output_path='data/a1_unequal.jsonl',
threshold=1e-3
)
if __name__=='__main__':
main()
Template
于 2025-06-25 15:29:57 首次发布

6863

被折叠的 条评论
为什么被折叠?



