shibing624/pycorrector
shibing624/pycorrector has 6,539 GitHub stars and 1,167 forks. GitHot measured +1 stars in 24 hours and +3 in 7 days. Last checked 2026-11-09 13:22 UTC. Last push 2026-07-25.
pycorrector is a toolkit for text error correction. 文本纠错,实现了Kenlm,T5,MacBERT,ChatGLM3,Qwen2.5等模型应用在纠错场景,开箱即用。
What you can do
- Kenlm模型:本项目基于Kenlm统计语言模型工具训练了中文NGram语言模型,结合规则方法、混淆集可以纠正中文拼写错误,方法速度快,扩展性强,效果一般
- DeepContext模型:本项目基于PyTorch实现了用于文本纠错的DeepContext模型,该模型结构参考Stanford University的NLC模型,2014英文纠错比赛得第一名,效果一般
- Seq2Seq模型:本项目基于PyTorch实现了用于中文文本纠错的ConvSeq2Seq模型,该模型在NLPCC-2018的中文语法纠错比赛中,使用单模型并取得第三名,可以并行训练,模型收敛快,效果一般
- T5模型:本项目基于PyTorch实现了用于中文文本纠错的T5模型,使用Langboat/mengzi-t5-base的预训练模型finetune中文纠错数据集,模型改造的潜力较大,效果好
Measured star growth
- in 24h
- +1
- in 7 days
- +3
- in 15 days
- Collecting history
- in 1 month
- Collecting history
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README_EN.md
Original🇨🇳中文 | 🌐English | 📖文档/Docs | 🤖模型/Models
pycorrector: useful python text correction toolkit
pycorrector: Chinese Text Error Correction Toolkit.
pycorrector Use the language model to detect errors, pinyin feature and shape feature to correct chinese text error, it can be used for Chinese Pinyin and stroke input method.
News
[2026/07/25] v1.1.4: Optimized proper-noun correction (ProperCorrector) performance by replacing the brute-force per-fragment scan over the entire proper-noun dictionary with a multi-level inverted index (by word length and position), and removed the redundant Trie and fixed OOV stroke false positives, achieving ~1-2 orders of magnitude speedup on a 40k-size dictionary; replaced regex with an Aho-Corasick automaton for custom confusion matching; fixed a crash when the custom confusion set is empty in the default config; corrected the pretrained language model filename (people2014corpus_chars.klm). See Release-v1.1.4
Features
language model
- Kenlm
- RNNLM
deep model
- rnn_attention
- seq2seq_attention
- conv_seq2seq
- transformer
- bert
- electra
Install
- auto:pip install pycorrector
- manual:
git clone https://github.com/shibing624/pycorrector.git
cd pycorrector
python setup.py install
Install Requires
- install kenlm
pip install https://github.com/kpu/kenlm/archive/master.zip
- install others
pip install -r requirements.txt
Usage
- Text Correction
import pycorrector
corrected_sent, detail = pycorrector.correct('少先队员因该为老人让坐')
print(corrected_sent, detail)
output:
少先队员应该为老人让座 [[('因该', '应该', 4, 6)], [('坐', '座', 10, 11)]]
model load from:
~/.pycorrector/datasets/zh_giga.no_cna_cmn.prune01244.klm, if not download auto, do it from file(2.8G).Correction
- Error Detection
import pycorrector
idx_errors = pycorrector.detect('少先队员因该为老人让坐')
print(idx_errors)
output:
[['因该', 4, 6, 'word'], ['坐', 10, 11, 'char']]
return
list,[error_word, begin_pos, end_pos, error_type],posindex starts with 0.
- English Seplling Error Correction
import pycorrector
sent_lst = ['what', 'hapenning', 'how', 'to', 'speling', 'it', 'you', 'can', 'gorrect', 'it']
for i in sent_lst:
print(i, '=>', pycorrector.en_correct(i))
output:
what => what
hapenning => happening
how => how
to => to
speling => spelling
it => it
you => you
can => can
gorrect => correct
it => it
Command Line Usage
- Command line
python -m pycorrector -h
usage: __main__.py [-h] -o OUTPUT [-n] [-d] input
@description:
positional arguments:
input the input file path, file encode need utf-8.
optional arguments:
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
the output file path.
-n, --no_char disable char detect mode.
-d, --detail print detail info
case:
python -m pycorrector input.txt -o out.txt -n -d
input file:
input.txt; output file:out.txt
Future work
P(c), the language model. We could create a better language model by collecting more data, and perhaps by using a little English morphology (such as adding "ility" or "able" to the end of a word).
P(w|c), the error model. So far, the error model has been trivial: the smaller the edit distance, the smaller the error. Clearly we could use a better model of the cost of edits. get a corpus of spelling errors, and count how likely it is to make each insertion, deletion, or alteration, given the surrounding characters.
It turns out that in many cases it is difficult to make a decision based only on a single word. This is most obvious when there is a word that appears in the dictionary, but the test set says it should be corrected to another word anyway: correction('where') => 'where' (123); expected 'were' (452) We can't possibly know that correction('where') should be 'were' in at least one case, but should remain 'where' in other cases. But if the query had been correction('They where going') then it seems likely that "where" should be corrected to "were".
Finally, we could improve the implementation by making it much faster, without changing the results. We could re-implement in a compiled language rather than an interpreted one. We could cache the results of computations so that we don't have to repeat them multiple times. One word of advice: before attempting any speed optimizations, profile carefully to see where the time is actually going.
Further Reading
Cite
@software{pycorrector,
author = {Xu Ming},
title = {{pycorrector: Text Error Correction Tool}},
year = {2020},
url = {https://github.com/shibing624/pycorrector},
}
License
Apache License 2.0
References
- Norvig’s spelling corrector
- Chinese Spelling Error Detection and Correction Based on Language Model, Pronunciation, and Shape[Yu, 2013]
- Chinese Spelling Checker Based on Statistical Machine Translation[Chiu, 2013]
- Chinese Word Spelling Correction Based on Rule Induction[yeh, 2014]
- Neural Language Correction with Character-Based Attention[Ziang Xie, 2016]
- Chinese Spelling Check System Based on Tri-gram Model[Qiang Huang, 2014]
- Neural Abstractive Text Summarization with Sequence-to-Sequence Models[Tian Shi, 2018]
- A Sequence to Sequence Learning for Chinese Grammatical Error Correction[Hongkai Ren, 2018]
- ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators
Explore related projects
codecrafters-io/build-your-own-x
Master programming by recreating your favorite technologies from scratch.
561,263 starspublic-apis/public-apis
A collective list of free APIs
497,524 starsmorluto/rea
Reverse engineer anything with agents, from app behavior down to native binaries.
445,962 starsopenclaw/openclaw
The AI that really does things. Any OS. Any Platform. The lobster way. 🦞
394,231 stars
