cathw commited on
Commit
01f62be
·
verified ·
1 Parent(s): e639f74

Upload reddit_climate_comment.py

Browse files
Files changed (1) hide show
  1. 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
- for post in data['Posts']:
59
- post_id = post['PostID']
60
- post_title = post['PostTitle']
61
- post_author = post['PostAuthor']
62
- post_body = post['PostBody'] if post['PostBody'] != "" else None
63
- post_url = post['PostUrl']
64
- post_pic = post['PostPic'] if post['PostPic'] != "" else None
65
- subreddit = post['Subreddit']
66
- post_timestamp = post['PostTimestamp']
67
- post_upvotes = int(post['PostUpvotes']) if post['PostUpvotes'] else None
68
- post_permalink = post['PostPermalink']
69
- comments = []
70
-
71
- for comment in post['Comments']:
72
- comment_id = comment['CommentID']
73
- comment_author = comment['CommentAuthor']
74
- comment_body = comment['CommentBody'] if comment['CommentBody'] != "" else None
75
- comment_timestamp = comment['CommentTimestamp']
76
- comment_upvotes = int(comment['CommentUpvotes']) if comment['CommentUpvotes'] else None
77
- comment_permalink = comment['CommentPermalink']
78
- replies = []
79
- comments.append({
80
- "CommentID": comment_id,
81
- "CommentAuthor": comment_author,
82
- "CommentBody": comment_body,
83
- "CommentTimestamp": comment_timestamp,
84
- "CommentUpvotes": comment_upvotes,
85
- "CommentPermalink": comment_permalink,
86
- "Replies": replies # Initialize an empty list for replies
 
 
 
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
- yield post_id, {
105
- "id": post_id,
106
- "post_title": post_title,
107
- "post_author": post_author,
108
- "post_body": post_body,
109
- "post_url": post_url,
110
- "post_pic": post_pic,
111
- "subreddit": subreddit,
112
- "post_timestamp": post_timestamp,
113
- "post_upvotes": post_upvotes,
114
- "post_permalink": post_permalink,
115
- "comments": comments
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
+ }