cathw commited on
Commit
cdeafd7
·
verified ·
1 Parent(s): 4d1d596

Upload reddit_climate_comment.py

Browse files
Files changed (1) hide show
  1. reddit_climate_comment.py +32 -27
reddit_climate_comment.py CHANGED
@@ -55,44 +55,48 @@ class NewDataset(GeneratorBasedBuilder):
55
 
56
  def _generate_examples(self, filepath):
57
  df = pd.read_csv(filepath)
 
58
 
59
- for index, row in df.iterrows():
60
- post_id = str(row['PostID'])
61
- post_title = row['PostTitle']
62
- post_author = row['PostAuthor']
63
- post_body = row['PostBody']
64
- post_url = row['PostUrl']
65
- post_pic = row['PostPic']
66
- subreddit = row['Subreddit']
67
- post_timestamp = row['PostTimestamp']
68
- post_upvotes = row['PostUpvotes']
69
- post_permalink = row['PostPermalink']
 
70
 
71
  comments = []
72
- if not pd.isnull(row['CommentID']):
73
  comment = {
74
- "CommentID": str(row['CommentID']),
75
- "CommentAuthor": row['CommentAuthor'],
76
- "CommentBody": row['CommentBody'],
77
- "CommentTimestamp": row['CommentTimestamp'],
78
- "CommentUpvotes": row['CommentUpvotes'],
79
- "CommentPermalink": row['CommentPermalink'],
80
  "replies": []
81
  }
82
- comments.append(comment)
83
 
84
- # If there are replies, add them to the comment
85
- if not pd.isnull(row['ReplyID']):
 
86
  reply = {
87
- "ReplyID": str(row['ReplyID']),
88
- "ReplyAuthor": row['ReplyAuthor'],
89
- "ReplyBody": row['ReplyBody'],
90
- "ReplyTimestamp": row['ReplyTimestamp'],
91
- "ReplyUpvotes": row['ReplyUpvotes'],
92
- "ReplyPermalink": row['ReplyPermalink']
93
  }
94
  comment["replies"].append(reply)
95
 
 
 
96
  yield post_id, {
97
  "id": post_id,
98
  "post_title": post_title,
@@ -106,4 +110,5 @@ class NewDataset(GeneratorBasedBuilder):
106
  "post_permalink": post_permalink,
107
  "comments": comments
108
  }
 
109
 
 
55
 
56
  def _generate_examples(self, filepath):
57
  df = pd.read_csv(filepath)
58
+ grouped_df = df.groupby('PostID')
59
 
60
+ for post_id, group in grouped_df:
61
+ post_data = group.iloc[0] # Get the data for the post
62
+
63
+ post_title = post_data['PostTitle']
64
+ post_author = post_data['PostAuthor']
65
+ post_body = post_data['PostBody']
66
+ post_url = post_data['PostUrl']
67
+ post_pic = post_data['PostPic']
68
+ subreddit = post_data['Subreddit']
69
+ post_timestamp = post_data['PostTimestamp']
70
+ post_upvotes = post_data['PostUpvotes']
71
+ post_permalink = post_data['PostPermalink']
72
 
73
  comments = []
74
+ for index, comment_row in group.iterrows():
75
  comment = {
76
+ "CommentID": str(comment_row['CommentID']),
77
+ "CommentAuthor": comment_row['CommentAuthor'],
78
+ "CommentBody": comment_row['CommentBody'],
79
+ "CommentTimestamp": comment_row['CommentTimestamp'],
80
+ "CommentUpvotes": comment_row['CommentUpvotes'],
81
+ "CommentPermalink": comment_row['CommentPermalink'],
82
  "replies": []
83
  }
 
84
 
85
+ # Add replies for the current comment
86
+ replies = group[group['CommentID'] == comment_row['CommentID']]
87
+ for _, reply_row in replies.iterrows():
88
  reply = {
89
+ "ReplyID": str(reply_row['ReplyID']),
90
+ "ReplyAuthor": reply_row['ReplyAuthor'],
91
+ "ReplyBody": reply_row['ReplyBody'],
92
+ "ReplyTimestamp": reply_row['ReplyTimestamp'],
93
+ "ReplyUpvotes": reply_row['ReplyUpvotes'],
94
+ "ReplyPermalink": reply_row['ReplyPermalink']
95
  }
96
  comment["replies"].append(reply)
97
 
98
+ comments.append(comment)
99
+
100
  yield post_id, {
101
  "id": post_id,
102
  "post_title": post_title,
 
110
  "post_permalink": post_permalink,
111
  "comments": comments
112
  }
113
+
114