Vadim Alperovich commited on
Commit
53340f9
·
1 Parent(s): b939b29

Create 20ng.py

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