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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""TODO: Add a description here."""
|
2 |
+
|
3 |
+
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
|
7 |
+
import datasets
|
8 |
+
|
9 |
+
_CITATION = """\
|
10 |
+
@inproceedings{zhang-etal-2017-position,
|
11 |
+
title = "Position-aware Attention and Supervised Data Improve Slot Filling",
|
12 |
+
author = "Zhang, Yuhao and
|
13 |
+
Zhong, Victor and
|
14 |
+
Chen, Danqi and
|
15 |
+
Angeli, Gabor and
|
16 |
+
Manning, Christopher D.",
|
17 |
+
booktitle = "Proceedings of the 2017 Conference on Empirical Methods in Natural Language Processing",
|
18 |
+
month = sep,
|
19 |
+
year = "2017",
|
20 |
+
address = "Copenhagen, Denmark",
|
21 |
+
publisher = "Association for Computational Linguistics",
|
22 |
+
url = "https://www.aclweb.org/anthology/D17-1004",
|
23 |
+
doi = "10.18653/v1/D17-1004",
|
24 |
+
pages = "35--45",
|
25 |
+
}
|
26 |
+
|
27 |
+
@inproceedings{alt-etal-2020-tacred,
|
28 |
+
title = "{TACRED} Revisited: A Thorough Evaluation of the {TACRED} Relation Extraction Task",
|
29 |
+
author = "Alt, Christoph and
|
30 |
+
Gabryszak, Aleksandra and
|
31 |
+
Hennig, Leonhard",
|
32 |
+
booktitle = "Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics",
|
33 |
+
month = jul,
|
34 |
+
year = "2020",
|
35 |
+
address = "Online",
|
36 |
+
publisher = "Association for Computational Linguistics",
|
37 |
+
url = "https://www.aclweb.org/anthology/2020.acl-main.142",
|
38 |
+
doi = "10.18653/v1/2020.acl-main.142",
|
39 |
+
pages = "1558--1569",
|
40 |
+
}
|
41 |
+
"""
|
42 |
+
|
43 |
+
# TODO: Add description of the dataset here
|
44 |
+
# You can copy an official description
|
45 |
+
_DESCRIPTION = """\
|
46 |
+
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
|
47 |
+
"""
|
48 |
+
|
49 |
+
# TODO: Add a link to an official homepage for the dataset here
|
50 |
+
_HOMEPAGE = ""
|
51 |
+
|
52 |
+
# TODO: Add the licence for the dataset here if you can find it
|
53 |
+
_LICENSE = ""
|
54 |
+
|
55 |
+
# TODO: Add link to the official dataset URLs here
|
56 |
+
# The HuggingFace dataset library don't host the datasets but only point to the original files
|
57 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
58 |
+
_PATCH_URLs = {
|
59 |
+
"dev": "https://raw.githubusercontent.com/DFKI-NLP/tacrev/master/patch/dev_patch.json",
|
60 |
+
"test": "https://raw.githubusercontent.com/DFKI-NLP/tacrev/master/patch/test_patch.json",
|
61 |
+
}
|
62 |
+
|
63 |
+
_CLASS_LABELS = [
|
64 |
+
"no_relation",
|
65 |
+
"org:alternate_names",
|
66 |
+
"org:city_of_headquarters",
|
67 |
+
"org:country_of_headquarters",
|
68 |
+
"org:dissolved",
|
69 |
+
"org:founded",
|
70 |
+
"org:founded_by",
|
71 |
+
"org:member_of",
|
72 |
+
"org:members",
|
73 |
+
"org:number_of_employees/members",
|
74 |
+
"org:parents",
|
75 |
+
"org:political/religious_affiliation",
|
76 |
+
"org:shareholders",
|
77 |
+
"org:stateorprovince_of_headquarters",
|
78 |
+
"org:subsidiaries",
|
79 |
+
"org:top_members/employees",
|
80 |
+
"org:website",
|
81 |
+
"per:age",
|
82 |
+
"per:alternate_names",
|
83 |
+
"per:cause_of_death",
|
84 |
+
"per:charges",
|
85 |
+
"per:children",
|
86 |
+
"per:cities_of_residence",
|
87 |
+
"per:city_of_birth",
|
88 |
+
"per:city_of_death",
|
89 |
+
"per:countries_of_residence",
|
90 |
+
"per:country_of_birth",
|
91 |
+
"per:country_of_death",
|
92 |
+
"per:date_of_birth",
|
93 |
+
"per:date_of_death",
|
94 |
+
"per:employee_of",
|
95 |
+
"per:origin",
|
96 |
+
"per:other_family",
|
97 |
+
"per:parents",
|
98 |
+
"per:religion",
|
99 |
+
"per:schools_attended",
|
100 |
+
"per:siblings",
|
101 |
+
"per:spouse",
|
102 |
+
"per:stateorprovince_of_birth",
|
103 |
+
"per:stateorprovince_of_death",
|
104 |
+
"per:stateorprovinces_of_residence",
|
105 |
+
"per:title",
|
106 |
+
]
|
107 |
+
|
108 |
+
|
109 |
+
def convert_ptb_token(token: str) -> str:
|
110 |
+
"""Convert PTB tokens to normal tokens"""
|
111 |
+
return {
|
112 |
+
"-lrb-": "(",
|
113 |
+
"-rrb-": ")",
|
114 |
+
"-lsb-": "[",
|
115 |
+
"-rsb-": "]",
|
116 |
+
"-lcb-": "{",
|
117 |
+
"-rcb-": "}",
|
118 |
+
}.get(token.lower(), token)
|
119 |
+
|
120 |
+
|
121 |
+
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
|
122 |
+
class TACRED(datasets.GeneratorBasedBuilder):
|
123 |
+
"""TODO: Short description of my dataset."""
|
124 |
+
|
125 |
+
# This is an example of a dataset with multiple configurations.
|
126 |
+
# If you don't want/need to define several sub-sets in your dataset,
|
127 |
+
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
128 |
+
|
129 |
+
# If you need to make complex sub-parts in the datasets with configurable options
|
130 |
+
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
|
131 |
+
# BUILDER_CONFIG_CLASS = MyBuilderConfig
|
132 |
+
|
133 |
+
# You will be able to load one or the other configurations in the following list with
|
134 |
+
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
135 |
+
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
136 |
+
BUILDER_CONFIGS = [
|
137 |
+
datasets.BuilderConfig(
|
138 |
+
name="original", version=datasets.Version("1.0.0"), description="The original TACRED."
|
139 |
+
),
|
140 |
+
datasets.BuilderConfig(
|
141 |
+
name="revised",
|
142 |
+
version=datasets.Version("1.0.0"),
|
143 |
+
description="The revised TACRED (corrected labels in dev and test split).",
|
144 |
+
),
|
145 |
+
]
|
146 |
+
|
147 |
+
DEFAULT_CONFIG_NAME = "original" # type: ignore
|
148 |
+
|
149 |
+
@property
|
150 |
+
def manual_download_instructions(self):
|
151 |
+
return (
|
152 |
+
"To use TACRED you have to download it manually. "
|
153 |
+
"It is available via the LDC at https://catalog.ldc.upenn.edu/LDC2018T24"
|
154 |
+
"Please extract all files in one folder and load the dataset with: "
|
155 |
+
"`datasets.load_dataset('tacred', data_dir='path/to/folder/folder_name')`"
|
156 |
+
)
|
157 |
+
|
158 |
+
def _info(self):
|
159 |
+
features = datasets.Features(
|
160 |
+
{
|
161 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
162 |
+
"head_start": datasets.Value("int32"),
|
163 |
+
"head_end": datasets.Value("int32"),
|
164 |
+
"tail_start": datasets.Value("int32"),
|
165 |
+
"tail_end": datasets.Value("int32"),
|
166 |
+
"label": datasets.ClassLabel(names=_CLASS_LABELS),
|
167 |
+
}
|
168 |
+
)
|
169 |
+
|
170 |
+
return datasets.DatasetInfo(
|
171 |
+
# This is the description that will appear on the datasets page.
|
172 |
+
description=_DESCRIPTION,
|
173 |
+
# This defines the different columns of the dataset and their types
|
174 |
+
features=features, # Here we define them above because they are different between the two configurations
|
175 |
+
# If there's a common (input, target) tuple from the features,
|
176 |
+
# specify them here. They'll be used if as_supervised=True in
|
177 |
+
# builder.as_dataset.
|
178 |
+
supervised_keys=None,
|
179 |
+
# Homepage of the dataset for documentation
|
180 |
+
homepage=_HOMEPAGE,
|
181 |
+
# License for the dataset if available
|
182 |
+
license=_LICENSE,
|
183 |
+
# Citation for the dataset
|
184 |
+
citation=_CITATION,
|
185 |
+
)
|
186 |
+
|
187 |
+
def _split_generators(self, dl_manager):
|
188 |
+
"""Returns SplitGenerators."""
|
189 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
190 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
191 |
+
|
192 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
|
193 |
+
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
194 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
195 |
+
patch_files = {}
|
196 |
+
if self.config.name == "revised":
|
197 |
+
patch_files = dl_manager.download_and_extract(_PATCH_URLs)
|
198 |
+
|
199 |
+
data_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
|
200 |
+
|
201 |
+
if not os.path.exists(data_dir):
|
202 |
+
raise FileNotFoundError(
|
203 |
+
"{} does not exist. Make sure you insert a manual dir via `datasets.load_dataset('tacred', data_dir=...)` that includes the unzipped files from the TACRED_LDC zip. Manual download instructions: {}".format(
|
204 |
+
data_dir, self.manual_download_instructions
|
205 |
+
)
|
206 |
+
)
|
207 |
+
|
208 |
+
return [
|
209 |
+
datasets.SplitGenerator(
|
210 |
+
name=datasets.Split.TRAIN,
|
211 |
+
gen_kwargs={
|
212 |
+
"filepath": os.path.join(data_dir, "train.json"),
|
213 |
+
"patch_filepath": None,
|
214 |
+
},
|
215 |
+
),
|
216 |
+
datasets.SplitGenerator(
|
217 |
+
name=datasets.Split.TEST,
|
218 |
+
gen_kwargs={
|
219 |
+
"filepath": os.path.join(data_dir, "test.json"),
|
220 |
+
"patch_filepath": patch_files.get("test"),
|
221 |
+
},
|
222 |
+
),
|
223 |
+
datasets.SplitGenerator(
|
224 |
+
name=datasets.Split.VALIDATION,
|
225 |
+
gen_kwargs={
|
226 |
+
"filepath": os.path.join(data_dir, "dev.json"),
|
227 |
+
"patch_filepath": patch_files.get("dev"),
|
228 |
+
},
|
229 |
+
),
|
230 |
+
]
|
231 |
+
|
232 |
+
def _generate_examples(self, filepath, patch_filepath):
|
233 |
+
"""Yields examples."""
|
234 |
+
# TODO: This method will receive as arguments the `gen_kwargs` defined in the previous `_split_generators` method.
|
235 |
+
# It is in charge of opening the given file and yielding (key, example) tuples from the dataset
|
236 |
+
# The key is not important, it's more here for legacy reason (legacy from tfds)
|
237 |
+
patch_examples = {}
|
238 |
+
if patch_filepath is not None:
|
239 |
+
with open(patch_filepath, encoding="utf-8") as f:
|
240 |
+
patch_examples = {example["id"]: example for example in json.load(f)}
|
241 |
+
|
242 |
+
with open(filepath, encoding="utf-8") as f:
|
243 |
+
data = json.load(f)
|
244 |
+
for example in data:
|
245 |
+
id_ = example["id"]
|
246 |
+
|
247 |
+
if id_ in patch_examples:
|
248 |
+
example.update(patch_examples[id_])
|
249 |
+
|
250 |
+
yield id_, {
|
251 |
+
"tokens": [convert_ptb_token(token) for token in example["token"]],
|
252 |
+
"head_start": example["subj_start"],
|
253 |
+
"head_end": example["subj_end"] + 1, # make end offset exclusive
|
254 |
+
"tail_start": example["obj_start"],
|
255 |
+
"tail_end": example["obj_end"] + 1, # make end offset exclusive
|
256 |
+
"label": example["relation"],
|
257 |
+
}
|