File size: 7,369 Bytes
1e012a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import json
import os
from typing import Dict, List
from datetime import datetime

def get_model_names() -> List[str]:
    """从evaluation_results文件夹获取所有模型名称"""
    model_names = []
    for item in os.listdir('.'):
        if item.startswith('evaluation_results.') and os.path.isdir(item):
            model_name = item.replace('evaluation_results.', '')
            model_names.append(model_name)
    return sorted(model_names)  # 排序以保持顺序一致

def load_report(model_name: str) -> Dict:
    """加载模型的评测报告"""
    report_path = f"evaluation_results.{model_name}/evaluation_report.json"
    with open(report_path, 'r', encoding='utf-8') as f:
        return json.load(f)

def format_percentage(value: float) -> str:
    """格式化百分比显示"""
    return f"{value*100:.2f}%"

def generate_markdown_report():
    """生成markdown格式的评测报告"""
    # 动态获取模型列表
    models = get_model_names()
    
    # 加载所有报告
    reports = {model: load_report(model) for model in models}
    
    # 生成报告内容
    report = []
    
    # 标题和时间
    report.append("# 中国古诗词大模型评测报告")
    report.append(f"生成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
    
    # 添加模型信息
    report.append(f"评测模型: {', '.join(models)}\n")
    
    # 1. 整体表现对比
    report.append("## 1. 整体表现对比")
    
    # 获取所有题型
    all_types = sorted(set(
        q_type
        for data in reports.values()
        for q_type in data['by_type'].keys()
    ))
    
    # 生成表头
    headers = ["模型"] + all_types + ["总计", "总正确率"]
    report.append("\n| " + " | ".join(headers) + " |")
    report.append("| " + " | ".join(["----" for _ in headers]) + " |")
    
    # 按总正确率排序
    sorted_models = sorted(reports.items(), 
                         key=lambda x: x[1]['overall']['accuracy'],
                         reverse=True)
    
    # 生成每个模型的数据行
    for model, data in sorted_models:
        row = [model]
        # 添加每个题型的数据
        for q_type in all_types:
            if q_type in data['by_type']:
                metrics = data['by_type'][q_type]
                row.append(f"{metrics['correct']}/{metrics['total']}")
            else:
                row.append("0/0")
        
        # 添加总计和总正确率
        overall = data['overall']
        row.append(f"{overall['correct']}/{overall['total']}")
        row.append(format_percentage(overall['accuracy']))
        
        report.append("| " + " | ".join(row) + " |")
    report.append("")
    
    # 2. 按题型分类表现
    report.append("## 2. 题型分类表现")
    report.append("\n| 模型 | 题型 | 总题数 | 正确数 | 准确率 |")
    report.append("| --- | --- | --- | --- | --- |")
    
    # 获取所有题型
    all_types = sorted(set(
        q_type
        for data in reports.values()
        for q_type in data['by_type'].keys()
    ))
    
    for q_type in all_types:
        type_results = []
        for model, data in reports.items():
            if q_type in data['by_type']:
                metrics = data['by_type'][q_type]
                type_results.append((model, metrics))
        
        # 按准确率排序
        sorted_results = sorted(type_results, 
                              key=lambda x: x[1]['accuracy'],
                              reverse=True)
        
        for model, metrics in sorted_results:
            report.append(f"| {model} | {q_type} | {metrics['total']} | {metrics['correct']} | {format_percentage(metrics['accuracy'])} |")
        report.append("| --- | --- | --- | --- | --- |")  # 添加分隔线
    report.append("")
    
    # 3. 难度分布表现
    report.append("## 3. 难度分布表现")
    report.append("\n| 模型 | 难度 | 总题数 | 正确数 | 准确率 |")
    report.append("| --- | --- | --- | --- | --- |")
    
    # 难度顺序
    difficulty_order = ['easy', 'medium', 'hard']
    
    for diff in difficulty_order:
        diff_results = []
        for model, data in reports.items():
            if diff in data['by_difficulty']:
                metrics = data['by_difficulty'][diff]
                diff_results.append((model, metrics))
        
        # 按准确率排序
        sorted_results = sorted(diff_results,
                              key=lambda x: x[1]['accuracy'],
                              reverse=True)
        
        for model, metrics in sorted_results:
            report.append(f"| {model} | {diff} | {metrics['total']} | {metrics['correct']} | {format_percentage(metrics['accuracy'])} |")
        report.append("| --- | --- | --- | --- | --- |")  # 添加分隔线
    report.append("")
    
    # 4. 朝代分布表现
    report.append("## 4. 朝代分布表现")
    report.append("\n| 模型 | 朝代 | 总题数 | 正确数 | 准确率 |")
    report.append("| --- | --- | --- | --- | --- |")
    
    # 获取所有朝代并排序
    all_dynasties = sorted(set(
        dynasty if dynasty else "未知"
        for data in reports.values()
        for dynasty in data['by_dynasty'].keys()
    ))
    
    for dynasty in all_dynasties:
        dynasty_results = []
        for model, data in reports.items():
            if dynasty in data['by_dynasty'] or (dynasty == "未知" and None in data['by_dynasty']):
                metrics = data['by_dynasty'][None if dynasty == "未知" else dynasty]
                dynasty_results.append((model, metrics))
        
        # 按准确率排序
        sorted_results = sorted(dynasty_results,
                              key=lambda x: x[1]['accuracy'],
                              reverse=True)
        
        for model, metrics in sorted_results:
            report.append(f"| {model} | {dynasty} | {metrics['total']} | {metrics['correct']} | {format_percentage(metrics['accuracy'])} |")
        report.append("| --- | --- | --- | --- | --- |")  # 添加分隔线
    report.append("")
    
    # 5. 结论分析
    report.append("## 5. 结论分析")
    report.append("\n### 5.1 整体表现")
    
    # 计算最佳表现模型
    best_model = max(reports.items(), key=lambda x: x[1]['overall']['accuracy'])
    report.append(f"- 最佳表现模型: {best_model[0]}, 整体准确率 {format_percentage(best_model[1]['overall']['accuracy'])}")
    
    # 计算各个维度的最佳表现
    report.append("\n### 5.2 分维度最佳表现")
    
    # 题型维度
    report.append("\n#### 题型维度:")
    for q_type in all_types:
        best = max(reports.items(), 
                  key=lambda x: x[1]['by_type'][q_type]['accuracy'])
        report.append(f"- {q_type}: {best[0]} ({format_percentage(best[1]['by_type'][q_type]['accuracy'])})")
    
    # 难度维度
    report.append("\n#### 难度维度:")
    for diff in difficulty_order:
        best = max(reports.items(),
                  key=lambda x: x[1]['by_difficulty'][diff]['accuracy'])
        report.append(f"- {diff}: {best[0]} ({format_percentage(best[1]['by_difficulty'][diff]['accuracy'])})")
    
    # 写入文件
    with open('evaluation_report.md', 'w', encoding='utf-8') as f:
        f.write('\n'.join(report))

if __name__ == '__main__':
    generate_markdown_report()