Macbert4csc V2
模型概述
該模型主要用於中文拼寫糾錯,支持多種領域文本的糾錯任務,包括文言文和常見高頻錯誤如'地得的'等。
模型特點
特定架構設計
在BertForMaskedLM後新增錯誤檢測分支(分類任務),訓練與推理時採用不同策略
高效訓練策略
使用MFT(動態mask 0.2的非錯誤tokens)訓練,det_loss權重為0.3
多領域適用性
使用多種領域數據訓練,適合作為預訓練模型,可用於專有領域數據的繼續微調
文言文支持
訓練數據包含文言文數據,支持文言文糾錯
高頻錯誤處理
對'地得的'等高頻錯誤具有較高的識別率和糾錯率
模型能力
中文文本拼寫糾錯
多領域文本糾錯
文言文糾錯
高頻錯誤識別
使用案例
通用文本糾錯
日常文本糾錯
糾正日常文本中的拼寫錯誤
示例:'少先隊員因該為老人讓坐' → '少先隊員應該為老人讓坐'
專業領域糾錯
糾正專業領域文本中的拼寫錯誤
示例:'機七學習是人工智能領遇最能體現智能的一個分知' → '機器學習是人工智能領域最能體現智能的一個分支'
特定錯誤類型處理
'地得的'糾錯
專門處理中文中常見的'地得的'使用錯誤
示例:'希望你們好好的跳無' → '希望你們好好地跳舞'
🚀 macbert4csc_v2
macbert4csc_v2是一款用於中文拼寫糾錯的模型,採用了特定架構和訓練策略,可通過多種方式調用,在多個測評數據集上表現良好,適用於多種領域的文本糾錯任務。
🚀 快速開始
本模型可用於中文拼寫糾錯測評和文本糾錯,權重使用方面有其獨特特點。項目地址在https://github.com/yongzhuo/macro-correct 。
本模型權重為macbert4csc_v2,使用macbert4csc架構(pycorrector版本),其特點是在BertForMaskedLM後新加一個分支用於錯誤檢測任務(分類任務,不交互);訓練時使用了MFT(動態mask 0.2的非錯誤tokens),同時det_loss的權重為0.3;推理時捨棄了macbert後面的部分(det-layer)。
使用方式有兩種:
- 使用transformers調用;
- 使用macro-correct項目調用;詳情見三、調用(Usage)。
✨ 主要特性
- 特定架構:在BertForMaskedLM後新加一個分支用於錯誤檢測任務(分類任務,不交互)。
- 訓練策略:訓練時使用了MFT(動態mask 0.2的非錯誤tokens),同時det_loss的權重為0.3。
- 推理優化:推理時捨棄了macbert後面的部分(det-layer)。
- 多領域適用性:使用多種領域數據訓練,比較均衡,適合作為第一步的預訓練模型,可用於專有領域數據的繼續微調。
- 文言文支持:訓練數據中存在文言文數據,訓練好的模型支持文言文糾錯。
- 高頻錯誤處理:對“地得的”等高頻錯誤具有較高的識別率和糾錯率。
📦 安裝指南
文檔未提及安裝步驟,故跳過此章節。
💻 使用示例
基礎用法
使用macro-correct
import os
os.environ["MACRO_CORRECT_FLAG_CSC_TOKEN"] = "1"
from macro_correct import correct
### 默認糾錯(list輸入)
text_list = ["真麻煩你了。希望你們好好的跳無",
"少先隊員因該為老人讓坐",
"機七學習是人工智能領遇最能體現智能的一個分知",
"一隻小魚船浮在平淨的河面上"
]
text_csc = correct(text_list)
print("默認糾錯(list輸入):")
for res_i in text_csc:
print(res_i)
print("#" * 128)
"""
默認糾錯(list輸入):
{'index': 0, 'source': '真麻煩你了。希望你們好好的跳無', 'target': '真麻煩你了。希望你們好好地跳舞', 'errors': [['的', '地', 12, 0.6584], ['無', '舞', 14, 1.0]]}
{'index': 1, 'source': '少先隊員因該為老人讓坐', 'target': '少先隊員應該為老人讓坐', 'errors': [['因', '應', 4, 0.995]]}
{'index': 2, 'source': '機七學習是人工智能領遇最能體現智能的一個分知', 'target': '機器學習是人工智能領域最能體現智能的一個分支', 'errors': [['七', '器', 1, 0.9998], ['遇', '域', 10, 0.9999], ['知', '支', 21, 1.0]]}
{'index': 3, 'source': '一隻小魚船浮在平淨的河面上', 'target': '一隻小魚船浮在平靜的河面上', 'errors': [['淨', '靜', 8, 0.9961]]}
"""
使用transformers
# !/usr/bin/python
# -*- coding: utf-8 -*-
# @time : 2021/2/29 21:41
# @author : Mo
# @function: transformers直接加載bert類模型測試
import traceback
import time
import sys
import os
os.environ["USE_TORCH"] = "1"
from transformers import BertConfig, BertTokenizer, BertForMaskedLM
import torch
# pretrained_model_name_or_path = "shibing624/macbert4csc-base-chinese"
# pretrained_model_name_or_path = "Macropodus/macbert4mdcspell_v1"
# pretrained_model_name_or_path = "Macropodus/macbert4csc_v1"
pretrained_model_name_or_path = "Macropodus/macbert4csc_v2"
# pretrained_model_name_or_path = "Macropodus/bert4csc_v1"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
max_len = 128
print("load model, please wait a few minute!")
tokenizer = BertTokenizer.from_pretrained(pretrained_model_name_or_path)
bert_config = BertConfig.from_pretrained(pretrained_model_name_or_path)
model = BertForMaskedLM.from_pretrained(pretrained_model_name_or_path)
model.to(device)
print("load model success!")
texts = [
"機七學習是人工智能領遇最能體現智能的一個分知",
"我是練習時長兩念半的鴿仁練習生蔡徐坤",
"真麻煩你了。希望你們好好的跳無",
"他法語說的很好,的語也不錯",
"遇到一位很棒的奴生跟我療天",
"我們為這個目標努力不解",
]
len_mid = min(max_len, max([len(t)+2 for t in texts]))
with torch.no_grad():
outputs = model(**tokenizer(texts, padding=True, max_length=len_mid,
return_tensors="pt").to(device))
def get_errors(source, target):
""" 極簡方法獲取 errors """
len_min = min(len(source), len(target))
errors = []
for idx in range(len_min):
if source[idx] != target[idx]:
errors.append([source[idx], target[idx], idx])
return errors
result = []
for probs, source in zip(outputs.logits, texts):
ids = torch.argmax(probs, dim=-1)
tokens_space = tokenizer.decode(ids[1:-1], skip_special_tokens=False)
text_new = tokens_space.replace(" ", "")
target = text_new[:len(source)]
errors = get_errors(source, target)
print(source, " => ", target, errors)
result.append([target, errors])
print(result)
"""
機七學習是人工智能領遇最能體現智能的一個分知 => 機器學習是人工智能領域最能體現智能的一個分支 [['七', '器', 1], ['遇', '域', 10], ['知', '支', 21]]
我是練習時長兩念半的鴿仁練習生蔡徐坤 => 我是練習時長兩年半的個人練習生蔡徐坤 [['念', '年', 7], ['鴿', '個', 10], ['仁', '人', 11]]
真麻煩你了。希望你們好好的跳無 => 真麻煩你了。希望你們好好地跳舞 [['的', '地', 12], ['無', '舞', 14]]
他法語說的很好,的語也不錯 => 他法語說得很好,德語也不錯 [['的', '得', 4], ['的', '德', 8]]
遇到一位很棒的奴生跟我療天 => 遇到一位很棒的女生跟我聊天 [['奴', '女', 7], ['療', '聊', 11]]
我們為這個目標努力不解 => 我們為這個目標努力不懈 [['解', '懈', 10]]
"""
📚 詳細文檔
一、測評(Test)
1.1 測評數據來源
地址為Macropodus/csc_eval_public,所有訓練數據均來自公網或開源數據,訓練數據為1千萬左右,混淆詞典較大。
1.gen_de3.json(5545): '的地得'糾錯,由人民日報/學習強國/chinese-poetry等高質量數據人工生成;
2.lemon_v2.tet.json(1053): relm論文提出的數據,多領域拼寫糾錯數據集(7個領域);包括game(GAM)、encyclopedia (ENC)、contract (COT)、medical care(MEC)、car (CAR)、novel (NOV)和news (NEW)等領域;
3.acc_rmrb.tet.json(4636): 來自NER - 199801(人民日報高質量語料);
4.acc_xxqg.tet.json(5000): 來自學習強國網站的高質量語料;
5.gen_passage.tet.json(10000): 源數據為qwen生成的好詞好句,由幾乎所有的開源數據彙總的混淆詞典生成;
6.textproof.tet.json(1447): NLP競賽數據,TextProofreadingCompetition;
7.gen_xxqg.tet.json(5000): 源數據為學習強國網站的高質量語料,由幾乎所有的開源數據彙總的混淆詞典生成;
8.faspell.dev.json(1000): 視頻字幕通過OCR後獲取的數據集;來自愛奇藝的論文faspell;
9.lomo_tet.json(5000): 主要為音似中文拼寫糾錯數據集;來自騰訊;人工標註的數據集CSCD - NS;
10.mcsc_tet.5000.json(5000): 醫學拼寫糾錯;來自騰訊醫典APP的真實歷史日誌;注意論文說該數據集只關注醫學實體的糾錯,常用字等的糾錯並不關注;
11.ecspell.dev.json(1500): 來自ECSpell論文,包括(law/med/gov)等三個領域;
12.sighan2013.dev.json(1000): 來自sighan13會議;
13.sighan2014.dev.json(1062): 來自sighan14會議;
14.sighan2015.dev.json(1100): 來自sighan15會議;
1.2 測評數據預處理
測評數據都經過全角轉半角,繁簡轉化,標點符號標準化等操作。
1.3 其他說明
1.指標帶common的極為寬鬆指標,同開源項目pycorrector的評估指標;
2.指標帶strict的極為嚴格指標,同開源項目[wangwang110/CSC](https://github.com/wangwang110/CSC);
3.macbert4mdcspell_v1模型為訓練使用mdcspell架構 + bert的mlm - loss,但是推理的時候只用bert - mlm;
4.acc_rmrb/acc_xxqg數據集沒有錯誤,用於評估模型的誤糾率(過度糾錯);
5.qwen25_1 - 5b_pycorrector的模型為shibing624/chinese - text - correction - 1.5b,其訓練數據包括了lemon_v2/mcsc_tet/ecspell的驗證集和測試集,其他的bert類模型的訓練不包括驗證集和測試集;
二、重要指標
2.1 F1(common_cor_f1)
model/common_cor_f1 | avg | gen_de3 | lemon_v2 | gen_passage | text_proof | gen_xxqg | faspell | lomo_tet | mcsc_tet | ecspell | sighan2013 | sighan2014 | sighan2015 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
macbert4csc_pycorrector | 45.8 | 42.44 | 42.89 | 31.49 | 46.31 | 26.06 | 32.7 | 44.83 | 27.93 | 55.51 | 70.89 | 61.72 | 66.81 |
bert4csc_v1 | 62.28 | 93.73 | 61.99 | 44.79 | 68.0 | 35.03 | 48.28 | 61.8 | 64.41 | 79.11 | 77.66 | 51.01 | 61.54 |
macbert4csc_v1 | 68.55 | 96.67 | 65.63 | 48.4 | 75.65 | 38.43 | 51.76 | 70.11 | 80.63 | 85.55 | 81.38 | 57.63 | 70.7 |
macbert4csc_v2 | 68.6 | 96.74 | 66.02 | 48.26 | 75.78 | 38.84 | 51.91 | 70.17 | 80.71 | 85.61 | 80.97 | 58.22 | 69.95 |
macbert4mdcspell_v1 | 71.1 | 96.42 | 70.06 | 52.55 | 79.61 | 43.37 | 53.85 | 70.9 | 82.38 | 87.46 | 84.2 | 61.08 | 71.32 |
qwen25_1 - 5b_pycorrector | 45.11 | 27.29 | 89.48 | 14.61 | 83.9 | 13.84 | 18.2 | 36.71 | 96.29 | 88.2 | 36.41 | 15.64 | 20.73 |
2.2 acc(common_cor_acc)
model/common_cor_acc | avg | gen_de3 | lemon_v2 | gen_passage | text_proof | gen_xxqg | faspell | lomo_tet | mcsc_tet | ecspell | sighan2013 | sighan2014 | sighan2015 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
macbert4csc_pycorrector | 48.26 | 26.96 | 28.68 | 34.16 | 55.29 | 28.38 | 22.2 | 60.96 | 57.16 | 67.73 | 55.9 | 68.93 | 72.73 |
bert4csc_v1 | 60.76 | 88.21 | 45.96 | 43.13 | 68.97 | 35.0 | 34.0 | 65.86 | 73.26 | 81.8 | 64.5 | 61.11 | 67.27 |
macbert4csc_v1 | 65.34 | 93.56 | 49.76 | 44.98 | 74.64 | 36.1 | 37.0 | 73.0 | 83.6 | 86.87 | 69.2 | 62.62 | 72.73 |
macbert4csc_v2 | 65.22 | 93.69 | 50.14 | 44.92 | 74.64 | 36.26 | 37.0 | 72.72 | 83.66 | 86.93 | 68.5 | 62.43 | 71.73 |
macbert4mdcspell_v1 | 67.15 | 93.09 | 54.8 | 47.71 | 78.09 | 39.52 | 38.8 | 71.92 | 84.78 | 88.27 | 73.2 | 63.28 | 72.36 |
qwen25_1 - 5b_pycorrector | 46.09 | 15.82 | 81.29 | 22.96 | 82.17 | 19.04 | 12.8 | 50.2 | 96.4 | 89.13 | 22.8 | 27.87 | 32.55 |
2.3 acc(acc_true, thr = 0.75)
model/acc | avg | acc_rmrb | acc_xxqg |
---|---|---|---|
macbert4csc_pycorrector | 99.24 | 99.22 | 99.26 |
bert4csc_v1 | 98.71 | 98.36 | 99.06 |
macbert4csc_v1 | 97.72 | 96.72 | 98.72 |
macbert4csc_v2 | 97.89 | 96.98 | 98.8 |
macbert4mdcspell_v1 | 97.75 | 96.51 | 98.98 |
qwen25_1 - 5b_pycorrector | 82.0 | 77.14 | 86.86 |
二、結論(Conclusion)
1.macbert4csc_v1/macbert4csc_v2/macbert4mdcspell_v1等模型使用多種領域數據訓練,比較均衡,也適合作為第一步的預訓練模型,可用於專有領域數據的繼續微調;
2.比較macbert4csc_pycorrector/bertbase4csc_v1/macbert4csc_v2/macbert4mdcspell_v1,觀察表2.3,可以發現訓練數據越多,準確率提升的同時,誤糾率也會稍微高一些;
3.MFT(Mask - Correct)依舊有效,不過對於數據量足夠的情形提升不明顯,可能也是誤糾率升高的一個重要原因;
4.訓練數據中也存在文言文數據,訓練好的模型也支持文言文糾錯;
5.訓練好的模型對“地得的”等高頻錯誤具有較高的識別率和糾錯率;
四、論文(Paper)
- 2024 - Refining: Refining Corpora from a Model Calibration Perspective for Chinese
- 2024 - ReLM: Chinese Spelling Correction as Rephrasing Language Model
- 2024 - DICS: DISC: Plug - and - Play Decoding Intervention with Similarity of Characters for Chinese Spelling Check
- 2023 - Bi - DCSpell: A Bi - directional Detector - Corrector Interactive Framework for Chinese Spelling Check
- 2023 - BERT - MFT: Rethinking Masked Language Modeling for Chinese Spelling Correction
- 2023 - PTCSpell: PTCSpell: Pre - trained Corrector Based on Character Shape and Pinyin for Chinese Spelling Correction
- 2023 - DR - CSC: [A Frustratingly Easy Plug - and - Play Detection - and - Reasoning Module for Chinese](https://aclanthology.org/2023.findings - emnlp.771)
- 2023 - DROM: Disentangled Phonetic Representation for Chinese Spelling Correction
- 2023 - EGCM: An Error - Guided Correction Model for Chinese Spelling Error Correction
- 2023 - IGPI: Investigating Glyph - Phonetic Information for Chinese Spell Checking: What Works and What’s Next?
- 2023 - CL: Contextual Similarity is More Valuable than Character Similarity - An Empirical Study for Chinese Spell Checking
- 2022 - CRASpell: [CRASpell: A Contextual Typo Robust Approach to Improve Chinese Spelling Correction](https://aclanthology.org/2022.findings - acl.237)
- 2022 - MDCSpell: [MDCSpell: A Multi - task Detector - Corrector Framework for Chinese Spelling Correction](https://aclanthology.org/2022.findings - acl.98)
- 2022 - SCOPE: Improving Chinese Spelling Check by Character Pronunciation Prediction: The Effects of Adaptivity and Granularity
- 2022 - ECOPO: The Past Mistake is the Future Wisdom: Error - driven Contrastive Probability Optimization for Chinese Spell Checking
- 2021 - MLMPhonetics: [Correcting Chinese Spelling Errors with Phonetic Pre - training](https://aclanthology.org/2021.findings - acl.198)
- 2021 - ChineseBERT: [ChineseBERT: Chinese Pretraining Enhanced by Glyph and Pinyin Information](https://aclanthology.org/2021.acl - long.161/)
- 2021 - BERTCrsGad: [Global Attention Decoder for Chinese Spelling Error Correction](https://aclanthology.org/2021.findings - acl.122)
- 2021 - ThinkTwice: [Think Twice: A Post - Processing Approach for the Chinese Spelling Error Correction](https://www.mdpi.com/2076 - 3417/11/13/5832)
- 2021 - PHMOSpell: [PHMOSpell: Phonological and Morphological Knowledge Guided Chinese Spelling Chec](https://aclanthology.org/2021.acl - long.464)
- 2021 - SpellBERT: [SpellBERT: A Lightweight Pretrained Model for Chinese Spelling Check](https://aclanthology.org/2021.emnlp - main.287)
- 2021 - TwoWays: [Exploration and Exploitation: Two Ways to Improve Chinese Spelling Correction Models](https://aclanthology.org/2021.acl - short.56)
- 2021 - ReaLiSe: Read, Listen, and See: Leveraging Multimodal Information Helps Chinese Spell Checking
- 2021 - DCSpell: DCSpell: A Detector - Corrector Framework for Chinese Spelling Error Correction
- 2021 - PLOME: [PLOME: Pre - training with Misspelled Knowledge for Chinese Spelling Correction](https://aclanthology.org/2021.acl - long.233)
- 2021 - DCN: [Dynamic Connected Networks for Chinese Spelling Check](https://aclanthology.org/2021.findings - acl.216/)
- 2020 - SoftMaskBERT: Spelling Error Correction with Soft - Masked BERT
- 2020 - SpellGCN: SpellGCN:Incorporating Phonological and Visual Similarities into Language Models for Chinese Spelling Check
- 2020 - ChunkCSC: [Chunk - based Chinese Spelling Check with Global Optimization](https://aclanthology.org/2020.findings - emnlp.184)
- 2020 - MacBERT: Revisiting Pre - Trained Models for Chinese Natural Language Processing
- 2019 - FASPell: [FASPell: A Fast, Adaptable, Simple, Powerful Chinese Spell Checker Based On DAE - Decoder Paradigm](https://aclanthology.org/D19 - 5522)
- 2018 - Hybrid: [A Hybrid Approach to Automatic Corpus Generation for Chinese Spelling Checking](https://aclanthology.org/D18 - 1273)
- 2015 - Sighan15: [Introduction to SIGHAN 2015 Bake - off for Chinese Spelling Check](https://aclanthology.org/W15 - 3106/)
- 2014 - Sighan14: [Overview of SIGHAN 2014 Bake - off for Chinese Spelling Check](https://aclanthology.org/W14 - 6820/)
- 2013 - Sighan13: [Chinese Spelling Check Evaluation at SIGHAN Bake - off 2013](https://aclanthology.org/W13 - 4406/)
五、參考(Refer)
- [nghuyong/Chinese - text - correction - papers](https://github.com/nghuyong/Chinese - text - correction - papers)
- destwang/CTCResources
- wangwang110/CSC
- [chinese - poetry/chinese - poetry](https://github.com/chinese - poetry/chinese - poetry)
- [chinese - poetry/huajianji](https://github.com/chinese - poetry/huajianji)
- garychowcmu/daizhigev20
- yangjianxin1/Firefly
- Macropodus/xuexiqiangguo_428w
- Macropodus/csc_clean_wang271k
- Macropodus/csc_eval_public
- shibing624/pycorrector
- iioSnail/MDCSpell_pytorch
- gingasan/lemon
- [Claude - Liu/ReLM](https://github.com/Claude - Liu/ReLM)
六、引用(Cite)
如需引用此項目,可參考當前GitHub項目。例如,使用BibTeX格式:
@software{macro-correct,
url = {https://github.com/yongzhuo/macro-correct},
author = {Yongzhuo Mo},
title = {macro-correct},
year = {2025}
🔧 技術細節
本模型權重為macbert4csc_v2,使用macbert4csc架構(pycorrector版本),其特點是在BertForMaskedLM後新加一個分支用於錯誤檢測任務(分類任務,不交互);訓練時使用了MFT(動態mask 0.2的非錯誤tokens),同時det_loss的權重為0.3;推理時捨棄了macbert後面的部分(det-layer)。
📄 許可證
本項目採用Apache 2.0許可證。
Phi 2 GGUF
其他
Phi-2是微軟開發的一個小型但強大的語言模型,具有27億參數,專注於高效推理和高質量文本生成。
大型語言模型 支持多種語言
P
TheBloke
41.5M
205
Roberta Large
MIT
基於掩碼語言建模目標預訓練的大型英語語言模型,採用改進的BERT訓練方法
大型語言模型 英語
R
FacebookAI
19.4M
212
Distilbert Base Uncased
Apache-2.0
DistilBERT是BERT基礎模型的蒸餾版本,在保持相近性能的同時更輕量高效,適用於序列分類、標記分類等自然語言處理任務。
大型語言模型 英語
D
distilbert
11.1M
669
Llama 3.1 8B Instruct GGUF
Meta Llama 3.1 8B Instruct 是一個多語言大語言模型,針對多語言對話用例進行了優化,在常見的行業基準測試中表現優異。
大型語言模型 英語
L
modularai
9.7M
4
Xlm Roberta Base
MIT
XLM-RoBERTa是基於100種語言的2.5TB過濾CommonCrawl數據預訓練的多語言模型,採用掩碼語言建模目標進行訓練。
大型語言模型 支持多種語言
X
FacebookAI
9.6M
664
Roberta Base
MIT
基於Transformer架構的英語預訓練模型,通過掩碼語言建模目標在海量文本上訓練,支持文本特徵提取和下游任務微調
大型語言模型 英語
R
FacebookAI
9.3M
488
Opt 125m
其他
OPT是由Meta AI發佈的開放預訓練Transformer語言模型套件,參數量從1.25億到1750億,旨在對標GPT-3系列性能,同時促進大規模語言模型的開放研究。
大型語言模型 英語
O
facebook
6.3M
198
1
基於transformers庫的預訓練模型,適用於多種NLP任務
大型語言模型
Transformers

1
unslothai
6.2M
1
Llama 3.1 8B Instruct
Llama 3.1是Meta推出的多語言大語言模型系列,包含8B、70B和405B參數規模,支持8種語言和代碼生成,優化了多語言對話場景。
大型語言模型
Transformers 支持多種語言

L
meta-llama
5.7M
3,898
T5 Base
Apache-2.0
T5基礎版是由Google開發的文本到文本轉換Transformer模型,參數規模2.2億,支持多語言NLP任務。
大型語言模型 支持多種語言
T
google-t5
5.4M
702
精選推薦AI模型
Llama 3 Typhoon V1.5x 8b Instruct
專為泰語設計的80億參數指令模型,性能媲美GPT-3.5-turbo,優化了應用場景、檢索增強生成、受限生成和推理任務
大型語言模型
Transformers 支持多種語言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一個基於SODA數據集訓練的超小型對話模型,專為邊緣設備推理設計,體積僅為Cosmo-3B模型的2%左右。
對話系統
Transformers 英語

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基於RoBERTa架構的中文抽取式問答模型,適用於從給定文本中提取答案的任務。
問答系統 中文
R
uer
2,694
98