SocialGrep
commited on
Commit
•
6c119b4
1
Parent(s):
dfe139d
Create new file
Browse files
reddit-r-bitcoin-data-for-jun-2022.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
"""The SocialGrep dataset loader base."""
|
16 |
+
|
17 |
+
|
18 |
+
import csv
|
19 |
+
import os
|
20 |
+
|
21 |
+
import datasets
|
22 |
+
|
23 |
+
|
24 |
+
DATASET_NAME = "reddit-r-bitcoin-data-for-jun-2022"
|
25 |
+
DATASET_TITLE = "Reddit /r/Bitcoin Data for Jun 2022"
|
26 |
+
|
27 |
+
DATASET_DESCRIPTION = """\
|
28 |
+
Lite version of our Reddit /r/Bitcoin dataset - CSV of all posts & comments to the /r/Bitcoin subreddit over Jun 2022.
|
29 |
+
"""
|
30 |
+
|
31 |
+
_HOMEPAGE = f"https://socialgrep.com/datasets/{DATASET_NAME}"
|
32 |
+
|
33 |
+
_LICENSE = "CC-BY v4.0"
|
34 |
+
|
35 |
+
URL_TEMPLATE = "https://exports.socialgrep.com/download/public/{dataset_file}.zip"
|
36 |
+
DATASET_FILE_TEMPLATE = "{dataset}-{type}.csv"
|
37 |
+
|
38 |
+
_DATASET_FILES = {
|
39 |
+
'posts': DATASET_FILE_TEMPLATE.format(dataset=DATASET_NAME, type="posts"),
|
40 |
+
'comments': DATASET_FILE_TEMPLATE.format(dataset=DATASET_NAME, type="comments"),
|
41 |
+
}
|
42 |
+
|
43 |
+
_CITATION = f"""\
|
44 |
+
@misc{{socialgrep:{DATASET_NAME},
|
45 |
+
title = {{{DATASET_TITLE}}},
|
46 |
+
author={{Lexyr Inc.
|
47 |
+
}},
|
48 |
+
year={{2022}}
|
49 |
+
}}
|
50 |
+
"""
|
51 |
+
|
52 |
+
|
53 |
+
class redditrbitcoindataforjun2022(datasets.GeneratorBasedBuilder):
|
54 |
+
VERSION = datasets.Version("1.0.0")
|
55 |
+
|
56 |
+
# This is an example of a dataset with multiple configurations.
|
57 |
+
# If you don't want/need to define several sub-sets in your dataset,
|
58 |
+
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
59 |
+
|
60 |
+
# If you need to make complex sub-parts in the datasets with configurable options
|
61 |
+
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
|
62 |
+
# BUILDER_CONFIG_CLASS = MyBuilderConfig
|
63 |
+
# You will be able to load one or the other configurations in the following list with
|
64 |
+
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
65 |
+
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
66 |
+
BUILDER_CONFIGS = [
|
67 |
+
datasets.BuilderConfig(name="posts", version=VERSION, description="The dataset posts."),
|
68 |
+
datasets.BuilderConfig(name="comments", version=VERSION, description="The dataset comments."),
|
69 |
+
]
|
70 |
+
|
71 |
+
def _info(self):
|
72 |
+
if self.config.name == "posts": # This is the name of the configuration selected in BUILDER_CONFIGS above
|
73 |
+
features = datasets.Features(
|
74 |
+
{
|
75 |
+
"type": datasets.Value("string"),
|
76 |
+
"id": datasets.Value("string"),
|
77 |
+
"subreddit.id": datasets.Value("string"),
|
78 |
+
"subreddit.name": datasets.Value("string"),
|
79 |
+
"subreddit.nsfw": datasets.Value("bool"),
|
80 |
+
"created_utc": datasets.Value("timestamp[s,tz=utc]"),
|
81 |
+
"permalink": datasets.Value("string"),
|
82 |
+
"domain": datasets.Value("string"),
|
83 |
+
"url": datasets.Value("string"),
|
84 |
+
"selftext": datasets.Value("large_string"),
|
85 |
+
"title": datasets.Value("string"),
|
86 |
+
"score": datasets.Value("int32"),
|
87 |
+
}
|
88 |
+
)
|
89 |
+
else: # This is an example to show how to have different features for "first_domain" and "second_domain"
|
90 |
+
features = datasets.Features(
|
91 |
+
{
|
92 |
+
"type": datasets.ClassLabel(num_classes=2, names=['post', 'comment']),
|
93 |
+
"id": datasets.Value("string"),
|
94 |
+
"subreddit.id": datasets.Value("string"),
|
95 |
+
"subreddit.name": datasets.Value("string"),
|
96 |
+
"subreddit.nsfw": datasets.Value("bool"),
|
97 |
+
"created_utc": datasets.Value("timestamp[s,tz=utc]"),
|
98 |
+
"permalink": datasets.Value("string"),
|
99 |
+
"body": datasets.Value("large_string"),
|
100 |
+
"sentiment": datasets.Value("float32"),
|
101 |
+
"score": datasets.Value("int32"),
|
102 |
+
}
|
103 |
+
)
|
104 |
+
return datasets.DatasetInfo(
|
105 |
+
# This is the description that will appear on the datasets page.
|
106 |
+
description=DATASET_DESCRIPTION,
|
107 |
+
# This defines the different columns of the dataset and their types
|
108 |
+
features=features, # Here we define them above because they are different between the two configurations
|
109 |
+
# If there's a common (input, target) tuple from the features,
|
110 |
+
# specify them here. They'll be used if as_supervised=True in
|
111 |
+
# builder.as_dataset.
|
112 |
+
supervised_keys=None,
|
113 |
+
# Homepage of the dataset for documentation
|
114 |
+
homepage=_HOMEPAGE,
|
115 |
+
# License for the dataset if available
|
116 |
+
license=_LICENSE,
|
117 |
+
# Citation for the dataset
|
118 |
+
citation=_CITATION,
|
119 |
+
)
|
120 |
+
|
121 |
+
def _split_generators(self, dl_manager):
|
122 |
+
"""Returns SplitGenerators."""
|
123 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
124 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
|
125 |
+
# 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.
|
126 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
127 |
+
my_urls = [URL_TEMPLATE.format(dataset_file=_DATASET_FILES[self.config.name])]
|
128 |
+
data_dir = dl_manager.download_and_extract(my_urls)[0]
|
129 |
+
return [
|
130 |
+
datasets.SplitGenerator(
|
131 |
+
name=datasets.Split.TRAIN,
|
132 |
+
# These kwargs will be passed to _generate_examples
|
133 |
+
gen_kwargs={
|
134 |
+
"filepath": os.path.join(data_dir, _DATASET_FILES[self.config.name]),
|
135 |
+
"split": "train",
|
136 |
+
},
|
137 |
+
)
|
138 |
+
]
|
139 |
+
|
140 |
+
def _generate_examples(
|
141 |
+
self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
142 |
+
):
|
143 |
+
""" Yields examples as (key, example) tuples. """
|
144 |
+
# This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
145 |
+
bool_cols = ["subreddit.nsfw"]
|
146 |
+
int_cols = ["score", "created_utc"]
|
147 |
+
float_cols = ["sentiment"]
|
148 |
+
|
149 |
+
with open(filepath, encoding="utf-8") as f:
|
150 |
+
reader = csv.DictReader(f)
|
151 |
+
for row in reader:
|
152 |
+
for col in bool_cols:
|
153 |
+
if col in row:
|
154 |
+
if row[col]:
|
155 |
+
row[col] = (row[col] == "true")
|
156 |
+
else:
|
157 |
+
row[col] = None
|
158 |
+
for col in int_cols:
|
159 |
+
if col in row:
|
160 |
+
if row[col]:
|
161 |
+
row[col] = int(row[col])
|
162 |
+
else:
|
163 |
+
row[col] = None
|
164 |
+
for col in float_cols:
|
165 |
+
if col in row:
|
166 |
+
if row[col]:
|
167 |
+
row[col] = float(row[col])
|
168 |
+
else:
|
169 |
+
row[col] = None
|
170 |
+
|
171 |
+
if row["type"] == "post":
|
172 |
+
key = f"t3_{row['id']}"
|
173 |
+
if row["type"] == "comment":
|
174 |
+
key = f"t1_{row['id']}"
|
175 |
+
yield key, row
|
176 |
+
|
177 |
+
|
178 |
+
if __name__ == "__main__":
|
179 |
+
print("Please use the HuggingFace dataset library, or")
|
180 |
+
print("download from https://socialgrep.com/datasets.")
|