Suomi24-toxicity-annotated / Suomi24-toxicity-annotated.py
annieske's picture
Update Suomi24-toxicity-annotated.py
f99b3d6
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)