osdg_cd / osdg_cd.py
Filippo Broggini
Reduce `num_labels` to 15. See https://github.com/osdg-ai/osdg-data/issues/3
4a821a6
raw
history blame
5.3 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
"""OSGD-CD: The OSDG Community Dataset."""
import csv
import json
import datasets
from datasets.tasks import TextClassification
logger = datasets.logging.get_logger(__name__)
_CITATION = """\
@dataset{osdg_2022_6393942,
author = {OSDG and
UNDP IICPSD SDG AI Lab and
PPMI},
title = {OSDG Community Dataset (OSDG-CD)},
month = apr,
year = 2022,
note = {{This CSV file uses UTF-8 character encoding. For
easy access on MS Excel, open the file using Data
→ From Text/CSV. Please split CSV data into
different columns by using a TAB delimiter.}},
publisher = {Zenodo},
version = {2022.04},
doi = {10.5281/zenodo.6393942},
url = {https://doi.org/10.5281/zenodo.6393942}
}
"""
_HOMEPAGE = "https://doi.org/10.5281/zenodo.6393942"
_LICENSE = "https://creativecommons.org/licenses/by/4.0/"
_DESCRIPTION = """\
The OSDG Community Dataset (OSDG-CD) is a public dataset of thousands of text excerpts, \
which were validated by approximately 1,000 OSDG Community Platform (OSDG-CP) \
citizen scientists from over 110 countries, with respect to the Sustainable Development Goals (SDGs).
"""
_VERSIONS = {
"2021.09": "1.0.0",
"2022.01": "1.0.1",
"2022.04": "1.0.2",
}
_VERSION = _VERSIONS["2022.04"]
_URLS = {
# "train" :"https://zenodo.org/record/6393942/files/osdg-community-dataset-v2022-04-01.csv?download=1"
"train" :"https://zenodo.org/record/6393942/files/osdg-community-dataset-v2022-04-01.csv"
}
class OSDGCDConfig(datasets.BuilderConfig):
"""BuilderConfig for OSDG-CD."""
def __init__(self, **kwargs):
"""BuilderConfig for OSDG-CD.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(OSDGCDConfig, self).__init__(**kwargs)
class OSDGCD(datasets.GeneratorBasedBuilder):
"""OSDG-CD: The OSDG Community Dataset (OSDG-CD). Version 2022.04."""
# This is an example of a dataset with multiple configurations.
# If you don't want/need to define several sub-sets in your dataset,
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
# If you need to make complex sub-parts in the datasets with configurable options
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
# BUILDER_CONFIG_CLASS = MyBuilderConfig
# You will be able to load one or the other configurations in the following list with
# data = datasets.load_dataset('my_dataset', 'first_domain')
# data = datasets.load_dataset('my_dataset', 'second_domain')
BUILDER_CONFIGS = [
OSDGCDConfig(
name="main_config",
version=datasets.Version(_VERSION, ""),
description="Main configuration",
),
]
DEFAULT_CONFIG_NAME = "main_config"
def _info(self) -> datasets.DatasetInfo:
return datasets.DatasetInfo(
description=_DESCRIPTION,
license=_LICENSE,
features=datasets.Features(
{
"doi": datasets.Value("string"),
"text_id": datasets.Value("string"),
"text": datasets.Value("string"),
"sdg": datasets.Value("uint16"),
"label": datasets.ClassLabel(num_classes=15, names=[f"SDG {sdg}" for sdg in range(1, 16)]),
"labels_negative": datasets.Value("uint16"),
"labels_positive": datasets.Value("uint16"),
"agreement": datasets.Value("float"),
}
),
homepage=_HOMEPAGE,
citation=_CITATION,
task_templates=[
TextClassification(
text_column="text", label_column="label",
)
],
)
def _split_generators(self, dl_manager):
downloaded_files = dl_manager.download_and_extract(_URLS)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
]
def _generate_examples(self, filepath):
"""This function returns the examples in the raw (text) form."""
logger.info("generating examples from = %s", filepath)
key = 0
with open(filepath, encoding="utf-8") as f:
osdg = csv.DictReader(f, delimiter="\t")
for row in osdg:
row["label"] = int(row["sdg"]) - 1
yield key, row
key += 1