项目地址:GitHub - TW-NLP/ChineseErrorCorrector: 中文拼写错误和语法错误纠正
文本纠错任务在审查、写作任务中至关重要,以前的纠错大多采用小模型进行训练,例如BART、T5、BERT等,但是小模型的泛化性较差,需要在不同领域训练不同的小模型进行纠错,为此我们使用200万数据进行大模型的训练,经过验证我们在GitHub - masr2000/NaCGEC数据集上,F1值比华为高10个点,遥遥领先,下面从三个方面进行详细的技术说明:数据集(涵盖业界所有的开源数据)、评估结果、使用方法,欢迎star,后续会持续更新纠错模型。
1、数据集
数据集名称 | 数据链接 | 数据量和类别说明 | 描述 |
---|---|---|---|
CSC(拼写纠错数据集) | twnlp/csc_data | W271K:279,816 条,Medical:39,303 条,Lemon:22,259 条,ECSpell:6,688 条,CSCD:35,001 条 | 中文拼写纠错的数据集 |
CGC(语法纠错数据集) | twnlp/cgc_data | CGED:20449 条,FCGEC:37354 条,MuCGEC:2467 条,NaSGEC:7568条 | 中文语法纠错的数据集 |
Lang8+HSK(百万语料-拼写和语法错误混合数据集) | twnlp/lang8_hsk | 1568885条 | 中文拼写和语法数据集 |
项目包含三个部分的数据集,分别为CSC、CGC和Lang8+HSK,涵盖了所有开源高质量的拼写纠错和语法纠错的数据集,也是我们分阶段训练的数据。
2、评估结果
Model Name | Model Link | Prec | Rec | F0.5 |
---|---|---|---|---|
twnlp/ChineseErrorCorrector2-7B | https://huggingface.co/twnlp/ChineseErrorCorrector2-7B | 0.6233 | 0.6228 | 0.6232 |
HW_TSC_nlpcc2023_cgec(华为) | 未开源 | 0.5095 | 0.3129 | 0.4526 |
鱼饼啾啾Plus | 未开源 | 0.5708 | 0.1294 | 0.3394 |
CUHK_SU | 未开源 | 0.3882 | 0.1558 | 0.2990 |
CGEC++ | 未开源 | 0.2414 | 0.0735 | 0.1657 |
zhao_jia | 未开源 | 0.1719 | 0.1478 | 0.1665 |
我们在NaCGEC数据集上,比最高的华为要高17个点,实测效果也很不错,强力推荐!
3、使用方法
transformers
通过 transformers
库,您可以方便地加载和使用中文拼写纠错模型:
# 安装 transformers 库
pip install transformers

以下是使用模型进行拼写纠错的代码示例:
# pip install transformers
from transformers import AutoModelForCausalLM, AutoTokenizer
checkpoint = "twnlp/ChineseErrorCorrector2-7B"
device = "cuda" # for GPU usage or "cpu" for CPU usage
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
input_content = "你是一个文本纠错专家,纠正输入句子中的语法错误,并输出正确的句子,输入句子为:\n少先队员因该为老人让坐。"
messages = [{"role": "user", "content": input_content}]
input_text=tokenizer.apply_chat_template(messages, tokenize=False)
print(input_text)
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
outputs = model.generate(inputs, max_new_tokens=1024, temperature=0, do_sample=False, repetition_penalty=1)
print(tokenizer.decode(outputs[0]))

VLLM
使用 VLLM
进行推理,支持快速高效地生成文本:
# 安装 VLLM
pip install vllm

以下是 VLLM
示例代码:
from transformers import AutoTokenizer
from vllm import LLM, SamplingParams
# Initialize the tokenizer
tokenizer = AutoTokenizer.from_pretrained("twnlp/ChineseErrorCorrector2-7B")
# Pass the default decoding hyperparameters of twnlp/ChineseErrorCorrector-7B
# max_tokens is for the maximum length for generation.
sampling_params = SamplingParams(seed=42, max_tokens=512)
# Input the model name or path. Can be GPTQ or AWQ models.
llm = LLM(model="twnlp/ChineseErrorCorrector2-7B")
# Prepare your prompts
prompt = "少先队员因该为老人让坐。"
messages = [
{"role": "user", "content": "你是一个文本纠错专家,纠正输入句子中的语法错误,并输出正确的句子,输入句子为:"+prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
# generate outputs
outputs = llm.generate([text], sampling_params)
# Print the outputs.
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
python

总结
ChineseErrorCorrector
是一个强大的中文拼写和语法纠错工具,开箱即用,后面会不断的跟进前沿的纠错方法和数据,不断更新开源模型。