Skip to content

Commit c200430

Browse files
authored
Merge pull request #54 from e06084/main
feat: add dataset process script
2 parents 389fba9 + df65aa8 commit c200430

File tree

4 files changed

+375
-5
lines changed

4 files changed

+375
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ print(f"Overall Score: {result.overall_metrics['overall']:.4f}")
9393

9494
- **trafilatura**: trafilatura抽取器
9595
- **resiliparse**: resiliparse抽取器
96+
- **llm-webkit**: llm-webkit 抽取器
97+
- **magic-html**: magic-html 抽取器
9698
- **自定义抽取器**: 通过继承 `BaseExtractor` 实现
9799

98100
## 评测榜单

scripts/README.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Scripts 使用说明
2+
3+
本目录包含数据处理和分析的工具脚本。
4+
5+
## 📋 主要脚本
6+
7+
### 数据处理脚本
8+
9+
| 脚本 | 功能 | 添加字段 |
10+
|------|------|----------|
11+
| `statics.py` | 统计分析 | `meta.level`, `meta.table`, `meta.code`, `meta.equation` |
12+
| `language_classify.py` | 语言检测 | `meta.language` |
13+
| `style_classify.py` | 类型分类 | `meta.style` |
14+
| `process_dataset.sh` | 一键处理 | 上述所有字段 |
15+
16+
### 数据管理脚本
17+
18+
| 脚本 | 功能 |
19+
|------|------|
20+
| `merge_jsonl.py` | 合并多个 JSONL 文件 |
21+
| `filter_by_scores.py` | 按评分筛选数据 |
22+
| `diff_jsonl.py` | 对比 JSONL 文件差异 |
23+
| `add_raw_html_field.py` | 添加原始 HTML 字段 |
24+
| `merge_meta_data.py` | 合并 meta 数据 |
25+
26+
### 分析脚本
27+
28+
| 脚本 | 功能 |
29+
|------|------|
30+
| `analyze_style_results.py` | 分析网页类型分布 |
31+
| `quick_style_stats.py` | 快速统计网页类型 |
32+
33+
## 🚀 快速开始
34+
35+
### 方式一:一键处理(推荐)
36+
37+
```bash
38+
# 赋予执行权限(仅首次需要)
39+
chmod +x scripts/process_dataset.sh
40+
41+
# 执行处理,默认用的 gpt-5 model
42+
./scripts/process_dataset.sh \
43+
data/sample_dataset_with_fields.jsonl \
44+
data/final_dataset.jsonl \
45+
YOUR_API_KEY \
46+
YOUR_BASE_URL
47+
```
48+
49+
### 方式二:分步处理
50+
51+
```bash
52+
# 步骤 1: 统计分析
53+
python scripts/statics.py \
54+
--input data/input.jsonl \
55+
--output data/step1.jsonl
56+
57+
# 步骤 2: 语言检测
58+
python scripts/language_classify.py \
59+
data/step1.jsonl \
60+
--output data/step2.jsonl \
61+
--api-key YOUR_API_KEY \
62+
--base-url https://api.deepseek.com/v1
63+
64+
# 步骤 3: 类型分类
65+
python scripts/style_classify.py \
66+
data/step2.jsonl \
67+
--output data/final.jsonl \
68+
--api-key YOUR_API_KEY \
69+
--base-url https://api.deepseek.com/v1
70+
```
71+
72+
## 🔑 环境变量
73+
74+
```bash
75+
# 设置 API 密钥(推荐)
76+
export OPENAI_API_KEY="your_api_key"
77+
78+
# 或在命令行中通过 --api-key 参数传递
79+
```
80+
81+
## ⚙️ 常用参数
82+
83+
### statics.py
84+
85+
```bash
86+
python scripts/statics.py --input <input_file> --output <output_file>
87+
```
88+
89+
**无需 API 密钥**
90+
91+
### language_classify.py
92+
93+
```bash
94+
python scripts/language_classify.py <input_file> \
95+
--output <output_file> \
96+
--api-key <api_key> \
97+
--base-url <base_url> \
98+
--batch-size <size>
99+
```
100+
101+
**参数:**
102+
- `--api-key`: API 密钥(必需)
103+
- `--base-url`: API 地址(默认:`https://api.deepseek.com/v1`
104+
- `--batch-size`: 批处理大小(默认:100)
105+
106+
### style_classify.py
107+
108+
```bash
109+
python scripts/style_classify.py <input_file> \
110+
--output <output_file> \
111+
--api-key <api_key> \
112+
--base-url <base_url> \
113+
--batch-size <size>
114+
```
115+
116+
**参数:**
117+
- `--api-key`: API 密钥(必需)
118+
- `--base-url`: API 地址(必需)
119+
- `--batch-size`: 批处理大小(默认:100)
120+
121+
122+
## 🐛 故障排查
123+
124+
### 问题 1: API 调用失败
125+
126+
```bash
127+
# 检查 API 密钥
128+
echo $OPENAI_API_KEY
129+
130+
# 测试 API 连接
131+
curl https://api.deepseek.com/v1/models \
132+
-H "Authorization: Bearer YOUR_API_KEY"
133+
```
134+
135+
### 问题 2: 数据行数不匹配
136+
137+
```bash
138+
# 检查文件行数
139+
wc -l data/input.jsonl
140+
wc -l data/output.jsonl
141+
142+
# 检查 JSON 格式
143+
head -n 1 data/output.jsonl | python -m json.tool
144+
```
145+
146+
### 问题 3: 脚本权限不足
147+
148+
```bash
149+
# 赋予执行权限
150+
chmod +x scripts/process_dataset.sh
151+
chmod +x scripts/*.py
152+
```
153+
154+

scripts/process_dataset.sh

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/bin/bash
2+
3+
###############################################################################
4+
# WebMainBench 数据集完整处理脚本
5+
#
6+
# 功能:为数据集添加完整的 meta 字段
7+
# - meta.level, meta.table, meta.code, meta.equation (通过 statics.py)
8+
# - meta.language (通过 language_classify.py)
9+
# - meta.style (通过 style_classify.py)
10+
#
11+
# 使用方法:
12+
# ./scripts/process_dataset.sh <input_file> <output_file> <api_key> [base_url]
13+
#
14+
# 示例:
15+
# ./scripts/process_dataset.sh \
16+
# data/sample_dataset_with_fields.jsonl \
17+
# data/final_dataset.jsonl \
18+
# sk-xxxxx \
19+
# https://api.deepseek.com/v1
20+
###############################################################################
21+
22+
set -e # 遇到错误立即退出
23+
24+
# 颜色定义
25+
RED='\033[0;31m'
26+
GREEN='\033[0;32m'
27+
YELLOW='\033[1;33m'
28+
BLUE='\033[0;34m'
29+
NC='\033[0m' # No Color
30+
31+
# 打印带颜色的消息
32+
print_info() {
33+
echo -e "${BLUE}ℹ️ $1${NC}"
34+
}
35+
36+
print_success() {
37+
echo -e "${GREEN}$1${NC}"
38+
}
39+
40+
print_warning() {
41+
echo -e "${YELLOW}⚠️ $1${NC}"
42+
}
43+
44+
print_error() {
45+
echo -e "${RED}$1${NC}"
46+
}
47+
48+
print_step() {
49+
echo -e "\n${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
50+
echo -e "${GREEN}$1${NC}"
51+
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"
52+
}
53+
54+
# 检查参数
55+
if [ $# -lt 3 ]; then
56+
print_error "参数不足!"
57+
echo ""
58+
echo "使用方法:"
59+
echo " $0 <input_file> <output_file> <api_key> [base_url] [batch_size]"
60+
echo ""
61+
echo "参数说明:"
62+
echo " input_file - 输入 JSONL 文件路径"
63+
echo " output_file - 最终输出文件路径"
64+
echo " api_key - OpenAI API 密钥"
65+
echo " base_url - API 基础 URL (可选,默认: https://api.deepseek.com/v1)"
66+
echo " batch_size - 批处理大小 (可选,默认: 50)"
67+
echo ""
68+
echo "示例:"
69+
echo " $0 data/input.jsonl data/output.jsonl sk-xxxxx"
70+
echo " $0 data/input.jsonl data/output.jsonl sk-xxxxx https://api.openai.com/v1 100"
71+
exit 1
72+
fi
73+
74+
# 获取参数
75+
INPUT_FILE="$1"
76+
FINAL_OUTPUT="$2"
77+
API_KEY="$3"
78+
BASE_URL="${4:-https://api.deepseek.com/v1}"
79+
BATCH_SIZE="${5:-50}"
80+
81+
# 生成中间文件名
82+
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
83+
TEMP_DIR="data/temp_${TIMESTAMP}"
84+
STEP1_OUTPUT="${TEMP_DIR}/step1_with_stats.jsonl"
85+
STEP2_OUTPUT="${TEMP_DIR}/step2_with_language.jsonl"
86+
87+
# 验证输入文件
88+
if [ ! -f "$INPUT_FILE" ]; then
89+
print_error "输入文件不存在: $INPUT_FILE"
90+
exit 1
91+
fi
92+
93+
# 创建临时目录
94+
mkdir -p "$TEMP_DIR"
95+
96+
# 打印配置信息
97+
print_step "🚀 开始处理数据集"
98+
print_info "输入文件: $INPUT_FILE"
99+
print_info "输出文件: $FINAL_OUTPUT"
100+
print_info "API 地址: $BASE_URL"
101+
print_info "批处理大小: $BATCH_SIZE"
102+
print_info "临时目录: $TEMP_DIR"
103+
echo ""
104+
105+
# 统计输入文件行数
106+
INPUT_LINES=$(wc -l < "$INPUT_FILE" | tr -d ' ')
107+
print_info "输入数据总数: $INPUT_LINES"
108+
echo ""
109+
110+
# ============================================================================
111+
# 步骤 1: 添加统计字段
112+
# ============================================================================
113+
print_step "📊 步骤 1/3: 计算统计字段 (level, table, code, equation)"
114+
115+
if python scripts/statics.py --input "$INPUT_FILE" --output "$STEP1_OUTPUT"; then
116+
STEP1_LINES=$(wc -l < "$STEP1_OUTPUT" | tr -d ' ')
117+
print_success "步骤 1 完成!处理了 $STEP1_LINES 条数据"
118+
119+
# 验证数据完整性
120+
if [ "$INPUT_LINES" -ne "$STEP1_LINES" ]; then
121+
print_warning "数据行数不一致!输入: $INPUT_LINES, 输出: $STEP1_LINES"
122+
fi
123+
else
124+
print_error "步骤 1 失败!"
125+
rm -rf "$TEMP_DIR"
126+
exit 1
127+
fi
128+
129+
# ============================================================================
130+
# 步骤 2: 添加语言字段
131+
# ============================================================================
132+
print_step "🌐 步骤 2/3: 检测语言 (language)"
133+
134+
export OPENAI_API_KEY="$API_KEY"
135+
136+
if python scripts/language_classify.py \
137+
"$STEP1_OUTPUT" \
138+
--output "$STEP2_OUTPUT" \
139+
--api-key "$API_KEY" \
140+
--base-url "$BASE_URL" \
141+
--batch-size "$BATCH_SIZE"; then
142+
143+
STEP2_LINES=$(wc -l < "$STEP2_OUTPUT" | tr -d ' ')
144+
print_success "步骤 2 完成!处理了 $STEP2_LINES 条数据"
145+
146+
# 验证数据完整性
147+
if [ "$STEP1_LINES" -ne "$STEP2_LINES" ]; then
148+
print_warning "数据行数不一致!输入: $STEP1_LINES, 输出: $STEP2_LINES"
149+
fi
150+
else
151+
print_error "步骤 2 失败!"
152+
print_warning "保留中间文件: $STEP1_OUTPUT"
153+
exit 1
154+
fi
155+
156+
# ============================================================================
157+
# 步骤 3: 添加网页类型字段
158+
# ============================================================================
159+
print_step "🎨 步骤 3/3: 分类网页类型 (style)"
160+
161+
if python scripts/style_classify.py \
162+
"$STEP2_OUTPUT" \
163+
--output "$FINAL_OUTPUT" \
164+
--api-key "$API_KEY" \
165+
--base-url "$BASE_URL" \
166+
--batch-size "$BATCH_SIZE"; then
167+
168+
FINAL_LINES=$(wc -l < "$FINAL_OUTPUT" | tr -d ' ')
169+
print_success "步骤 3 完成!处理了 $FINAL_LINES 条数据"
170+
171+
# 验证数据完整性
172+
if [ "$STEP2_LINES" -ne "$FINAL_LINES" ]; then
173+
print_warning "数据行数不一致!输入: $STEP2_LINES, 输出: $FINAL_LINES"
174+
fi
175+
else
176+
print_error "步骤 3 失败!"
177+
print_warning "保留中间文件: $STEP2_OUTPUT"
178+
exit 1
179+
fi
180+
181+
# ============================================================================
182+
# 完成与清理
183+
# ============================================================================
184+
print_step "🎉 处理完成!"
185+
186+
print_info "最终输出: $FINAL_OUTPUT"
187+
print_info "处理数据: $FINAL_LINES"
188+
echo ""
189+
190+
# 询问是否删除临时文件
191+
read -p "是否删除临时文件?(y/n) " -n 1 -r
192+
echo ""
193+
if [[ $REPLY =~ ^[Yy]$ ]]; then
194+
rm -rf "$TEMP_DIR"
195+
print_success "已删除临时文件"
196+
else
197+
print_info "临时文件保留在: $TEMP_DIR"
198+
fi
199+
200+
# 显示输出文件示例
201+
print_step "📋 输出数据示例"
202+
print_info "查看第一条数据的 meta 字段:"
203+
echo ""
204+
head -n 1 "$FINAL_OUTPUT" | python -c "
205+
import json
206+
import sys
207+
208+
data = json.loads(sys.stdin.read())
209+
meta = data.get('meta', {})
210+
211+
print('Meta 字段内容:')
212+
print(json.dumps(meta, indent=2, ensure_ascii=False))
213+
"
214+
215+
print_success "全部完成!🎊"
216+

0 commit comments

Comments
 (0)