osdg_cd / osdg_cd.py
Filippo B
Update to version 2023.10
e8ae5ab
# 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_2023_8397907,
author = {OSDG and
UNDP IICPSD SDG AI Lab and
PPMI},
title = {OSDG Community Dataset (OSDG-CD)},
month = oct,
year = 2023,
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 = {2023.10},
doi = {10.5281/zenodo.8397907},
url = {https://doi.org/10.5281/zenodo.8397907}
}
"""
_HOMEPAGE = "https://zenodo.org/record/8397907"
_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",
"2022.07": "1.0.3",
"2022.10": "1.0.4",
"2023.01": "1.0.5",
"2023.04": "1.0.6",
"2023.07": "1.0.7",
"2023.10": "1.0.8",
}
_VERSION = _VERSIONS["2023.10"]
_URLS = {
#"train": "https://zenodo.org/record/8107038/files/osdg-community-data-v2023-07-01.csv",
"train": "https://zenodo.org/record/8397907/files/osdg-community-data-v2023-10-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)"""
# 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=16, names=[f"SDG {sdg}" for sdg in range(1, 17)]),
"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)
with open(filepath, encoding="utf-8") as f:
osdg = csv.DictReader(f, delimiter="\t")
for row in osdg:
id_ = row["text_id"]
sdg = int(row["sdg"])
row["label"] = f"SDG {sdg}"
yield id_, row