Delete loading script
Browse files
qanta.py
DELETED
|
@@ -1,289 +0,0 @@
|
|
| 1 |
-
"""qanta dataset."""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import json
|
| 5 |
-
from typing import List, Tuple
|
| 6 |
-
|
| 7 |
-
import datasets
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
_CITATION = """
|
| 11 |
-
@article{Rodriguez2019QuizbowlTC,
|
| 12 |
-
title={Quizbowl: The Case for Incremental Question Answering},
|
| 13 |
-
author={Pedro Rodriguez and Shi Feng and Mohit Iyyer and He He and Jordan L. Boyd-Graber},
|
| 14 |
-
journal={ArXiv},
|
| 15 |
-
year={2019},
|
| 16 |
-
volume={abs/1904.04792}
|
| 17 |
-
}
|
| 18 |
-
"""
|
| 19 |
-
|
| 20 |
-
_DESCRIPTION = """
|
| 21 |
-
The Qanta dataset is a question answering dataset based on the academic trivia game Quizbowl.
|
| 22 |
-
"""
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
_QANTA_URL = "https://s3-us-west-2.amazonaws.com/pinafore-us-west-2/qanta-jmlr-datasets/qanta.mapped.2018.04.18.json"
|
| 26 |
-
_TRICK_URL = "https://s3-us-west-2.amazonaws.com/pinafore-us-west-2/trick-tacl-datasets/qanta.tacl-trick.json"
|
| 27 |
-
_VERSION = datasets.Version("2018.04.18")
|
| 28 |
-
_FIRST = "first"
|
| 29 |
-
_FULL = "full"
|
| 30 |
-
_SENTENCES = "sentences"
|
| 31 |
-
_RUNS = "runs"
|
| 32 |
-
# Order matters, the first one is default
|
| 33 |
-
_MODES = [_FULL, _FIRST, _SENTENCES, _RUNS]
|
| 34 |
-
_DEFAULT_CHAR_SKIP = 25
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
class QantaConfig(datasets.BuilderConfig):
|
| 38 |
-
"""BuilderConfig for Qanta."""
|
| 39 |
-
|
| 40 |
-
def __init__(self, mode: str, char_skip: int, **kwargs):
|
| 41 |
-
super(QantaConfig, self).__init__(version=_VERSION, **kwargs)
|
| 42 |
-
self.mode = mode
|
| 43 |
-
self.char_skip = char_skip
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
def create_char_runs(text: str, char_skip: int) -> List[Tuple[str, int]]:
|
| 47 |
-
"""
|
| 48 |
-
Returns runs of the question based on skipping char_skip characters at a time. Also returns the indices used
|
| 49 |
-
q: name this first united states president.
|
| 50 |
-
runs with char_skip=10:
|
| 51 |
-
['name this ',
|
| 52 |
-
'name this first unit',
|
| 53 |
-
'name this first united state p',
|
| 54 |
-
'name this first united state president.']
|
| 55 |
-
:param char_skip: Number of characters to skip each time
|
| 56 |
-
"""
|
| 57 |
-
char_indices = list(range(char_skip, len(text) + char_skip, char_skip))
|
| 58 |
-
return [(text[:idx], idx) for idx in char_indices]
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def with_default(key, lookup, default):
|
| 62 |
-
if key in lookup:
|
| 63 |
-
value = lookup[key]
|
| 64 |
-
if value is None:
|
| 65 |
-
return default
|
| 66 |
-
else:
|
| 67 |
-
return value
|
| 68 |
-
else:
|
| 69 |
-
return default
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
def question_to_examples(question, mode: str, char_skip: int):
|
| 73 |
-
features = {
|
| 74 |
-
"qanta_id": question["qanta_id"],
|
| 75 |
-
"proto_id": with_default("proto_id", question, ""),
|
| 76 |
-
"qdb_id": with_default("qdb_id", question, -1),
|
| 77 |
-
# We refer to the actual answer as page, but this
|
| 78 |
-
# may be misleading externally, so rename here to
|
| 79 |
-
# be clearer
|
| 80 |
-
"page": question["page"],
|
| 81 |
-
"answer": question["page"],
|
| 82 |
-
"raw_answer": question["answer"],
|
| 83 |
-
"dataset": with_default("dataset", question, ""),
|
| 84 |
-
"full_question": question["text"],
|
| 85 |
-
"first_sentence": question["first_sentence"],
|
| 86 |
-
"tokenizations": question["tokenizations"],
|
| 87 |
-
"fold": question["fold"],
|
| 88 |
-
"gameplay": question["gameplay"],
|
| 89 |
-
"category": with_default("category", question, ""),
|
| 90 |
-
"subcategory": with_default("subcategory", question, ""),
|
| 91 |
-
"tournament": question["tournament"],
|
| 92 |
-
"difficulty": with_default("difficulty", question, ""),
|
| 93 |
-
"year": question["year"],
|
| 94 |
-
"char_idx": -1,
|
| 95 |
-
"sentence_idx": -1,
|
| 96 |
-
}
|
| 97 |
-
if mode == _FULL:
|
| 98 |
-
yield {
|
| 99 |
-
"text": question["text"],
|
| 100 |
-
"id": str(question["qanta_id"]) + "-full",
|
| 101 |
-
**features,
|
| 102 |
-
}
|
| 103 |
-
elif mode == _FIRST:
|
| 104 |
-
yield {
|
| 105 |
-
"text": question["first_sentence"],
|
| 106 |
-
"id": str(question["qanta_id"]) + "-first",
|
| 107 |
-
**features,
|
| 108 |
-
}
|
| 109 |
-
elif mode == _RUNS:
|
| 110 |
-
text = question["text"]
|
| 111 |
-
for text_run, char_idx in create_char_runs(text, char_skip):
|
| 112 |
-
yield {
|
| 113 |
-
"text": text_run,
|
| 114 |
-
"char_idx": char_idx,
|
| 115 |
-
"id": str(question["qanta_id"]) + "-char-" + str(char_idx),
|
| 116 |
-
**features,
|
| 117 |
-
}
|
| 118 |
-
elif mode == _SENTENCES:
|
| 119 |
-
for sentence_idx, (start, end) in enumerate(question["tokenizations"]):
|
| 120 |
-
sentence = question["text"][start:end]
|
| 121 |
-
yield {
|
| 122 |
-
"text": sentence,
|
| 123 |
-
"sentence_idx": sentence_idx,
|
| 124 |
-
"id": str(question["qanta_id"]) + "-sentence-" + str(sentence_idx),
|
| 125 |
-
**features,
|
| 126 |
-
}
|
| 127 |
-
else:
|
| 128 |
-
raise ValueError(f"Invalid mode: {mode}")
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
_FEATURES = {
|
| 132 |
-
# Generated ID based modes set, unique
|
| 133 |
-
"id": datasets.Value("string"),
|
| 134 |
-
# Dataset defined IDs
|
| 135 |
-
"qanta_id": datasets.Value("int32"),
|
| 136 |
-
"proto_id": datasets.Value("string"),
|
| 137 |
-
"qdb_id": datasets.Value("int32"),
|
| 138 |
-
"dataset": datasets.Value("string"),
|
| 139 |
-
# Inputs
|
| 140 |
-
"text": datasets.Value("string"),
|
| 141 |
-
"full_question": datasets.Value("string"),
|
| 142 |
-
"first_sentence": datasets.Value("string"),
|
| 143 |
-
"char_idx": datasets.Value("int32"),
|
| 144 |
-
"sentence_idx": datasets.Value("int32"),
|
| 145 |
-
# Character indices of sentences: List[Tuple[int, int]]
|
| 146 |
-
"tokenizations": datasets.features.Sequence(datasets.features.Sequence(datasets.Value("int32"), length=2)),
|
| 147 |
-
# Labels: Number is equal to number of unique pages across all folds
|
| 148 |
-
"answer": datasets.Value("string"),
|
| 149 |
-
"page": datasets.Value("string"),
|
| 150 |
-
"raw_answer": datasets.Value("string"),
|
| 151 |
-
# Meta Information
|
| 152 |
-
"fold": datasets.Value("string"),
|
| 153 |
-
"gameplay": datasets.Value("bool"),
|
| 154 |
-
"category": datasets.Value("string"),
|
| 155 |
-
"subcategory": datasets.Value("string"),
|
| 156 |
-
"tournament": datasets.Value("string"),
|
| 157 |
-
"difficulty": datasets.Value("string"),
|
| 158 |
-
"year": datasets.Value("int32"),
|
| 159 |
-
}
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
class Qanta(datasets.GeneratorBasedBuilder):
|
| 163 |
-
"""The Qanta dataset is a question answering dataset based on the academic trivia game Quizbowl."""
|
| 164 |
-
|
| 165 |
-
VERSION = _VERSION
|
| 166 |
-
BUILDER_CONFIGS = [
|
| 167 |
-
QantaConfig(
|
| 168 |
-
name=f"mode={mode},char_skip={_DEFAULT_CHAR_SKIP}",
|
| 169 |
-
description=f"Question format: {mode}, char_skip: {_DEFAULT_CHAR_SKIP}",
|
| 170 |
-
mode=mode,
|
| 171 |
-
char_skip=_DEFAULT_CHAR_SKIP,
|
| 172 |
-
)
|
| 173 |
-
for mode in _MODES
|
| 174 |
-
]
|
| 175 |
-
|
| 176 |
-
def _info(self):
|
| 177 |
-
return datasets.DatasetInfo(
|
| 178 |
-
# This is the description that will appear on the datasets page.
|
| 179 |
-
description=_DESCRIPTION,
|
| 180 |
-
# datasets.features.FeatureConnectors
|
| 181 |
-
features=datasets.Features(_FEATURES),
|
| 182 |
-
# Number of classes is a function of the dataset, ClassLabel doesn't support dynamic
|
| 183 |
-
# definition, so have to defer conversion to classes to later, so can't define
|
| 184 |
-
# supervied keys
|
| 185 |
-
supervised_keys=None,
|
| 186 |
-
# Homepage of the dataset for documentation
|
| 187 |
-
homepage="http://www.qanta.org/",
|
| 188 |
-
citation=_CITATION,
|
| 189 |
-
)
|
| 190 |
-
|
| 191 |
-
def _split_generators(self, dl_manager):
|
| 192 |
-
"""Returns SplitGenerators."""
|
| 193 |
-
qanta_path = dl_manager.download_and_extract(_QANTA_URL)
|
| 194 |
-
trick_path = dl_manager.download_and_extract(_TRICK_URL)
|
| 195 |
-
return [
|
| 196 |
-
datasets.SplitGenerator(
|
| 197 |
-
name=datasets.Split("guesstrain"),
|
| 198 |
-
gen_kwargs={
|
| 199 |
-
"qanta_filepath": qanta_path,
|
| 200 |
-
"trick_filepath": trick_path,
|
| 201 |
-
"fold": "guesstrain",
|
| 202 |
-
"mode": self.config.mode,
|
| 203 |
-
"char_skip": self.config.char_skip,
|
| 204 |
-
},
|
| 205 |
-
),
|
| 206 |
-
datasets.SplitGenerator(
|
| 207 |
-
name=datasets.Split("buzztrain"),
|
| 208 |
-
gen_kwargs={
|
| 209 |
-
"qanta_filepath": qanta_path,
|
| 210 |
-
"trick_filepath": trick_path,
|
| 211 |
-
"fold": "buzztrain",
|
| 212 |
-
"mode": self.config.mode,
|
| 213 |
-
"char_skip": self.config.char_skip,
|
| 214 |
-
},
|
| 215 |
-
),
|
| 216 |
-
datasets.SplitGenerator(
|
| 217 |
-
name=datasets.Split("guessdev"),
|
| 218 |
-
gen_kwargs={
|
| 219 |
-
"qanta_filepath": qanta_path,
|
| 220 |
-
"trick_filepath": trick_path,
|
| 221 |
-
"fold": "guessdev",
|
| 222 |
-
"mode": self.config.mode,
|
| 223 |
-
"char_skip": self.config.char_skip,
|
| 224 |
-
},
|
| 225 |
-
),
|
| 226 |
-
datasets.SplitGenerator(
|
| 227 |
-
name=datasets.Split("buzzdev"),
|
| 228 |
-
gen_kwargs={
|
| 229 |
-
"qanta_filepath": qanta_path,
|
| 230 |
-
"trick_filepath": trick_path,
|
| 231 |
-
"fold": "buzzdev",
|
| 232 |
-
"mode": self.config.mode,
|
| 233 |
-
"char_skip": self.config.char_skip,
|
| 234 |
-
},
|
| 235 |
-
),
|
| 236 |
-
datasets.SplitGenerator(
|
| 237 |
-
name=datasets.Split("guesstest"),
|
| 238 |
-
gen_kwargs={
|
| 239 |
-
"qanta_filepath": qanta_path,
|
| 240 |
-
"trick_filepath": trick_path,
|
| 241 |
-
"fold": "guesstest",
|
| 242 |
-
"mode": self.config.mode,
|
| 243 |
-
"char_skip": self.config.char_skip,
|
| 244 |
-
},
|
| 245 |
-
),
|
| 246 |
-
datasets.SplitGenerator(
|
| 247 |
-
name=datasets.Split("buzztest"),
|
| 248 |
-
gen_kwargs={
|
| 249 |
-
"qanta_filepath": qanta_path,
|
| 250 |
-
"trick_filepath": trick_path,
|
| 251 |
-
"fold": "buzztest",
|
| 252 |
-
"mode": self.config.mode,
|
| 253 |
-
"char_skip": self.config.char_skip,
|
| 254 |
-
},
|
| 255 |
-
),
|
| 256 |
-
datasets.SplitGenerator(
|
| 257 |
-
name=datasets.Split("adversarial"),
|
| 258 |
-
gen_kwargs={
|
| 259 |
-
"qanta_filepath": qanta_path,
|
| 260 |
-
"trick_filepath": trick_path,
|
| 261 |
-
"fold": "adversarial",
|
| 262 |
-
"mode": self.config.mode,
|
| 263 |
-
"char_skip": self.config.char_skip,
|
| 264 |
-
},
|
| 265 |
-
),
|
| 266 |
-
]
|
| 267 |
-
|
| 268 |
-
def _generate_examples(
|
| 269 |
-
self,
|
| 270 |
-
qanta_filepath: str,
|
| 271 |
-
trick_filepath: str,
|
| 272 |
-
fold: str,
|
| 273 |
-
mode: str,
|
| 274 |
-
char_skip: int,
|
| 275 |
-
):
|
| 276 |
-
"""Yields examples."""
|
| 277 |
-
if mode not in _MODES:
|
| 278 |
-
raise ValueError(f"Invalid mode: {mode}")
|
| 279 |
-
|
| 280 |
-
if fold == "adversarial":
|
| 281 |
-
path = trick_filepath
|
| 282 |
-
else:
|
| 283 |
-
path = qanta_filepath
|
| 284 |
-
with open(path, encoding="utf-8") as f:
|
| 285 |
-
questions = json.load(f)["questions"]
|
| 286 |
-
for q in questions:
|
| 287 |
-
if q["page"] is not None and q["fold"] == fold:
|
| 288 |
-
for example in question_to_examples(q, mode, char_skip):
|
| 289 |
-
yield example["id"], example
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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