elsevier-oa-cc-by / elsevier-oa-cc-by.py
orieg's picture
add base code for elsevier-oa-cc-by corpus
82e8c1e
raw
history blame
7.28 kB
# coding=utf-8
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
#
# 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.
# Lint as: python3
"""Elsevier OA CC-By Corpus Dataset."""
import json
import glob
import os
import math
import datasets
_CITATION = """
@article{Kershaw2020ElsevierOC,
title = {Elsevier OA CC-By Corpus},
author = {Daniel James Kershaw and R. Koeling},
journal = {ArXiv},
year = {2020},
volume = {abs/2008.00774},
doi = {https://doi.org/10.48550/arXiv.2008.00774},
url = {https://elsevier.digitalcommonsdata.com/datasets/zm33cdndxs},
keywords = {Science, Natural Language Processing, Machine Learning, Open Dataset},
abstract = {We introduce the Elsevier OA CC-BY corpus. This is the first open
corpus of Scientific Research papers which has a representative sample
from across scientific disciplines. This corpus not only includes the
full text of the article, but also the metadata of the documents,
along with the bibliographic information for each reference.}
}
"""
_DESCRIPTION = """
Elsevier OA CC-By is a corpus of 40k (40, 091) open access (OA) CC-BY articles
from across Elsevier’s journals and include the full text of the article, the metadata,
the bibliographic information for each reference, and author highlights.
"""
_HOMEPAGE = "https://elsevier.digitalcommonsdata.com/datasets/zm33cdndxs/3"
_LICENSE = "CC-BY-4.0"
_URLS = {
"mendeley": "https://data.mendeley.com/public-files/datasets/zm33cdndxs/files/4e03ae48-04a7-44d4-b103-ce73e548679c/file_downloaded"
}
class ElsevierOaCcBy(datasets.GeneratorBasedBuilder):
"""Elsevier OA CC-By Dataset."""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="mendeley", version=VERSION, description="Official Mendeley dataset"),
]
DEFAULT_CONFIG_NAME = "mendeley"
def _info(self):
features = datasets.Features(
{
"title": datasets.Value("string"),
"abstract": datasets.Value("string"),
"subjareas": datasets.Sequence(datasets.Value("string")),
"keywords": datasets.Sequence(datasets.Value("string")),
"asjc": datasets.Sequence(datasets.Value("string")),
"body_text": datasets.Sequence(datasets.Value("string")),
"author_highlights": datasets.Sequence(datasets.Value("string")),
}
)
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, uncomment supervised_keys line below and
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
# supervised_keys=("sentence", "label"),
# Homepage of the dataset for documentation
homepage=_HOMEPAGE,
# License for the dataset if available
license=_LICENSE,
# Citation for the dataset
citation=_CITATION,
)
def _split_generators(self, dl_manager):
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
# 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.
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
urls = _URLS[self.config.name]
data_dir = dl_manager.download_and_extract(urls)
corpus_path = os.path.join(data_dir, "json")
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": corpus_path,
"split": "train",
"split_range": [0, 32072]
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": corpus_path,
"split": "test",
"split_range": [32073, 36082]
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": corpus_path,
"split": "validation",
"split_range": [36083, 40091]
},
),
]
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
def _generate_examples(self, filepath, split, split_range):
# TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
json_files = glob.glob(f"{filepath}/*.json")
for doc in json_files[split_range[0]:split_range[1]]:
with open(doc) as f:
paper = json.loads(f.read())
# Yields examples as (key, example) tuples
yield paper['docId'], {
'title': paper['metadata']['title'],
'subjareas': paper['metadata']['subjareas'] if 'subjareas' in paper['metadata'] else [],
'keywords': paper['metadata']['keywords'] if 'keywords' in paper['metadata'] else [],
'asjc': paper['metadata']['asjc'] if 'asjc' in paper['metadata'] else [],
'abstract': paper['abstract'] if 'abstract' in paper else "",
"body_text": [s['sentence'] for s in sorted(paper['body_text'], key = lambda i: (i['secId'], i['startOffset']))],
"author_highlights": [s['sentence'] for s in sorted(paper['author_highlights'], key = lambda i: i['startOffset'])] if 'author_highlights' in paper else [],
}