pycorrector is a toolkit for text error correction. 文本纠错,实现了Kenlm,T5,MacBERT,ChatGLM3,Qwen2.5等模型应用在纠错场景,开箱即用。
pycorrector is a toolkit for text error correction. 文本纠错,实现了Kenlm,T5,MacBERT,ChatGLM3,Qwen2.5等模型应用在纠错场景,开箱即用。
中文 | English | 文档/Docs | 模型/Models
pycorrector: 中文文本纠错工具。支持中文音似、形似、语法错误纠正,python3.8开发。
pycorrector实现了Kenlm、ConvSeq2Seq、BERT、MacBERT、ELECTRA、ERNIE、GPT等多种模型的文本纠错,评估各模型的效果。
Guide
中文文本纠错任务,常见错误类型:
当然,针对不同业务场景,这些问题并不一定全部存在,比如拼音输入法、语音识别校对关注音似错误;五笔输入法、OCR校对关注形似错误, 搜索引擎query纠错关注所有错误类型。
本项目重点解决其中的"音似、形字、语法、专名错误"等类型。
[2026/07/25] v1.1.4版本:优化专名纠错(ProperCorrector)性能,将逐片段全量扫描专名词典改为按词长+位置的多级倒排索引,并移除冗余Trie、修复OOV笔画假阳性,4万规模专名词典下纠错速度提升约80倍。详见Release-v1.1.4
[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
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
评估脚本examples/evaluate_models/evaluate_models.py:
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 run -it -v ~/.pycorrector:/root/.pycorrector shibing624/pycorrector:0.0.2
本项目的初衷之一是比对、调研各种中文文本纠错方法,抛砖引玉。
项目实现了kenlm、macbert、seq2seq、 ernie_csc、T5、deepcontext、GPT(Qwen/ChatGLM)等模型应用于文本纠错任务,各模型均可基于已经训练好的纠错模型快速预测,也可使用自有数据训练、预测。
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的listexample: 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开始。example: examples/kenlm/use_custom_proper.py
from pycorrector import Corrector
m = Corrector(proper_name_path='./my_custom_proper.txt')
x = ['报应接中迩来', '这块名表带带相传',]
for i in x:
print(i, ' -> ', m.correct(i))
output:
报应接中迩来 -> {'source': '报应接踵而来', 'target': '报应接踵而来', 'errors': [('接中迩来', '接踵而来', 2)]}
这块名表带带相传 -> {'source': '这块名表代代相传', 'target': '这块名表代代相传', 'errors': [('带带相传', '代代相传', 4)]}
通过加载自定义混淆集,支持用户纠正已知的错误,包括两方面功能:1)【提升准确率】误杀加白;2)【提升召回率】补充召回。
example: examples/kenlm/use_custom_confusion.py
from pycorrector import Corrector
error_sentences = [
'买iphonex,要多少钱',
'共同实际控制人萧华、霍荣铨、张旗康',
]
m = Corrector()
print(m.correct_batch(error_sentences))
print('*' * 42)
m = Corrector(custom_confusion_path_or_dict='./my_custom_confusion.txt')
print(m.correct_batch(error_sentences))
output:
('买iphonex,要多少钱', []) # "iphonex"漏召,应该是"iphoneX"
('共同实际控制人萧华、霍荣铨、张启康', [('张旗康', '张启康', 14)]) # "张启康"误杀,应该不用纠
*****************************************************
('买iphonex,要多少钱', [('iphonex', 'iphoneX', 1)])
('共同实际控制人萧华、霍荣铨、张旗康', [])
./my_custom_confusion.txt的内容格式如下,以空格间隔:iPhone差 iPhoneX
张旗康 张旗康
自定义混淆集ConfusionCorrector类,除了上面演示的和Corrector类一起使用,还可以和MacBertCorrector一起使用,也可以独立使用。示例代码 examples/macbert/model_correction_pipeline_demo.py
默认提供下载并使用的kenlm语言模型zh_giga.no_cna_cmn.prune01244.klm文件是2.8G,内存小的电脑使用pycorrector程序可能会吃力些。
支持用户加载自己训练的kenlm语言模型,或使用2014版人民日报数据训练的模型,模型小(140M),准确率稍低,模型下载地址:shibing624/chinese-kenlm-klm | people2014corpus_chars.klm(密码o5e9)。
example:examples/kenlm/load_custom_language_model.py
from pycorrector import Corrector
model = Corrector(language_model_path='people2014corpus_chars.klm')
print(model.correct('少先队员因该为老人让坐'))
支持英文单词级别的拼写错误纠正。
example:examples/kenlm/en_correct_demo.py
from pycorrector import EnSpellCorrector
m = EnSpellCorrector()
sent = "what happending? how to speling it, can you gorrect it?"
print(m.correct(sent))
output:
{'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)]}
支持中文繁体到简体的转换,和简体到繁体的转换。
example:[examples/kenlm/traditional_simplified_chinese_demo.py](https://github.com/shibing624/pycorrector/blob/master/examples/kenlm/t
暂无开放 Issues,或尚未同步最近议题。