cathw commited on
Commit
6242849
·
verified ·
1 Parent(s): 2ad35fe

Upload reddit_climate_comment.py

Browse files
Files changed (1) hide show
  1. reddit_climate_comment.py +66 -59
reddit_climate_comment.py CHANGED
@@ -12,7 +12,7 @@ _URL = ""
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):
@@ -50,65 +50,72 @@ class NewDataset(GeneratorBasedBuilder):
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
- comment_data = {
82
- "CommentID": row["CommentID"],
83
- "CommentAuthor": row["CommentAuthor"],
84
- "CommentBody": row["CommentBody"],
85
- "CommentTimestamp": row["CommentTimestamp"],
86
- "CommentUpvotes": int(row["CommentUpvotes"]),
87
- "CommentPermalink": row["CommentPermalink"],
88
- "replies": []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  }
90
- posts[post_id]["comments"].append(comment_data)
91
-
92
- reply_id = row["ReplyID"]
93
- if reply_id:
94
- # Construct reply data dictionary
95
- reply_data = {
96
- "ReplyID": reply_id,
97
- "ReplyAuthor": row["ReplyAuthor"],
98
- "ReplyBody": row["ReplyBody"],
99
- "ReplyTimestamp": row["ReplyTimestamp"],
100
- "ReplyUpvotes": int(row["ReplyUpvotes"]),
101
- "ReplyPermalink": row["ReplyPermalink"]
102
- }
103
- # Append reply data to replies list of corresponding comment
104
- for comment in posts[post_id]["comments"]:
105
- if comment["CommentID"] == row["CommentID"]:
106
- comment["replies"].append(reply_data)
107
- break
108
-
109
- for key, post_data in posts.items():
110
- yield key, post_data
111
-
112
-
113
-
114
-
 
12
  _HOMEPAGE = ""
13
  _LICENSE = ""
14
 
15
+ _URL = "https://github.com/catherine-ywang/reddit_climate_comment_data/raw/main/climate_comments.json.zip"
16
 
17
  class NewDataset(GeneratorBasedBuilder):
18
  def _info(self):
 
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.json"})
54
  return [train_splits]
55
 
56
  def _generate_examples(self, filepath):
57
+ with open(filepath, "r", encoding="utf-8") as f:
58
+ data = json.load(f)
59
+
60
+ for post in data['Posts']:
61
+ post_id = post['PostID']
62
+ post_title = post['PostTitle'] if post['PostTitle'] != "" else None
63
+ post_author = post['PostAuthor'] if post['PostAuthor'] != "" else None
64
+ post_body = post['PostBody'] if post['PostBody'] != "" else None
65
+ post_url = post['PostUrl'] if post['PostUrl'] != "" else None
66
+ post_pic = post['PostPic'] if post['PostPic'] != "" else None
67
+ subreddit = post['Subreddit'] if post['Subreddit'] != "" else None
68
+ post_timestamp = post['PostTimestamp'] if post['PostTimestamp'] != "" else None
69
+ post_upvotes = int(post['PostUpvotes']) if post['PostUpvotes'] else None
70
+ post_permalink = post['PostPermalink'] if post['PostPermalink'] != "" else None
71
+
72
+ comments = []
73
+ for comment in post['Comments']:
74
+ comment_id = comment['CommentID']
75
+ comment_author = comment['CommentAuthor'] if comment['CommentAuthor'] != "" else None
76
+ comment_body = comment['CommentBody'] if comment['CommentBody'] != "" else None
77
+ comment_timestamp = comment['CommentTimestamp'] if comment['CommentTimestamp'] != "" else None
78
+ comment_upvotes = int(comment['CommentUpvotes']) if comment['CommentUpvotes'] else None
79
+ comment_permalink = comment['CommentPermalink'] if comment['CommentPermalink'] != "" else None
80
+
81
+ replies = []
82
+ for reply in comment['Replies']:
83
+ reply_id = reply['ReplyID']
84
+ reply_author = reply['ReplyAuthor'] if reply['ReplyAuthor'] != "" else None
85
+ reply_body = reply['ReplyBody'] if reply['ReplyBody'] != "" else None
86
+ reply_timestamp = reply['ReplyTimestamp'] if reply['ReplyTimestamp'] != "" else None
87
+ reply_upvotes = int(reply['ReplyUpvotes']) if reply['ReplyUpvotes'] else None
88
+ reply_permalink = reply['ReplyPermalink'] if reply['ReplyPermalink'] != "" else None
89
+
90
+ replies.append({
91
+ "ReplyID": reply_id,
92
+ "ReplyAuthor": reply_author,
93
+ "ReplyBody": reply_body,
94
+ "ReplyTimestamp": reply_timestamp,
95
+ "ReplyUpvotes": reply_upvotes,
96
+ "ReplyPermalink": reply_permalink
97
+ })
98
+
99
+ comments.append({
100
+ "CommentID": comment_id,
101
+ "CommentAuthor": comment_author,
102
+ "CommentBody": comment_body,
103
+ "CommentTimestamp": comment_timestamp,
104
+ "CommentUpvotes": comment_upvotes,
105
+ "CommentPermalink": comment_permalink,
106
+ "replies": replies
107
+ })
108
+
109
+ yield post_id, {
110
+ "id": post_id,
111
+ "post_title": post_title,
112
+ "post_author": post_author,
113
+ "post_body": post_body,
114
+ "post_url": post_url,
115
+ "post_pic": post_pic,
116
+ "subreddit": subreddit,
117
+ "post_timestamp": post_timestamp,
118
+ "post_upvotes": post_upvotes,
119
+ "post_permalink": post_permalink,
120
+ "comments": comments
121
  }