FinLLM-EvalSets / scripts /prep_fineval.py
shiyan688's picture
upload
796575b verified
Raw History Blame Contribute Delete
6.43 kB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
FinEval 数据准备:CSV -> JSON
来源:HuggingFace SUFE-AIFLM-Lab/FinEval(cc-by-nc-sa-4.0)
对应 NAACL 2025 论文中的「金融学术知识」部分:4,661 道选择题 / 34 学科。
另外三部分(金融行业知识 1434 / 金融安全知识 1640 / 金融代理 616)官方未开源。
口径(与 C-Eval 基线对齐,保证两套基准可比):
few-shot 取自 dev(每科恰好 5 题),评测集为 val;test 集官方不公开答案,不使用。
输出:data/fineval/json/<subject>/{dev,val}.json
字段与 C-Eval 导出保持一致:id / question / A / B / C / D / answer
"""
import csv
import glob
import json
import os
ROOT = r"D:\ceval-baseline"
SRC = os.path.join(ROOT, "data", "fineval", "extracted")
OUT = os.path.join(ROOT, "data", "fineval", "json")
# 34 学科中文名(用于 prompt 头部「以下是中国关于<学科>的单项选择题」)
CN = {
"accounting": "会计",
"advanced_financial_accounting": "高级财务会计",
"auditing": "审计",
"banking_practitioner_qualification_certificate": "银行从业资格",
"central_banking": "中央银行学",
"certified_management_accountant": "管理会计师",
"certified_practising_accountant": "注册会计师",
"china_actuary": "中国精算师",
"commercial_bank_finance": "商业银行金融",
"corporate_finance": "公司金融",
"corporate_strategy_and_risk_management": "公司战略与风险管理",
"cost_accounting": "成本会计",
"econometrics": "计量经济学",
"economic_law": "经济法",
"finance": "金融学",
"financial_engineering": "金融工程",
"financial_management": "财务管理",
"financial_markets": "金融市场",
"fund_qualification_certificate": "基金从业资格",
"futures_practitioner_qualification_certificate": "期货从业资格",
"insurance": "保险",
"intermediate_financial_accounting": "中级财务会计",
"international_economics": "国际经济学",
"international_finance": "国际金融",
"investments": "投资学",
"macroeconomics": "宏观经济学",
"management_accounting": "管理会计",
"microeconomics": "微观经济学",
"monetary_finance": "货币金融",
"political_economy": "政治经济学",
"public_finance": "财政学",
"securities_practitioner_qualification_certificate": "证券从业资格",
"statistics": "统计学",
"tax_law": "税法",
}
# FinEval 把 34 科分为金融/经济/会计/金融类考证 4 大类
GROUP = {
"finance": ["finance", "corporate_finance", "financial_engineering", "financial_management",
"financial_markets", "investments", "international_finance", "monetary_finance",
"commercial_bank_finance", "central_banking", "insurance"],
"economy": ["microeconomics", "macroeconomics", "econometrics", "international_economics",
"political_economy", "public_finance", "statistics", "economic_law"],
"accounting": ["accounting", "advanced_financial_accounting", "intermediate_financial_accounting",
"auditing", "cost_accounting", "management_accounting"],
"certificate": ["certified_practising_accountant", "certified_management_accountant",
"china_actuary", "banking_practitioner_qualification_certificate",
"securities_practitioner_qualification_certificate",
"fund_qualification_certificate",
"futures_practitioner_qualification_certificate",
"corporate_strategy_and_risk_management", "tax_law"],
}
SUBJ_GROUP = {s: g for g, ss in GROUP.items() for s in ss}
def load_split(split):
out = {}
for f in sorted(glob.glob(os.path.join(SRC, split, "*.csv"))):
subj = os.path.basename(f)[: -len(f"_{split}.csv")]
rows = []
with open(f, encoding="utf-8-sig") as fh:
for row in csv.DictReader(fh):
if row.get("answer") not in list("ABCD"):
continue
# 选项文本里可能残留 LaTeX 转义(如 3$\%$),统一清洗为普通字符
clean = lambda s: (s or "").replace("$\\%$", "%").replace("\\%", "%").strip()
rows.append({
"id": f"{subj}_{row['id']}",
"question": (row.get("question") or "").strip(),
"A": clean(row.get("A")),
"B": clean(row.get("B")),
"C": clean(row.get("C")),
"D": clean(row.get("D")),
"answer": row["answer"],
})
out[subj] = rows
return out
def main():
dev = load_split("dev")
val = load_split("val")
os.makedirs(OUT, exist_ok=True)
tot_dev = tot_val = 0
summary = []
for subj in sorted(dev):
if subj not in val:
print("!! val 缺少学科:", subj)
continue
d, v = dev[subj], val[subj]
os.makedirs(os.path.join(OUT, subj), exist_ok=True)
with open(os.path.join(OUT, subj, "dev.json"), "w", encoding="utf-8") as f:
json.dump(d, f, ensure_ascii=False, indent=1)
with open(os.path.join(OUT, subj, "val.json"), "w", encoding="utf-8") as f:
json.dump(v, f, ensure_ascii=False, indent=1)
tot_dev += len(d)
tot_val += len(v)
summary.append({
"subject": subj, "subject_cn": CN.get(subj, subj),
"group": SUBJ_GROUP.get(subj, "?"),
"n_dev": len(d), "n_val": len(v),
})
with open(os.path.join(ROOT, "data", "fineval", "inventory.json"), "w", encoding="utf-8") as f:
json.dump({"n_subjects": len(summary), "n_dev": tot_dev, "n_val": tot_val,
"subjects": summary}, f, ensure_ascii=False, indent=2)
print(f"学科数 {len(summary)} | dev {tot_dev} 题 | val {tot_val} 题")
# 缺中文名的学科
miss = [s["subject"] for s in summary if s["subject"] not in CN]
if miss:
print("!! 缺中文名:", miss)
# 分组统计
import collections
c = collections.Counter(s["group"] for s in summary)
print("按大类:", dict(c))
print("每科 dev 题数分布:", sorted({s["n_dev"] for s in summary}))
print("输出 ->", OUT)
if __name__ == "__main__":
main()