File size: 3,388 Bytes
b1c0243
 
 
 
 
 
 
 
 
 
 
 
913821e
b1c0243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
913821e
b1c0243
 
f99b3d6
b1c0243
 
 
 
 
 
 
 
 
 
 
780e8cd
b1c0243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7102db0
b1c0243
 
 
 
7102db0
b1c0243
 
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
import json
import pandas as pd
import datasets


_DESCRIPTION = """\
This dataset consists of Suomi24 comments which have been labeled by human raters for toxic behavior.
"""

_HOMEPAGE = "https://turkunlp.org/"

_URLS = {
    "test": "https://huggingface.co/datasets/TurkuNLP/Suomi24-toxicity-annotated/resolve/main/all_annotations.tsv"
}


class Suomi24ToxicityPred(datasets.GeneratorBasedBuilder):
    """This is a dataset of comments sampled from Suomi24 and annotated using jigsaw toxicity labels."""

    VERSION = datasets.Version("1.1.0")


    def _info(self):

        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=datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "label": datasets.Value("string") # we only have one label for each text
                }
            ),
            # If there's a common (input, target) tuple from the features,
            # specify them here. They'll be used if as_supervised=True in
            # builder.as_dataset.
            supervised_keys=None,
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        # 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

        urls_to_download = _URLS
        downloaded_files = dl_manager.download_and_extract(urls_to_download)

        return [
            datasets.SplitGenerator(
                name="test",
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": downloaded_files["test"]
                },
            ),
        ]

    def _generate_examples(self, filepath):
        """Yields examples."""
        # This method will receive as arguments the `gen_kwargs` defined in the previous `_split_generators` method.
        # It is in charge of opening the given file and yielding (key, example) tuples from the dataset
        # The key is not important, it's more here for legacy reason (legacy from tfds)

        # read the tsv file
        with open(filepath, "r") as f:
            data = f.readlines()
        data = data[1:]
        for i in range(len(data)):
            data[i] = data[i].replace("\n", "")
            data[i] = data[i].split("\t")
            assert len(data[i]) == 3
        
        from collections import Counter
        from itertools import count

        ids = [one[0] for one in data]
        c = Counter(ids)

        iters = {k: count(1) for k, v in c.items() if v > 1}
        output_list = [x+str(next(iters[x])) if x in iters else x for x in ids]

        count = 0
        # here make the data into a proper thing
        for one in data:
            example = {}
            text_id = output_list[count] # change this somehow
            count = count + 1
            example["text"] = one[2]
            example["label"] = one[1]

            yield (text_id, example)