Datasets:
Add science_ie.py
Browse files- science_ie.py +174 -0
science_ie.py
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""ScienceIE is a dataset for the SemEval task of extracting key phrases and relations between them from scientific documents"""
|
15 |
+
|
16 |
+
|
17 |
+
import glob
|
18 |
+
import datasets
|
19 |
+
from spacy.lang.en import English
|
20 |
+
|
21 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
22 |
+
_CITATION = """\
|
23 |
+
@article{DBLP:journals/corr/AugensteinDRVM17,
|
24 |
+
author = {Isabelle Augenstein and
|
25 |
+
Mrinal Das and
|
26 |
+
Sebastian Riedel and
|
27 |
+
Lakshmi Vikraman and
|
28 |
+
Andrew McCallum},
|
29 |
+
title = {SemEval 2017 Task 10: ScienceIE - Extracting Keyphrases and Relations
|
30 |
+
from Scientific Publications},
|
31 |
+
journal = {CoRR},
|
32 |
+
volume = {abs/1704.02853},
|
33 |
+
year = {2017},
|
34 |
+
url = {http://arxiv.org/abs/1704.02853},
|
35 |
+
eprinttype = {arXiv},
|
36 |
+
eprint = {1704.02853},
|
37 |
+
timestamp = {Mon, 13 Aug 2018 16:46:36 +0200},
|
38 |
+
biburl = {https://dblp.org/rec/journals/corr/AugensteinDRVM17.bib},
|
39 |
+
bibsource = {dblp computer science bibliography, https://dblp.org}
|
40 |
+
}
|
41 |
+
"""
|
42 |
+
|
43 |
+
# You can copy an official description
|
44 |
+
_DESCRIPTION = """\
|
45 |
+
ScienceIE is a dataset for the SemEval task of extracting key phrases and relations between them from scientific documents.
|
46 |
+
A corpus for the task was built from ScienceDirect open access publications and was available freely for participants, without the need to sign a copyright agreement. Each data instance consists of one paragraph of text, drawn from a scientific paper.
|
47 |
+
Publications were provided in plain text, in addition to xml format, which included the full text of the publication as well as additional metadata. 500 paragraphs from journal articles evenly distributed among the domains Computer Science, Material Sciences and Physics were selected.
|
48 |
+
The training data part of the corpus consists of 350 documents, 50 for development and 100 for testing. This is similar to the pilot task described in Section 5, for which 144 articles were used for training, 40 for development and for 100 testing.
|
49 |
+
|
50 |
+
The dataset has three labels: Material, Process, Task
|
51 |
+
"""
|
52 |
+
|
53 |
+
_HOMEPAGE = "https://scienceie.github.io/resources.html"
|
54 |
+
|
55 |
+
# TODO: Add the licence for the dataset here if you can find it
|
56 |
+
_LICENSE = ""
|
57 |
+
|
58 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
59 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
60 |
+
_URLS = {
|
61 |
+
"train": "https://github.com/ScienceIE/scienceie.github.io/raw/master/resources/scienceie2017_train.zip",
|
62 |
+
"validation": "https://github.com/ScienceIE/scienceie.github.io/raw/master/resources/scienceie2017_dev.zip",
|
63 |
+
"test": "https://github.com/ScienceIE/scienceie.github.io/raw/master/resources/semeval_articles_test.zip"
|
64 |
+
}
|
65 |
+
|
66 |
+
|
67 |
+
class ScienceIE(datasets.GeneratorBasedBuilder):
|
68 |
+
"""ScienceIE is a dataset for the task of extracting key phrases and relations between them from scientific documents"""
|
69 |
+
|
70 |
+
VERSION = datasets.Version("1.0.0")
|
71 |
+
|
72 |
+
BUILDER_CONFIGS = [
|
73 |
+
datasets.BuilderConfig(name="ner", version=VERSION, description="NER part of ScienceIE")
|
74 |
+
]
|
75 |
+
|
76 |
+
DEFAULT_CONFIG_NAME = "ner"
|
77 |
+
|
78 |
+
def _info(self):
|
79 |
+
features = datasets.Features(
|
80 |
+
{
|
81 |
+
"id": datasets.Value("string"),
|
82 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
83 |
+
"ner_tags": datasets.Sequence(
|
84 |
+
datasets.features.ClassLabel(
|
85 |
+
names=[
|
86 |
+
"O",
|
87 |
+
"B-Process",
|
88 |
+
"I-Process",
|
89 |
+
"B-Task",
|
90 |
+
"I-Task",
|
91 |
+
"B-Material",
|
92 |
+
"I-Material"
|
93 |
+
]
|
94 |
+
)
|
95 |
+
)
|
96 |
+
}
|
97 |
+
)
|
98 |
+
return datasets.DatasetInfo(
|
99 |
+
# This is the description that will appear on the datasets page.
|
100 |
+
description=_DESCRIPTION,
|
101 |
+
# This defines the different columns of the dataset and their types
|
102 |
+
features=features, # Here we define them above because they are different between the two configurations
|
103 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
104 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
105 |
+
# supervised_keys=("sentence", "label"),
|
106 |
+
# Homepage of the dataset for documentation
|
107 |
+
homepage=_HOMEPAGE,
|
108 |
+
# License for the dataset if available
|
109 |
+
license=_LICENSE,
|
110 |
+
# Citation for the dataset
|
111 |
+
citation=_CITATION,
|
112 |
+
)
|
113 |
+
|
114 |
+
def _split_generators(self, dl_manager):
|
115 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
116 |
+
|
117 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
118 |
+
# 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.
|
119 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
120 |
+
downloaded_files = dl_manager.download_and_extract(_URLS)
|
121 |
+
|
122 |
+
return [datasets.SplitGenerator(name=i, gen_kwargs={"dir_path": downloaded_files[str(i)]})
|
123 |
+
for i in [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]]
|
124 |
+
|
125 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
126 |
+
def _generate_examples(self, dir_path):
|
127 |
+
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
128 |
+
annotation_files = glob.glob(dir_path + "/**/*.ann", recursive=True)
|
129 |
+
word_splitter = English()
|
130 |
+
key = 0
|
131 |
+
for f_anno_path in annotation_files:
|
132 |
+
f_text_path = f_anno_path.replace(".ann", ".txt")
|
133 |
+
with open(f_anno_path, mode="r", encoding="utf8") as f_anno, \
|
134 |
+
open(f_text_path, mode="r", encoding="utf8") as f_text:
|
135 |
+
text = f_text.read()
|
136 |
+
entities = []
|
137 |
+
for line in f_anno:
|
138 |
+
anno_inst = line.strip("\n").split("\t")
|
139 |
+
if len(anno_inst) == 3:
|
140 |
+
anno_inst1 = anno_inst[1].split(" ")
|
141 |
+
if len(anno_inst1) == 3:
|
142 |
+
keytype, start, end = anno_inst1
|
143 |
+
else:
|
144 |
+
keytype, start, _, end = anno_inst1
|
145 |
+
if not keytype.endswith("-of"): # NER annotation, relation annotation contains "-of"
|
146 |
+
# look up span in text and print error message if it doesn't match the .ann span text
|
147 |
+
keyphr_text_lookup = text[int(start):int(end)]
|
148 |
+
keyphr_ann = anno_inst[2]
|
149 |
+
if keyphr_text_lookup != keyphr_ann:
|
150 |
+
print("Spans don't match for anno " + line.strip() + " in file " + f_anno_path)
|
151 |
+
|
152 |
+
entities.append({
|
153 |
+
"id": anno_inst[0],
|
154 |
+
"start": int(start),
|
155 |
+
"end": int(end),
|
156 |
+
"type": keytype
|
157 |
+
})
|
158 |
+
doc = word_splitter(text)
|
159 |
+
tokens = [token.text for token in doc]
|
160 |
+
ner_tags = ["O" for _ in tokens]
|
161 |
+
for entity in entities:
|
162 |
+
entity_span = doc.char_span(entity["start"], entity["end"], alignment_mode="expand")
|
163 |
+
entity_start = entity_span.start
|
164 |
+
entity_end = entity_span.end
|
165 |
+
ner_tags[entity_start] = "B-" + entity["type"]
|
166 |
+
for i in range(entity_start + 1, entity_end):
|
167 |
+
ner_tags[i] = "I-" + entity["type"]
|
168 |
+
key += 1
|
169 |
+
# Yields examples as (key, example) tuples
|
170 |
+
yield key, {
|
171 |
+
"id": str(key),
|
172 |
+
"tokens": tokens,
|
173 |
+
"ner_tags": ner_tags
|
174 |
+
}
|