|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" QA-SRL Bank v2 Dataset""" |
|
|
|
|
|
import datasets |
|
from dataclasses import dataclass |
|
from typing import List, Tuple, Union, Set, Iterable |
|
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} |
|
} |
|
""" |
|
|
|
|
|
_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, a.k.a "QASRL Bank", "QASRL-v2" or "QASRL-LS" (Large Scale), was constructed via crowdsourcing. |
|
""" |
|
|
|
_HOMEPAGE = "https://qasrl.org" |
|
|
|
|
|
_LICENSE = "" |
|
|
|
|
|
_URLs = { |
|
"qasrl_v2": "http://qasrl.org/data/qasrl-v2.tar", |
|
"qasrl_v2_1": "https://qasrl.org/data/qasrl-v2_1.tar" |
|
} |
|
|
|
SpanFeatureType = datasets.Sequence(datasets.Value("int32"), length=2) |
|
|
|
SUPPOERTED_DOMAINS = {"wikinews", "wikipedia", "TQA"} |
|
|
|
@dataclass |
|
class QASRL2018BuilderConfig(datasets.BuilderConfig): |
|
""" Allow the loader to provide a subset of acceptable domains. Acceptable domains are {"wikipedia", "wikinews", "TQA"}. |
|
""" |
|
dataset_version: str = "v2_1" |
|
|
|
domains: Union[str, Iterable[str]] = "all" |
|
|
|
|
|
|
|
class QaSrl2018(datasets.GeneratorBasedBuilder): |
|
"""QA-SRL2018: Large-Scale Question-Answer Driven Semantic Role Labeling corpus""" |
|
|
|
VERSION = datasets.Version("1.2.0") |
|
|
|
BUILDER_CONFIG_CLASS = QASRL2018BuilderConfig |
|
|
|
BUILDER_CONFIGS = [ |
|
QASRL2018BuilderConfig( |
|
name="v2", dataset_version="v2", version=VERSION, |
|
description="This provides WIKIPEDIA dataset for qa_srl corpus (original version from Fitzgerald et. al., 2018)" |
|
), |
|
QASRL2018BuilderConfig( |
|
name="v2_1", dataset_version="v2_1", version=VERSION, |
|
description="This provides WIKIPEDIA dataset for qa_srl corpus (version 2.1)" |
|
), |
|
] |
|
|
|
DEFAULT_CONFIG_NAME = ( |
|
"v2_1" |
|
) |
|
|
|
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( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=features, |
|
|
|
|
|
|
|
supervised_keys=None, |
|
|
|
homepage=_HOMEPAGE, |
|
|
|
license=_LICENSE, |
|
|
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager: datasets.utils.download_manager.DownloadManager): |
|
"""Returns SplitGenerators.""" |
|
|
|
|
|
|
|
qasrl_dataset_version = self.config.dataset_version |
|
corpus_base_path = Path(dl_manager.download_and_extract(_URLs[f"qasrl_{qasrl_dataset_version}"])) |
|
corpus_orig = corpus_base_path / f"qasrl-{qasrl_dataset_version}" / "orig" |
|
|
|
|
|
domains: Set[str] = [] |
|
if self.config.domains == "all": |
|
domains = SUPPOERTED_DOMAINS |
|
elif isinstance(self.config.domains, str): |
|
if self.config.domains in SUPPOERTED_DOMAINS: |
|
domains = {self.config.domains} |
|
else: |
|
raise ValueError(f"Unrecognized domain '{self.config.domains}'; only {SUPPOERTED_DOMAINS} are supported") |
|
else: |
|
domains = set(self.config.domains) & SUPPOERTED_DOMAINS |
|
if len(domains) == 0: |
|
raise ValueError(f"Unrecognized domains '{self.config.domains}'; only {SUPPOERTED_DOMAINS} are supported") |
|
self.config.domains = domains |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"filepath": corpus_orig / "train.jsonl.gz", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
|
|
gen_kwargs={ |
|
"filepath": corpus_orig / "dev.jsonl.gz", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
|
|
gen_kwargs={ |
|
"filepath": corpus_orig / "test.jsonl.gz", |
|
}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath): |
|
|
|
""" Yields examples from a '.jsonl.gz' file .""" |
|
empty_to_underscore = lambda s: "_" if s=="" else s |
|
with gzip.open(filepath, "rt") as f: |
|
qa_counter = 0 |
|
for line in f: |
|
sent_obj = json.loads(line.strip()) |
|
tokens = sent_obj['sentenceTokens'] |
|
sentence = ' '.join(tokens) |
|
sent_id = sent_obj['sentenceId'] |
|
|
|
sent_domain = "TQA" if sent_id.startswith("TQA") else sent_id.split(":")[1] |
|
if sent_domain not in self.config.domains: |
|
continue |
|
for predicate_idx, verb_obj in sent_obj['verbEntries'].items(): |
|
verb_forms = verb_obj['verbInflectedForms'] |
|
predicate = tokens[int(predicate_idx)] |
|
for question_obj in verb_obj['questionLabels'].values(): |
|
question_slots = question_obj['questionSlots'] |
|
verb_form = question_slots['verb'] |
|
verb_surface = verb_forms[verb_form.split(" ")[-1]] |
|
question_slots_in_order = [ |
|
question_slots["wh"], |
|
question_slots["aux"], |
|
question_slots["subj"], |
|
verb_surface, |
|
question_slots["obj"], |
|
empty_to_underscore(question_slots["prep"]), |
|
question_slots["obj2"], |
|
'?' |
|
] |
|
|
|
answer_spans = [] |
|
for ans in question_obj['answerJudgments']: |
|
if ans['isValid']: |
|
answer_spans.extend(ans['spans']) |
|
answer_spans = list(set(tuple(a) for a in answer_spans)) |
|
|
|
answer_strs = [' '.join([tokens[i] for i in range(*span)]) |
|
for span in answer_spans] |
|
|
|
yield qa_counter, { |
|
"sentence": sentence, |
|
"sent_id": sent_id, |
|
"predicate_idx": predicate_idx, |
|
"predicate": predicate, |
|
"is_verbal": True, |
|
"verb_form": verb_forms['stem'], |
|
"question": question_slots_in_order, |
|
"answers": answer_strs, |
|
"answer_ranges": answer_spans |
|
} |
|
qa_counter += 1 |
|
|