字
字节笔记本
2026年2月22日
pycorrector:中文文本纠错工具包
本文介绍 pycorrector,一个功能强大的中文文本纠错开源工具包。该项目实现了多种先进的文本纠错模型,包括 KenLM、T5、MacBERT、ChatGLM3、Qwen2.5 等,开箱即用,适用于多种中文文本纠错场景。
项目简介
pycorrector 是由 shibing624 开发维护的中文文本纠错工具包,目前在 GitHub 上已获得 6.4k+ Stars 和 1.2k+ Forks。该项目专注于解决中文文本中的音似、形似、语法、专名等错误类型,支持 Python 3.8+ 环境。
中文文本纠错任务中常见的错误类型包括:
- 音似错误:如"因该"→"应该"
- 形似错误:如"高心"→"高兴"
- 语法错误:如"少先队员因该为老人让坐"
- 专名错误:如人名、地名等专有名词的错误
核心特性
pycorrector 实现了多种文本纠错模型,各模型特点如下:
1. KenLM 模型(统计模型)
- 基于 KenLM 统计语言模型训练的中文 NGram 语言模型
- 结合规则方法和混淆集纠正中文拼写错误
- 优点:速度快,扩展性强,CPU 即可运行
- 缺点:效果一般,适合对速度要求高的场景
2. DeepContext 模型
- 基于 PyTorch 实现的 DeepContext 模型
- 参考斯坦福大学 NLC 模型结构
- 在 2014 年英文纠错比赛中获得第一名
3. ConvSeq2Seq 模型
- 基于 PyTorch 的 Seq2Seq 模型
- 在 NLPCC-2018 中文语法纠错比赛中获得第三名
- 支持并行训练,模型收敛快
4. T5 模型
- 基于 PyTorch 的 T5 模型
- 使用 Langboat/mengzi-t5-base 预训练模型
- 模型改造潜力大,效果好
5. MacBERT 模型(推荐)
- 基于 PyTorch 的 MacBERT4CSC 模型
- 加入错误检测和纠正网络
- 适配中文拼写纠错任务,效果好
6. ERNIE_CSC 模型
- 基于 PaddlePaddle 的 ERNIE_CSC 模型
- 在 ERNIE-1.0 上微调
- 模型结构适配中文拼写纠错任务
7. GPT 模型(效果最好)
- 基于 ChatGLM3、Qwen2.5、Qwen3 等大语言模型
- 在中文 CSC 和语法纠错数据集上微调
- 支持多字、少字、错字、词序、语法等错误纠正
- 效果最佳,推荐使用
评估结果
项目在多个评测集上进行了评估,包括 SIGHAN-2015、EC-LAW、MCSC 等。以下是各模型的 F1 分数对比:
| 模型名称 | 平均 F1 | SIGHAN-2015 | EC-LAW | MCSC | GPU | QPS |
|---|---|---|---|---|---|---|
| Kenlm-CSC | 0.3409 | 0.3147 | 0.3763 | 0.3317 | CPU | 9 |
| MacBERT-CSC | 0.3993 | 0.8314 | 0.1610 | 0.2055 | GPU | 224 |
| ChatGLM3-6B-CSC | 0.4538 | 0.6572 | 0.4369 | 0.2672 | GPU | 3 |
| Qwen2.5-1.5B-CTC | 0.6802 | 0.3032 | 0.7846 | 0.9529 | GPU | 6 |
| Qwen2.5-7B-CTC | 0.8225 | 0.4917 | 0.9798 | 0.9959 | GPU | 3 |
| Qwen3-4B-CTC | 0.8521 | 0.6340 | 0.9360 | 0.9864 | GPU | 5 |
从评估结果可以看出,基于 Qwen 系列的 GPT 模型在文本纠错任务上表现最佳,其中 Qwen3-4B-CTC 模型达到了 0.8521 的平均 F1 分数。
安装指南
使用 pip 安装
bash
pip install -U pycorrector从源码安装
bash
git clone https://github.com/shibing624/pycorrector.git
cd pycorrector
pip install -r requirements.txt
pip install --no-deps .使用 Docker
bash
docker run -it -v ~/.pycorrector:/root/.pycorrector shibing624/pycorrector:0.0.2使用示例
1. KenLM 模型快速开始
python
from pycorrector import Corrector
m = Corrector()
result = m.correct_batch([
'少先队员因该为老人让坐',
'你找到你最喜欢的工作,我也很高心。'
])
print(result)输出:
python
[
{
'source': '少先队员因该为老人让坐',
'target': '少先队员应该为老人让座',
'errors': [('因该', '应该', 4), ('坐', '座', 10)]
},
{
'source': '你找到你最喜欢的工作,我也很高心。',
'target': '你找到你最喜欢的工作,我也很高兴。',
'errors': [('心', '兴', 15)]
}
]2. MacBERT 模型使用
python
from pycorrector import MacBertCorrector
m = MacBertCorrector("shibing624/macbert4csc-base-chinese")
result = m.correct_batch([
'今天新情很好',
'你找到你最喜欢的工作,我也很高心。'
])
print(result)3. GPT 模型使用(推荐)
python
from pycorrector.gpt.gpt_corrector import GptCorrector
m = GptCorrector()
result = m.correct_batch([
'今天新情很好',
'你找到你最喜欢的工作,我也很高心。'
])
print(result)4. 英文拼写纠错
python
from pycorrector import EnSpellCorrector
m = EnSpellCorrector()
sent = "what happending? how to speling it, can you gorrect it?"
print(m.correct(sent))输出:
python
{
'source': 'what happending? how to speling it, can you gorrect it?',
'target': 'what happening? how to spelling it, can you correct it?',
'errors': [('happending', 'happening', 5), ('speling', 'spelling', 24), ('gorrect', 'correct', 44)]
}5. 中文简繁转换
python
import pycorrector
# 繁体转简体
traditional = '憂郁的臺灣烏龜'
simplified = pycorrector.traditional2simplified(traditional)
print(f'{traditional} => {simplified}')
# 简体转繁体
simplified = '忧郁的台湾乌龟'
traditional = pycorrector.simplified2traditional(simplified)
print(f'{simplified} => {traditional}')自定义配置
自定义混淆集
通过加载自定义混淆集,可以:
- 提升准确率:将误杀的词加入白名单
- 提升召回率:补充需要召回的错误
python
from pycorrector import Corrector
# 自定义混淆集文件格式:每行以空格间隔错误词和正确词
# iPhone差 iPhoneX
# 张旗康 张旗康
m = Corrector(custom_confusion_path_or_dict='./my_custom_confusion.txt')
result = m.correct_batch([
'买iphonex,要多少钱',
'共同实际控制人萧华、霍荣铨、张旗康'
])自定义语言模型
python
from pycorrector import Corrector
# 使用自定义训练的 kenlm 语言模型
model = Corrector(language_model_path='people2014corpus_chars.klm')
print(model.correct('少先队员因该为老人让坐'))在线演示
- 官方 Demo: https://www.mulanai.com/product/corrector/
- HuggingFace Demo: https://huggingface.co/spaces/shibing624/pycorrector
项目链接
- GitHub 仓库: https://github.com/shibing624/pycorrector
- 官方文档: https://github.com/shibing624/pycorrector/wiki
- 模型仓库: https://huggingface.co/shibing624
- 问题反馈: https://github.com/shibing624/pycorrector/issues
总结
pycorrector 是一个功能全面、易于使用的中文文本纠错工具包。无论是传统的统计模型(KenLM)还是最新的深度学习模型(Qwen3),都能在项目中找到合适的解决方案。对于生产环境,推荐使用 MacBERT 或 Qwen 系列模型以获得更好的纠错效果;对于资源受限的场景,KenLM 模型提供了轻量级的选择。
分享: