Datasets:
Upload reddit_climate_comment.py
Browse files- reddit_climate_comment.py +55 -60
reddit_climate_comment.py
CHANGED
|
@@ -55,65 +55,60 @@ class NewDataset(GeneratorBasedBuilder):
|
|
| 55 |
def _generate_examples(self, filepath):
|
| 56 |
with open(filepath, "r", encoding="utf-8") as f:
|
| 57 |
data = json.load(f)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
comments.
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
"
|
| 85 |
-
"
|
| 86 |
-
"
|
|
|
|
|
|
|
|
|
|
| 87 |
})
|
| 88 |
-
for reply in comment.get('Replies', []):
|
| 89 |
-
reply_id = reply['ReplyID']
|
| 90 |
-
reply_author = reply['ReplyAuthor']
|
| 91 |
-
reply_body = reply['ReplyBody'] if reply['ReplyBody'] != "" else None
|
| 92 |
-
reply_timestamp = reply['ReplyTimestamp']
|
| 93 |
-
reply_upvotes = int(reply['ReplyUpvotes']) if reply['ReplyUpvotes'] else None
|
| 94 |
-
reply_permalink = reply['ReplyPermalink']
|
| 95 |
-
replies.append({
|
| 96 |
-
"ReplyID": reply_id,
|
| 97 |
-
"ReplyAuthor": reply_author,
|
| 98 |
-
"ReplyBody": reply_body,
|
| 99 |
-
"ReplyTimestamp": reply_timestamp,
|
| 100 |
-
"ReplyUpvotes": reply_upvotes,
|
| 101 |
-
"ReplyPermalink": reply_permalink
|
| 102 |
-
})
|
| 103 |
|
| 104 |
-
|
| 105 |
-
"
|
| 106 |
-
"
|
| 107 |
-
"
|
| 108 |
-
"
|
| 109 |
-
"
|
| 110 |
-
"
|
| 111 |
-
"
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def _generate_examples(self, filepath):
|
| 56 |
with open(filepath, "r", encoding="utf-8") as f:
|
| 57 |
data = json.load(f)
|
| 58 |
+
|
| 59 |
+
for post in data['Posts']:
|
| 60 |
+
post_id = post['PostID']
|
| 61 |
+
post_title = post['PostTitle']
|
| 62 |
+
post_author = post['PostAuthor']
|
| 63 |
+
post_body = post['PostBody'] if post['PostBody'] != "" else None
|
| 64 |
+
post_url = post['PostUrl']
|
| 65 |
+
post_pic = post['PostPic'] if post['PostPic'] != "" else None
|
| 66 |
+
subreddit = post['Subreddit']
|
| 67 |
+
post_timestamp = post['PostTimestamp']
|
| 68 |
+
post_upvotes = int(post['PostUpvotes']) if post['PostUpvotes'] else None
|
| 69 |
+
post_permalink = post['PostPermalink']
|
| 70 |
+
comments = []
|
| 71 |
+
|
| 72 |
+
for comment_id, comment_author in zip(post['comments']['CommentID'], post['comments']['CommentAuthor']):
|
| 73 |
+
comment_body = post['comments'].get(comment_id, {}).get('CommentBody')
|
| 74 |
+
comment_timestamp = post['comments'].get(comment_id, {}).get('CommentTimestamp')
|
| 75 |
+
comment_upvotes = post['comments'].get(comment_id, {}).get('CommentUpvotes')
|
| 76 |
+
comment_permalink = post['comments'].get(comment_id, {}).get('CommentPermalink')
|
| 77 |
+
replies = []
|
| 78 |
+
for reply_id, reply_author in zip(post['comments'].get(comment_id, {}).get('Replies', {}).get('ReplyID', []), post['comments'].get(comment_id, {}).get('Replies', {}).get('ReplyAuthor', [])):
|
| 79 |
+
reply_body = post['comments'].get(comment_id, {}).get('Replies', {}).get(reply_id, {}).get('ReplyBody')
|
| 80 |
+
reply_timestamp = post['comments'].get(comment_id, {}).get('Replies', {}).get(reply_id, {}).get('ReplyTimestamp')
|
| 81 |
+
reply_upvotes = post['comments'].get(comment_id, {}).get('Replies', {}).get(reply_id, {}).get('ReplyUpvotes')
|
| 82 |
+
reply_permalink = post['comments'].get(comment_id, {}).get('Replies', {}).get(reply_id, {}).get('ReplyPermalink')
|
| 83 |
+
replies.append({
|
| 84 |
+
"ReplyID": reply_id,
|
| 85 |
+
"ReplyAuthor": reply_author,
|
| 86 |
+
"ReplyBody": reply_body,
|
| 87 |
+
"ReplyTimestamp": reply_timestamp,
|
| 88 |
+
"ReplyUpvotes": reply_upvotes,
|
| 89 |
+
"ReplyPermalink": reply_permalink
|
| 90 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
comments.append({
|
| 93 |
+
"CommentID": comment_id,
|
| 94 |
+
"CommentAuthor": comment_author,
|
| 95 |
+
"CommentBody": comment_body,
|
| 96 |
+
"CommentTimestamp": comment_timestamp,
|
| 97 |
+
"CommentUpvotes": comment_upvotes,
|
| 98 |
+
"CommentPermalink": comment_permalink,
|
| 99 |
+
"Replies": replies
|
| 100 |
+
})
|
| 101 |
+
|
| 102 |
+
yield post_id, {
|
| 103 |
+
"id": post_id,
|
| 104 |
+
"post_title": post_title,
|
| 105 |
+
"post_author": post_author,
|
| 106 |
+
"post_body": post_body,
|
| 107 |
+
"post_url": post_url,
|
| 108 |
+
"post_pic": post_pic,
|
| 109 |
+
"subreddit": subreddit,
|
| 110 |
+
"post_timestamp": post_timestamp,
|
| 111 |
+
"post_upvotes": post_upvotes,
|
| 112 |
+
"post_permalink": post_permalink,
|
| 113 |
+
"comments": comments
|
| 114 |
+
}
|