# BertBasedCorrectionModels
基于BERT的文本纠错模型,使用PyTorch实现
## 数据准备
1. 从 [http://nlp.ee.ncu.edu.tw/resource/csc.html](http://nlp.ee.ncu.edu.tw/resource/csc.html)下载SIGHAN数据集
2. 解压上述数据集并将文件夹中所有 ''.sgml'' 文件复制至 datasets/csc/ 目录
3. 复制 ''SIGHAN15_CSC_TestInput.txt'' 和 ''SIGHAN15_CSC_TestTruth.txt'' 至 datasets/csc/ 目录
4. 下载 [https://github.com/wdimmy/Automatic-Corpus-Generation/blob/master/corpus/train.sgml](https://github.com/wdimmy/Automatic-Corpus-Generation/blob/master/corpus/train.sgml) 至 datasets/csc 目录
5. 请确保以下文件在 datasets/csc 中
```
train.sgml
B1_training.sgml
C1_training.sgml
SIGHAN15_CSC_A2_Training.sgml
SIGHAN15_CSC_B2_Training.sgml
SIGHAN15_CSC_TestInput.txt
SIGHAN15_CSC_TestTruth.txt
```
## 环境准备
1. 使用已有编码环境或通过 `conda create -n <your_env_name> python=3.7` 创建一个新环境(推荐)
2. 克隆本项目并进入项目根目录
3. 安装所需依赖 `pip install -r requirements.txt`
4. 如果出现报错 GLIBC 版本过低的问题(GLIBC 的版本更迭容易出事故,不推荐更新),openCC 改为安装较低版本(例如 1.1.0)
5. 在当前终端将此目录加入环境变量 `export PYTHONPATH=.`
## 训练
运行以下命令以训练模型,首次运行会自动处理数据。
```shell
python tools/train_csc.py --config_file csc/train_SoftMaskedBert.yml
```
可选择不同配置文件以训练不同模型,目前支持以下配置文件:
- train_bert4csc.yml
- train_macbert4csc.yml
- train_SoftMaskedBert.yml
如有其他需求,可根据需要自行调整配置文件中的参数。
## 实验结果
### SoftMaskedBert
|component|sentence level acc|p|r|f|
|:-:|:-:|:-:|:-:|:-:|
|Detection|0.5045|0.8252|0.8416|0.8333|
|Correction|0.8055|0.9395|0.8748|0.9060|
### Bert类
#### char level
|MODEL|p|r|f|
|:-:|:-:|:-:|:-:|
|BERT4CSC|0.9269|0.8651|0.8949|
|MACBERT4CSC|0.9380|0.8736|0.9047|
#### sentence level
|model|acc|p|r|f|
|:-:|:-:|:-:|:-:|:-:|
|BERT4CSC|0.7990|0.8482|0.7214|0.7797|
|MACBERT4CSC|0.8027|0.8525|0.7251|0.7836|
## 推理
### 方法一,使用inference脚本:
```shell
cd tools
python inference.py --ckpt_fn epoch=0-val_loss=0.03.ckpt --texts "我今天很高心"
推理输出:['我今天很高兴']
# 或给出line by line格式的文本地址
cd tools
python inference.py --ckpt_fn epoch=0-val_loss=0.03.ckpt --text_file ./ml/data/text.txt
推理输出:['我今天很高兴', '你这个辣鸡模型只能做错别字纠正']
```
其中/ml/data/text.txt文本如下:
```text
我今天很高心
你这个辣鸡模型只能做错别字纠正
```
### 方法二,直接调用
```python
from tools.inference import *
ckpt_fn = 'SoftMaskedBert/epoch=02-val_loss=0.02904.ckpt' # find it in checkpoints/
config_file = 'csc/train_SoftMaskedBert.yml' # find it in configs/
model = load_model_directly(ckpt_fn, config_file)
texts = ['今天我很高心', '测试', '继续测试']
model.predict(texts)
推理输出:
['今天我很高兴', '测试', '继续测试']
```
### 方法三、导出bert权重,使用transformers或pycorrector调用
1. 使用convert_to_pure_state_dict.py导出bert权重
2. 后续步骤参考[https://github.com/shibing624/pycorrector/blob/master/examples/macbert/README.md](https://github.com/shibing624/pycorrector/blob/master/examples/macbert/README.md)
## 模型下载
1. SoftMaskedBert、macbert4csc及bert4csc三个模型文件夹及训练参数可从[模型文件](https://pan.baidu.com/s/1TKFFTLuEFXNh-g7xBY0IOg?pwd=za92)下载后放入: BertBasedCorrectionModels/checkpoints/路径下推理使用。
## 引用
如果你在研究中使用了本项目,请按如下格式引用:
```
@article{cai2020pre,
title={BERT Based Correction Models},
author={Cai, Heng and Chen, Dian},
journal={GitHub. Note: https://github.com/gitabtion/BertBasedCorrectionModels},
year={2020}
}
```
## License
本源代码的授权协议为 Apache License 2.0,可免费用做商业用途。请在产品说明中附加本项目的链接和授权协议。本项目受版权法保护,侵权必究。
## 更新记录
### 20240104
1. update the web link in readme file as preivous one is not vaild.
### 20220517
1. trained and add the trained SoftMaskedBert、macbert4csc及bert4csc checkpoint model file download info in README.md
### 20220513
1. new add the model inference postprocess module in tools/inference.py
### 20220511
1. 更新README.md 推理部分方法1+2 code
### 20210618
1. 修复数据处理的编码报错问题
### 20210518
1. 将BERT4CSC检错任务改为使用FocalLoss
2. 更新修改后的模型实验结果
3. 降低数据处理时保留原文的概率
### 20210517
1. 对BERT4CSC模型新增检错任务
2. 新增基于LineByLine文件的inference
## References
1. [Spelling Error Correction with Soft-Masked BERT](https://arxiv.org/abs/2005.07421)
2. [http://ir.itc.ntnu.edu.tw/lre/sighan8csc.html](http://ir.itc.ntnu.edu.tw/lre/sighan8csc.html)
3. [https://github.com/wdimmy/Automatic-Corpus-Generation](https://github.com/wdimmy/Automatic-Corpus-Generation)
4. [transformers](https://huggingface.co/)
5. [https://github.com/sunnyqiny/Confusionset-guided-Pointer-Networks-for-Chinese-Spelling-Check](https://github.com/sunnyqiny/Confusionset-guided-Pointer-Networks-for-Chinese-Spelling-Check)
6. [SoftMaskedBert-PyTorch](https://github.com/gitabtion/SoftMaskedBert-PyTorch)
7. [Deep-Learning-Project-Template](https://github.com/L1aoXingyu/Deep-Learning-Project-Template)
8. [https://github.com/lonePatient/TorchBlocks](https://github.com/lonePatient/TorchBlocks)
9. [https://github.com/shibing624/pycorrector](https://github.com/shibing624/pycorrector)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
<项目介绍> 使用已有编码环境或通过 conda create -n <your_env_name> python=3.7 创建一个新环境(推荐) 克隆本项目并进入项目根目录 安装所需依赖 pip install -r requirements.txt 如果出现报错 GLIBC 版本过低的问题(GLIBC 的版本更迭容易出事故,不推荐更新),openCC 改为安装 - 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------
资源推荐
资源详情
资源评论
收起资源包目录
BertBasedCorrectionModels-master.zip (70个子文件)
BertBasedCorrectionModels-master
tools
convert_to_pure_state_dict.py 1KB
train_csc.sh 85B
train_csc.py 2KB
inference.py 9KB
bases.py 3KB
LICENSE 11KB
configs
csc
train_SoftMaskedBert.yml 458B
train_bert4csc.yml 470B
train_macbert4csc.yml 524B
dict
white_name_list.json 14KB
tests
test_embedding.py 265B
datasets
csc
.gitignore 1B
.gitignore 0B
bbcm
__init__.py 120B
layers
__init__.py 120B
data
__init__.py 120B
loaders
__init__.py 138B
collator.py 1KB
csc.py 362B
__pycache__
csc.cpython-37.pyc 2KB
__init__.cpython-37.pyc 337B
datasets
__init__.py 120B
csc.py 477B
__pycache__
csc.cpython-37.pyc 1KB
__init__.cpython-37.pyc 313B
processors
__init__.py 120B
csc.py 7KB
__pycache__
csc.cpython-37.pyc 5KB
__init__.cpython-37.pyc 315B
build.py 1KB
__pycache__
build.cpython-37.pyc 849B
__init__.cpython-37.pyc 304B
utils
__init__.py 143B
file_io.py 20KB
evaluations.py 9KB
logger.py 899B
__pycache__
file_io.cpython-37.pyc 20KB
evaluations.cpython-37.pyc 2KB
__init__.cpython-37.pyc 334B
logger.cpython-37.pyc 950B
engine
__init__.py 120B
bases.py 801B
__pycache__
__init__.cpython-37.pyc 306B
bases.cpython-37.pyc 1KB
csc_trainer.cpython-37.pyc 3KB
csc_trainer.py 3KB
solver
__init__.py 120B
losses.py 2KB
lr_scheduler.py 6KB
build.py 2KB
__pycache__
build.cpython-37.pyc 855B
__init__.cpython-37.pyc 306B
config
__init__.py 152B
defaults.py 3KB
__pycache__
defaults.cpython-37.pyc 1KB
__init__.cpython-37.pyc 351B
config.py 5KB
modeling
__init__.py 120B
csc
__init__.py 221B
modeling_soft_masked_bert.py 6KB
modeling_bert4csc.py 2KB
__pycache__
modeling_soft_masked_bert.cpython-37.pyc 5KB
modeling_bert4csc.cpython-37.pyc 1KB
__init__.cpython-37.pyc 386B
modeling_abert4csc.py 3KB
__pycache__
__init__.cpython-37.pyc 308B
requirements.txt 190B
checkpoints
.gitignore 1B
.gitignore 1KB
README.md 6KB
共 70 条
- 1
资源评论
Android安卓科研室
- 粉丝: 1528
- 资源: 1164
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功