Skip to content

Commit 0a6ce17

Browse files
authored
Merge pull request #46 from e06084/main
feat: add language and style classify
2 parents 0c42898 + 6eeaf82 commit 0a6ce17

File tree

10 files changed

+1182
-164
lines changed

10 files changed

+1182
-164
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ ocr_demo
3838
_build/
3939

4040

41-
output/
41+
results/
4242
**/temp.py
4343

4444
# coverage file

README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,105 @@ python scripts/statics.py data/sample_dataset.jsonl --output data/analyzed_datas
292292
✅ 成功写入 1,827 条数据
293293
```
294294

295+
### 语言分类工具
296+
297+
WebMainBench 提供了语言分类工具 `scripts/language_classify.py`,用于为数据集中的文本内容自动添加符合 ISO 639-1 标准的语言标签。
298+
299+
#### 主要特性
300+
301+
- **多种检测方式**:支持基于规则的快速检测和基于LLM的高精度检测
302+
- **ISO 639-1 标准**:返回标准的两字母语言代码(如 en, zh, es)
303+
- **广泛语言支持**:支持80+种主要语言的检测
304+
- **批量处理**:高效处理大规模数据集
305+
- **智能回退**:多字段检测,自动处理缺失数据
306+
307+
#### 使用方法
308+
309+
```bash
310+
# 基于规则的快速检测(推荐用于大规模数据)
311+
python scripts/language_classify.py data/input.jsonl --output data/output.jsonl
312+
313+
# 使用LLM进行高精度检测
314+
python scripts/language_classify.py data/input.jsonl --output data/output.jsonl \
315+
--use-llm --api-key YOUR_OPENAI_API_KEY
316+
317+
# 自定义批处理大小
318+
python scripts/language_classify.py data/input.jsonl --output data/output.jsonl \
319+
--batch-size 50
320+
```
321+
322+
#### Prompt设计建议
323+
324+
如果你使用LLM进行语言检测,工具内置了优化的prompt模板:
325+
326+
**核心设计原则:**
327+
1. **明确输出格式**:只返回ISO 639-1两字母代码
328+
2. **处理边界情况**:空文本、多语言文本、符号等
329+
3. **语言映射规则**:中文统一返回"zh",未支持语言返回最接近的
330+
4. **文本截断**:只分析前2000字符,提高效率
331+
332+
**示例Prompt结构:**
333+
```
334+
Please identify the primary language of the following text and return ONLY the ISO 639-1 two-letter language code.
335+
336+
SUPPORTED LANGUAGES: en (English), zh (Chinese), es (Spanish), ...
337+
338+
RULES:
339+
1. Return ONLY the two-letter code
340+
2. For mixed languages, return the DOMINANT language
341+
3. Empty text defaults to "en"
342+
4. Chinese variants all return "zh"
343+
344+
TEXT TO ANALYZE: [your text here]
345+
346+
LANGUAGE CODE:
347+
```
348+
349+
#### 输出结果
350+
351+
工具会在数据的 `meta.language` 字段中添加语言标签:
352+
353+
```json
354+
{
355+
"convert_main_content": "Hello, this is sample content.",
356+
"meta": {
357+
"language": "en"
358+
}
359+
}
360+
```
361+
362+
#### 运行示例
363+
364+
```bash
365+
# 处理示例
366+
python scripts/language_classify.py data/sample.jsonl --output data/sample_with_lang.jsonl
367+
368+
# 输出:
369+
🔄 开始处理语言分类...
370+
📄 输入文件: data/sample.jsonl
371+
📄 输出文件: data/sample_with_lang.jsonl
372+
🧠 检测方法: 基于规则
373+
📊 已处理 100 条数据...
374+
📊 已处理 200 条数据...
375+
376+
✅ 处理完成!
377+
📊 总计处理: 1,000 条数据
378+
📊 语言分布:
379+
en (English): 650 (65.0%)
380+
zh (Chinese): 200 (20.0%)
381+
es (Spanish): 80 (8.0%)
382+
fr (French): 40 (4.0%)
383+
de (German): 30 (3.0%)
384+
```
385+
386+
#### 支持的语言
387+
388+
工具支持80+种主要语言,包括:
389+
- **欧洲语言**:英语(en)、西班牙语(es)、法语(fr)、德语(de)、意大利语(it)等
390+
- **亚洲语言**:中文(zh)、日语(ja)、韩语(ko)、泰语(th)、越南语(vi)等
391+
- **其他语言**:阿拉伯语(ar)、俄语(ru)、葡萄牙语(pt)、印地语(hi)等
392+
393+
完整列表请运行:`python examples/language_classify_demo.py`
295394

296395
## 项目架构
297396

examples/multi_extractor_compare.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def all_extractor_comparison():
88
print("\n=== 多抽取器对比演示 ===\n")
99

1010
# 创建数据集
11-
dataset_path = Path("/home/lulindong/Pycharm_projects/cc/1827_split_jsonl/1-200.jsonl")
11+
dataset_path = Path("data/sample_dataset.jsonl")
1212
dataset = DataLoader.load_jsonl(dataset_path)
1313

1414
# 创建webkit抽取器
@@ -27,6 +27,8 @@ def all_extractor_comparison():
2727
# 运行对比
2828
evaluator = Evaluator()
2929
extractors = [webkit_extractor, magic_extractor, trafilatura_extractor, resiliparse_extractor]
30+
# extractors = [webkit_extractor]
31+
3032

3133
results = evaluator.compare_extractors(
3234
dataset=dataset,

results/dataset_with_results.jsonl

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)