Datasets:
Upload reddit_climate_comment.py
Browse files- reddit_climate_comment.py +80 -91
reddit_climate_comment.py
CHANGED
|
@@ -12,108 +12,97 @@ _URL = ""
|
|
| 12 |
_HOMEPAGE = ""
|
| 13 |
_LICENSE = ""
|
| 14 |
|
| 15 |
-
_URL = "https://github.com/catherine-ywang/reddit_climate_comment_data/raw/main/climate_comments.
|
| 16 |
|
| 17 |
class NewDataset(GeneratorBasedBuilder):
|
| 18 |
def _info(self):
|
| 19 |
return DatasetInfo(
|
| 20 |
description=_DESCRIPTION,
|
| 21 |
-
features
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
}),
|
| 49 |
homepage=_HOMEPAGE,
|
| 50 |
)
|
| 51 |
def _split_generators(self, dl_manager):
|
| 52 |
path = dl_manager.download_and_extract(_URL)
|
| 53 |
-
train_splits = SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": path+"/climate_comments.
|
| 54 |
return [train_splits]
|
|
|
|
| 55 |
def _generate_examples(self, filepath):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
comment_upvotes = int(post['Comments'][i]['CommentUpvotes']) if post['Comments'][i]['CommentUpvotes'] else None
|
| 78 |
-
comment_permalink = post['Comments'][i]['CommentPermalink']
|
| 79 |
-
replies = []
|
| 80 |
-
for reply in post['Comments'][i].get('Replies', []):
|
| 81 |
-
reply_id = reply['ReplyID']
|
| 82 |
-
reply_author = reply['ReplyAuthor']
|
| 83 |
-
reply_body = reply['ReplyBody'] if reply['ReplyBody'] != "" else None
|
| 84 |
-
reply_timestamp = reply['ReplyTimestamp']
|
| 85 |
-
reply_upvotes = int(reply['ReplyUpvotes']) if reply['ReplyUpvotes'] else None
|
| 86 |
-
reply_permalink = reply['ReplyPermalink']
|
| 87 |
-
replies.append({
|
| 88 |
-
"ReplyID": reply_id,
|
| 89 |
-
"ReplyAuthor": reply_author,
|
| 90 |
-
"ReplyBody": reply_body,
|
| 91 |
-
"ReplyTimestamp": reply_timestamp,
|
| 92 |
-
"ReplyUpvotes": reply_upvotes,
|
| 93 |
-
"ReplyPermalink": reply_permalink
|
| 94 |
-
})
|
| 95 |
-
comment_dict = {
|
| 96 |
-
"CommentID": comment_id,
|
| 97 |
-
"CommentAuthor": comment_author,
|
| 98 |
-
"CommentBody": comment_body,
|
| 99 |
-
"CommentTimestamp": comment_timestamp,
|
| 100 |
-
"CommentUpvotes": comment_upvotes,
|
| 101 |
-
"CommentPermalink": comment_permalink,
|
| 102 |
-
"Replies": replies
|
| 103 |
}
|
| 104 |
-
|
| 105 |
|
| 106 |
-
|
| 107 |
-
"
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
|
|
|
| 12 |
_HOMEPAGE = ""
|
| 13 |
_LICENSE = ""
|
| 14 |
|
| 15 |
+
_URL = "https://github.com/catherine-ywang/reddit_climate_comment_data/raw/main/climate_comments.csv.zip"
|
| 16 |
|
| 17 |
class NewDataset(GeneratorBasedBuilder):
|
| 18 |
def _info(self):
|
| 19 |
return DatasetInfo(
|
| 20 |
description=_DESCRIPTION,
|
| 21 |
+
features=Features({
|
| 22 |
+
"id": Value("string"),
|
| 23 |
+
"post_title": Value("string"),
|
| 24 |
+
"post_author": Value("string"),
|
| 25 |
+
"post_body": Value("string"),
|
| 26 |
+
"post_url": Value("string"),
|
| 27 |
+
"post_pic": Value("string"),
|
| 28 |
+
"subreddit": Value("string"),
|
| 29 |
+
"post_timestamp": Value("string"),
|
| 30 |
+
"post_upvotes": Value("int32"),
|
| 31 |
+
"post_permalink": Value("string"),
|
| 32 |
+
"comments": Sequence({
|
| 33 |
+
"CommentID": Value("string"),
|
| 34 |
+
"CommentAuthor": Value("string"),
|
| 35 |
+
"CommentBody": Value("string"),
|
| 36 |
+
"CommentTimestamp": Value("string"),
|
| 37 |
+
"CommentUpvotes": Value("int32"),
|
| 38 |
+
"CommentPermalink": Value("string"),
|
| 39 |
+
"replies": Sequence({
|
| 40 |
+
"ReplyID": Value("string"),
|
| 41 |
+
"ReplyAuthor": Value("string"),
|
| 42 |
+
"ReplyBody": Value("string"),
|
| 43 |
+
"ReplyTimestamp": Value("string"),
|
| 44 |
+
"ReplyUpvotes": Value("int32"),
|
| 45 |
+
"ReplyPermalink": Value("string"),
|
| 46 |
+
})
|
| 47 |
+
})
|
| 48 |
+
}),
|
| 49 |
homepage=_HOMEPAGE,
|
| 50 |
)
|
| 51 |
def _split_generators(self, dl_manager):
|
| 52 |
path = dl_manager.download_and_extract(_URL)
|
| 53 |
+
train_splits = SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": path+"/climate_comments.csv"})
|
| 54 |
return [train_splits]
|
| 55 |
+
|
| 56 |
def _generate_examples(self, filepath):
|
| 57 |
+
df = pd.read_csv(filepath)
|
| 58 |
+
# Replace pd.NA with None for all columns
|
| 59 |
+
for column in df.columns:
|
| 60 |
+
df[column] = df[column].replace({pd.NA: None})
|
| 61 |
+
|
| 62 |
+
posts = {}
|
| 63 |
+
for id_, row in df.iterrows():
|
| 64 |
+
post_id = row["PostID"]
|
| 65 |
+
if post_id not in posts:
|
| 66 |
+
post_data = {
|
| 67 |
+
"id": post_id,
|
| 68 |
+
"post_title": row["PostTitle"],
|
| 69 |
+
"post_author": row["PostAuthor"],
|
| 70 |
+
"post_body": row["PostBody"],
|
| 71 |
+
"post_url": row["PostUrl"],
|
| 72 |
+
"post_pic": row["PostPic"],
|
| 73 |
+
"subreddit": row["Subreddit"],
|
| 74 |
+
"post_timestamp": row["PostTimestamp"],
|
| 75 |
+
"post_upvotes": int(row["PostUpvotes"]),
|
| 76 |
+
"post_permalink": row["PostPermalink"],
|
| 77 |
+
"comments": []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
+
posts[post_id] = post_data
|
| 80 |
|
| 81 |
+
if (len(row["CommentID"]) != 0):
|
| 82 |
+
for i in range(0,len(row["CommentID"])):
|
| 83 |
+
comment_data = {
|
| 84 |
+
"CommentID": row["CommentID"][i],
|
| 85 |
+
"CommentAuthor": row["CommentAuthor"][i],
|
| 86 |
+
"CommentBody": row["CommentBody"][i],
|
| 87 |
+
"CommentTimestamp": row["CommentTimestamp"][i],
|
| 88 |
+
"CommentUpvotes": int(row["CommentUpvotes"][i]),
|
| 89 |
+
"CommentPermalink": row["CommentPermalink"][i],
|
| 90 |
+
"replies": []
|
| 91 |
+
}
|
| 92 |
+
reply = {}
|
| 93 |
+
reply_id = row["ReplyID"]
|
| 94 |
+
if reply_id:
|
| 95 |
+
for j in range(0,len(reply_id)):
|
| 96 |
+
reply_data = {"ReplyID": reply_id[j],
|
| 97 |
+
"ReplyAuthor": row["ReplyAuthor"][j],
|
| 98 |
+
"ReplyBody": row["ReplyBody"][j],
|
| 99 |
+
"ReplyTimestamp": row["ReplyTimestamp"][j],
|
| 100 |
+
"ReplyUpvotes": int(row["ReplyUpvotes"][j]),
|
| 101 |
+
"ReplyPermalink": row["ReplyPermalink"][j]
|
| 102 |
+
}
|
| 103 |
+
comment_data["replies"].append(reply_data)
|
| 104 |
+
post_data["comments"].append(comment_data)
|
| 105 |
+
|
| 106 |
+
for key, post_data in posts.items():
|
| 107 |
+
yield key, post_data
|
| 108 |
|