Giter Site home page Giter Site logo

xiaoaugenstern / unifiedgec Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ankate/unifiedgec

0.0 0.0 0.0 13.19 MB

Repository of the paper "UnifiedGEC: Integrating Grammatical Error Correction Approaches for Multi-languages with a Unified Framework"

License: Apache License 2.0

Shell 2.86% C++ 0.57% Python 95.38% Lua 0.10% Cuda 0.83% Makefile 0.01% Batchfile 0.02% Cython 0.24%

unifiedgec's Introduction

UnifiedGEC: Integrating Grammatical Error Correction Approaches for Multi-languages with a Unified Framework

English | 简体中文

UnifiedGEC是一个面向GEC设计的开源框架,集成了5个不同架构的GEC模型和7个不同语种的GEC数据集。我们的框架结构如图所示,提供了dataset、dataloader、evaluator、model、trainer等模块的抽象类,允许用户自行实现相关模块,具有非常好的扩展性。

用户可以通过一行简单的命令,调用指定的模型在数据集上进行训练。此外,通过额外的命令行参数,用户可以使用我们提供的数据增强方法来应对低资源情况,或是调用我们给出的prompt进行LLM相关的实验。

框架特点

  • 易于使用:UnifiedGEC为用户提供了非常方便的调用方式,只需要在命令行中输入指令,指定使用的模型、数据集,即可迅速便捷地开始训练或推理。调整参数、使用数据增强或prompt模块,也只需要一行指令即可启动。
  • 模块化设计,扩展性强:UnifiedGEC包含了Dataset、Dataloader、Config等多个模块,并提供了相应的抽象类。用户可以通过继承相应的类,轻松地实现自己的方法。
  • 集成内容多,综合性强:UnifiedGEC集成了3个Seq2Seq模型、2个Seq2Edit模型、2个中文数据集、2个英文数据集及3个小语种数据集,并且在上述的数据集上,对5个模型的性能进行了评估,为用户提供了有关GEC任务和模型更为全面的认识。

框架结构

UnifiedGEC的完整结构如下:

.
|-- gectoolkit  # 框架的主要代码
    |-- config  # 全局配置及Config类
    |-- data    # Dataset和Dataloader的抽象类
    |-- evaluate    # Evaluator抽象类及GEC Evaluator
    |-- llm     # prompts for LLMs
    |-- model   # Model抽象类及已经集成的模型
    |-- module  # 可复用的组件(Transformer Layer)
    |-- properties  # 模型的详细配置
    |-- trainer # Trainer的抽象类和supervised_trainer
    |-- utils   # 使用到的其他代码
    |-- quick_start.py      # run_gectoolkit.py通过调用该代码启动框架
|-- log         # 训练日志
|-- checkpoint  # 训练结果及checkpoint
|-- dataset     # 处理后的json格式数据集
|-- augmentation    # 数据增强模块
    |-- data    # error patterns方法所需的依赖项
    |-- translation_model   # back-translation方法使用的预训练模型
    |-- noise_pattern.py    # 添加噪声的数据增强方法(error patterns)
    |-- translation.py      # 翻译成其他语言再翻译回源语言的数据增强方法(back-translation)
|-- evaluation  # 评估工具及转换脚本
    |-- m2scorer    # M2Scorer, 适用于NLPCC18、CoNLL14、FCE
    |-- errant      # ERRANT, 适用于AKCES、Falko-MERLIN、Cowsl2h
    |-- cherrant    # ChERRANT, 适用于MuCGEC
    |-- convert.py  # 将生成结果转化为待评估格式的脚本
|-- run_gectoolkit.py       # 框架的启动文件

模型

我们在框架中集成了5个GEC模型,按照架构可以分成Seq2SeqSeq2Edit两类,如下表所示:

type model reference
Seq2Seq Transformer (Vaswani et al., 2017)
T5 (Xue et al., 2021)
SynGEC (Zhang et al., 2022)
Seq2Edit Levenshtein Transformer (Gu et al., 2019)
GECToR (Omelianchuk et al., 2020)

数据集

我们在框架中集成了7个GEC数据集,包含中文、英语、西语、捷克语和德语:

dataset language reference
FCE English (Yannakoudakis et al., 2011)
CoNLL14 English (Ng et al., 2014)
NLPCC18 Chinese (Zhao et al., 2018)
MuCGEC Chinese (Zhang et al., 2022)
COWSL2H Spanish (Yamada et al., 2020)
Falko-MERLIN German (Boyd et al., 2014)
AKCES-GEC Czech (Náplava et al., 2019)

UnifiedGEC集成的数据集均是处理完成的json格式:

[
    {
        "id": 0,
        "source_text": "My town is a medium size city with eighty thousand inhabitants .",
        "target_text": "My town is a medium - sized city with eighty thousand inhabitants ."
    }
]

我们使用的处理后数据集可以从此处下载

Quick Start

环境

我们的框架使用Python 3.8,请先安装allennlp 1.3.0,再安装其他依赖:

pip install allennlp==1.3.0
pip install -r requirements.txt

注:在conda环境下使用pip安装allennlp时,jsonnet依赖项可能报错,可以使用conda install jsonnet完成安装。

模型调用

使用前,请先创建存放日志和checkpoint的目录:

mkdir log
mkdir checkpoint

使用时,输入指令:

python run_gectoolkit.py -m $MODEL_NAME -d $DATASET_NAME

训练轮数、学习率等参数配置请见./gectoolkit/config/config.json文件,模型的详细参数请见./gectoolkit/properties/model/下的对应配置。

除Transformer外的其他模型需要使用到预训练模型,请下载后存储至./gectoolkit/properties/model/对应的模型目录下。我们提供部分预训练模型的下载地址,用户也可以前往huggingface自行下载。

UnifiedGEC也支持通过命令行修改对应参数:

python run_gectoolkit.py -m $MODEL_NAME -d $DATASET_NAME --learning_rate $LR

新增数据集

我们的框架支持用户自行添加数据集,添加的数据集文件夹dataset_name/需要包含训练集、验证集和测试集三个JSON格式的文件,并且放置于dataset/目录下:

dataset
    |-- dataset_name
        |-- trainset.json
        |-- validset.json
        |-- testset.json

另外,还需要在gectoolkit/properties/dataset/目录下新增相应的数据集配置文件,内容可以参考同目录下的其他数据集配置。

完成配置后,就可以通过上述的命令行参数运行新增的数据集。

数据增强

我们提供了两种数据增强方法(暂时只支持中英文):

  • error patterns:以增、删、改的方式向句子中添加噪声
  • back-translation:将句子翻译成另一语言,再翻译回原本的语言

使用数据增强模块时,在命令行中添加augment参数,可选的值分为noise和translation:

python run_gectoolkit.py -m $MODEL_NAME -d $DATASET_NAME --augment noise

初次使用时,UnifieGEC执行数据增强方法并生成相应的文件到本地,translation方法可能需要比较久的时间来生成。再次执行时,UnifiedGEC会直接使用先前生成的增强数据。

Prompts

我们提供了中英文的prompt,包括zero-shot和few-shot两种设置。

调用prompt时,需要在命令行中使用use_llm参数,并且通过example_num参数指定In-context learning的样例数量:

python run_gectoolkit.py -m $MODEL_NAME -d $ DATASET_NAME --use_llm --example_num $EXAMPLE_NUM

此处使用的模型名称应该是huggingface的LLM名称,如Qwen/Qwen-7B-chat

Evaluation

我们在Evaluation模块中集成了GEC任务上主流的评估工具,包括M2Scorer、ERRANT与ChERRANT。此外,还提供了转换脚本及部分数据集的ground truth。UnifiedGEC框架在训练过程中计算得到的是Micro-level PRF,用户如果希望得到Macro-level PRF,可以使用本模块对生成结果进行整体评估。

首先使用脚本将json格式的输出结果转化为评估工具对应的格式:

python convert.py --predict_file $PREDICT_FILE --dataset $DATASET

此处的dataset参数和评估工具的对应关系:

数据集 评估工具
CoNLL14、FCE、NLPCC18 M2Scorer
AKCES-GEC、Falko-MERLIN、COWSL2H ERRANT
MuCGEC ChERRANT

然后进入对应文件夹进行后续处理。

M2Scorer

官方仓库:https://github.com/nusnlp/m2scorer

对于英语数据集 (CoNLL14、FCE),直接使用m2scorer进行评估即可:

cd m2scorer
m2scorer/m2scorer predict.txt m2scorer/conll14.gold

对于中文数据集 (NLPCC18),需要先使用pkunlp工具进行分词。我们同样提供了转换脚本:

cd m2scorer
python pkunlp/convert_output.py --input_file predict.txt --output_file seg_predict.txt
m2scorer/m2scorer seg_predict.txt m2scorer/nlpcc18.gold

ERRANT

官方仓库:https://github.com/chrisjbryant/errant

使用方法参考官方仓库:

cd errant
errant_parallel -orig source.txt -cor target.txt -out ref.m2
errant_parallel -orig source.txt -cor predict.txt -out hyp.m2
errant_compare -hyp hyp.m2 -ref ref.m2

ChERRANT

官方仓库:https://github.com/HillZhang1999/MuCGEC

使用方法参考官方仓库:

cd cherrant/ChERRANT
python parallel_to_m2.py -f ../hyp.txt -o hyp.m2 -g char
python compare_m2_for_evaluation.py -hyp hyp.m2 -ref ref.m2

实验结果

模型表现

UnifiedGEC共集成了5个模型和7个不同语言的GEC数据集,各模型在中英文的数据集上测得的最好表现如下:

model dataset
CoNLL14(EN) FCE(EN) NLPCC18(ZH) MuCGEC(ZH)
P R F0.5 P R F0.5 P R F0.5 P R F0.5
Levenshtein Transformer 13.5 12.6 13.3 6.3 6.9 6.4 12.6 8.5 10.7 6.6 6.4 6.6
GECToR 52.3 21.7 40.8 36.0 20.7 31.3 30.9 20.9 28.2 33.5 19.1 29.1
Transformer 24.1 15.5 21.7 20.8 15.9 19.6 22.3 20.8 22.0 19.7 9.2 16.0
T5 36.6 39.5 37.1 29.2 29.4 29.3 32.5 21.1 29.4 30.2 14.4 24.8
SynGEC 50.6 51.8 50.9 59.5 52.7 58.0 36.0 36.8 36.2 22.3 26.2 23.6

各模型在小语种数据集上的最好表现如下:

model dataset
AKCES-GEC(CS) Falko-MERLIN(DE) COWSL2H
P R F0.5 P R F0.5 P R F0.5
Levenshtein Transformer 4.4 5.0 4.5 2.3 4.2 2.5 1.9 2.3 2.0
GECToR 46.8 8.9 25.3 50.8 20.5 39.2 24.4 12.9 20.7
Transformer 44.4 23.6 37.8 33.1 18.7 28.7 11.8 15.0 12.3
T5 52.5 40.5 49.6 47.4 50.0 47.9 53.7 39.1 49.9
SynGEC 21.9 27.6 22.8 32.2 33.4 32.4 9.3 18.8 10.3

数据增强

我们在NLPCC18和CoNLL14上做了实验,选取10%的数据来模拟低资源任务的情况:

model data augmentation methods dataset
CoNLL14 NLPCC18
F0.5 delta F0.5 delta
Levenshtein Transformer w/o augmentation 9.5 - 6.0 -
w/ error patterns 6.4 -3.1 4.9 -1.1
w/ back-translation 12.5 3.0 5.9 -0.1
GECToR w/o augmentation 14.2 - 17.4 -
w/ error patterns 15.1 0.9 19.9 2.5
w/ back-translation 16.7 2.5 19.4 2.0
Transformer w/o augmentation 12.6 - 9.5 -
w/ error patterns 14.5 1.9 9.9 0.4
w/ back-translation 16.6 4.0 10.4 0.9
T5 w/o augmentation 31.7 - 26.3 -
w/ error patterns 32.0 0.3 27.0 0.7
w/ back-translation 32.2 0.5 24.1 -2.2
SynGEC w/o augmentation 47.7 - 32.4 -
w/ error patterns 48.2 0.5 34.9 2.5
w/ back-translation 47.7 0.0 34.6 2.2

Prompts

我们使用Qwen1.5-14B-chatLlama2-7B-chat,在NLPCC18和CoNLL14数据集上对提供的prompt进行了测试,得到的最好结果如下:

Setting Dataset
CoNLL14 NLPCC18
P R F0.5 P R F0.5
zero-shot 48.8 49.1 48.8 24.7 38.3 26.6
few-shot 50.4 50.2 50.4 24.8 39.8 26.8

License

UnifiedGEC使用Apache License.

unifiedgec's People

Contributors

ankate avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.