File size: 18,321 Bytes
b0b4dd0 3a1ebc9 5499db7 b0b4dd0 19851dc b0b4dd0 19851dc b0b4dd0 5499db7 3a1ebc9 19851dc 5499db7 3a1ebc9 b0b4dd0 5499db7 3a1ebc9 b0b4dd0 3a1ebc9 b0b4dd0 3a1ebc9 b0b4dd0 19851dc 5499db7 19851dc 3a1ebc9 b0b4dd0 3a1ebc9 b0b4dd0 3a1ebc9 b0b4dd0 3a1ebc9 b0b4dd0 19851dc 5499db7 19851dc 5499db7 19851dc b0b4dd0 19851dc b0b4dd0 3a1ebc9 b0b4dd0 5499db7 b0b4dd0 92f0cf8 b0b4dd0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# 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.
"""A Dataset loading script for the QANom dataset (klein et. al., COLING 2000)."""
from dataclasses import dataclass
from typing import Optional, Tuple, Union, Iterable, Set
import datasets
from pathlib import Path
import pandas as pd
import gzip
import json
import itertools
_CITATION = """\
@inproceedings{klein2020qanom,
title={QANom: Question-Answer driven SRL for Nominalizations},
author={Klein, Ayal and Mamou, Jonathan and Pyatkin, Valentina and Stepanov, Daniela and He, Hangfeng and Roth, Dan and Zettlemoyer, Luke and Dagan, Ido},
booktitle={Proceedings of the 28th International Conference on Computational Linguistics},
pages={3069--3083},
year={2020}
}
"""
_DESCRIPTION = """\
The dataset contains question-answer pairs to model predicate-argument structure of deverbal nominalizations.
The questions start with wh-words (Who, What, Where, What, etc.) and contain a the verbal form of a nominalization from the sentence;
the answers are phrases in the sentence.
See the paper for details: QANom: Question-Answer driven SRL for Nominalizations (Klein et. al., COLING 2020)
For previewing the QANom data along with the verbal annotations of QASRL, check out "https://browse.qasrl.org/".
This dataset was annotated by selected workers from Amazon Mechanical Turk.
"""
_HOMEPAGE = "https://github.com/kleinay/QANom"
_LICENSE = """MIT License
Copyright (c) 2020 Ayal Klein (kleinay)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE."""
_URLs = {
"qanom_csv": "https://github.com/kleinay/QANom/raw/master/qanom_dataset.zip",
"qanom_jsonl": "https://qasrl.org/data/qanom.tar"
}
SpanFeatureType = datasets.Sequence(datasets.Value("int32"), length=2)
SUPPOERTED_DOMAINS = {"wikinews", "wikipedia"}
@dataclass
class QANomBuilderConfig(datasets.BuilderConfig):
""" Allow the loader to re-distribute the original dev and test splits between train, dev and test. """
redistribute_dev: Tuple[float, float, float] = (0., 1., 0.)
redistribute_test: Tuple[float, float, float] = (0., 0., 1.)
load_from: str = "jsonl" # "csv" or "jsonl"
domains: Union[str, Iterable[str]] = "all" # can provide also a subset of acceptable domains.
# Acceptable domains are {"wikipedia", "wikinews"} for dev and test (qasrl-2020)
# and {"wikipedia", "wikinews", "TQA"} for train (qasrl-2018)
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
class Qanom(datasets.GeneratorBasedBuilder):
"""QANom: Question-Answer driven SRL for Nominalizations corpus.
Notice: This datasets genrally follows the format of `qa_srl` and `kleinay\qa_srl2018` datasets.
However, it extends Features to include "is_verbal" and "verb_form" fields (required for nominalizations).
In addition, and most critically, unlike these verbal qasrl datasets, in the qanom datset some examples
are for canidate nominalization which are judged to be non-predicates ("is_verbal"==False) or predicates with no QAs.
In these cases, the qa fields (question, answers, answer_ranges) would be empty lists. """
VERSION = datasets.Version("1.2.0")
BUILDER_CONFIG_CLASS = QANomBuilderConfig
BUILDER_CONFIGS = [
QANomBuilderConfig(
name="default", version=VERSION, description="This provides the QANom dataset"#, redistribute_dev=(0,1,0)
),
]
DEFAULT_CONFIG_NAME = (
"default" # 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 _prepare_wiktionary_verb_inflections(self, dl_manager):
wiktionary_url = "https://raw.githubusercontent.com/nafitzgerald/nrl-qasrl/master/data/wiktionary/en_verb_inflections.txt"
wiktionary_path = dl_manager.download(wiktionary_url)
verb_map = {}
with open(wiktionary_path, 'r', encoding="utf-8") as f:
for l in f.readlines():
inflections = l.strip().split('\t')
stem, presentsingular3rd, presentparticiple, past, pastparticiple = inflections
for inf in inflections:
verb_map[inf] = {"Stem" : stem, "PresentSingular3rd" : presentsingular3rd, "PresentParticiple":presentparticiple, "Past":past, "PastParticiple":pastparticiple}
self.verb_inflections = verb_map
def _split_generators(self, dl_manager: datasets.utils.download_manager.DownloadManager):
"""Returns SplitGenerators."""
assert self.config.load_from in ("csv", "jsonl")
# Handle domain selection
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
self.corpus_base_path = Path(dl_manager.download_and_extract(_URLs[f"qanom_{self.config.load_from}"]))
if self.config.load_from == "csv":
# prepare wiktionary for verb inflections inside 'self.verb_inflections'
self._prepare_wiktionary_verb_inflections(dl_manager)
self.dataset_files = [
self.corpus_base_path / "annot.train.csv",
self.corpus_base_path / "annot.dev.csv",
self.corpus_base_path / "annot.test.csv"
]
elif self.config.load_from == "jsonl":
self.dataset_files = [
self.corpus_base_path / "qanom" / "train.jsonl.gz",
self.corpus_base_path / "qanom" / "dev.jsonl.gz",
self.corpus_base_path / "qanom" / "test.jsonl.gz"
]
# proportional segment (start,end) to take from every original split to returned SplitGenerator
orig_dev_segments = ((0, self.config.redistribute_dev[0]),
(self.config.redistribute_dev[0], sum(self.config.redistribute_dev[:2])),
(sum(self.config.redistribute_dev[:2]), 1))
orig_tst_segments = ((0, self.config.redistribute_test[0]),
(self.config.redistribute_test[0], sum(self.config.redistribute_test[:2])),
(sum(self.config.redistribute_test[:2]), 1))
train_proportion = ((0,1), # from train
orig_dev_segments[0], # from dev
orig_tst_segments[0]) # from test
dev_proportion = ((0,0), # from train
orig_dev_segments[1], # from dev
orig_tst_segments[1]) # from test
test_proportion = ((0,0), # from train
orig_dev_segments[2], # from dev
orig_tst_segments[2]) # from test
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"split_proportion": train_proportion
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"split_proportion": dev_proportion
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"split_proportion": test_proportion
},
),
]
def _generate_examples(self, split_proportion):
if self.config.load_from == "csv":
return self._generate_examples_from_csv(split_proportion=split_proportion)
elif self.config.load_from == "jsonl":
return self._generate_examples_from_jsonl(split_proportion=split_proportion)
def _generate_examples_from_jsonl(self, split_proportion):
""" Yields examples from a jsonl.gz file, in same format as qasrl-v2."""
empty_to_underscore = lambda s: "_" if s=="" else s
def read_lines(filepath):
with gzip.open(filepath, "rt") as f:
return [line.strip() for line in f]
orig_splits_jsons = [read_lines(filepath)
for filepath in self.dataset_files] # train, dev, test
# Each json-line stands for a sentence with several predicates and QAs; we will redistribute
# the new proportions of the splits on the sentence level for convenience
lines_from_orig_splits = [jsonlines[int(len(jsonlines)*start) : int(len(jsonlines)*end)]
for jsonlines, (start,end) in zip(orig_splits_jsons, split_proportion)]
this_split_lines = list(itertools.chain(*lines_from_orig_splits))
qa_counter = 0
for line in this_split_lines:
sent_obj = json.loads(line.strip())
tokens = sent_obj['sentenceTokens']
sentence = ' '.join(tokens)
sent_id = sent_obj['sentenceId']
# consider only selected domains
sent_domain = 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]] # if verb_form in verb_forms else verb_forms['stem']
question_slots_in_order = [
question_slots["wh"],
question_slots["aux"],
question_slots["subj"],
verb_surface,
question_slots["obj"],
empty_to_underscore(question_slots["prep"]), # fix bug in data
question_slots["obj2"],
'?'
]
# retrieve answers
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_spans = list(set(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
# also return non-predicates with empty data
for non_predicate_idx, non_predicate in sent_obj["nonPredicates"].items():
yield qa_counter, {
"sentence": sentence,
"sent_id": sent_obj['sentenceId'],
"predicate_idx": int(non_predicate_idx),
"predicate": non_predicate,
"is_verbal": False,
"verb_form": "",
"question": [],
"answers": [],
"answer_ranges": []
}
qa_counter += 1
@classmethod
def span_from_str(cls, s:str):
start, end = s.split(":")
return [int(start), int(end)]
def _generate_examples_from_csv(self, split_proportion):
""" Yields examples from a 'annot.?.csv' file in QANom's format."""
# construct concatenated DataFrame from different source splits
orig_splits_dfs = [pd.read_csv(filepath)
for filepath in self.dataset_files] # train, dev, test
segment_df_from_orig_splits = [df.iloc[int(len(df)*start) : int(len(df)*end)]
for df, (start,end) in zip(orig_splits_dfs, split_proportion)]
df = pd.concat(segment_df_from_orig_splits, ignore_index=True)
for counter, row in df.iterrows():
# Each record (row) in csv is a QA or is stating a predicate/non-predicate with no QAs
# consider only selected domains
sent_domain = row.qasrl_id.split(":")[1]
if sent_domain not in self.config.domains:
continue
# Prepare question (slots)
na_to_underscore = lambda s: "_" if pd.isna(s) else str(s)
question = [] if pd.isna(row.question) else list(map(na_to_underscore, [
row.wh, row.aux, row.subj, row.verb_slot_inflection, row.obj, row.prep, row.obj2
])) + ['?']
# fix verb slot - replace with actual verb inflection, and prepend verb_prefix
if question:
if row.verb_form in self.verb_inflections and not pd.isna(row.verb_slot_inflection):
verb_surface = self.verb_inflections[row.verb_form][row.verb_slot_inflection]
else:
verb_surface = row.verb_form
if not pd.isna(row.verb_prefix):
verb_surface = row.verb_prefix.replace("~!~", " ") + " " + verb_surface
question[3] = verb_surface
answers = [] if pd.isna(row.answer) else row.answer.split("~!~")
answer_ranges = [] if pd.isna(row.answer_range) else [Qanom.span_from_str(s) for s in row.answer_range.split("~!~")]
yield counter, {
"sentence": row.sentence,
"sent_id": row.qasrl_id,
"predicate_idx": row.target_idx,
"predicate": row.noun,
"is_verbal": row.is_verbal,
"verb_form": row.verb_form,
"question": question,
"answers": answers,
"answer_ranges": answer_ranges
}
|