项目地址:GitHub - TW-NLP/ChineseErrorCorrector: 中文拼写错误和语法错误纠正

数据集

CTC(拼写纠错数据集)

数据集名称 数据链接 数据量和类别说明 描述
CTC(拼写纠错数据集) 后面整理开源 W271K:279,816 条,Medical:39,303 条,Lemon:22,259 条,ECSpell:6,688 条,CSCD:35,001 条 用于中文拼写纠错的多类别大规模数据集

该数据集包含了来自不同领域的大量中文文本,适用于拼写错误的纠正任务,能够帮助模型学习到丰富的拼写错误类型。

评估结果

评估使用 F1 值指标进行,以下是部分模型的评估结果:

Model Name Model Link Base Model Avg SIGHAN-2015 EC-LAW EC-MED EC-ODW
twnlp/ChineseErrorCorrector-7B https://huggingface.co/twnlp/ChineseErrorCorrector-7B/tree/main Qwen/Qwen2.5-7B-Instruct 0.712 0.592 0.787 0.677 0.793
twnlp/ChineseErrorCorrector-32B-LORA https://huggingface.co/twnlp/ChineseErrorCorrector-32B-LORA/tree/main Qwen/Qwen2.5-32B-Instruct 0.757 0.594 0.776 0.794 0.864

使用方法

transformers

通过 transformers 库,您可以方便地加载和使用中文拼写纠错模型:

# 安装 transformers 库
pip install transformers

以下是使用模型进行拼写纠错的代码示例:

from transformers import AutoModelForCausalLM, AutoTokenizer

checkpoint = "twnlp/ChineseErrorCorrector-7B"
device = "cuda"  # 使用 GPU,若使用 CPU 可设置为 "cpu"
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.08)

print(tokenizer.decode(outputs[0]))

VLLM

使用 VLLM 进行推理,支持快速高效地生成文本:

# 安装 VLLM
pip install vllm

以下是 VLLM 示例代码:

from transformers import AutoTokenizer
from vllm import LLM, SamplingParams

# 初始化 tokenizer
tokenizer = AutoTokenizer.from_pretrained("twnlp/ChineseErrorCorrector-7B")

# 配置解码超参数
sampling_params = SamplingParams(repetition_penalty=1.05, max_tokens=512)

# 初始化 LLM
llm = LLM(model="twnlp/ChineseErrorCorrector-7B")

# 准备输入的文本
prompt = "少先队员因该为老人让坐。"
messages = [
    {"role": "system", "content": "你是一个拼写纠错专家,对原文进行错别字纠正,不要更改原文字数,原文为:"},
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)

# 生成结果
outputs = llm.generate([text], sampling_params)

# 输出结果
for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

总结

ChineseErrorCorrector 提供了一个强大的中文拼写和语法纠错工具,结合了深度学习模型与丰富的行业数据,能够为各种应用场景提供精准的纠错支持。无论是拼写错误还是语法问题,都能在实际场景中得到有效的解决。通过本文提供的代码,您可以快速开始并将其应用于自己的项目中。

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐