qa_srl / qa_srl.py
kleinay's picture
first commit
5b94fa5
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Dataset loading script for loading the Large-Scale-QASRL (FitzGeralds et. al., ACL 2018) training set, along with the QASRL-GS evaluation dataset (Roit et. al., ACL 2020)."""
import datasets
from pathlib import Path
import gzip
import json
_CITATION = """\
@inproceedings{fitzgerald2018large,
title={Large-Scale QA-SRL Parsing},
author={FitzGerald, Nicholas and Michael, Julian and He, Luheng and Zettlemoyer, Luke},
booktitle={Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)},
pages={2051--2060},
year={2018}
}
@inproceedings{roit2020controlled,
title={Controlled Crowdsourcing for High-Quality QA-SRL Annotation},
author={Roit, Paul and Klein, Ayal and Stepanov, Daniela and Mamou, Jonathan and Michael, Julian and Stanovsky, Gabriel and Zettlemoyer, Luke and Dagan, Ido},
booktitle={Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics},
pages={7008--7013},
year={2020}
}
"""
_DESCRIPTION = """\
The dataset contains question-answer pairs to model verbal predicate-argument structure.
The questions start with wh-words (Who, What, Where, What, etc.) and contain a verb predicate in the sentence; the answers are phrases in the sentence.
This dataset loads the train split from "QASRL Bank", a.k.a "QASRL-v2" or "QASRL-LS" (Large Scale),
which was constructed via crowdsourcing and presented at (FitzGeralds et. al., ACL 2018),
and the dev and test splits from QASRL-GS (Gold Standard), introduced in (Roit et. al., ACL 2020).
"""
_HOMEPAGE = "https://qasrl.org"
# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""
SpanFeatureType = datasets.Sequence(datasets.Value("int32"), length=2)
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
class QaSrl(datasets.GeneratorBasedBuilder):
"""QA-SRL: Question-Answer Driven Semantic Role Labeling corpus"""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="plain_text", version=VERSION, description=""
),
]
DEFAULT_CONFIG_NAME = (
"plain_text" # It's not mandatory to have a default configuration. Just use one if it make sense.
)
def _info(self):
features = datasets.Features(
{
"sentence": datasets.Value("string"),
"sent_id": datasets.Value("string"),
"predicate_idx": datasets.Value("int32"),
"predicate": datasets.Value("string"),
"is_verbal": datasets.Value("bool"),
"verb_form": datasets.Value("string"),
"question": datasets.Sequence(datasets.Value("string")),
"answers": datasets.Sequence(datasets.Value("string")),
"answer_ranges": datasets.Sequence(SpanFeatureType)
}
)
return datasets.DatasetInfo(
# This is the description that will appear on the datasets page.
description=_DESCRIPTION,
# This defines the different columns of the dataset and their types
features=features, # Here we define them above because they are different between the two configurations
# If there's a common (input, target) tuple from the features,
# specify them here. They'll be used if as_supervised=True in
# builder.as_dataset.
supervised_keys=None,
# Homepage of the dataset for documentation
homepage=_HOMEPAGE,
# License for the dataset if available
license=_LICENSE,
# Citation for the dataset
citation=_CITATION,
)
def _split_generators(self, dl_manager: datasets.utils.download_manager.DownloadManager):
"""Returns SplitGenerators."""
# iterate the tar file of the corpus
# Older version of the corpus (has some format errors):
# corpus_base_path = Path(dl_manager.download_and_extract(_URLs["qasrl_v2.0"]))
# corpus_orig = corpus_base_path / "qasrl-v2" / "orig"
self.qasrl2018 = datasets.load_dataset("biu-nlp/qa_srl2018")
self.qasrl2020 = datasets.load_dataset("biu-nlp/qa_srl2020")
# TODO add optional kwarg for genre (wikinews)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"dataset": self.qasrl2018["train"],
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"dataset": self.qasrl2020["validation"],
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"dataset": self.qasrl2020["test"],
},
),
]
def _generate_examples(self, dataset):
""" Yields examples from a '.jsonl.gz' file ."""
for idx, instance in enumerate(dataset):
yield idx, instance