cathw commited on
Commit
33b86eb
·
verified ·
1 Parent(s): 68da27e

Upload reddit_climate_comment.py

Browse files
Files changed (1) hide show
  1. reddit_climate_comment.py +34 -23
reddit_climate_comment.py CHANGED
@@ -56,9 +56,6 @@ class NewDataset(GeneratorBasedBuilder):
56
  def _generate_examples(self, filepath):
57
  df = pd.read_csv(filepath)
58
 
59
- # Drop duplicate comments
60
- df = df.drop_duplicates(subset=['CommentID', 'CommentBody'])
61
-
62
  # Group the DataFrame by post ID
63
  grouped_df = df.groupby('PostID')
64
 
@@ -77,34 +74,47 @@ class NewDataset(GeneratorBasedBuilder):
77
 
78
  comments = []
79
 
80
- # Extract unique comments
81
- unique_comment_ids = group['CommentID'].unique()
82
- for comment_id in unique_comment_ids:
83
  comment_data = group[group['CommentID'] == comment_id].iloc[0]
84
 
85
- comment = {
86
- "CommentID": comment_id,
87
- "CommentAuthor": comment_data['CommentAuthor'],
88
- "CommentBody": comment_data['CommentBody'],
89
- "CommentTimestamp": comment_data['CommentTimestamp'],
90
- "CommentUpvotes": comment_data['CommentUpvotes'],
91
- "CommentPermalink": comment_data['CommentPermalink'],
92
- "replies": [] # Initialize empty list for replies
93
- }
94
 
95
  # Get all replies for the current comment
96
- reply_group = group[group['CommentID'] == comment_id]
 
97
  for _, reply_data in reply_group.iterrows():
 
 
 
 
 
 
 
98
  reply = {
99
- "ReplyID": reply_data['ReplyID'],
100
- "ReplyAuthor": reply_data['ReplyAuthor'],
101
- "ReplyBody": reply_data['ReplyBody'],
102
- "ReplyTimestamp": reply_data['ReplyTimestamp'],
103
- "ReplyUpvotes": reply_data['ReplyUpvotes'],
104
- "ReplyPermalink": reply_data['ReplyPermalink']
105
  }
106
- comment["replies"].append(reply)
107
 
 
 
 
 
 
 
 
 
 
 
108
  comments.append(comment)
109
 
110
  yield post_id, {
@@ -121,3 +131,4 @@ class NewDataset(GeneratorBasedBuilder):
121
  "comments": comments
122
  }
123
 
 
 
56
  def _generate_examples(self, filepath):
57
  df = pd.read_csv(filepath)
58
 
 
 
 
59
  # Group the DataFrame by post ID
60
  grouped_df = df.groupby('PostID')
61
 
 
74
 
75
  comments = []
76
 
77
+ # Iterate over each unique comment ID
78
+ for comment_id in group['CommentID'].unique():
 
79
  comment_data = group[group['CommentID'] == comment_id].iloc[0]
80
 
81
+ comment_author = comment_data['CommentAuthor']
82
+ comment_body = comment_data['CommentBody']
83
+ comment_timestamp = comment_data['CommentTimestamp']
84
+ comment_upvotes = comment_data['CommentUpvotes']
85
+ comment_permalink = comment_data['CommentPermalink']
 
 
 
 
86
 
87
  # Get all replies for the current comment
88
+ replies = []
89
+ reply_group = df[df['ParentCommentID'] == comment_id]
90
  for _, reply_data in reply_group.iterrows():
91
+ reply_id = reply_data['ReplyID']
92
+ reply_author = reply_data['ReplyAuthor']
93
+ reply_body = reply_data['ReplyBody']
94
+ reply_timestamp = reply_data['ReplyTimestamp']
95
+ reply_upvotes = reply_data['ReplyUpvotes']
96
+ reply_permalink = reply_data['ReplyPermalink']
97
+
98
  reply = {
99
+ "ReplyID": reply_id,
100
+ "ReplyAuthor": reply_author,
101
+ "ReplyBody": reply_body,
102
+ "ReplyTimestamp": reply_timestamp,
103
+ "ReplyUpvotes": reply_upvotes,
104
+ "ReplyPermalink": reply_permalink
105
  }
106
+ replies.append(reply)
107
 
108
+ # Add comment with its replies to the list
109
+ comment = {
110
+ "CommentID": comment_id,
111
+ "CommentAuthor": comment_author,
112
+ "CommentBody": comment_body,
113
+ "CommentTimestamp": comment_timestamp,
114
+ "CommentUpvotes": comment_upvotes,
115
+ "CommentPermalink": comment_permalink,
116
+ "replies": replies
117
+ }
118
  comments.append(comment)
119
 
120
  yield post_id, {
 
131
  "comments": comments
132
  }
133
 
134
+