File size: 5,428 Bytes
de196e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8ae5ab
de196e7
 
 
 
e8ae5ab
ecf2698
ab91fd6
de196e7
 
 
 
e8ae5ab
 
 
de196e7
 
 
e8ae5ab
de196e7
 
 
 
 
 
 
 
 
 
 
 
 
ab91fd6
 
 
ecf2698
 
e8ae5ab
de196e7
 
e8ae5ab
de196e7
 
e8ae5ab
 
de196e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab91fd6
de196e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab91fd6
de196e7
 
 
 
 
 
 
 
 
 
 
 
950e962
de196e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
950e962
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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