khalidalt commited on
Commit
3db59d9
1 Parent(s): 99f93c2

Create HuffPost.py

Browse files
Files changed (1) hide show
  1. HuffPost.py +147 -0
HuffPost.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """HuffPost Dataset."""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+
24
+
25
+
26
+ _CITATION = """\
27
+ @book{book,
28
+ author = {Misra, Rishabh and Grover, Jigyasa},
29
+ year = {2021},
30
+ month = {01},
31
+ pages = {},
32
+ title = {Sculpting Data for ML: The first act of Machine Learning},
33
+ isbn = {978-0-578-83125-1}
34
+ }
35
+
36
+ @dataset{dataset,
37
+ author = {Misra, Rishabh},
38
+ year = {2018},
39
+ month = {06},
40
+ pages = {},
41
+ title = {News Category Dataset},
42
+ doi = {10.13140/RG.2.2.20331.18729}
43
+ }
44
+ """
45
+
46
+
47
+ _DESCRIPTION = """\
48
+ A dataset of approximately 200K news headlines from the year 2012 to 2018 collected from HuffPost."""
49
+
50
+ _HOMEPAGE = "https://www.kaggle.com/datasets/rmisra/news-category-dataset"
51
+
52
+ _LICENSE = "CC0: Public Domain"
53
+
54
+ _URLS = { "train":"https://huggingface.co/datasets/khalidalt/HuffPost/resolve/main/News_Category_Dataset_v2.json",
55
+ }
56
+
57
+
58
+
59
+ class HuffPost(datasets.GeneratorBasedBuilder):
60
+ """HuffPost Dataset."""
61
+
62
+ VERSION = datasets.Version("1.1.0")
63
+
64
+ # This is an example of a dataset with multiple configurations.
65
+ # If you don't want/need to define several sub-sets in your dataset,
66
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
67
+
68
+ # If you need to make complex sub-parts in the datasets with configurable options
69
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
70
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
71
+
72
+ # You will be able to load one or the other configurations in the following list with
73
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
74
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
75
+ BUILDER_CONFIGS = [
76
+ datasets.BuilderConfig(name="text", version=VERSION, description="text of the data"),
77
+ ]
78
+
79
+
80
+ def _info(self):
81
+
82
+ features = datasets.Features(
83
+ {
84
+ "category": datasets.Value("string"),
85
+ "headline": datasets.Value("string"),
86
+ "authors": datasets.Value("string"),
87
+ "link": datasets.Value("string"),
88
+ "short_description": datasets.Value("string"),
89
+ "date": dataset.Value("string"),
90
+ # These are the features of your dataset like images, labels ...
91
+ }
92
+ )
93
+
94
+ return datasets.DatasetInfo(
95
+ # This is the description that will appear on the datasets page.
96
+ description=_DESCRIPTION,
97
+ # This defines the different columns of the dataset and their types
98
+ features=features, # Here we define them above because they are different between the two configurations
99
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
100
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
101
+ # supervised_keys=("sentence", "label"),
102
+ # Homepage of the dataset for documentation
103
+ homepage=_HOMEPAGE,
104
+ # License for the dataset if available
105
+ license=_LICENSE,
106
+ # Citation for the dataset
107
+ citation=_CITATION,
108
+ )
109
+
110
+ def _split_generators(self, dl_manager):
111
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
112
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
113
+
114
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
115
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
116
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
117
+ urls = _URLS[self.config.name]
118
+ data_dir = dl_manager.download_and_extract(urls)
119
+ return [
120
+ datasets.SplitGenerator(
121
+ name=datasets.Split.TRAIN,
122
+ # These kwargs will be passed to _generate_examples
123
+ gen_kwargs={
124
+ "filepath": os.path.join(data_dir, "train.json"),
125
+ "split": "train",
126
+ },
127
+ ),
128
+
129
+ ]
130
+
131
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
132
+ def _generate_examples(self, filepath, split):
133
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
134
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
135
+ with open(filepath, encoding="utf-8") as f:
136
+ for key, row in enumerate(f):
137
+ data = json.loads(row)
138
+
139
+ # Yields examples as (key, example) tuples
140
+ yield key, {
141
+ "category": data["category"],
142
+ "headline": data["headline"],
143
+ "authors": data["authors"],
144
+ "link": data["link"],
145
+ "short_description": data["short_description"],
146
+ "date": data["date"],
147
+ }