annieske commited on
Commit
b1c0243
·
1 Parent(s): b09bbec

Upload suomi24_toxicity_pred.py

Browse files
Files changed (1) hide show
  1. suomi24_toxicity_pred.py +96 -0
suomi24_toxicity_pred.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import pandas as pd
3
+ import datasets
4
+
5
+
6
+ _DESCRIPTION = """\
7
+ This dataset consists of Suomi24 comments which have been labeled by human raters for toxic behavior.
8
+ """
9
+
10
+ _HOMEPAGE = "https://turkunlp.org/"
11
+
12
+ _URLS = {
13
+ "test": "https://huggingface.co/datasets/TurkuNLP/Suomi24-toxicity-annotated/resolve/main/all_annotations.tsv"
14
+ }
15
+
16
+
17
+ class Suomi24ToxicityPred(datasets.GeneratorBasedBuilder):
18
+ """This is a dataset of comments sampled from Suomi24 and annotated using jigsaw toxicity labels."""
19
+
20
+ VERSION = datasets.Version("1.1.0")
21
+
22
+
23
+ def _info(self):
24
+
25
+ return datasets.DatasetInfo(
26
+ # This is the description that will appear on the datasets page.
27
+ description=_DESCRIPTION,
28
+ # This defines the different columns of the dataset and their types
29
+ features=datasets.Features(
30
+ {
31
+ "text": datasets.Value("string"),
32
+ "label": datasets.Value("string") # we only have one label for each text
33
+ }
34
+ ),
35
+ # If there's a common (input, target) tuple from the features,
36
+ # specify them here. They'll be used if as_supervised=True in
37
+ # builder.as_dataset.
38
+ supervised_keys=None,
39
+ # Homepage of the dataset for documentation
40
+ homepage=_HOMEPAGE
41
+ )
42
+
43
+ def _split_generators(self, dl_manager):
44
+ """Returns SplitGenerators."""
45
+ # This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
46
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
47
+
48
+ urls_to_download = _URLS
49
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
50
+
51
+ return [
52
+ datasets.SplitGenerator(
53
+ name=datasets.Split.TEST,
54
+ # These kwargs will be passed to _generate_examples
55
+ gen_kwargs={
56
+ "filepath": downloaded_files["test"],
57
+ },
58
+ ),
59
+ ]
60
+
61
+ def _generate_examples(self, filepath):
62
+ """Yields examples."""
63
+ # This method will receive as arguments the `gen_kwargs` defined in the previous `_split_generators` method.
64
+ # It is in charge of opening the given file and yielding (key, example) tuples from the dataset
65
+ # The key is not important, it's more here for legacy reason (legacy from tfds)
66
+
67
+ # read the tsv file
68
+ with open(filepath, "rt", encoding="utf-8") as f:
69
+ data = f.readlines()
70
+ data = data[1:]
71
+ for i in range(len(data)):
72
+ data[i] = data[i].replace("\n", "")
73
+ data[i] = data[i].split("\t")
74
+ assert len(data[i]) == 3
75
+
76
+ from collections import Counter
77
+ from itertools import count
78
+
79
+ ids = [one[0] for one in data]
80
+ c = Counter(ids)
81
+
82
+ iters = {k: count(1) for k, v in c.items() if v > 1}
83
+ output_list = [x+str(next(iters[x])) if x in iters else x for x in ids]
84
+
85
+ count = 0
86
+ # here make the data into a proper thing
87
+ for one in data:
88
+ example = {}
89
+ id = output_list[count] # change this somehow
90
+ count = count + 1
91
+ example["text"] = one[2]
92
+ example["label"] = one[1]
93
+
94
+ yield (id, example)
95
+
96
+