Upload SituatedQA.py
Browse files- SituatedQA.py +146 -0
SituatedQA.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
# Lint as: python3
|
17 |
+
"""SituatedQA: Incorporating Extra-Linguistic Contexts into QA."""
|
18 |
+
|
19 |
+
|
20 |
+
import json
|
21 |
+
import re
|
22 |
+
import datasets
|
23 |
+
|
24 |
+
|
25 |
+
logger = datasets.logging.get_logger(__name__)
|
26 |
+
|
27 |
+
|
28 |
+
_CITATION = """\
|
29 |
+
@article{ zhang2021situatedqa,
|
30 |
+
title={ {S}ituated{QA}: Incorporating Extra-Linguistic Contexts into {QA} },
|
31 |
+
author={ Zhang, Michael J.Q. and Choi, Eunsol },
|
32 |
+
journal={ Proceedings of the Conference on Empirical Methods in Natural Language Processing (EMNLP) },
|
33 |
+
year={ 2021 }
|
34 |
+
}
|
35 |
+
"""
|
36 |
+
|
37 |
+
_DESCRIPTION = """\
|
38 |
+
"""
|
39 |
+
|
40 |
+
_URL = "https://raw.githubusercontent.com/mikejqzhang/SituatedQA/master/data/qa_data/"
|
41 |
+
_URLS = {
|
42 |
+
"geo_train": _URL + "geo.train.jsonl",
|
43 |
+
"geo_dev": _URL + "geo.dev.jsonl",
|
44 |
+
"geo_test": _URL + "geo.test.jsonl",
|
45 |
+
"temp_train": _URL + "temp.train.jsonl",
|
46 |
+
"temp_dev": _URL + "temp.dev.jsonl",
|
47 |
+
"temp_test": _URL + "temp.test.jsonl",
|
48 |
+
}
|
49 |
+
|
50 |
+
class SituatedQAConfig(datasets.BuilderConfig):
|
51 |
+
|
52 |
+
def __init__(self, **kwargs):
|
53 |
+
"""BuilderConfig
|
54 |
+
Args:
|
55 |
+
**kwargs: keyword arguments forwarded to super.
|
56 |
+
"""
|
57 |
+
super(SituatedQAConfig, self).__init__(**kwargs)
|
58 |
+
|
59 |
+
|
60 |
+
class Squall(datasets.GeneratorBasedBuilder):
|
61 |
+
|
62 |
+
BUILDER_CONFIGS = [
|
63 |
+
SituatedQAConfig(name = 'geo'),
|
64 |
+
SituatedQAConfig(name = 'temp')]
|
65 |
+
|
66 |
+
def _info(self):
|
67 |
+
return datasets.DatasetInfo(
|
68 |
+
description=_DESCRIPTION,
|
69 |
+
features=datasets.Features(
|
70 |
+
{
|
71 |
+
"question": datasets.Value("string"),
|
72 |
+
"id": datasets.Value("string"),
|
73 |
+
"edited_question": datasets.Value("string"),
|
74 |
+
"date": datasets.Value("string"),
|
75 |
+
"date_type": datasets.Value("string"),
|
76 |
+
"location": datasets.Value("string"),
|
77 |
+
"answer": datasets.features.Sequence(datasets.Value("string")),
|
78 |
+
"any_answer": datasets.features.Sequence(datasets.Value("string")),
|
79 |
+
}
|
80 |
+
),
|
81 |
+
# No default supervised_keys (as we have to pass both question
|
82 |
+
# and context as input).
|
83 |
+
supervised_keys=None,
|
84 |
+
homepage="https://github.com/mikejqzhang/SituatedQA/tree/master",
|
85 |
+
citation=_CITATION,
|
86 |
+
)
|
87 |
+
|
88 |
+
def _split_generators(self, dl_manager):
|
89 |
+
urls_to_download = {
|
90 |
+
"geo_train": _URLS["geo_train"],
|
91 |
+
"geo_dev": _URLS["geo_dev"],
|
92 |
+
"geo_test": _URLS["geo_test"],
|
93 |
+
"temp_train": _URLS["temp_train"],
|
94 |
+
"temp_dev": _URLS["temp_dev"],
|
95 |
+
"temp_test": _URLS["temp_test"],
|
96 |
+
}
|
97 |
+
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
98 |
+
|
99 |
+
if self.config.name == 'geo':
|
100 |
+
train_file = downloaded_files["geo_train"]
|
101 |
+
dev_file = downloaded_files["geo_dev"]
|
102 |
+
test_file = downloaded_files["geo_test"]
|
103 |
+
else:
|
104 |
+
train_file = downloaded_files["temp_train"]
|
105 |
+
dev_file = downloaded_files["temp_dev"]
|
106 |
+
test_file = downloaded_files["temp_test"]
|
107 |
+
|
108 |
+
return [
|
109 |
+
datasets.SplitGenerator(
|
110 |
+
name=datasets.Split.TRAIN,
|
111 |
+
gen_kwargs={"split_key": "train", "filepath": train_file}),
|
112 |
+
datasets.SplitGenerator(
|
113 |
+
name=datasets.Split.VALIDATION,
|
114 |
+
gen_kwargs={"split_key": "dev", "filepath": dev_file}),
|
115 |
+
datasets.SplitGenerator(
|
116 |
+
name=datasets.Split.TEST,
|
117 |
+
gen_kwargs={"split_key": "test", "filepath": test_file}),
|
118 |
+
]
|
119 |
+
|
120 |
+
def _generate_examples(self, split_key, filepath):
|
121 |
+
"""This function returns the examples in the raw (text) form."""
|
122 |
+
logger.info("generating examples from = %s", filepath)
|
123 |
+
|
124 |
+
with open(filepath, 'r') as file:
|
125 |
+
json_list = list(file)
|
126 |
+
|
127 |
+
for i, line in enumerate(json_list):
|
128 |
+
data = json.loads(line)
|
129 |
+
if 'location' not in data:
|
130 |
+
data['location']=''
|
131 |
+
if 'date' not in data:
|
132 |
+
data['date']=''
|
133 |
+
if 'date_type' not in data:
|
134 |
+
data['date_type']=''
|
135 |
+
|
136 |
+
yield i, {
|
137 |
+
"question": data["question"],
|
138 |
+
"id": str(data["id"]),
|
139 |
+
"edited_question": data["edited_question"],
|
140 |
+
"date": data["date"],
|
141 |
+
"date_type": data["date_type"],
|
142 |
+
"location": data["location"],
|
143 |
+
"answer": data["answer"],
|
144 |
+
"any_answer": data["any_answer"]
|
145 |
+
}
|
146 |
+
|