cathw commited on
Commit
57f4032
1 Parent(s): 0ee4564

Upload reddit_climate_comment.py

Browse files
Files changed (1) hide show
  1. reddit_climate_comment.py +39 -25
reddit_climate_comment.py CHANGED
@@ -54,8 +54,8 @@ class NewDataset(GeneratorBasedBuilder):
54
  path = dl_manager.download_and_extract(_URL)
55
  train_splits = SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": path+"/climate_comments.json"})
56
  return [train_splits]
57
- def _generate_examples(self, filepath):
58
- with open(filepath, encoding="utf-8") as f:
59
  data = json.load(f)
60
 
61
  for post in data["Posts"]:
@@ -71,6 +71,20 @@ class NewDataset(GeneratorBasedBuilder):
71
  post_permalink = post["PostPermalink"]
72
  comments = post["Comments"]
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  for comment in comments:
75
  comment_id = comment["CommentID"]
76
  comment_author = comment["CommentAuthor"]
@@ -79,6 +93,16 @@ class NewDataset(GeneratorBasedBuilder):
79
  comment_upvotes = int(comment["CommentUpvotes"])
80
  comment_permalink = comment["CommentPermalink"]
81
 
 
 
 
 
 
 
 
 
 
 
82
  replies = comment.get("Replies", [])
83
  for reply in replies:
84
  reply_id = reply["ReplyID"]
@@ -88,28 +112,18 @@ class NewDataset(GeneratorBasedBuilder):
88
  reply_upvotes = int(reply["ReplyUpvotes"])
89
  reply_permalink = reply["ReplyPermalink"]
90
 
91
- yield f"{post_id}_{comment_id}_{reply_id}", {
92
- "post_id": post_id,
93
- "post_title": post_title,
94
- "post_author": post_author,
95
- "post_body": post_body,
96
- "post_url": post_url,
97
- "post_pic": post_pic,
98
- "subreddit": subreddit,
99
- "post_timestamp": post_timestamp,
100
- "post_upvotes": post_upvotes,
101
- "post_permalink": post_permalink,
102
- "comment_id": comment_id,
103
- "comment_author": comment_author,
104
- "comment_body": comment_body,
105
- "comment_timestamp": comment_timestamp,
106
- "comment_upvotes": comment_upvotes,
107
- "comment_permalink": comment_permalink,
108
- "reply_id": reply_id,
109
- "reply_author": reply_author,
110
- "reply_body": reply_body,
111
- "reply_timestamp": reply_timestamp,
112
- "reply_upvotes": reply_upvotes,
113
- "reply_permalink": reply_permalink,
114
  }
 
 
 
 
 
 
115
 
 
54
  path = dl_manager.download_and_extract(_URL)
55
  train_splits = SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": path+"/climate_comments.json"})
56
  return [train_splits]
57
+ def _generate_examples(self, path):
58
+ with open(path, encoding="utf-8") as f:
59
  data = json.load(f)
60
 
61
  for post in data["Posts"]:
 
71
  post_permalink = post["PostPermalink"]
72
  comments = post["Comments"]
73
 
74
+ post_data = {
75
+ "id": post_id,
76
+ "post_title": post_title,
77
+ "post_author": post_author,
78
+ "post_body": post_body,
79
+ "post_url": post_url,
80
+ "post_pic": post_pic,
81
+ "subreddit": subreddit,
82
+ "post_timestamp": post_timestamp,
83
+ "post_upvotes": post_upvotes,
84
+ "post_permalink": post_permalink,
85
+ "comments": []
86
+ }
87
+
88
  for comment in comments:
89
  comment_id = comment["CommentID"]
90
  comment_author = comment["CommentAuthor"]
 
93
  comment_upvotes = int(comment["CommentUpvotes"])
94
  comment_permalink = comment["CommentPermalink"]
95
 
96
+ comment_data = {
97
+ "CommentID": comment_id,
98
+ "CommentAuthor": comment_author,
99
+ "CommentBody": comment_body,
100
+ "CommentTimestamp": comment_timestamp,
101
+ "CommentUpvotes": comment_upvotes,
102
+ "CommentPermalink": comment_permalink,
103
+ "Replies": []
104
+ }
105
+
106
  replies = comment.get("Replies", [])
107
  for reply in replies:
108
  reply_id = reply["ReplyID"]
 
112
  reply_upvotes = int(reply["ReplyUpvotes"])
113
  reply_permalink = reply["ReplyPermalink"]
114
 
115
+ reply_data = {
116
+ "ReplyID": reply_id,
117
+ "ReplyAuthor": reply_author,
118
+ "ReplyBody": reply_body,
119
+ "ReplyTimestamp": reply_timestamp,
120
+ "ReplyUpvotes": reply_upvotes,
121
+ "ReplyPermalink": reply_permalink
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  }
123
+
124
+ comment_data["Replies"].append(reply_data)
125
+
126
+ post_data["comments"].append(comment_data)
127
+
128
+ yield post_id, post_data
129