annieske commited on
Commit
7b8aba7
1 Parent(s): 00e15c4

Upload jigsaw_toxicity_pred_fi.py

Browse files
Files changed (1) hide show
  1. jigsaw_toxicity_pred_fi.py +98 -0
jigsaw_toxicity_pred_fi.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Comments from Jigsaw Toxic Comment Classification Kaggle Competition """
2
+
3
+ import json
4
+
5
+ import pandas as pd
6
+
7
+ import datasets
8
+
9
+
10
+ _DESCRIPTION = """\
11
+ This dataset consists of a large number of Wikipedia comments translated to Finnish which have been labeled by human raters for toxic behavior.
12
+ """
13
+
14
+ _HOMEPAGE = "https://turkunlp.org/"
15
+
16
+ _URLS = {
17
+ "train": "https://huggingface.co/datasets/TurkuNLP/wikipedia-toxicity-data-fi/resolve/main/train_fi_deepl.jsonl.bz2",
18
+ "test": "https://huggingface.co/datasets/TurkuNLP/wikipedia-toxicity-data-fi/resolve/main/test_fi_deepl.jsonl.bz2"
19
+ }
20
+
21
+
22
+ class JigsawToxicityPred(datasets.GeneratorBasedBuilder):
23
+ """This is a dataset of comments from Wikipedia’s talk page edits which have been labeled by human raters for toxic behavior."""
24
+
25
+ VERSION = datasets.Version("1.1.0")
26
+
27
+
28
+ def _info(self):
29
+
30
+ return datasets.DatasetInfo(
31
+ # This is the description that will appear on the datasets page.
32
+ description=_DESCRIPTION,
33
+ # This defines the different columns of the dataset and their types
34
+ features=datasets.Features(
35
+ {
36
+ "text": datasets.Value("string"),
37
+ "label_toxicity": datasets.ClassLabel(names=["false", "true"]),
38
+ "label_severe_toxicity": datasets.ClassLabel(names=["false", "true"]),
39
+ "label_obscene": datasets.ClassLabel(names=["false", "true"]),
40
+ "label_threat": datasets.ClassLabel(names=["false", "true"]),
41
+ "label_insult": datasets.ClassLabel(names=["false", "true"]),
42
+ "label_identity_attack": datasets.ClassLabel(names=["false", "true"]),
43
+ }
44
+ ),
45
+ # If there's a common (input, target) tuple from the features,
46
+ # specify them here. They'll be used if as_supervised=True in
47
+ # builder.as_dataset.
48
+ supervised_keys=None,
49
+ # Homepage of the dataset for documentation
50
+ homepage=_HOMEPAGE,
51
+ # License for the dataset if available
52
+ license=_LICENSE,
53
+ )
54
+
55
+ def _split_generators(self, dl_manager):
56
+ """Returns SplitGenerators."""
57
+ # This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
58
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
59
+
60
+ urls_to_download = _URLS
61
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
62
+
63
+ return [
64
+ datasets.SplitGenerator(
65
+ name=datasets.Split.TRAIN,
66
+ # These kwargs will be passed to _generate_examples
67
+ gen_kwargs={"filepath": downloaded_files["train"]}
68
+ ),
69
+ datasets.SplitGenerator(
70
+ name=datasets.Split.TEST,
71
+ # These kwargs will be passed to _generate_examples
72
+ gen_kwargs={
73
+ "filepath": downloaded_files["test"],
74
+ },
75
+ ),
76
+ ]
77
+
78
+ def _generate_examples(self, filepath):
79
+ """Yields examples."""
80
+ # This method will receive as arguments the `gen_kwargs` defined in the previous `_split_generators` method.
81
+ # It is in charge of opening the given file and yielding (key, example) tuples from the dataset
82
+ # The key is not important, it's more here for legacy reason (legacy from tfds)
83
+
84
+ # read the json into dictionaries
85
+ with open(filepath, 'r') as json_file:
86
+ json_list = list(json_file)
87
+ lines = [json.loads(jline) for jline in json_list]
88
+
89
+ for data in lines:
90
+ example = {}
91
+ example["text"] = data["text"]
92
+
93
+ for label in ["label_toxicity", "label_severe_toxicity", "label_obscene", "label_threat", "label_insult", "label_identity_attack"]:
94
+ example[label] = int(data[label])
95
+
96
+ yield (data["id"], example)
97
+
98
+