Vadim Alperovich commited on
Commit
3190cf8
·
1 Parent(s): 0b625da

Create 20ng_not_enough_data.py

Browse files
Files changed (1) hide show
  1. 20ng_not_enough_data.py +83 -0
20ng_not_enough_data.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Lint as: python3
2
+ """20ng classification dataset."""
3
+
4
+
5
+ import csv
6
+
7
+ import datasets
8
+ from datasets.tasks import TextClassification
9
+ import sys
10
+ csv.field_size_limit(sys.maxsize)
11
+
12
+ _DESCRIPTION = """\
13
+ This data collection contains all the data used in our learning question classification experiments(see [1]), which has question class definitions, the training and testing question sets, examples of preprocessing the questions, feature definition scripts and examples of semantically related word features.
14
+ This work has been done by Xin Li and Dan Roth and supported by [2].
15
+ """
16
+
17
+ _CITATION = """"""
18
+
19
+ _TRAIN_DOWNLOAD_URL = "https://huggingface.co/datasets/vmalperovich/20ng_not_enough_data/resolve/main/train.csv"
20
+ _TEST_DOWNLOAD_URL = "https://huggingface.co/datasets/vmalperovich/20ng_not_enough_data/resolve/main/test.csv"
21
+ _VALID_DOWNLOAD_URL = "https://huggingface.co/datasets/vmalperovich/20ng_not_enough_data/raw/main/validaton.csv"
22
+
23
+
24
+ CATEGORY_MAPPING = {'comp.sys.mac.hardware': 0,
25
+ 'comp.graphics': 1,
26
+ 'sci.space': 2,
27
+ 'talk.politics.guns': 3,
28
+ 'sci.med': 4,
29
+ 'comp.sys.ibm.pc.hardware': 5,
30
+ 'comp.os.ms-windows.misc': 6,
31
+ 'rec.motorcycles': 7,
32
+ 'misc.forsale': 8,
33
+ 'alt.atheism': 9,
34
+ 'rec.autos': 10,
35
+ 'sci.electronics': 11,
36
+ 'comp.windows.x': 12,
37
+ 'rec.sport.hockey': 13,
38
+ 'rec.sport.baseball': 14,
39
+ 'talk.politics.mideast': 15,
40
+ 'sci.crypt': 16,
41
+ 'soc.religion.christian': 17,
42
+ 'talk.politics.misc': 18,
43
+ 'talk.religion.misc': 19}
44
+
45
+ class NG(datasets.GeneratorBasedBuilder):
46
+ """20ng classification dataset."""
47
+
48
+ def _info(self):
49
+ return datasets.DatasetInfo(
50
+ description=_DESCRIPTION,
51
+ features=datasets.Features(
52
+ {
53
+ "text": datasets.Value("string"),
54
+ "label": datasets.features.ClassLabel(names=list(CATEGORY_MAPPING.keys())),
55
+ "labeled_mask": datasets.Value("bool"),
56
+ }
57
+ ),
58
+ homepage="",
59
+ citation=_CITATION,
60
+ task_templates=[TextClassification(text_column="text", label_column="label")],
61
+ )
62
+
63
+ def _split_generators(self, dl_manager):
64
+ train_path = dl_manager.download_and_extract(_TRAIN_DOWNLOAD_URL)
65
+ test_path = dl_manager.download_and_extract(_TEST_DOWNLOAD_URL)
66
+ valid_path = dl_manager.download_and_extract(_VALID_DOWNLOAD_URL)
67
+ return [
68
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path}),
69
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": test_path}),
70
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": valid_path}),
71
+ ]
72
+
73
+ def _generate_examples(self, filepath):
74
+ """Generate examples."""
75
+ with open(filepath, encoding="utf-8") as csv_file:
76
+ csv_reader = csv.reader(
77
+ csv_file, quotechar='"', delimiter=";", quoting=csv.QUOTE_ALL, skipinitialspace=True
78
+ )
79
+ _ = next(csv_reader) # skip header
80
+ for id_, row in enumerate(csv_reader):
81
+ text, label, label_mask = row
82
+ label = CATEGORY_MAPPING.get(label, label)
83
+ yield id_, {"text": text, "label": label, "labeled_mask": label_mask}