Pycorrector
pycorrector is a toolkit for text error correction. 文本纠错,实现了Kenlm,T5,MacBERT,ChatGLM3,Qwen2.5等模型应用在纠错场景,开箱即用。
Install / Use
/learn @shibing624/PycorrectorREADME
🇨🇳中文 | 🌐English | 📖文档/Docs | 🤖模型/Models
<div align="center"> <a href="https://github.com/shibing624/pycorrector"> <img src="https://github.com/shibing624/pycorrector/blob/master/docs/pycorrector.png" alt="Logo" height="156"> </a> </div>pycorrector: useful python text correction toolkit
pycorrector: 中文文本纠错工具。支持中文音似、形似、语法错误纠正,python3.8开发。
pycorrector实现了Kenlm、ConvSeq2Seq、BERT、MacBERT、ELECTRA、ERNIE、GPT等多种模型的文本纠错,评估各模型的效果。
Guide
Introduction
中文文本纠错任务,常见错误类型:
<img src="https://github.com/shibing624/pycorrector/blob/master/docs/git_image/error_type.png" width="600" />当然,针对不同业务场景,这些问题并不一定全部存在,比如拼音输入法、语音识别校对关注音似错误;五笔输入法、OCR校对关注形似错误, 搜索引擎query纠错关注所有错误类型。
本项目重点解决其中的"音似、形字、语法、专名错误"等类型。
News
[2025/07/08] v1.1.2版本:支持了基于Qwen3的中文文本纠错模型twnlp/ChineseErrorCorrector3-4B,支持多字、少字、错字、词序、语法等错误纠正。详见Release-v1.1.2
[2024/10/14] v1.1.0版本:新增了基于Qwen2.5的中文文本纠错模型,支持多字、少字、错字、词序、语法等错误纠正,发布了shibing624/chinese-text-correction-1.5b和shibing624/chinese-text-correction-7b模型,及其对应的LoRA模型。详见Release-v1.1.0
[2023/11/07] v1.0.0版本:新增了ChatGLM3/LLaMA2等GPT模型用于中文文本纠错,发布了基于ChatGLM3-6B的shibing624/chatglm3-6b-csc-chinese-lora拼写和语法纠错模型;重写了DeepContext、ConvSeq2Seq、T5等模型的实现。详见Release-v1.0.0
Features
- Kenlm模型:本项目基于Kenlm统计语言模型工具训练了中文NGram语言模型,结合规则方法、混淆集可以纠正中文拼写错误,方法速度快,扩展性强,效果一般
- DeepContext模型:本项目基于PyTorch实现了用于文本纠错的DeepContext模型,该模型结构参考Stanford University的NLC模型,2014英文纠错比赛得第一名,效果一般
- Seq2Seq模型:本项目基于PyTorch实现了用于中文文本纠错的ConvSeq2Seq模型,该模型在NLPCC-2018的中文语法纠错比赛中,使用单模型并取得第三名,可以并行训练,模型收敛快,效果一般
- T5模型:本项目基于PyTorch实现了用于中文文本纠错的T5模型,使用Langboat/mengzi-t5-base的预训练模型finetune中文纠错数据集,模型改造的潜力较大,效果好
- ERNIE_CSC模型:本项目基于PaddlePaddle实现了用于中文文本纠错的ERNIE_CSC模型,模型在ERNIE-1.0上finetune,模型结构适配了中文拼写纠错任务,效果好
- MacBERT模型【推荐】:本项目基于PyTorch实现了用于中文文本纠错的MacBERT4CSC模型,模型加入了错误检测和纠正网络,适配中文拼写纠错任务,效果好
- MuCGECBart模型:本项目基于ModelScope实现了用于文本纠错的Seq2Seq方法的MuCGECBart模型,该模型中文文本纠错效果较好
- NaSGECBart模型: MuCGECBart的同作者模型,无需modelscope依赖,基于中文母语纠错数据集NaSGEC在Bart模型上微调训练得到,效果好
- GPT模型:本项目基于PyTorch实现了用于中文文本纠错的ChatGLM/LLaMA模型,模型在中文CSC和语法纠错数据集上finetune,适配中文文本纠错任务,效果很好
- 延展阅读:中文文本纠错实践和原理解读
Demo
-
Official demo: https://www.mulanai.com/product/corrector/
-
HuggingFace demo: https://huggingface.co/spaces/shibing624/pycorrector

run example: examples/macbert/gradio_demo.py to see the demo:
python examples/macbert/gradio_demo.py
Evaluation
评估脚本examples/evaluate_models/evaluate_models.py:
- 评测集:SIGHAN-2015(sighan2015_test.tsv)、 EC-LAW(ec_law_test.tsv)、MCSC(mcsc_test.tsv)
- 评估标准:纠错准召率,采用严格句子粒度(Sentence Level)计算方式,把模型纠正之后的与正确句子完成相同的视为正确,否则为错
评估结果
- 评估指标:F1
- CSC(Chinese Spelling Correction): 拼写纠错模型,表示模型可以处理音似、形似、语法等长度对齐的错误纠正
- CTC(CHinese Text Correction): 文本纠错模型,表示模型支持拼写、语法等长度对齐的错误纠正,还可以处理多字、少字等长度不对齐的错误纠正
- GPU:Tesla V100,显存 32 GB
| Model Name | Model Link | Base Model | Avg | SIGHAN-2015 | EC-LAW | MCSC | GPU | QPS | |:-----------------|:------------------------------------------------------------------------------------------------------------------------|:-------------------------------|:-----------|:------------|:-------|:-------|:--------|:--------| | Kenlm-CSC | shibing624/chinese-kenlm-klm | kenlm | 0.3409 | 0.3147 | 0.3763 | 0.3317 | CPU | 9 | | Mengzi-T5-CSC | shibing624/mengzi-t5-base-chinese-correction | mengzi-t5-base | 0.3984 | 0.7758 | 0.3156 | 0.1039 | GPU | 214 | | ERNIE-CSC | PaddleNLP/ernie-csc | PaddlePaddle/ernie-1.0-base-zh | 0.4353 | 0.8383 | 0.3357 | 0.1318 | GPU | 114 | | MacBERT-CSC | shibing624/macbert4csc-base-chinese | hfl/chinese-macbert-base | 0.3993 | 0.8314 | 0.1610 | 0.2055 | GPU | 224 | | ChatGLM3-6B-CSC | shibing624/chatglm3-6b-csc-chinese-lora | THUDM/chatglm3-6b | 0.4538 | 0.6572 | 0.4369 | 0.2672 | GPU | 3 | | Qwen2.5-1.5B-CTC | shibing624/chinese-text-correction-1.5b | Qwen/Qwen2.5-1.5B-Instruct | 0.6802 | 0.3032 | 0.7846 | 0.9529 | GPU | 6 | | Qwen2.5-7B-CTC | shibing624/chinese-text-correction-7b | Qwen/Qwen2.5-7B-Instruct | 0.8225 | 0.4917 | 0.9798 | 0.9959 | GPU | 3 | | Qwen3-4B-CTC | twnlp/ChineseErrorCorrector3-4B | Qwen/Qwen3-4B | 0.8521 | 0.6340 | 0.9360 | 0.9864 | GPU | 5 |
Install
pip install -U pycorrector
or
pip install -r requirements.txt
git clone https://github.com/shibing624/pycorrector.git
cd pycorrector
pip install --no-deps .
通过以上两种方法的任何一种完成安装都可以。如果不想安装依赖包,可以拉docker环境。
- docker使用
docker run -it -v ~/.pycorrector:/root/.pycorrector shibing624/pycorrector:0.0.2
- colab在线使用
Usage
本项目的初衷之一是比对、调研各种中文文本纠错方法,抛砖引玉。
项目实现了kenlm、macbert、seq2seq、 ernie_csc、T5、deepcontext、GPT(Qwen/ChatGLM)等模型应用于文本纠错任务,各模型均可基于已经训练好的纠错模型快速预测,也可使用自有数据训练、预测。
kenlm模型(统计模型)
中文拼写纠错
example: examples/kenlm/demo.py
from pycorrector import Corrector
m = Corrector()
print(m.correct_batch(['少先队员因该为老人让坐', '你找到你最喜欢的工作,我也很高心。']))
output:
[{'source': '少先队员因该为老人让坐', 'target': '少先队员应该为老人让座', 'errors': [('因该', '应该', 4), ('坐', '座', 10)]}
{'source': '你找到你最喜欢的工作,我也很高心。', 'target': '你找到你最喜欢的工作,我也很高兴。', 'errors': [('心', '兴', 15)]}]
Corrector()类是kenlm统计模型的纠错方法实现,默认会从路径~/.pycorrector/datasets/zh_giga.no_cna_cmn.prune01244.klm加载kenlm语言模型文件,如果检测没有该文件, 则程序会自动联网下载。当然也可以手动下载模型文件(2.8G)并放置于该位置- 返回值:
correct方法返回dict,{'source': '原句子', 'target': '纠正后的句子', 'errors': [('错误词', '正确词', '错误位置'), ...]},correct_batch方法返回包含多个dict的list
错误检测
example: examples/kenlm/detect_demo.py
from pycorrector import Corrector
m = Corrector()
idx_errors = m.detect('少先队员因该为老人让坐')
print(idx_errors)
output:
[['因该', 4, 6, 'word'], ['坐', 10, 11, 'char']]
- 返回值:
list,[error_word, begin_pos, end_pos, error_type],pos索引位置以0开始。
成语、专名纠错
examp
Related Skills
node-connect
337.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
337.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.2kCommit, push, and open a PR
