SRL S2S

Python 2025-07-26

语义角色标签的编码器模型

该存储库包含:

  • 论文:翻译和标签!用于语义角色标签的编码器解码器方法(EMNLP 2019介绍)
  • 作者:Angel Daza,Anette Frank

该代码在Allennlp工具包上运行。

要求

  • Python 3.6
  • Pytorch 1.0
  • Allennlp 0.8.2
  • Flair 0.4.3(用于谓词预测)

入门

设置环境

  1. 使用Anaconda创建SRL-S2S环境
 conda create -n SRL-S2S python=3.6
  1. 激活环境
 source activate SRL-S2S
  1. 在环境中安装要求:

安装Pytorch 1.0(建议使用CUDA 8的GPU版本):

 conda install pytorch torchvision cuda80 -c pytorch

安装进一步的依赖...

 bash scripts/install_requirements.sh

注意:在Mac OS X 10.14 [Mojave]上安装AllennLP时,有一些报告的问题(尤其是JSONNET模块错误)。如果安装失败,请运行以下命令:

 xcode-select --install
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

然后再次运行install_requirlements脚本。如果问题持续存在,请尝试以下解决方法之一:

Allenai/Allennlp#1938

Google/jsonnet#573

简短教程

我们以玩具示例显示:

  • 我们的模型可以阅读的预处理和构建数据集
  • 训练模型
  • 使用训练有素的模型预测新输出

预处理

所有模型都需要JSON文件作为输入。在pre-processing文件夹中,我们包括脚本CoNLL_to_JSON.py ,以按照conll-U数据格式将文件转换为合适的输入JSON数据集。

也可以使用Text_to_JSON.py脚本将任何文本文件转换为我们的JSON格式(包括并行机器翻译文件)。

最简单的情况是将conll文件转换为json,其中源序列是句子(仅词),而目标序列是标记的句子。构建一个单语数据集以进行培训:

" --token_type CoNLL09">
 python pre_processing/CoNLL_to_JSON.py 
	--source_file datasets/raw/CoNLL2009-ST-English-trial.txt 
	--output_file datasets/json/EN_conll09_trial.json 
	--dataset_type mono 
	--src_lang "" 
	--token_type CoNLL09

JSON文件中的每一行EN_conll09_trial.json看起来像这样:

", "tgt_lang": "", "seq_marked": ["The", "economy", "'s", "temperature", "will", "be", "", "taken", "from", "several", "vantage", "points", "this", "week", ",", "with", "readings", "on", "trade", ",", "output", ",", "housing", "and", "inflation", "."] }">
 {
   "seq_words": ["The", "economy", "'s", "temperature", "will", "be", "taken", "from", "several", "vantage", "points", "this", "week", ",", "with", "readings", "on", "trade", ",", "output", ",", "housing", "and", "inflation", "."], 
   "BIO": ["O", "O", "O", "B-A1", "B-AM-MOD", "O", "B-V", "B-A2", "O", "O", "O", "O", "B-AM-TMP", "O", "B-AM-ADV", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"], 
   "pred_sense": [6, "taken", "take.01", "VBN"], 
   "seq_tag_tokens": ["The", "economy", "'s", "(#", "temperature", "A1)", "(#", "will", "AM-MOD)", "be", "(#", "taken", "V)", "(#", "from", "A2)", "several", "vantage", "points", "this", "(#", "week", "AM-TMP)", ",", "(#", "with", "AM-ADV)", "readings", "on", "trade", ",", "output", ",", "housing", "and", "inflation", "."], 
   "src_lang": "", 
   "tgt_lang": "", 
   "seq_marked": ["The", "economy", "'s", "temperature", "will", "be", "", "taken", "from", "several", "vantage", "points", "this", "week", ",", "with", "readings", "on", "trade", ",", "output", ",", "housing", "and", "inflation", "."]

}

要构建一个跨语言数据集(例如,在目标侧的英语句子作为源和德语标记的序列)运行:

" --tgt_lang """>
 python pre_processing/CoNLL_to_JSON.py 
	--source_file datasets/raw/CrossLang_ENDE_EN_trial.txt 
	--target_file datasets/raw/CrossLang_ENDE_DE_trial.conll09 
	--output_file datasets/json/En2DeSRL.json 
	--dataset_type cross 
	--src_lang "" 
	--tgt_lang ""

JSON文件En2DeSRL.json中的每一行都会像这样:

", "tgt_lang": "" }">
 {
	"seq_words": ["We", "need", "to", "take", "this", "responsibility", "seriously", "."], 
	"BIO": ["O", "B-V", "O", "O", "O", "O", "O", "O"], 
	"pred_sense_origin": [1, "need", "need.01", "V"], 
	"pred_sense": [1, "mu00fcssen", "need.01", "VMFIN"], 
	"seq_tag_tokens": ["(#", "Wir", "A0)", "(#", "mu00fcssen", "V)", "diese", "Verantwortung", "ernst", "nehmen", "."], 
	"src_lang": "", 
	"tgt_lang": ""
}

最后,要创建一个并行MT数据的JSON数据集文件(例如,具有英语 - 及格翻译的Europarl文件)可以运行:

" --tgt_key """>
 python pre_processing/Text_to_JSON.py --path datasets/raw/ 
            --source_file mini_europarl-v7.de-en.en 
            --target_file mini_europarl-v7.de-en.de 
            --output datasets/json/MiniEuroparl.en_to_de.json 
            --src_key "" --tgt_key ""

脚本pre-processing/make_all_trial.py嵌入所有预处理选项和可用数据集类型。

训练模型

  • 模型配置可在training_config文件夹和子文件夹中找到。请注意,在此配置文件中,可以操纵模型超参数,并指向所需的数据集。
  • 要训练模型,请选择一个实验配置文件(例如training_config/test/en_copynet-srl-conll09.json ),然后在主目录中运行以下命令:
 allennlp train training_config/test/en_copynet-srl-conll09.json -s saved_models/example-srl-en/ --include-package src

  • 结果和培训信息将存储在saved_models/example-srl-en目录中。
  • 注意: training_config内部包含用于纸的实验的模型超参数,应以相同的方式进行培训。

使用训练有素的模型

将txt文件转换为json

在推理时,只需要提供一个file.txt ,每行一个句子。这样,我们可以使用天赋来预测句子内部的谓词,然后使用我们的模型来预测每个谓词的SRL标签。

首先,我们需要将输入转换为JSON格式并给出所需的目标语言(例如,如果我们想要标记为德语,我们应该指示tgt_key as):

" --tgt_key "" --predict_frames True --sense_dict datasets/aux/En_De_TopSenses.tsv">
 python pre_processing/Text_to_JSON.py --source_file datasets/raw/mini_europarl-v7.de-en.en 
             --output datasets/test/MiniEuroparl.PREDICT.json 
             --src_key "" --tgt_key "" 
             --predict_frames True 
             --sense_dict datasets/aux/En_De_TopSenses.tsv

得到预测

使用训练有素的模型进行预测(使用开发集中最佳BLEU得分的检查点)运行:

 allennlp predict saved_models/example-srl-en/model.tar.gz datasets/test/MiniEuroparl.PREDICT.json 
	--output-file saved_models/example-srl-en/output_trial.json 
	--include-package src 
	--predictor seq2seq-srl

其中EN_conll09_trial_to_predict.json包含要预测的源序列。

请注意,提供这些文件只是为了提供工作流程的示例,因此使用这些设置的预测将是随机的!

再现结果

为了重现本文中的结果,有必要拥有CONLL-2005和CONLL-2009共享任务数据集的许可证:

  • CONLL-2005共享任务,这是Penn Treebank数据集的一部分
  • CONLL-2009共享任务:第1部分和第2部分是LDC目录的一部分

在这里,需要公开提供法语的SRL数据(需要注册)。

使用的机器翻译公司是:

  • Europarl(英语 - 德国)
  • 联合国(英语法国)

向我们的培训的跨语言SRL数据要求:

为多语种语义角色标签生成高质量的命题银行(Akbik等,2015)。

我们在此存储库的training_config文件夹中包括了每个实验设置(单语,多语言和跨语义)的配置文件。他们必须以与上一个教程相似的方式运行。

下载源码

通过命令行克隆项目:

git clone https://github.com/Heidelberg-NLP/SRL-S2S.git